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 package ca.uhn.hl7v2.testpanel.model.conf;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import ca.uhn.hl7v2.HL7Exception;
34 import ca.uhn.hl7v2.Location;
35 import ca.uhn.hl7v2.conf.spec.message.AbstractSegmentContainer;
36 import ca.uhn.hl7v2.conf.spec.message.SegGroup;
37 import ca.uhn.hl7v2.conf.spec.message.StaticDef;
38 import ca.uhn.hl7v2.model.GenericSegment;
39 import ca.uhn.hl7v2.model.Group;
40 import ca.uhn.hl7v2.model.MessageVisitor;
41 import ca.uhn.hl7v2.model.Segment;
42 import ca.uhn.hl7v2.model.Structure;
43
44 public class ConformanceStructureHolderSupport implements ConformanceStructureHolder {
45
46 private AbstractSegmentContainer myConfDefinition;
47 private Group myGroup;
48 private List<Short> myMaxReps = new ArrayList<Short>();
49 private List<Short> myMinReps = new ArrayList<Short>();
50 private List<String> myNames = new ArrayList<String>();
51 private Map<String, List<Segment>> myNamesToNonStandardSegments = new HashMap<String, List<Segment>>();
52 private Map<String, Integer> myNameToIndex = new HashMap<String, Integer>();
53 private List<ConformanceStructure<?>> myPrototypes = new ArrayList<ConformanceStructure<?>>();
54
55 public ConformanceStructureHolderSupport(Group theGroup, AbstractSegmentContainer theConfDefinition) {
56 myConfDefinition = theConfDefinition;
57 myGroup = theGroup;
58 }
59
60 public void addChild(ConformanceStructure<?> theStructure, String theName, short theMinReps, short theMaxReps) throws HL7Exception {
61 String name = theName;
62 if (nameExists(name)) {
63 int version = 2;
64 String newName = name;
65 while (nameExists(newName)) {
66 newName = name + version++;
67 }
68 name = newName;
69 }
70
71 myNameToIndex.put(name, myNames.size());
72 myMinReps.add(theMinReps);
73 myMaxReps.add(theMaxReps);
74 myNames.add(theName);
75 myPrototypes.add(theStructure);
76 }
77
78 public String addNonstandardSegment(String theName) throws HL7Exception {
79 return addNonstandardSegment(theName, myNames.size());
80 }
81
82 public String addNonstandardSegment(String theName, int theIndex) throws HL7Exception {
83 String name = null;
84 if (nameExists(theName)) {
85 int version = 2;
86 String newName = theName;
87 while (nameExists(newName)) {
88 newName = theName + version++;
89 }
90 name = newName;
91 } else {
92 name = theName;
93 }
94
95 myMinReps.add(theIndex, (short) 0);
96 myMaxReps.add(theIndex, (short) 0);
97 myNames.add(theIndex, name);
98 myPrototypes.add(theIndex, null);
99 myNamesToNonStandardSegments.put(name, new ArrayList<Segment>());
100
101 int index = 0;
102 for (String next : myNames) {
103 myNameToIndex.put(next, index++);
104 }
105
106 return name;
107 }
108
109 public Structure get(String theName) throws HL7Exception {
110 throw new UnsupportedOperationException();
111 }
112
113 public Structure get(String theName, int theRep) throws HL7Exception {
114 int index = getIndex(theName);
115 Structure retVal;
116 if (myPrototypes.get(index) == null) {
117 retVal = new GenericSegment(myGroup, theName);
118 } else if (theRep == 0) {
119 retVal = myPrototypes.get(index);
120 } else {
121 retVal = myPrototypes.get(index).instantiateClone();
122 }
123 return retVal;
124 }
125
126 public Structure[] getAll(String theName) throws HL7Exception {
127 throw new UnsupportedOperationException();
128 }
129
130 public Structure[] getAllNonStandardSegmentsIfNameExists(String theName) {
131 List<Segment> segments = myNamesToNonStandardSegments.get(theName);
132 if (segments == null) {
133 return null;
134 }
135 return segments.toArray(new Structure[segments.size()]);
136 }
137
138 public int getChildCount() {
139 return myNames.size();
140 }
141
142 public Class<? extends Structure> getClass(String theName) {
143 throw new UnsupportedOperationException();
144 }
145
146 private int getIndex(String theName) throws HL7Exception {
147 Integer retVal = myNameToIndex.get(theName);
148 if (retVal == null) {
149 throw new HL7Exception("Unknown name: " + theName);
150 }
151 return retVal;
152 }
153
154 public ConformanceMessage getMessage() {
155 throw new UnsupportedOperationException();
156 }
157
158 public String getName() {
159 if (myConfDefinition instanceof StaticDef) {
160 StaticDef sd = (StaticDef) myConfDefinition;
161 return sd.getMsgType() + "^" + sd.getEventType();
162 } else {
163 return ((SegGroup) myConfDefinition).getName();
164 }
165 }
166
167 public String[] getNames() {
168 return myNames.toArray(new String[myNames.size()]);
169 }
170
171
172
173
174 public Map<String, List<Segment>> getNamesToNonStandardSegments() {
175 return myNamesToNonStandardSegments;
176 }
177
178 public Segment getNonStandardSegmentIfNameExists(String theName, int theIndex) throws HL7Exception {
179 List<Segment> segments = myNamesToNonStandardSegments.get(theName);
180 if (segments == null) {
181 return null;
182 }
183 if (theIndex > segments.size()) {
184 throw new HL7Exception("Invalid index " + theIndex + ", must be <= " + segments.size());
185 }
186 if (segments.size() == theIndex) {
187 segments.add(new GenericSegment(myGroup, theName));
188 }
189 return segments.get(theIndex);
190 }
191
192 public Group getParent() {
193 throw new UnsupportedOperationException();
194 }
195
196 public ConformanceStructureHolderSupport instantiateClone() throws HL7Exception {
197 ConformanceStructureHolderSupport retVal = new ConformanceStructureHolderSupport(myGroup, myConfDefinition);
198
199 for (int i = 0; i < myNames.size(); i++) {
200
201 ConformanceStructure<?> structure = myPrototypes.get(i).instantiateClone();
202 retVal.addChild(structure, myNames.get(i), myMinReps.get(i), myMaxReps.get(i));
203
204 }
205
206 return retVal;
207 }
208
209 public boolean isChoiceElement(String theArg0) throws HL7Exception {
210 throw new UnsupportedOperationException();
211 }
212
213 public boolean isEmpty() throws HL7Exception {
214 return myGroup.isEmpty();
215 }
216
217 public boolean isGroup(String theName) throws HL7Exception {
218 throw new UnsupportedOperationException();
219 }
220
221 public boolean isRepeating(String theName) throws HL7Exception {
222 int index = getIndex(theName);
223 return myMaxReps.get(index) > 1 || myMaxReps.get(index) == -1;
224 }
225
226 public boolean isRequired(String theName) throws HL7Exception {
227 int index = getIndex(theName);
228 return myMinReps.get(index) > 0;
229 }
230
231 private boolean nameExists(String theName) {
232 return myNameToIndex.containsKey(theName);
233 }
234
235 public boolean accept(MessageVisitor theVisitor, Location theCurrentLocation) throws HL7Exception {
236 throw new UnsupportedOperationException();
237 }
238
239 public Location/../../../ca/uhn/hl7v2/Location.html#Location">Location provideLocation(Location theParentLocation, int theIndex, int theRepetition) {
240 throw new UnsupportedOperationException();
241 }
242
243 }