Coverage Report - ca.uhn.hl7v2.conf.store.AbstractCodeStore
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractCodeStore
86%
19/22
95%
19/20
9
 
 1  
 package ca.uhn.hl7v2.conf.store;
 2  
 
 3  
 import java.util.StringTokenizer;
 4  
 
 5  
 import org.slf4j.Logger;
 6  
 import org.slf4j.LoggerFactory;
 7  
 
 8  
 /**
 9  
  * Abstract class for used retrieving and validating codes from user defined and HL7 specific tables
 10  
  * that correspond to a conformance profile.
 11  
  * 
 12  
  * @author Neal Acharya
 13  
  */
 14  120
 public abstract class AbstractCodeStore implements CodeStore {
 15  
 
 16  5
     private static Logger log = LoggerFactory.getLogger(AbstractCodeStore.class);
 17  
     
 18  5
     private static final RegisteredPattern[] WILDCARDS = { 
 19  
             new RegisteredPattern("ISOnnnn", "ISO\\d\\d\\d\\d"),
 20  
             new RegisteredPattern("HL7nnnn", "HL7\\d\\d\\d\\d"), 
 21  
             new RegisteredPattern("99zzz", "99[\\w]*"),
 22  
             new RegisteredPattern("NNxxx", "99[\\w]*") 
 23  
             };
 24  
 
 25  
     /**
 26  
      * @see ca.uhn.hl7v2.conf.store.CodeStore#isValidCode(java.lang.String, java.lang.String)
 27  
      */
 28  
     public boolean isValidCode(String codeSystem, String code) {
 29  
         try {
 30  865
             for (String validCode : getValidCodes(codeSystem)) {
 31  855
                 if (checkCode(code, validCode)) return true;
 32  
             }
 33  10
             return false;
 34  
         }
 35  0
         catch (Exception e) {
 36  0
             log.error("Error checking code " + code + " in code system " + codeSystem, e);
 37  0
             return false;
 38  
         } 
 39  
     }
 40  
 
 41  
     /**
 42  
      * Checks a code for an exact match, and using certain sequences where some characters are
 43  
      * wildcards (e.g. HL7nnnn). If the pattern contains one of " or ", " OR ", or "," each operand
 44  
      * is checked.
 45  
      */
 46  
     private boolean checkCode(String code, String pattern) {
 47  
         // mod by Neal acharya - Do full match on with the pattern. If code matches pattern then
 48  
         // return true
 49  
         // else parse pattern to look for wildcard characters
 50  935
         if (code.equals(pattern)) return true;
 51  
         
 52  905
         if (pattern.indexOf(' ') >= 0 || pattern.indexOf(',') >= 0) {
 53  45
             StringTokenizer tok = new StringTokenizer(pattern, ", ", false);
 54  130
             while (tok.hasMoreTokens()) {
 55  100
                 String t = tok.nextToken();
 56  100
                 if (!t.equalsIgnoreCase("or") && checkCode(code, t)) 
 57  15
                     return true;
 58  85
             }
 59  30
         } else {
 60  4245
             for (RegisteredPattern wildcard : WILDCARDS) {
 61  3405
                 if (wildcard.matches(pattern, code)) 
 62  20
                     return true;
 63  
             }
 64  
         }
 65  
         
 66  870
         return false;
 67  
     }
 68  
 
 69  
 }