1 | |
package ca.uhn.hl7v2.util; |
2 | |
|
3 | |
import java.io.BufferedReader; |
4 | |
import java.io.File; |
5 | |
import java.io.FileFilter; |
6 | |
import java.io.FileReader; |
7 | |
import java.io.FilenameFilter; |
8 | |
import java.io.IOException; |
9 | |
import java.util.HashMap; |
10 | |
import java.util.Map; |
11 | |
import java.util.StringTokenizer; |
12 | |
|
13 | |
import org.slf4j.Logger; |
14 | |
import org.slf4j.LoggerFactory; |
15 | |
|
16 | |
import ca.uhn.hl7v2.ErrorCode; |
17 | |
import ca.uhn.hl7v2.HL7Exception; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class FileCodeMapper extends CodeMapper { |
42 | |
|
43 | 0 | private static final Logger log = LoggerFactory.getLogger(FileCodeMapper.class); |
44 | |
|
45 | 0 | private boolean throwIfNoMatch = false; |
46 | |
File baseDir; |
47 | |
private Map<String, Map<String, Map<String, String>>> interfaceToLocal; |
48 | |
private Map<String, Map<String, Map<String, String>>> localToInterface; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 0 | public FileCodeMapper() throws HL7Exception { |
58 | 0 | baseDir = new File(Home.getHomeDirectory().getAbsolutePath() + "/codemap"); |
59 | 0 | refreshCache(); |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public void refreshCache() throws HL7Exception { |
67 | 0 | localToInterface = new HashMap<String, Map<String, Map<String, String>>>(10); |
68 | 0 | interfaceToLocal = new HashMap<String, Map<String, Map<String, String>>>(10); |
69 | |
|
70 | 0 | log.info("Refreshing cache"); |
71 | |
|
72 | |
try { |
73 | |
|
74 | 0 | File[] interfaceDirs = this.baseDir.listFiles(new FileFilter() { |
75 | |
public boolean accept(File pathname) { |
76 | 0 | boolean acc = false; |
77 | 0 | if (pathname.isDirectory()) |
78 | 0 | acc = true; |
79 | 0 | return acc; |
80 | |
} |
81 | |
}); |
82 | |
|
83 | |
|
84 | 0 | for (int i = 0; i < interfaceDirs.length; i++) { |
85 | |
|
86 | 0 | log.info( |
87 | 0 | "Checking directory {} for interface code maps.", interfaceDirs[i].getName()); |
88 | |
|
89 | |
|
90 | 0 | File[] mapFiles = interfaceDirs[i].listFiles(new FilenameFilter() { |
91 | |
public boolean accept(File dir, String name) { |
92 | 0 | boolean acc = false; |
93 | 0 | if (name.toUpperCase().startsWith("HL7")) { |
94 | 0 | if (name.substring(name.lastIndexOf('.')).equals(".li") |
95 | 0 | || name.substring(name.lastIndexOf('.')).equals(".il")) |
96 | 0 | acc = true; |
97 | |
} |
98 | 0 | return acc; |
99 | |
} |
100 | |
}); |
101 | |
|
102 | |
|
103 | 0 | HashMap<String, Map<String, String>> li = new HashMap<String, Map<String, String>>(50); |
104 | 0 | HashMap<String, Map<String, String>> il = new HashMap<String, Map<String, String>>(50); |
105 | 0 | for (int j = 0; j < mapFiles.length; j++) { |
106 | 0 | log.info("Reading map entries from file {}", mapFiles[j]); |
107 | |
|
108 | 0 | String fName = mapFiles[j].getName(); |
109 | 0 | String tableName = fName.substring(0, fName.lastIndexOf('.')); |
110 | 0 | String mapDirection = fName.substring(fName.lastIndexOf('.') + 1); |
111 | |
|
112 | |
|
113 | 0 | Map<String, String> codeMap = new HashMap<String, String>(25); |
114 | 0 | BufferedReader in = null; |
115 | |
try { |
116 | 0 | in = new BufferedReader(new FileReader(mapFiles[j])); |
117 | 0 | while (in.ready()) { |
118 | 0 | String line = in.readLine(); |
119 | 0 | if (!line.startsWith("//")) { |
120 | 0 | StringTokenizer tok = new StringTokenizer(line, "\t", false); |
121 | 0 | String from = null; |
122 | 0 | String to = null; |
123 | 0 | if (tok.hasMoreTokens()) |
124 | 0 | from = tok.nextToken(); |
125 | 0 | if (tok.hasMoreTokens()) |
126 | 0 | to = tok.nextToken(); |
127 | 0 | if (from != null && to != null) |
128 | 0 | codeMap.put(from, to); |
129 | |
} |
130 | 0 | } |
131 | |
} |
132 | |
finally { |
133 | 0 | if (in != null) in.close(); |
134 | |
} |
135 | |
|
136 | |
|
137 | 0 | if (mapDirection.equals("il")) { |
138 | 0 | il.put(tableName.toUpperCase(), codeMap); |
139 | 0 | log.debug("Adding {} codes to interface -> local map for {} in {} interface", |
140 | 0 | new Object[] {codeMap.size(), tableName, interfaceDirs[i].getName()}); |
141 | |
} |
142 | |
else { |
143 | 0 | li.put(tableName.toUpperCase(), codeMap); |
144 | 0 | log.debug("Adding {} codes to local -> interface map for {} in {} interface", |
145 | 0 | new Object[] {codeMap.size(), tableName, interfaceDirs[i].getName()}); |
146 | |
} |
147 | |
} |
148 | |
|
149 | |
|
150 | 0 | interfaceToLocal.put(interfaceDirs[i].getName(), il); |
151 | 0 | localToInterface.put(interfaceDirs[i].getName(), li); |
152 | |
} |
153 | |
|
154 | |
} |
155 | 0 | catch (IOException e) { |
156 | 0 | throw new HL7Exception( |
157 | |
"Can't read interface code maps from disk", e); |
158 | 0 | } |
159 | 0 | } |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public String getLocalCode(String interfaceName, int hl7Table, String interfaceCode) throws HL7Exception { |
166 | 0 | String localCode = null; |
167 | |
try { |
168 | 0 | Map<String, Map<String, String>> interfaceMap = interfaceToLocal.get(interfaceName); |
169 | 0 | localCode = getCode(interfaceMap, hl7Table, interfaceCode); |
170 | |
} |
171 | 0 | catch (NullPointerException npe) { |
172 | 0 | if (this.throwIfNoMatch) |
173 | 0 | throw new HL7Exception( |
174 | |
"No local mapping for the interface code " |
175 | |
+ interfaceCode |
176 | |
+ " for HL7 table " |
177 | |
+ hl7Table |
178 | |
+ " for the interface '" |
179 | |
+ interfaceName |
180 | |
+ "'", |
181 | |
ErrorCode.TABLE_VALUE_NOT_FOUND); |
182 | 0 | } |
183 | 0 | return localCode; |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
private String getCode(Map<String, Map<String, String>> interfaceMap, int hl7Table, String code) { |
190 | 0 | String ret = null; |
191 | |
|
192 | |
|
193 | 0 | StringBuffer tableName = new StringBuffer(); |
194 | 0 | tableName.append("HL7"); |
195 | 0 | if (hl7Table < 1000) |
196 | 0 | tableName.append("0"); |
197 | 0 | if (hl7Table < 100) |
198 | 0 | tableName.append("0"); |
199 | 0 | if (hl7Table < 10) |
200 | 0 | tableName.append("0"); |
201 | 0 | tableName.append(hl7Table); |
202 | 0 | Map<String, String> tableMap = interfaceMap.get(tableName.toString()); |
203 | |
|
204 | |
|
205 | 0 | ret = tableMap.get(code).toString(); |
206 | 0 | return ret; |
207 | |
} |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public String getInterfaceCode(String interfaceName, int hl7Table, String localCode) throws HL7Exception { |
214 | 0 | String interfaceCode = null; |
215 | |
try { |
216 | 0 | Map<String, Map<String, String>> interfaceMap = localToInterface.get(interfaceName); |
217 | 0 | interfaceCode = getCode(interfaceMap, hl7Table, localCode); |
218 | |
} |
219 | 0 | catch (NullPointerException npe) { |
220 | 0 | if (this.throwIfNoMatch) |
221 | 0 | throw new HL7Exception( |
222 | |
"No interface mapping for the local code " |
223 | |
+ localCode |
224 | |
+ " for HL7 table " |
225 | |
+ hl7Table |
226 | |
+ " for the interface '" |
227 | |
+ interfaceName |
228 | |
+ "'", |
229 | |
ErrorCode.TABLE_VALUE_NOT_FOUND); |
230 | 0 | } |
231 | 0 | return interfaceCode; |
232 | |
} |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
public void throwExceptionIfNoMatch(boolean throwException) { |
240 | 0 | this.throwIfNoMatch = throwException; |
241 | 0 | } |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
public static void main(String args[]) { |
247 | |
try { |
248 | |
|
249 | 0 | CodeMapper.getInstance().throwExceptionIfNoMatch(true); |
250 | 0 | System.out.println("Local code for M is " + CodeMapper.getLocal("test", 1, "M")); |
251 | 0 | System.out.println("Interface code for female is " + CodeMapper.getInt("test", 1, "female")); |
252 | |
|
253 | |
} |
254 | 0 | catch (HL7Exception e) { |
255 | 0 | e.printStackTrace(); |
256 | 0 | } |
257 | 0 | } |
258 | |
|
259 | |
} |