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 "GroupDef.java". Description:
10 "Contains the information needed to create source code for a Group (a
11 Group is a part of a message that may repeat, and that contains two or
12 more segments or other groups)"
13
14 The Initial Developer of the Original Code is University Health Network. Copyright (C)
15 2001. All Rights Reserved.
16
17 Contributor(s): ______________________________________.
18
19 Alternatively, the contents of this file may be used under the terms of the
20 GNU General Public License (the �GPL�), in which case the provisions of the GPL are
21 applicable instead of those above. If you wish to allow use of your version of this
22 file only under the terms of the GPL and not to allow others to use your version
23 of this file under the MPL, indicate your decision by deleting the provisions above
24 and replace them with the notice and other provisions required by the GPL License.
25 If you do not delete the provisions above, a recipient may use your version of
26 this file under either the MPL or the GPL.
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 * Contains the information needed to create source code for a Group (a
39 * Group is a part of a message that may repeat, and that contains two or
40 * more segments or other groups).
41 * @author Bryan Tripp (bryan_tripp@sourceforge.net)
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 /** Creates new GroupDef */
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 * Returns the Java class name of this Group. This is derived from the
69 * message structure and the group elements. This should only be called
70 * after all the elements are added.
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 * @return group name without message name prepended
103 */
104 public String getUnqualifiedName() {
105 String name = getName();
106 return name.substring(messageName.length() + 1);
107 }
108
109 /**
110 * Adds an element (segment or group) to this group.
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 * Returns the structures in this group.
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 * Returns true if this structure is required in the Group.
133 */
134 public boolean isRequired() {
135 return this.required;
136 }
137
138
139 /**
140 * Returns true if this structure can repeat in the Group.
141 */
142 public boolean isRepeating() {
143 return this.repeating;
144 }
145
146 /**
147 * Returns the name by which a particular structure can be accessed (eg for use
148 * in writing accessor source code). This may differ from the class name of the
149 * structure of there are >1 structures in the same group with the same class.
150 * For example in ADT_A01 there are two ROL's that are not in sub-groups - AbstractGroup
151 * stores the first one under the name ROL and the second under the name ROL2. This
152 * method returns names using the same naming scheme. The order in which this
153 * method is called matters: it should be called ONCE for each element of the group in the
154 * order in which they appear.
155 */
156 public String getIndexName(String name) {
157 //see if this name is already being used
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 * Returns a text description of the structure.
172 */
173 public String getDescription() {
174 return this.description;
175 }
176
177 /**
178 * Returns a list of the names of the segments that are children of this Structure.
179 * If the structure is a Segment, a 1-element array is returned containing the segment
180 * name. If a Group, an array of all the segments in the Group, including those nested
181 * in subgroups (depth first). This method is used to support the XML SIG's convention
182 * for deriving group names.
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 * {@inheritDoc}
199 */
200 public String toString() {
201 return "GroupDef[" + groupName +"]";
202 }
203
204 public boolean isGroup() {
205 return true;
206 }
207
208 /**
209 * {@inheritDoc}
210 */
211 public String getIndexName() {
212 return myIndexName;
213 }
214
215 /**
216 * {@inheritDoc}
217 */
218 public void setIndexName(String theIndexName) {
219 myIndexName = theIndexName;
220 }
221
222 /**
223 * {@inheritDoc}
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 * @return the associatedStructures
239 */
240 public List<String> getAssociatedStructures() {
241 if (associatedStructures == null) {
242 associatedStructures = new ArrayList<>();
243 }
244 return associatedStructures;
245 }
246
247 /**
248 * @param theAssociatedStructure the associatedStructures to set
249 */
250 public void addAssociatedStructure(String theAssociatedStructure) {
251 getAssociatedStructures().add(theAssociatedStructure);
252 }
253
254
255 }