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.GenericSegment; 035import ca.uhn.hl7v2.model.SuperStructure; 036import ca.uhn.hl7v2.util.Terser; 037import ca.uhn.hl7v2.validation.MessageRule; 038import ca.uhn.hl7v2.validation.Rule; 039import ca.uhn.hl7v2.validation.builder.support.ChoiceElementsRespectedRule; 040import ca.uhn.hl7v2.validation.builder.support.OnlyAllowableSegmentsInSuperstructureRule; 041import ca.uhn.hl7v2.validation.builder.support.OnlyKnownSegmentsRule; 042import ca.uhn.hl7v2.validation.builder.support.TerserMessageRule; 043import ca.uhn.hl7v2.validation.builder.support.WrongVersionRule; 044import ca.uhn.hl7v2.validation.impl.ConformanceProfileRule; 045import ca.uhn.hl7v2.validation.impl.MessageRuleBinding; 046import ca.uhn.hl7v2.validation.impl.RuleBinding; 047 048/** 049 * Rule Builder for MessageRules 050 * 051 * @author Christian Ohr 052 */ 053@SuppressWarnings("serial") 054public class MessageRuleBuilder extends RuleTypeBuilder<MessageRuleBuilder, MessageRule> { 055 056 private String messageType; 057 private String triggerEvent; 058 059 protected MessageRuleBuilder(List<RuleBinding<? extends Rule<?>>> rules, Set<Version> versions, 060 String messageType, String triggerEvent) { 061 super(rules, versions); 062 this.messageType = messageType; 063 this.triggerEvent = triggerEvent; 064 } 065 066 /** 067 * Builds a {@link MessageRule} that extracts a primitive value using a {@link Terser} 068 * expression and evaluates the specified {@link Predicate}. 069 * 070 * @param spec Terser expression 071 * @param predicate Predicate to evaluate against the value 072 * @return this instance to build more rules 073 */ 074 public MessageRuleBuilder terser(String spec, Predicate predicate) { 075 return test(prepareRule(new TerserMessageRule(spec, predicate))); 076 } 077 078 /** 079 * Builds a {@link MessageRule} that disallows the existence of {@link GenericSegment}s, i.e. 080 * segments that are not defined in the structure of a message. 081 * 082 * @return this instance to build more rules 083 */ 084 public MessageRuleBuilder onlyKnownSegments() { 085 return test(prepareRule(OnlyKnownSegmentsRule.ONLY_KNOWN_SEGMENTS)); 086 } 087 088 /** 089 * Builds a {@link MessageRule} that disallows the existence of segments which 090 * are not alowed in a given message type when the message is an instance 091 * of {@link SuperStructure} (e.g. PID2 within an ADT^A01) 092 * 093 * @return this instance to build more rules 094 */ 095 public MessageRuleBuilder onlyAllowableSegmentsInSuperStructure() { 096 return test(prepareRule(OnlyAllowableSegmentsInSuperstructureRule.ONLY_ALLOWABLE_SEGMENTS)); 097 } 098 099 /** 100 * Builds a {@link MessageRule} that enforces choice elements. This means that 101 * if several segments are listed as being a possible choice for the first segment 102 * in a group, only one of them may have content. 103 * 104 * @return this instance to build more rules 105 */ 106 public MessageRuleBuilder choiceElementsRespected() { 107 return test(prepareRule(ChoiceElementsRespectedRule.CHOICE_ELEMENTS_RESPECTED)); 108 } 109 110 /** 111 * Builds a {@link MessageRule} that disallows the selected HL7 version(s). It is basically 112 * equivalent with: 113 * 114 * <pre> 115 * forAllVersions().message(....).terser("MSH-12", in(allowedVersions)) 116 * </pre> 117 * 118 * However, when using this specific rule the builder expression and the resulting exception 119 * message is more specific: 120 * 121 * <pre> 122 * forVersion().except(allowedVersions).message(...).wrongVersion() 123 * </pre> 124 * 125 * @return this instance to build more rules 126 */ 127 public MessageRuleBuilder wrongVersion() { 128 return test(prepareRule(WrongVersionRule.WRONG_VERSION)); 129 } 130 131 /** 132 * Builds a {@link MessageRule} that evaluates the message against the Conformance Profile 133 * referred to in MSH-21. 134 * 135 * @return this instance to build more rules 136 */ 137 public MessageRuleBuilder conformance() { 138 return conformance(null); 139 } 140 141 /** 142 * Builds a {@link MessageRule} that evaluates the message against the Conformance Profile 143 * referred to by the profileId parameter 144 * 145 * @param profileId conformance profile id (file name) 146 * @return this instance to build more rules 147 */ 148 public MessageRuleBuilder conformance(String profileId) { 149 return test(prepareRule(new ConformanceProfileRule(profileId))); 150 } 151 152 // for tests only 153 String getMessageType() { 154 return messageType; 155 } 156 157 // for tests only 158 String getTriggerEvent() { 159 return triggerEvent; 160 } 161 162 @Override 163 protected Collection<RuleBinding<MessageRule>> getRuleBindings(MessageRule rule, String version) { 164 RuleBinding<MessageRule> binding = new MessageRuleBinding(version, messageType, 165 triggerEvent, rule); 166 return activate(Collections.singletonList(binding)); 167 } 168 169}