Coverage Report - ca.uhn.hl7v2.validation.impl.ConformanceProfileRule
 
Classes in this File Line Coverage Branch Coverage Complexity
ConformanceProfileRule
84%
48/57
85%
12/14
2.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 "ConformanceProfileRule.java".  Description: 
 10  
 "A MessageRule that checks conformance to message profiles." 
 11  
 
 12  
 The Initial Developer of the Original Code is University Health Network. Copyright (C) 
 13  
 2005.  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.impl;
 27  
 
 28  
 import java.io.IOException;
 29  
 import java.util.ArrayList;
 30  
 import java.util.Arrays;
 31  
 import java.util.List;
 32  
 
 33  
 import org.slf4j.Logger;
 34  
 import org.slf4j.LoggerFactory;
 35  
 
 36  
 import ca.uhn.hl7v2.HL7Exception;
 37  
 import ca.uhn.hl7v2.HapiContext;
 38  
 import ca.uhn.hl7v2.conf.ProfileException;
 39  
 import ca.uhn.hl7v2.conf.check.Validator;
 40  
 import ca.uhn.hl7v2.conf.parser.ProfileParser;
 41  
 import ca.uhn.hl7v2.conf.spec.RuntimeProfile;
 42  
 import ca.uhn.hl7v2.conf.store.ProfileStore;
 43  
 import ca.uhn.hl7v2.model.Message;
 44  
 import ca.uhn.hl7v2.util.Terser;
 45  
 import ca.uhn.hl7v2.validation.ValidationException;
 46  
 
 47  
 /**
 48  
  * A MessageRule that checks conformance to message profiles. Messages can either be tested 
 49  
  * against the profiles they declare, or against a pre-defined profile. If you want both, 
 50  
  * use two <code>ConformanceProfileRule</code>s.  
 51  
  * 
 52  
  * @author Bryan Tripp
 53  
  * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:40 $ by $Author: jamesagnew $
 54  
  */
 55  6
 @SuppressWarnings("serial")
 56  
 public class ConformanceProfileRule extends AbstractMessageRule {
 57  
 
 58  1
     private static final Logger log = LoggerFactory.getLogger(ConformanceProfileRule.class);
 59  1
     private static final ProfileParser PARSER = new ProfileParser(true);
 60  
     private String myProfileID;
 61  
 
 62  
     /**
 63  
      * Creates an instance that tests messages against whatever profiles they declare in 
 64  
      * MSH-21. The ID declared in MSH-21 is evaluated in a way that the corresponding
 65  
      * profile file is expected to be BASEDIR/profiles/ID.xml.
 66  
      */
 67  
     public ConformanceProfileRule() {
 68  4
             super();
 69  4
             setDescription("Unknown segments found in message");
 70  4
             setSectionReference("HL7 2.5 section 2.12");
 71  4
     }
 72  
     
 73  
     /**
 74  
      * @param theProfileID the ID of a constant profile against which to test all messages
 75  
      *      (instead of the profiles they declare in MSH-21). The ID is evaluated in a way
 76  
      *      that the corresponding profile file is expected to be BASEDIR/profiles/ID.xml.
 77  
      */
 78  
     public ConformanceProfileRule(String theProfileID) {
 79  3
             this();
 80  3
         myProfileID = theProfileID;
 81  3
     }
 82  
     
 83  
 
 84  
     /** 
 85  
      * @see ca.uhn.hl7v2.validation.MessageRule#test(ca.uhn.hl7v2.model.Message)
 86  
      */
 87  
     public ValidationException[] apply(Message msg) {
 88  6
         List<ValidationException> problems = new ArrayList<ValidationException>();
 89  6
         String[] ids = {myProfileID};
 90  
         
 91  
         try {
 92  6
             if (myProfileID == null) {
 93  3
                 ids = getDeclaredProfileIDs(msg);
 94  
             }
 95  
             
 96  12
             for (String id : ids) {
 97  6
                 log.debug("Testing message against profile: {}", id);
 98  
                 try {
 99  6
                     ValidationException[] shortList = testAgainstProfile(msg, id);
 100  6
                     log.debug("{} non-conformances", shortList.length);
 101  6
                     problems.addAll(Arrays.asList(shortList));
 102  0
                 } catch (ProfileException e) {
 103  0
                     problems.add(new ValidationException("Can't validate against profile: " + e.getMessage(), e));
 104  6
                 }
 105  
             }            
 106  0
         } catch (HL7Exception e) {
 107  0
             problems.add(new ValidationException("Can't validate against profile: " + e.getMessage(), e));
 108  6
         }
 109  
         
 110  6
         return problems.toArray(new ValidationException[problems.size()]);
 111  
     }
 112  
     
 113  
     private String[] getDeclaredProfileIDs(Message theMessage) throws HL7Exception {
 114  3
         Terser t = new Terser(theMessage);
 115  3
         boolean noMore = false;
 116  3
         int c = 0;
 117  3
         List<String> declaredProfiles = new ArrayList<String>(8);
 118  9
         while (!noMore) {
 119  6
             String path = "MSH-21(" + c++ + ")";
 120  6
             String idRep = t.get(path);
 121  
             //FIXME fails if empty rep precedes full rep ... should add getAll() to Terser and use that
 122  6
             if (idRep == null || idRep.equals("")) {
 123  3
                 noMore = true;
 124  
             } else {
 125  3
                 declaredProfiles.add(idRep);
 126  
             }
 127  6
         }
 128  3
         return declaredProfiles.toArray(new String[declaredProfiles.size()]);
 129  
     }
 130  
     
 131  
     private ValidationException[] testAgainstProfile(Message message, String id) throws ProfileException, HL7Exception {
 132  
         HL7Exception[] exceptions;
 133  6
         HapiContext context = message.getParser().getHapiContext();
 134  6
         Validator validator = context.getConformanceValidator();
 135  
         try {
 136  6
             ProfileStore profileStore = context.getProfileStore();
 137  6
             String profileString = profileStore.getProfile(id);
 138  6
             if (profileString != null) {
 139  6
                 RuntimeProfile profile = PARSER.parse(profileString);               
 140  6
                 exceptions = validator.validate(message, profile.getMessage());
 141  6
             } else {
 142  0
                 throw new ProfileException("Unable to find the profile " + id);
 143  
             }
 144  0
         } catch (IOException e) {
 145  0
             throw new ProfileException("Error retreiving profile " + id, e);
 146  6
         }
 147  
         
 148  6
         ValidationException[] result = new ValidationException[exceptions.length];
 149  53
         for (int i = 0; i < exceptions.length; i++) {
 150  47
             result[i] = ValidationException.fromHL7Exception(exceptions[i]);
 151  
         }
 152  6
         return result;
 153  
     }
 154  
     
 155  
 
 156  
     /** 
 157  
      * @see ca.uhn.hl7v2.validation.Rule#getDescription()
 158  
      */
 159  
     public String getDescription() {
 160  0
         return "expected conformance to declared or predefined message profiles";
 161  
     }
 162  
 
 163  
     /** 
 164  
      * @see ca.uhn.hl7v2.validation.Rule#getSectionReference()
 165  
      */
 166  
     public String getSectionReference() {
 167  0
         return "HL7 2.5 section 2.12";
 168  
     }
 169  
 
 170  
         public String getProfileID() {
 171  2
                 return myProfileID;
 172  
         }
 173  
     
 174  
     
 175  
 
 176  
 }