View Javadoc

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  public abstract class AbstractCodeStore implements CodeStore {
15  
16      private static Logger log = LoggerFactory.getLogger(AbstractCodeStore.class);
17      
18      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              for (String validCode : getValidCodes(codeSystem)) {
31                  if (checkCode(code, validCode)) return true;
32              }
33              return false;
34          }
35          catch (Exception e) {
36              log.error("Error checking code " + code + " in code system " + codeSystem, e);
37              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          if (code.equals(pattern)) return true;
51          
52          if (pattern.indexOf(' ') >= 0 || pattern.indexOf(',') >= 0) {
53              StringTokenizer tok = new StringTokenizer(pattern, ", ", false);
54              while (tok.hasMoreTokens()) {
55                  String t = tok.nextToken();
56                  if (!t.equalsIgnoreCase("or") && checkCode(code, t)) 
57                      return true;
58              }
59          } else {
60              for (RegisteredPattern wildcard : WILDCARDS) {
61                  if (wildcard.matches(pattern, code)) 
62                      return true;
63              }
64          }
65          
66          return false;
67      }
68  
69  }