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 ca.uhn.hl7v2.HL7Exception;
29  import ca.uhn.hl7v2.conf.spec.message.SegGroup;
30  import ca.uhn.hl7v2.model.AbstractGroup;
31  import ca.uhn.hl7v2.model.GenericSegment;
32  import ca.uhn.hl7v2.model.Group;
33  import ca.uhn.hl7v2.model.Message;
34  import ca.uhn.hl7v2.model.Structure;
35  
36  public class ConformanceGroup extends AbstractGroup implements ConformanceStructure<ConformanceGroup>, ConformanceStructureHolder {
37  
38  	private SegGroup myConfDefinition;
39  	
40  	private ConformanceStructureHolderSupport mySupport;
41  
42  	public ConformanceGroup(Group theParent, SegGroup theConfDefinition) {
43  		super(theParent, (((ConformanceMessage) theParent.getMessage()).getModelClassFactory()));
44  
45  		mySupport = new ConformanceStructureHolderSupport(this, theConfDefinition);
46  		myConfDefinition = theConfDefinition;
47  	}
48  
49  	public ConformanceGroup(Message theMessage, ConformanceStructureHolderSupport theSupport, SegGroup theConfDefinition) {
50  		this(theMessage, theConfDefinition);
51  
52  		myConfDefinition = theConfDefinition;
53  		mySupport = theSupport;
54  	}
55  
56  	/**
57  	 * Internal method for adding child structures
58  	 */
59  	public void addChild(ConformanceStructure<?> theStructure, String theName, short theMinReps, short theMaxReps) throws HL7Exception {
60  		int num = mySupport.getChildCount();
61  		mySupport.addChild(theStructure, theName, theMinReps, theMaxReps);
62  		super.insert(theStructure.getClass(), mySupport.isRequired(theName), mySupport.isRepeating(theName), num, theName);
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 */
68  	@Override
69  	public Structure get(String theName, int theRep) throws HL7Exception {
70  
71  		Structure retVal = mySupport.getNonStandardSegmentIfNameExists(theName, theRep);
72  		if (retVal != null) {
73  			return retVal;
74  		}
75  
76  		Structure[] currentReps = getAll(theName);
77  		int currentRepsNum = currentReps.length;
78  		if (theRep > currentRepsNum) {
79  			throw new HL7Exception("Can't create rep " + theRep + " as there are currently only " + currentRepsNum);
80  		}
81  
82  		if (theRep < currentRepsNum) {
83  			return super.get(theName, theRep);
84  		}
85  
86  		if (getClass(theName) == GenericSegment.class) {
87  			retVal = super.get(theName, theRep);
88  		} else {
89  			retVal = mySupport.get(theName, theRep);
90  		}
91  		insertRepetition(theName, retVal, theRep);
92  		return retVal;
93  	}
94  
95  	/**
96  	 * {@inheritDoc}
97  	 */
98  	@Override
99  	public Structure[] getAll(String theName) throws HL7Exception {
100 		Structure[] retVal = mySupport.getAllNonStandardSegmentsIfNameExists(theName);
101 		if (retVal == null) {
102 			return super.getAll(theName);
103 		}
104 		return retVal;
105 	}
106 
107 	// /**
108 	// * {@inheritDoc}
109 	// */
110 	// @Override
111 	// public String addNonstandardSegment(String theName) throws HL7Exception {
112 	// return mySupport.addNonstandardSegment(theName);
113 	// }
114 	//
115 	// /**
116 	// * {@inheritDoc}
117 	// */
118 	// @Override
119 	// public String addNonstandardSegment(String theName, int theIndex) throws
120 	// HL7Exception {
121 	// return mySupport.addNonstandardSegment(theName, theIndex);
122 	// }
123 
124 	/**
125 	 * @return the confDefinition
126 	 */
127 	public SegGroup getConfDefinition() {
128 		return myConfDefinition;
129 	}
130 
131 	@Override
132 	public ConformanceMessage getMessage() {
133 		return (ConformanceMessage) super.getMessage();
134 	}
135 
136 	/**
137 	 * {@inheritDoc}
138 	 */
139 	@Override
140 	public String getName() {
141 		return mySupport.getName();
142 	}
143 
144 	// /**
145 	// * {@inheritDoc}
146 	// */
147 	// @Override
148 	// public String[] getNames() {
149 	// return mySupport.getNames();
150 	// }
151 
152 	/**
153 	 * {@inheritDoc}
154 	 */
155 	public ConformanceGroup instantiateClone() throws HL7Exception {
156 		ConformanceStructureHolderSupport support = mySupport.instantiateClone();
157 		ConformanceGroup retVal = new ConformanceGroup(getMessage(), support, myConfDefinition);
158 
159 		int idx = 0;
160 		for (String next : getNames()) {
161 			Class<? extends Structure> clazz = getClass(next);
162 			boolean rep = isRepeating(next);
163 			boolean req = isRequired(next);
164 
165 			retVal.insert(clazz, req, rep, idx, next);
166 
167 			idx++;
168 		}
169 
170 		return retVal;
171 	}
172 
173 	/**
174 	 * {@inheritDoc}
175 	 */
176 	@Override
177 	protected Structure tryToInstantiateStructure(Class<? extends Structure> theC, String theName) throws HL7Exception {
178 		return mySupport.get(theName, 0);
179 	}
180 
181 }