1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package ca.uhn.hl7v2.sourcegen;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
36
37
38
39
40
41
42
43 public class GroupDef implements StructureDef {
44
45 private final ArrayList<StructureDef> elements;
46 private final String messageName;
47 private String groupName;
48 private final String description;
49 private final boolean required;
50 private boolean repeating;
51 private final HashMap<String, String> existingNames;
52 private String myIndexName;
53 private List<String> associatedStructures;
54
55
56
57 public GroupDef(String messageName, String groupName, boolean required, boolean repeating, String description) {
58 this.messageName = messageName;
59 this.groupName = groupName;
60 this.elements = new ArrayList<>();
61 this.required = required;
62 this.repeating = repeating;
63 this.description = description;
64 this.existingNames = new HashMap<>();
65 }
66
67
68
69
70
71
72 public String getName() {
73 String result;
74
75 if (groupName != null && groupName.length() > 0) {
76 result = messageName + "_" + groupName;
77 } else {
78 StringBuilder name = new StringBuilder();
79 name.append(messageName);
80 name.append("_");
81 String[] children = getChildSegments();
82 for (String child : children) {
83 name.append(child);
84 }
85 result = name.toString();
86 }
87
88 return result;
89 }
90
91
92 public String getRawGroupName() {
93 return groupName;
94 }
95
96 public void setRawGroupName(String groupName) {
97 this.groupName = groupName;
98 }
99
100
101
102
103
104 public String getUnqualifiedName() {
105 String name = getName();
106 return name.substring(messageName.length() + 1);
107 }
108
109
110
111
112 public void addStructure(StructureDef s) {
113 elements.add(s);
114
115 String indexName = getIndexName(s.getUnqualifiedName());
116 s.setIndexName(indexName);
117 }
118
119
120
121
122 public StructureDef[] getStructures() {
123 StructureDefef.html#StructureDef">StructureDef[] ret = new StructureDef[elements.size()];
124 for (int i = 0; i < ret.length; i++) {
125 ret[i] = elements.get(i);
126 }
127 return ret;
128 }
129
130
131
132
133
134 public boolean isRequired() {
135 return this.required;
136 }
137
138
139
140
141
142 public boolean isRepeating() {
143 return this.repeating;
144 }
145
146
147
148
149
150
151
152
153
154
155
156 public String getIndexName(String name) {
157
158 Object o = existingNames.get(name);
159 int c = 2;
160 String newName = name;
161 while (o != null) {
162 newName = name + c++;
163 o = existingNames.get(newName);
164 }
165 name = newName;
166 existingNames.put(name, name);
167 return name;
168 }
169
170
171
172
173 public String getDescription() {
174 return this.description;
175 }
176
177
178
179
180
181
182
183
184 public String[] getChildSegments() {
185 ArrayList<String> deepChildList = new ArrayList<>();
186 for (StructureDef element : elements) {
187 String[] childStructChildren = element.getChildSegments();
188 deepChildList.addAll(Arrays.asList(childStructChildren));
189 }
190 String[] result = new String[deepChildList.size()];
191 for (int i = 0; i < result.length; i++) {
192 result[i] = deepChildList.get(i);
193 }
194 return result;
195 }
196
197
198
199
200 public String toString() {
201 return "GroupDef[" + groupName +"]";
202 }
203
204 public boolean isGroup() {
205 return true;
206 }
207
208
209
210
211 public String getIndexName() {
212 return myIndexName;
213 }
214
215
216
217
218 public void setIndexName(String theIndexName) {
219 myIndexName = theIndexName;
220 }
221
222
223
224
225 public boolean isSegment() {
226 return false;
227 }
228
229 public void setRepeating(boolean theB) {
230 repeating = theB;
231 }
232
233 public boolean isChoice() {
234 return false;
235 }
236
237
238
239
240 public List<String> getAssociatedStructures() {
241 if (associatedStructures == null) {
242 associatedStructures = new ArrayList<>();
243 }
244 return associatedStructures;
245 }
246
247
248
249
250 public void addAssociatedStructure(String theAssociatedStructure) {
251 getAssociatedStructures().add(theAssociatedStructure);
252 }
253
254
255 }