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.List;
30  
31  import ca.uhn.hl7v2.HL7Exception;
32  import ca.uhn.hl7v2.conf.spec.message.Seg;
33  import ca.uhn.hl7v2.model.AbstractSegment;
34  import ca.uhn.hl7v2.model.Type;
35  
36  
37  public class ConformanceSegment extends AbstractSegment implements ConformanceStructure<ConformanceSegment> {
38  
39  	private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
40  	private Seg myConfDefinition;
41  	private List<Integer> myMinReps = new ArrayList<Integer>();
42  	private List<Boolean> myPrototypeHasBeenUsed = new ArrayList<Boolean>();
43  	private List<ConformanceType<?>> myPrototypes = new ArrayList<ConformanceType<?>>();
44  
45  	public ConformanceSegment(ConformanceStructureHolder theParent, Seg theConfDefinition) {
46  		super(theParent, ((ConformanceMessage)theParent.getMessage()).getModelClassFactory());
47  		
48  		myConfDefinition = theConfDefinition;
49  	}
50  
51  	@Override
52  	public ConformanceMessage getMessage() {
53  		return (ConformanceMessage) super.getMessage();
54  	}
55  
56  	/**
57  	 * Add a child prototype
58  	 */
59  	void addChild(ConformanceType<?> theChild, String theName, int theMinReps, int theMaxReps, int theLength) throws HL7Exception {
60  		super.add(theChild.getClass(), theMinReps > 0, theMaxReps, theLength, EMPTY_OBJECT_ARRAY, theName);
61  
62  		myMinReps.add(theMinReps);
63  		myPrototypes.add(theChild);
64  		myPrototypeHasBeenUsed.add(Boolean.FALSE);
65  	}
66  
67  	/**
68  	 * {@inheritDoc}
69  	 */
70  	@Override
71  	public String getName() {
72  		return myConfDefinition.getName();
73  	}
74  
75  	/**
76  	 * {@inheritDoc}
77  	 */
78  	@Override
79  	protected Type createNewTypeWithoutReflection(int theField) {
80  		if (Boolean.TRUE.equals(myPrototypeHasBeenUsed.get(theField))) {
81  			return myPrototypes.get(theField).instantiateClone();
82  		}
83  		
84  		myPrototypeHasBeenUsed.set(theField, Boolean.TRUE);
85  		return myPrototypes.get(theField);
86  	}
87  	
88  	/**
89  	 * @return the confDefinition
90  	 */
91  	public Seg getConfDefinition() {
92  		return myConfDefinition;
93  	}
94  
95  	public int getMinReps(int theFieldNum) {
96  		return myMinReps.get(theFieldNum-1);
97  	}
98  	
99  	/**
100 	 * Create a clone of this segment (for the same message)
101 	 */
102 	public ConformanceSegment instantiateClone() throws HL7Exception {
103 		ConformanceSegment retVal = new ConformanceSegment((ConformanceStructureHolder) getParent(), myConfDefinition);
104 		
105 		int index = 0;
106 		for (ConformanceType<?> next : myPrototypes) {
107 			ConformanceType<?> clone = next.instantiateClone();
108 			index++;
109 			retVal.addChild(clone, getNames()[index-1], getMinReps(index), getMaxCardinality(index), getLength(index));
110 		}
111 		
112 		return retVal;
113 	}
114 
115 	public short getMinReps() {
116 		return myConfDefinition.getMin();
117 	}
118 
119 	public short getMaxReps() {
120 		return myConfDefinition.getMax();
121 	}
122 	
123 }