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  23
 public abstract class AbstractCodeStore implements CodeStore {
 15  
 
 16  1
     private static Logger log = LoggerFactory.getLogger(AbstractCodeStore.class);
 17  
     
 18  1
     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  173
             for (String validCode : getValidCodes(codeSystem)) {
 31  171
                 if (checkCode(code, validCode)) return true;
 32  
             }
 33  2
             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  187
         if (code.equals(pattern)) return true;
 51  
         
 52  181
         if (pattern.indexOf(' ') >= 0 || pattern.indexOf(',') >= 0) {
 53  9
             StringTokenizer tok = new StringTokenizer(pattern, ", ", false);
 54  26
             while (tok.hasMoreTokens()) {
 55  20
                 String t = tok.nextToken();
 56  20
                 if (!t.equalsIgnoreCase("or") && checkCode(code, t)) 
 57  3
                     return true;
 58  17
             }
 59  6
         } else {
 60  849
             for (RegisteredPattern wildcard : WILDCARDS) {
 61  681
                 if (wildcard.matches(pattern, code)) 
 62  4
                     return true;
 63  
             }
 64  
         }
 65  
         
 66  174
         return false;
 67  
     }
 68  
 
 69  
 }