001/** 002The contents of this file are subject to the Mozilla Public License Version 1.1 003(the "License"); you may not use this file except in compliance with the License. 004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 005Software distributed under the License is distributed on an "AS IS" basis, 006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 007specific language governing rights and limitations under the License. 008 009The Original Code is "MessageRuleBuilder.java". Description: 010"Rule Builder for MessageRules." 011 012The Initial Developer of the Original Code is University Health Network. Copyright (C) 0132012. All Rights Reserved. 014 015Contributor(s): ______________________________________. 016 017Alternatively, the contents of this file may be used under the terms of the 018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 019applicable instead of those above. If you wish to allow use of your version of this 020file only under the terms of the GPL and not to allow others to use your version 021of this file under the MPL, indicate your decision by deleting the provisions above 022and replace them with the notice and other provisions required by the GPL License. 023If you do not delete the provisions above, a recipient may use your version of 024this file under either the MPL or the GPL. 025 */ 026package ca.uhn.hl7v2.validation.builder; 027 028import java.util.Collection; 029import java.util.Collections; 030import java.util.List; 031import java.util.Set; 032 033import ca.uhn.hl7v2.Version; 034import ca.uhn.hl7v2.model.MessageVisitorFactory; 035import ca.uhn.hl7v2.validation.MessageRule; 036import ca.uhn.hl7v2.validation.Rule; 037import ca.uhn.hl7v2.validation.builder.support.*; 038import ca.uhn.hl7v2.validation.impl.ConformanceProfileRule; 039import ca.uhn.hl7v2.validation.impl.MessageRuleBinding; 040import ca.uhn.hl7v2.validation.impl.RuleBinding; 041 042/** 043 * Rule Builder for MessageRules 044 * 045 * @author Christian Ohr 046 */ 047@SuppressWarnings("serial") 048public class MessageRuleBuilder extends RuleTypeBuilder<MessageRuleBuilder, MessageRule> { 049 050 private String messageType; 051 private String triggerEvent; 052 053 protected MessageRuleBuilder(List<RuleBinding<? extends Rule<?>>> rules, Set<Version> versions, 054 String messageType, String triggerEvent) { 055 super(rules, versions); 056 this.messageType = messageType; 057 this.triggerEvent = triggerEvent; 058 } 059 060 /** 061 * Builds a {@link MessageRule} that extracts a primitive value using a {@link ca.uhn.hl7v2.util.Terser} 062 * expression and evaluates the specified {@link Predicate}. 063 * 064 * @param spec Terser expression 065 * @param predicate Predicate to evaluate against the value 066 * @return this instance to build more rules 067 */ 068 public MessageRuleBuilder terser(String spec, Predicate predicate) { 069 return test(prepareRule(new TerserMessageRule(spec, predicate))); 070 } 071 072 /** 073 * Builds a {@link MessageRule} that runs a 074 * {@link ca.uhn.hl7v2.validation.builder.support.ValidatingMessageVisitor ValidatingMessageVisitor} 075 * over the message that collects {@link ca.uhn.hl7v2.validation.ValidationException ValidationExceptions} 076 * in a single pass. 077 * 078 * @param visitorFactory MessageVisitorFactory that creates ValidatingMessageVisitor instances 079 * @param predicate Predicate to evaluate against the value 080 * @return this instance to build more rules 081 */ 082 public MessageRuleBuilder inspect(MessageVisitorFactory<? extends ValidatingMessageVisitor> visitorFactory) { 083 return test(prepareRule(new VisitorMessageRule(visitorFactory))); 084 } 085 086 /** 087 * Builds a {@link MessageRule} that disallows the existence of {@link ca.uhn.hl7v2.model.GenericSegment}s, i.e. 088 * segments that are not defined in the structure of a message. 089 * 090 * @return this instance to build more rules 091 */ 092 public MessageRuleBuilder onlyKnownSegments() { 093 return test(prepareRule(OnlyKnownSegmentsRule.ONLY_KNOWN_SEGMENTS)); 094 } 095 096 /** 097 * Builds a {@link MessageRule} that disallows the existence of segments which 098 * are not alowed in a given message type when the message is an instance 099 * of {@link ca.uhn.hl7v2.model.SuperStructure} (e.g. PID2 within an ADT^A01) 100 * 101 * @return this instance to build more rules 102 */ 103 public MessageRuleBuilder onlyAllowableSegmentsInSuperStructure() { 104 return test(prepareRule(OnlyAllowableSegmentsInSuperstructureRule.ONLY_ALLOWABLE_SEGMENTS)); 105 } 106 107 /** 108 * Builds a {@link MessageRule} that enforces choice elements. This means that 109 * if several segments are listed as being a possible choice for the first segment 110 * in a group, only one of them may have content. 111 * 112 * @return this instance to build more rules 113 */ 114 public MessageRuleBuilder choiceElementsRespected() { 115 return test(prepareRule(ChoiceElementsRespectedRule.CHOICE_ELEMENTS_RESPECTED)); 116 } 117 118 /** 119 * Builds a {@link MessageRule} that disallows the selected HL7 version(s). It is basically 120 * equivalent with: 121 * 122 * <pre> 123 * forAllVersions().message(....).terser("MSH-12", in(allowedVersions)) 124 * </pre> 125 * 126 * However, when using this specific rule the builder expression and the resulting exception 127 * message is more specific: 128 * 129 * <pre> 130 * forVersion().except(allowedVersions).message(...).wrongVersion() 131 * </pre> 132 * 133 * @return this instance to build more rules 134 */ 135 public MessageRuleBuilder wrongVersion() { 136 return test(prepareRule(WrongVersionRule.WRONG_VERSION)); 137 } 138 139 /** 140 * Builds a {@link MessageRule} that evaluates the message against the Conformance Profile 141 * referred to in MSH-21. 142 * 143 * @return this instance to build more rules 144 */ 145 public MessageRuleBuilder conformance() { 146 return conformance(null); 147 } 148 149 /** 150 * Builds a {@link MessageRule} that evaluates the message against the Conformance Profile 151 * referred to by the profileId parameter 152 * 153 * @param profileId conformance profile id (file name) 154 * @return this instance to build more rules 155 */ 156 public MessageRuleBuilder conformance(String profileId) { 157 return test(prepareRule(new ConformanceProfileRule(profileId))); 158 } 159 160 // for tests only 161 String getMessageType() { 162 return messageType; 163 } 164 165 // for tests only 166 String getTriggerEvent() { 167 return triggerEvent; 168 } 169 170 @Override 171 protected Collection<RuleBinding<MessageRule>> getRuleBindings(MessageRule rule, String version) { 172 RuleBinding<MessageRule> binding = new MessageRuleBinding(version, messageType, 173 triggerEvent, rule); 174 return activate(Collections.singletonList(binding)); 175 } 176 177}