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 "SegmentDef.java". Description:
10 "Information about a message segment used in the creation of
11 source code for a Group class"
12
13 The Initial Developer of the Original Code is University Health Network. Copyright (C)
14 2001. All Rights Reserved.
15
16 Contributor(s): ______________________________________.
17
18 Alternatively, the contents of this file may be used under the terms of the
19 GNU General Public License (the �GPL�), in which case the provisions of the GPL are
20 applicable instead of those above. If you wish to allow use of your version of this
21 file only under the terms of the GPL and not to allow others to use your version
22 of this file under the MPL, indicate your decision by deleting the provisions above
23 and replace them with the notice and other provisions required by the GPL License.
24 If you do not delete the provisions above, a recipient may use your version of
25 this file under either the MPL or the GPL.
26
27 */
28
29 package ca.uhn.hl7v2.sourcegen;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import org.codehaus.plexus.util.StringUtils;
35
36 import edu.emory.mathcs.backport.java.util.Collections;
37
38 /**
39 * Information about a message segment used in the creation of
40 * source code for a Group class. SegmentDef is a slight misnomer because this
41 * also includes group start/end indicators, with group names.
42 *
43 * @author Bryan Tripp (bryan_tripp@sourceforge.net)
44 */
45 public class SegmentDef implements StructureDef {
46
47 private final String name;
48 private final String groupName;
49 private final String description;
50 private final boolean required;
51 private final boolean repeating;
52 private String myIndexName;
53 private final List<DatatypeDef> myFieldDefs = new ArrayList<>();
54 private final boolean choice;
55 private List<String> associatedStructures;
56
57 /** Creates new SegmentDef */
58 public SegmentDef(String name, String groupName, boolean required, boolean repeating, boolean choice, String description) {
59 this.name = name;
60 this.groupName = groupName;
61 this.required = required;
62 this.repeating = repeating;
63 this.description = StringUtils.defaultString(description);
64 this.choice = choice;
65 }
66
67 /**
68 * @return the choice
69 */
70 public boolean isChoice() {
71 return choice;
72 }
73
74 public void addFieldDef(DatatypeDef theFieldDef) {
75 myFieldDefs.add(theFieldDef);
76 }
77
78 public List<DatatypeDef> getFieldDefs() {
79 return myFieldDefs;
80 }
81
82 /**
83 * @return name of segment
84 */
85 public String getName() {
86 String result = this.name;
87 if (result != null && result.equals("?")) {
88 result = "GenericSegment";
89 }
90 return result;
91 }
92
93 /**
94 * @return name of group, if this is not really a segment but a group start indicator
95 */
96 public String getGroupName() {
97 return this.groupName;
98 }
99
100 /**
101 * Returns true if this structure is required in the Group.
102 */
103 public boolean isRequired() {
104 return this.required;
105 }
106
107 /**
108 * Returns true if this structure can repeat in the Group.
109 */
110 public boolean isRepeating() {
111 return this.repeating;
112 }
113
114 /**
115 * Returns a text description of the structure.
116 */
117 public String getDescription() {
118 return this.description;
119 }
120
121 /**
122 * Returns a list of the names of the segments that are children of this Structure.
123 * If the structure is a Segment, a 1-element array is returned containing the segment
124 * name. If a Group, an array of all the segments in the Group, including those nested
125 * in subgroups (depth first). This method is used to support the XML SIG's convention
126 * for deriving group names.
127 */
128 public String[] getChildSegments() {
129 return new String[]{getName()};
130 }
131
132 /**
133 * {@inheritDoc}
134 */
135 public String toString() {
136 return "SegmentDef[name=" + name + ", groupName=" + groupName + ", description=" + description + "]";
137 }
138
139 public boolean isGroup() {
140 return false;
141 }
142
143 /**
144 * {@inheritDoc}
145 */
146 public String getUnqualifiedName() {
147 return getName();
148 }
149
150 /**
151 * {@inheritDoc}
152 */
153 public String getIndexName() {
154 return myIndexName;
155 }
156
157 /**
158 * {@inheritDoc}
159 */
160 public void setIndexName(String theIndexName) {
161 myIndexName = theIndexName;
162 }
163
164 /**
165 * {@inheritDoc}
166 */
167 public boolean isSegment() {
168 return true;
169 }
170
171 /**
172 * @return the associatedStructures
173 */
174 public List<String> getAssociatedStructures() {
175 if (associatedStructures == null) {
176 associatedStructures = new ArrayList<>();
177 }
178 return associatedStructures;
179 }
180
181 /**
182 * @param theAssociatedStructure the associatedStructures to set
183 */
184 public void addAssociatedStructure(String theAssociatedStructure) {
185 if (getAssociatedStructures().contains(theAssociatedStructure)) {
186 return;
187 }
188 getAssociatedStructures().add(theAssociatedStructure);
189 Collections.sort(getAssociatedStructures());
190 }
191
192
193 }