Coverage Report - ca.uhn.hl7v2.validation.builder.support.OnlyAllowableSegmentsInSuperstructureRule
 
Classes in this File Line Coverage Branch Coverage Complexity
OnlyAllowableSegmentsInSuperstructureRule
91%
21/23
85%
12/14
5.5
 
 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 "OnlyKnownSegmentsRule.java".  Description: 
 10  
 "Validation rule for detecting unknown Segments in a message" 
 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.support;
 27  
 
 28  
 import java.util.ArrayList;
 29  
 import java.util.List;
 30  
 import java.util.Set;
 31  
 
 32  
 import ca.uhn.hl7v2.HL7Exception;
 33  
 import ca.uhn.hl7v2.model.Message;
 34  
 import ca.uhn.hl7v2.model.Structure;
 35  
 import ca.uhn.hl7v2.model.SuperStructure;
 36  
 import ca.uhn.hl7v2.validation.MessageRule;
 37  
 import ca.uhn.hl7v2.validation.ValidationException;
 38  
 import ca.uhn.hl7v2.validation.impl.AbstractMessageRule;
 39  
 
 40  
 /**
 41  
  * Validation rule for detecting populated elements in a {@link SuperStructure}
 42  
  * which do not belong to the given message type
 43  
  * 
 44  
  * @author James Agnew
 45  
  */
 46  5003
 @SuppressWarnings("serial")
 47  5
 public class OnlyAllowableSegmentsInSuperstructureRule extends AbstractMessageRule {
 48  
 
 49  5
         public static final MessageRule ONLY_ALLOWABLE_SEGMENTS = new OnlyAllowableSegmentsInSuperstructureRule();
 50  
         
 51  
         public ValidationException[] apply(Message msg) {
 52  5003
                 List<ValidationException> exceptions = new ArrayList<ValidationException>();
 53  
                 
 54  5003
                 if (msg instanceof SuperStructure) {
 55  30
                         checkStructure((SuperStructure)msg, exceptions);
 56  
                 }
 57  
                 
 58  5003
                 return exceptions.toArray(new ValidationException[exceptions.size()]);
 59  
         }
 60  
 
 61  
         private void checkStructure(SuperStructure theMsg, List<ValidationException> theExceptions) {
 62  30
                 String messageStructure = theMsg.getMessage().getName();
 63  30
                 Set<String> nonStandardNames = theMsg.getNonStandardNames();
 64  
 
 65  
                 FORNAME:
 66  1025
                 for (String name : theMsg.getNames()) {
 67  995
                         if (nonStandardNames != null && nonStandardNames.contains(name)) {
 68  5
                                 continue;
 69  
                         }
 70  
                         
 71  
                         try {
 72  990
                                 for (Structure rep : theMsg.getAll(name)) {
 73  
                                         
 74  100
                                         if (!rep.isEmpty()) {
 75  100
                                                 Set<String> structuresWhichChildAppliesTo = theMsg.getStructuresWhichChildAppliesTo(name);
 76  100
                                                 if (!structuresWhichChildAppliesTo.contains(messageStructure)) {
 77  5
                                                         String msgSimpleName = theMsg.getMessage().getClass().getSimpleName();
 78  5
                                                         theExceptions.add(new ValidationException("Message (superstructure " + msgSimpleName + ") of type " + messageStructure + " must not have content in " + name));
 79  
                                                 }
 80  100
                                                 continue FORNAME;
 81  
                                         }
 82  
                                         
 83  
                                 }
 84  0
                         } catch (HL7Exception e) {
 85  
                                 // should not happen
 86  0
                                 throw new Error("Can't get rep of structure " + name + ". This is probably a HAPI bug");
 87  890
                         }
 88  
                         
 89  
                 }
 90  30
         }
 91  
 
 92  
 }