View Javadoc

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 "DBTableRepository.java".  Description: 
10  "Implements TableRepository by looking up values from the default HL7
11    normative database" 
12  
13  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
14  2001.  All Rights Reserved. 
15  
16  Contributor(s): ______________________________________. 
17  
18  Alternatively, the contents of this file may be used under the terms of the 
19  GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
20  applicable instead of those above.  If you wish to allow use of your version of this 
21  file only under the terms of the GPL and not to allow others to use your version 
22  of this file under the MPL, indicate your decision by deleting  the provisions above 
23  and replace  them with the notice and other provisions required by the GPL License.  
24  If you do not delete the provisions above, a recipient may use your version of 
25  this file under either the MPL or the GPL. 
26  
27  */
28  
29  package ca.uhn.hl7v2.database;
30  
31  import java.sql.Connection;
32  import java.sql.ResultSet;
33  import java.sql.SQLException;
34  import java.sql.Statement;
35  import java.util.HashMap;
36  
37  import ca.uhn.hl7v2.LookupException;
38  import ca.uhn.hl7v2.UndefinedTableException;
39  import ca.uhn.hl7v2.UnknownValueException;
40  
41  /**
42   * <p>
43   * Implements TableRepository by looking up values from the default HL7
44   * normative database.  Values are cached after they are looked up once. 
45   * </p>
46   * <p>
47   * This can be used to provide vaidation of message coded values
48   * agaist the official HL7 Database, although you will need 
49   * a copy in order to use this.
50   * </p>
51   *  
52   * @author Bryan Tripp (bryan_tripp@sourceforge.net)
53   * @deprecated This has not been used in a long time. Please let us know if you are stil
54   * using it, otherwise it will be removed in a future release.
55   */
56  @SuppressWarnings("deprecation")
57  public class DBTableRepository extends TableRepository {
58  
59      private int[] tableList;
60      private HashMap<Integer, String[]> tables;
61      private final int bufferSize = 3000; //max # of tables or values that can be cached at a time
62      private NormativeDatabase normativeDatabase;
63  
64      protected DBTableRepository(String jdbcUrl) throws SQLException {
65          tableList = null;
66          tables = new HashMap<Integer, String[]>();
67          normativeDatabase = NormativeDatabase.getInstance();
68      }
69  
70      /**
71       * Returns a list of HL7 lookup tables that are defined in the normative database.  
72       */
73      public int[] getTables() throws LookupException {
74          if (this.tableList == null) {
75              try {
76                  Connection conn = normativeDatabase.getConnection();
77                  Statement stmt = conn.createStatement();
78                  ResultSet rs = stmt.executeQuery("select distinct table_id from TableValues");
79                  int[] roomyList = new int[bufferSize];
80                  int c = 0;
81                  while (rs.next()) {
82                      roomyList[c++] = rs.getInt(1);
83                  }
84                  stmt.close();
85                  normativeDatabase.returnConnection(conn);
86  
87                  this.tableList = new int[c];
88                  System.arraycopy(roomyList, 0, this.tableList, 0, c);
89              }
90              catch (SQLException sqle) {
91                  throw new LookupException("Can't get table list from database: " + sqle.getMessage());
92              }
93          }
94          return this.tableList;
95      }
96  
97      /**
98       * Returns true if the given value exists in the given table.
99       */
100     public boolean checkValue(int table, String value) throws UndefinedTableException, LookupException {
101         boolean exists = false;
102 
103         String[] values = this.getValues(table);
104 
105         int c = 0;
106         while (c < values.length && !exists) {
107             if (value.equals(values[c++]))
108                 exists = true;
109         }
110 
111         return exists;
112     }
113 
114     /**
115      * Returns a list of the values for the given table in the normative database. 
116      */
117 	public String[] getValues(int table) throws LookupException, UndefinedTableException {
118         Integer key = new Integer(table);
119         String[] values = null;
120 
121         //see if the value list exists in the cache
122         Object o = this.tables.get(key);
123 
124         if (o != null) {
125             values = (String[]) o;
126         }
127         else {
128             //not cached yet ...
129             int c;
130             String[] roomyValues = new String[bufferSize];
131 
132             try {
133                 Connection conn = normativeDatabase.getConnection();
134                 Statement stmt = conn.createStatement();
135                 StringBuffer sql = new StringBuffer("select table_value from TableValues where table_id = ");
136                 sql.append(table);
137                 ResultSet rs = stmt.executeQuery(sql.toString());
138 
139                 c = 0;
140                 while (rs.next()) {
141                     roomyValues[c++] = rs.getString(1);
142                 }
143 
144                 stmt.close();
145                 normativeDatabase.returnConnection(conn);
146             }
147             catch (SQLException sqle) {
148                 throw new LookupException("Couldn't look up values for table " + table + ": " + sqle.getMessage());
149             }
150 
151             if (c == 0)
152                 throw new UndefinedTableException("No values found for table " + table);
153 
154             values = new String[c];
155             System.arraycopy(roomyValues, 0, values, 0, c);
156 
157             tables.put(key, values);
158         }
159 
160         return values;
161     }
162 
163     /**
164      * Returns the description matching the table and value given.  As currently implemented
165      * this method performs a database call each time - caching should probably be added,
166      * although this method will probably not be used very often.   
167      */
168     public String getDescription(int table, String value) throws LookupException, UnknownValueException {
169         String description = null;
170 
171         StringBuffer sql = new StringBuffer("select Description from TableValues where table_id = ");
172         sql.append(table);
173         sql.append(" and table_value = '");
174         sql.append(value);
175         sql.append("'");
176 
177         try {
178             Connection conn = normativeDatabase.getConnection();
179             Statement stmt = conn.createStatement();
180             ResultSet rs = stmt.executeQuery(sql.toString());
181             if (rs.next()) {
182                 description = rs.getString(1);
183             }
184             else {
185                 throw new UnknownValueException(
186                     "The value " + value + " could not be found in the table " + table + " - SQL: " + sql.toString());
187             }
188             stmt.close();
189             normativeDatabase.returnConnection(conn);
190         }
191         catch (SQLException e) {
192             throw new LookupException( "Can't find value " + value + " in table " + table, e );
193         }
194 
195         return description;
196     }
197 
198     //test 
199     /*
200     public static void main(String args[]) {
201         try {
202           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
203           System.setProperty("ca.on.uhn.hl7.database.url", "jdbc:odbc:hl7");
204             
205           DBTableRepository rep = new DBTableRepository();
206           
207           int[] tables = rep.getTables();
208           for (int i = 0; i < tables.length; i++) {
209               //System.out.println("Table " + i + ": " + tables[i]);
210           }
211     
212           for (int c = 0; c < 10; c++) {
213               int table = 1;
214               String val = "M";
215               long before = System.currentTimeMillis();
216               boolean yep = rep.checkValue(table, val);
217               long after = System.currentTimeMillis();
218               System.out.println(val + " exists in table " + table + "? " + yep + " - checking took " + (after-before) + " ms");
219               
220           }
221           
222           for (int h = 0; h < 2; h++) {
223               int table = 1;
224               long before = System.currentTimeMillis();
225               String[] vals = rep.getValues(table);
226               long after = System.currentTimeMillis();
227               for (int i = 0; i < vals.length; i++) {
228                   System.out.println("Value " + i + ": " + vals[i] + ": " + rep.getDescription(table, vals[i]));
229               }
230               
231               System.out.println("That took " + (after-before) + " ms");
232           }
233           
234           
235         } catch (ClassNotFoundException cne) {
236             cne.printStackTrace();
237         } catch (LookupException le) {
238             le.printStackTrace();
239         } catch (UnknownValueException uve) {
240             uve.printStackTrace();
241         }
242     }*/
243 
244 }