Coverage Report - ca.uhn.hl7v2.validation.builder.MessageRuleBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageRuleBuilder
100%
16/16
N/A
1
 
 1  
 /**
 2  
 The contents of this file are subject to the Mozilla Public License Version 1.1 
 3  
 (the "License"); you may not use this file except in compliance with the License. 
 4  
 You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
 5  
 Software distributed under the License is distributed on an "AS IS" basis, 
 6  
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
 7  
 specific language governing rights and limitations under the License. 
 8  
 
 9  
 The Original Code is "MessageRuleBuilder.java".  Description: 
 10  
 "Rule Builder for MessageRules." 
 11  
 
 12  
 The Initial Developer of the Original Code is University Health Network. Copyright (C) 
 13  
 2012.  All Rights Reserved. 
 14  
 
 15  
 Contributor(s): ______________________________________. 
 16  
 
 17  
 Alternatively, the contents of this file may be used under the terms of the 
 18  
 GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
 19  
 applicable instead of those above.  If you wish to allow use of your version of this 
 20  
 file only under the terms of the GPL and not to allow others to use your version 
 21  
 of this file under the MPL, indicate your decision by deleting  the provisions above 
 22  
 and replace  them with the notice and other provisions required by the GPL License.  
 23  
 If you do not delete the provisions above, a recipient may use your version of 
 24  
 this file under either the MPL or the GPL. 
 25  
  */
 26  
 package ca.uhn.hl7v2.validation.builder;
 27  
 
 28  
 import java.util.Collection;
 29  
 import java.util.Collections;
 30  
 import java.util.List;
 31  
 import java.util.Set;
 32  
 
 33  
 import ca.uhn.hl7v2.Version;
 34  
 import ca.uhn.hl7v2.model.GenericSegment;
 35  
 import ca.uhn.hl7v2.model.SuperStructure;
 36  
 import ca.uhn.hl7v2.util.Terser;
 37  
 import ca.uhn.hl7v2.validation.MessageRule;
 38  
 import ca.uhn.hl7v2.validation.Rule;
 39  
 import ca.uhn.hl7v2.validation.builder.support.ChoiceElementsRespectedRule;
 40  
 import ca.uhn.hl7v2.validation.builder.support.OnlyAllowableSegmentsInSuperstructureRule;
 41  
 import ca.uhn.hl7v2.validation.builder.support.OnlyKnownSegmentsRule;
 42  
 import ca.uhn.hl7v2.validation.builder.support.TerserMessageRule;
 43  
 import ca.uhn.hl7v2.validation.builder.support.WrongVersionRule;
 44  
 import ca.uhn.hl7v2.validation.impl.ConformanceProfileRule;
 45  
 import ca.uhn.hl7v2.validation.impl.MessageRuleBinding;
 46  
 import ca.uhn.hl7v2.validation.impl.RuleBinding;
 47  
 
 48  
 /**
 49  
  * Rule Builder for MessageRules
 50  
  * 
 51  
  * @author Christian Ohr
 52  
  */
 53  927
 @SuppressWarnings("serial")
 54  
 public class MessageRuleBuilder extends RuleTypeBuilder<MessageRuleBuilder, MessageRule> {
 55  
 
 56  
         private String messageType;
 57  
         private String triggerEvent;
 58  
 
 59  
         protected MessageRuleBuilder(List<RuleBinding<? extends Rule<?>>> rules, Set<Version> versions,
 60  
                         String messageType, String triggerEvent) {
 61  930
                 super(rules, versions);
 62  930
                 this.messageType = messageType;
 63  930
                 this.triggerEvent = triggerEvent;
 64  930
         }
 65  
         
 66  
         /**
 67  
          * Builds a {@link MessageRule} that extracts a primitive value using a {@link Terser}
 68  
          * expression and evaluates the specified {@link Predicate}.
 69  
          * 
 70  
          * @param spec Terser expression
 71  
          * @param predicate Predicate to evaluate against the value
 72  
          * @return this instance to build more rules
 73  
          */
 74  
         public MessageRuleBuilder terser(String spec, Predicate predicate) {
 75  1
                 return test(prepareRule(new TerserMessageRule(spec, predicate)));
 76  
         }
 77  
 
 78  
         /**
 79  
          * Builds a {@link MessageRule} that disallows the existence of {@link GenericSegment}s, i.e.
 80  
          * segments that are not defined in the structure of a message.
 81  
          * 
 82  
          * @return this instance to build more rules
 83  
          */
 84  
         public MessageRuleBuilder onlyKnownSegments() {
 85  1
                 return test(prepareRule(OnlyKnownSegmentsRule.ONLY_KNOWN_SEGMENTS));
 86  
         }
 87  
 
 88  
         /**
 89  
          * Builds a {@link MessageRule} that disallows the existence of segments which
 90  
          * are not alowed in a given message type when the message is an instance
 91  
          * of {@link SuperStructure} (e.g. PID2 within an ADT^A01)
 92  
          * 
 93  
          * @return this instance to build more rules
 94  
          */
 95  
         public MessageRuleBuilder onlyAllowableSegmentsInSuperStructure() {
 96  458
                 return test(prepareRule(OnlyAllowableSegmentsInSuperstructureRule.ONLY_ALLOWABLE_SEGMENTS));
 97  
         }
 98  
 
 99  
         /**
 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  458
                 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  1
                 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  1
                 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  2
                 return test(prepareRule(new ConformanceProfileRule(profileId)));
 150  
         }
 151  
 
 152  
         // for tests only
 153  
         String getMessageType() {
 154  1
                 return messageType;
 155  
         }
 156  
 
 157  
         // for tests only
 158  
         String getTriggerEvent() {
 159  2
                 return triggerEvent;
 160  
         }
 161  
 
 162  
         @Override
 163  
         protected Collection<RuleBinding<MessageRule>> getRuleBindings(MessageRule rule, String version) {
 164  927
                 RuleBinding<MessageRule> binding = new MessageRuleBinding(version, messageType,
 165  
                                 triggerEvent, rule);
 166  927
                 return activate(Collections.singletonList(binding));
 167  
         }
 168  
 
 169  
 }