1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.disid.fiebdc3;
20
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Set;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class Database {
57
58
59
60
61
62
63 public enum Charset {
64 ANSI("ANSI"), C850("850"), C437("437");
65
66 private final String code;
67
68
69
70
71
72
73
74 private Charset(String code) {
75 this.code = code;
76 }
77
78 @Override
79 public String toString() {
80 return code;
81 }
82
83
84
85
86
87
88
89
90 public static Charset parseCharset(String code) {
91 for (Charset c : Charset.values()) {
92 if (c.code.equals(code)) {
93 return c;
94 }
95 }
96 return null;
97 }
98 }
99
100 private String fileProperty;
101
102 private String fileFormat;
103
104 private String generatedBy;
105
106 private String header;
107
108 private Charset charset = Charset.C850;
109
110 private String comments;
111
112 private int infoType;
113
114 private int certNum;
115
116 private Date certDate;
117
118 private ConceptBreakdown root;
119
120 private Map<String, ConceptBreakdown> orphanBreakdowns = new HashMap<String, ConceptBreakdown>();
121
122 private Set<Measurement> orphanMeasurements = new HashSet<Measurement>();
123
124 private Map<String, Concept> concepts = new HashMap<String, Concept>();
125
126
127
128
129
130
131
132 public String getFileProperty() {
133 return fileProperty;
134 }
135
136 public void setFileProperty(String fileProperty) {
137 this.fileProperty = fileProperty;
138 }
139
140
141
142
143
144
145 public String getFileFormat() {
146 return fileFormat;
147 }
148
149 public void setFileFormat(String fileFormat) {
150 this.fileFormat = fileFormat;
151 }
152
153
154
155
156
157
158 public String getGeneratedBy() {
159 return generatedBy;
160 }
161
162 public void setGeneratedBy(String generatedBy) {
163 this.generatedBy = generatedBy;
164 }
165
166
167
168
169
170 public String getHeader() {
171 return header;
172 }
173
174 public void setHeader(String header) {
175 this.header = header;
176 }
177
178
179
180
181
182
183
184
185
186 public Charset getCharset() {
187 return charset;
188 }
189
190 public void setCharset(Charset charset) {
191 this.charset = charset;
192 }
193
194
195
196
197
198 public String getComments() {
199 return comments;
200 }
201
202 public void setComments(String comments) {
203 this.comments = comments;
204 }
205
206
207
208
209
210
211
212 public int getInfoType() {
213 return infoType;
214 }
215
216 public void setInfoType(int infoType) {
217 this.infoType = infoType;
218 }
219
220
221
222
223
224
225
226 public int getCertNum() {
227 return certNum;
228 }
229
230 public void setCertNum(int certNum) {
231 this.certNum = certNum;
232 }
233
234
235
236
237
238
239
240
241 public Date getCertDate() {
242 return certDate;
243 }
244
245 public void setCertDate(Date certDate) {
246 this.certDate = certDate;
247 }
248
249
250
251
252
253
254 public ConceptBreakdown getRoot() {
255 return root;
256 }
257
258 private void setRoot(ConceptBreakdown root) {
259 this.root = root;
260 }
261
262
263
264
265
266
267
268
269 public ConceptBreakdown setRoot(String code) {
270 ConceptBreakdown bd = orphanBreakdowns.remove(code);
271 if (bd == null) {
272 bd = new ConceptBreakdown(code);
273 }
274 setRoot(bd);
275 return bd;
276 }
277
278 public ConceptBreakdown addConceptBreakdown(String code) {
279 ConceptBreakdown bd = getExistingConceptBreakdown(code);
280 if (bd == null) {
281 bd = new ConceptBreakdown(code);
282 orphanBreakdowns.put(code, bd);
283 }
284 return bd;
285 }
286
287 public ConceptBreakdown addConceptBreakdown(String parentCode, String code) {
288 if (parentCode == null) {
289 return addConceptBreakdown(code);
290 }
291 ConceptBreakdown parent = addConceptBreakdown(parentCode);
292
293 ConceptBreakdown bd = parent.getCodeBreakdown(code);
294
295 if (bd == null) {
296 bd = orphanBreakdowns.remove(code);
297 }
298
299 if (bd == null) {
300 bd = new ConceptBreakdown(parentCode, code);
301 }
302 parent.addChildBreakdownInfo(bd);
303 bd.setParentConceptCode(parentCode);
304 return bd;
305 }
306
307 private ConceptBreakdown getExistingConceptBreakdown(String code) {
308 ConceptBreakdown bd = null;
309 if (getRoot() != null) {
310 bd = getRoot().getCodeBreakdown(code);
311 }
312
313 if (bd == null) {
314 bd = orphanBreakdowns.get(code);
315 }
316
317 return bd;
318 }
319
320
321
322
323
324
325
326
327 public Concept getConcept(String code) {
328 return concepts.get(code);
329 }
330
331
332
333
334
335
336
337
338 public Concept getConcept(ConceptBreakdown bd) {
339 return getConcept(bd.getConceptCode());
340 }
341
342
343
344
345
346
347
348
349
350 public Concept getOrAddConcept(String code) {
351 Concept concept = getConcept(code);
352 return concept == null ? addConcept(code) : concept;
353 }
354
355
356
357
358
359
360
361
362 private Concept addConcept(String code) {
363 Concept concept = new Concept(code);
364 concepts.put(code, concept);
365 return concept;
366 }
367
368
369
370
371
372
373
374
375 public boolean hasOrphanedConceptBreakdowns() {
376 return orphanBreakdowns != null && !orphanBreakdowns.isEmpty();
377 }
378
379
380
381
382
383
384
385
386 public boolean hasOrphanedMeasurements() {
387 return orphanMeasurements != null && !orphanMeasurements.isEmpty();
388 }
389
390
391
392
393
394
395
396 public Measurement createMeasurement() {
397 Measurement measurement = new Measurement();
398 orphanMeasurements.add(measurement);
399 return measurement;
400 }
401
402
403
404
405
406
407
408
409
410 public void setMeasurement(String conceptCode, Measurement measurement) {
411 ConceptBreakdown bd = addConceptBreakdown(
412 measurement.getParentConcept(), conceptCode);
413 bd.setMeasurement(measurement);
414 orphanMeasurements.remove(measurement);
415 }
416
417 @Override
418 public String toString() {
419 return "Fiebdc3Database {" + "File property of: " + fileProperty
420 + ", Format version: " + fileFormat + ", Generated by: "
421 + generatedBy + ", Header: " + header + ", Charset: " + charset
422 + ", Comments: " + comments + ", Information type: " + infoType
423 + ", Certification number: " + certNum
424 + ", Certification date: " + certDate + ", Concepts: "
425 + concepts + ", Root Concept Breakdown: \n\t" + root
426 + "\n, Orphan Breakdowns: " + orphanBreakdowns
427 + ", Orphan Measurements: " + orphanMeasurements + "}";
428 }
429 }