1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.modules.hl7.model;
17
18 import java.util.LinkedHashMap;
19 import java.util.Map;
20
21 import org.openehealth.ipf.modules.hl7.HL7v2Exception;
22
23 import ca.uhn.hl7v2.HL7Exception;
24 import ca.uhn.hl7v2.model.Structure;
25 import ca.uhn.hl7v2.parser.DefaultModelClassFactory;
26 import ca.uhn.hl7v2.parser.ModelClassFactory;
27
28
29
30
31
32
33
34 @SuppressWarnings("serial")
35 public abstract class AbstractMessage extends ca.uhn.hl7v2.model.AbstractMessage {
36
37 protected enum Cardinality {
38 REQUIRED, OPTIONAL, REQUIRED_REPEATING, OPTIONAL_REPEATING
39 }
40
41 public AbstractMessage() {
42 super(new DefaultModelClassFactory());
43 init();
44 }
45
46 public AbstractMessage(ModelClassFactory factory) {
47 super(factory);
48 init();
49 }
50
51 private void init() {
52 try {
53 for (Map.Entry<Class<? extends Structure>, Cardinality> structure : structures(
54 new LinkedHashMap<>())
55 .entrySet()) {
56 switch (structure.getValue()) {
57 case REQUIRED:
58 add(structure.getKey(), true, false);
59 break;
60 case REQUIRED_REPEATING:
61 add(structure.getKey(), true, true);
62 break;
63 case OPTIONAL:
64 add(structure.getKey(), false, false);
65 break;
66 case OPTIONAL_REPEATING:
67 add(structure.getKey(), false, true);
68 break;
69 default:
70
71 break;
72 }
73
74 }
75 } catch (HL7Exception e) {
76 throw new HL7v2Exception(e);
77 }
78 }
79
80
81
82
83
84 protected <T extends Structure> T get(Class<T> structureClass) {
85 return super.getTyped(structureClass.getSimpleName(), structureClass);
86 }
87
88
89
90
91 protected <T extends Structure> T get(String structure, Class<T> structureClass) {
92 return super.getTyped(structure, structureClass);
93 }
94
95
96
97
98 protected <T extends Structure> T get(Class<T> structureClass, int rep) {
99 return super.getTyped(structureClass.getSimpleName(), rep, structureClass);
100 }
101
102
103
104
105 protected <T extends Structure> T get(String structure, Class<T> structureClass, int rep) {
106 return super.getTyped(structure, rep, structureClass);
107 }
108
109
110
111
112 protected <T extends Structure> int getReps(Class<T> structureClass) {
113 return super.getReps(structureClass.getSimpleName());
114 }
115
116
117
118
119 protected <T extends Structure> int getReps(String structure, Class<T> structureClass) {
120 return super.getReps(structure);
121 }
122
123 abstract protected Map<Class<? extends Structure>, Cardinality> structures(
124 Map<Class<? extends Structure>, Cardinality> structures);
125
126 }