View Javadoc

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 ""  Description:
10   * ""
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2001.  All Rights Reserved.
14   *
15   * Contributor(s): ______________________________________.
16   *
17   * Alternatively, the contents of this file may be used under the terms of the
18   * GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
19   * applicable instead of those above.  If you wish to allow use of your version of this
20   * file only under the terms of the GPL and not to allow others to use your version
21   * of this file under the MPL, indicate your decision by deleting  the provisions above
22   * and replace  them with the notice and other provisions required by the GPL License.
23   * If you do not delete the provisions above, a recipient may use your version of
24   * this file under either the MPL or the GPL.
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 	
114 	public Structure get(String theName, int theRep) throws HL7Exception {
115 		int index = getIndex(theName);
116 		Structure retVal;
117 		if (myPrototypes.get(index) == null) {
118 			retVal = new GenericSegment(myGroup, theName);
119 		} else if (theRep == 0) {
120 			retVal = myPrototypes.get(index);
121 		} else {
122 			retVal = myPrototypes.get(index).instantiateClone();
123 		}
124 		return retVal;
125 	}
126 
127 	public Structure[] getAll(String theName) throws HL7Exception {
128 		throw new UnsupportedOperationException();
129 	}
130 
131 	public Structure[] getAllNonStandardSegmentsIfNameExists(String theName) {
132 		List<Segment> segments = myNamesToNonStandardSegments.get(theName);
133 		if (segments == null) {
134 			return null;
135 		}
136 		return segments.toArray(new Structure[segments.size()]);
137 	}
138 
139 	public int getChildCount() {
140 		return myNames.size();
141 	}
142 
143 	public Class<? extends Structure> getClass(String theName) {
144 		throw new UnsupportedOperationException();
145 	}
146 
147 	private int getIndex(String theName) throws HL7Exception {
148 		Integer retVal = myNameToIndex.get(theName);
149 		if (retVal == null) {
150 			throw new HL7Exception("Unknown name: " + theName);
151 		}
152 		return retVal;
153 	}
154 
155 	public ConformanceMessage getMessage() {
156 		throw new UnsupportedOperationException();
157 	}
158 
159 	public String getName() {
160 		if (myConfDefinition instanceof StaticDef) {
161 			StaticDef sd = (StaticDef)myConfDefinition;
162 			return sd.getMsgType() + "^" + sd.getEventType();
163 		} else {
164 			return ((SegGroup)myConfDefinition).getName();
165 		}
166 	}
167 
168 	public String[] getNames() {
169 		return myNames.toArray(new String[myNames.size()]);
170 	}
171 
172 	/**
173 	 * @return the namesToNonStandardSegments
174 	 */
175 	public Map<String, List<Segment>> getNamesToNonStandardSegments() {
176 		return myNamesToNonStandardSegments;
177 	}
178 
179 	public Segment getNonStandardSegmentIfNameExists(String theName, int theIndex) throws HL7Exception {
180 		List<Segment> segments = myNamesToNonStandardSegments.get(theName);
181 		if (segments == null) {
182 			return null;
183 		}
184 		if (theIndex > segments.size()) {
185 			throw new HL7Exception("Invalid index " + theIndex + ", must be <= " + segments.size());
186 		}
187 		if (segments.size() == theIndex) {
188 			segments.add(new GenericSegment(myGroup, theName));
189 		}
190 		return segments.get(theIndex);
191 	}
192 
193 	public Group getParent() {
194 		throw new UnsupportedOperationException();
195 	}
196 
197 	public ConformanceStructureHolderSupport instantiateClone() throws HL7Exception {
198 		ConformanceStructureHolderSupport retVal = new ConformanceStructureHolderSupport(myGroup, myConfDefinition);
199 
200 		for (int i = 0; i < myNames.size(); i++) {
201 
202 			ConformanceStructure<?> structure = myPrototypes.get(i).instantiateClone();
203 			retVal.addChild(structure, myNames.get(i), myMinReps.get(i), myMaxReps.get(i));
204 
205 		}
206 
207 		return retVal;
208 	}
209 
210 	@Override
211 	public boolean isChoiceElement(String theArg0) throws HL7Exception {
212 		throw new UnsupportedOperationException();
213 	}
214 
215 	@Override
216     public boolean isEmpty() throws HL7Exception {
217 	    return myGroup.isEmpty();
218     }
219 
220 	public boolean isGroup(String theName) throws HL7Exception {
221 		throw new UnsupportedOperationException();
222 	}
223 
224 	public boolean isRepeating(String theName) throws HL7Exception {
225 		int index = getIndex(theName);
226 		return myMaxReps.get(index) > 1 || myMaxReps.get(index) == -1;
227 	}
228 
229 	public boolean isRequired(String theName) throws HL7Exception {
230 		int index = getIndex(theName);
231 		return myMinReps.get(index) > 0;
232 	}
233 
234 	private boolean nameExists(String theName) {
235 		return myNameToIndex.containsKey(theName);
236 	}
237 
238     public boolean accept(MessageVisitor theVisitor, Location theCurrentLocation) throws HL7Exception {
239     	throw new UnsupportedOperationException();
240     }
241 
242     public Location provideLocation(Location theParentLocation, int theIndex, int theRepetition) {
243     	throw new UnsupportedOperationException();
244     }
245 
246 }