001package ca.uhn.hl7v2.conf.spec.message;
002
003import ca.uhn.hl7v2.conf.ProfileException;
004
005/**
006 * A specification for a message segment in a conformance profile.  
007 * @author Bryan Tripp
008 */
009public class Seg implements ProfileStructure {
010    
011    
012    /** Utility field used by bound properties. */
013    private java.beans.PropertyChangeSupport propertyChangeSupport =  new java.beans.PropertyChangeSupport(this);
014    
015    /** Utility field used by constrained properties. */
016    private java.beans.VetoableChangeSupport vetoableChangeSupport =  new java.beans.VetoableChangeSupport(this);
017    
018    private String impNote;    
019    private String description;
020    private String reference;
021    private String predicate;
022    private Field[] fields;
023    private String name;
024    private String longName;
025    private String usage;
026    private short min;
027    private short max;
028    
029    /** Creates a new instance of Segment */
030    public Seg() {
031        this.fields = new Field[0];
032    }
033    
034    /** Adds a PropertyChangeListener to the listener list.
035     * @param l The listener to add.
036     */
037    public void addPropertyChangeListener(java.beans.PropertyChangeListener l) {
038        propertyChangeSupport.addPropertyChangeListener(l);
039    }
040    
041    /** Removes a PropertyChangeListener from the listener list.
042     * @param l The listener to remove.
043     */
044    public void removePropertyChangeListener(java.beans.PropertyChangeListener l) {
045        propertyChangeSupport.removePropertyChangeListener(l);
046    }
047    
048    /** Adds a VetoableChangeListener to the listener list.
049     * @param l The listener to add.
050     */
051    public void addVetoableChangeListener(java.beans.VetoableChangeListener l) {
052        vetoableChangeSupport.addVetoableChangeListener(l);
053    }
054    
055    /** Removes a VetoableChangeListener from the listener list.
056     * @param l The listener to remove.
057     */
058    public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) {
059        vetoableChangeSupport.removeVetoableChangeListener(l);
060    }
061    
062    /** Getter for property impNote.
063     * @return Value of property impNote.
064     */
065    public String getImpNote() {
066        return this.impNote;
067    }
068    
069    /** Setter for property impNote.
070     * @param impNote New value of property impNote.
071     *
072     * @throws ProfileException
073     */
074    public void setImpNote(String impNote) throws ProfileException {
075        String oldImpNote = this.impNote;
076        try {
077            vetoableChangeSupport.fireVetoableChange("impNote", oldImpNote, impNote);
078        } catch (Exception e) {
079            throw new ProfileException(null, e);
080        }
081        this.impNote = impNote;
082        propertyChangeSupport.firePropertyChange("impNote", oldImpNote, impNote);
083    }
084    
085    /** Getter for property description.
086     * @return Value of property description.
087     */
088    public String getDescription() {
089        return this.description;
090    }
091    
092    /** Setter for property description.
093     * @param description New value of property description.
094     *
095     * @throws ProfileException
096     */
097    public void setDescription(String description) throws ProfileException {
098        String oldDescription = this.description;
099        try {
100            vetoableChangeSupport.fireVetoableChange("description", oldDescription, description);
101        } catch (Exception e) {
102            throw new ProfileException(null, e);
103        }
104        this.description = description;
105        propertyChangeSupport.firePropertyChange("description", oldDescription, description);
106    }
107    
108    /** Getter for property reference.
109     * @return Value of property reference.
110     */
111    public String getReference() {
112        return this.reference;
113    }
114    
115    /** Setter for property reference.
116     * @param reference New value of property reference.
117     *
118     * @throws ProfileException
119     */
120    public void setReference(String reference) throws ProfileException {
121        String oldReference = this.reference;
122        try {
123            vetoableChangeSupport.fireVetoableChange("reference", oldReference, reference);
124        } catch (Exception e) {
125            throw new ProfileException(null, e);
126        }
127        this.reference = reference;
128        propertyChangeSupport.firePropertyChange("reference", oldReference, reference);
129    }
130    
131    /** Getter for property predicate.
132     * @return Value of property predicate.
133     */
134    public String getPredicate() {
135        return this.predicate;
136    }
137    
138    /** Setter for property predicate.
139     * @param predicate New value of property predicate.
140     *
141     * @throws ProfileException
142     */
143    public void setPredicate(String predicate) throws ProfileException {
144        String oldPredicate = this.predicate;
145        try {
146            vetoableChangeSupport.fireVetoableChange("predicate", oldPredicate, predicate);
147        } catch (Exception e) {
148            throw new ProfileException(null, e);
149        }
150        this.predicate = predicate;
151        propertyChangeSupport.firePropertyChange("predicate", oldPredicate, predicate);
152    }
153    
154    /** Indexed getter for property field (index starts at 1 following HL7 convention).
155     * @param index Index of the property (starts at 1 following HL7 convention).
156     * @return Value of the property at <CODE>index</CODE>.
157     */
158    public Field getField(int index) {
159        return this.fields[index - 1];
160    }
161    
162    /** Indexed setter for property field (index starts at 1 following HL7 convention).
163     * @param index Index of the property (starts at 1 following HL7 convention).
164     * @param field New value of the property at <CODE>index</CODE>.
165     *
166     * @throws ProfileException
167     */
168    public void setField(int index, Field field) throws ProfileException {
169        index--;
170        extendChildList(index);
171        Field oldField = this.fields[index];
172        this.fields[index] = field;
173        try {
174            vetoableChangeSupport.fireVetoableChange("fields", null, null );
175        }
176        catch(java.beans.PropertyVetoException vetoException ) {
177            this.fields[index] = oldField;
178            throw new ProfileException(null, vetoException);
179        }
180        propertyChangeSupport.firePropertyChange("fields", null, null );
181    }
182    
183    /** Getter for property name.
184     * @return Value of property name.
185     */
186    public String getName() {
187        return this.name;
188    }
189    
190    /** Setter for property name.
191     * @param name New value of property name.
192     *
193     * @throws ProfileException
194     */
195    public void setName(String name) throws ProfileException {
196        String oldName = this.name;
197        try {
198            vetoableChangeSupport.fireVetoableChange("name", oldName, name);
199        } catch (Exception e) {
200            throw new ProfileException(null, e);
201        }
202        this.name = name;
203        propertyChangeSupport.firePropertyChange("name", oldName, name);
204    }
205    
206    /** Getter for property longName.
207     * @return Value of property longName.
208     */
209    public String getLongName() {
210        return this.longName;
211    }
212    
213    /** Setter for property longName.
214     * @param longName New value of property longName.
215     *
216     * @throws ProfileException
217     */
218    public void setLongName(String longName) throws ProfileException {
219        String oldLongName = this.longName;
220        try {
221            vetoableChangeSupport.fireVetoableChange("longName", oldLongName, longName);
222        } catch (Exception e) {
223            throw new ProfileException(null, e);
224        }
225        this.longName = longName;
226        propertyChangeSupport.firePropertyChange("longName", oldLongName, longName);
227    }
228    
229    /** Getter for property usage.
230     * @return Value of property usage.
231     */
232    public String getUsage() {
233        return this.usage;
234    }
235    
236    /** Setter for property usage.
237     * @param optionality New value of property usage.
238     *
239     * @throws ProfileException
240     */
241    public void setUsage(String usage) throws ProfileException {
242        String oldUsage = this.usage;
243        try {
244            vetoableChangeSupport.fireVetoableChange("usage", oldUsage, usage);
245        } catch (Exception e) {
246            throw new ProfileException(null, e);
247        }            
248        this.usage = usage;
249        propertyChangeSupport.firePropertyChange("usage", oldUsage, usage);
250    }
251    
252    /** Getter for property min.
253     * @return Value of property min.
254     */
255    public short getMin() {
256        return this.min;
257    }
258    
259    /** Setter for property min.
260     * @param min New value of property min.
261     *
262     * @throws ProfileException
263     */
264    public void setMin(short min) throws ProfileException {
265        short oldMin = this.min;
266        try {
267            vetoableChangeSupport.fireVetoableChange("min", new Short(oldMin), new Short(min));
268        } catch (Exception e) {
269            throw new ProfileException(null, e);
270        }            
271        this.min = min;
272        propertyChangeSupport.firePropertyChange("min", new Short(oldMin), new Short(min));
273    }
274    
275    /** Getter for property max.
276     * @return Value of property max.
277     */
278    public short getMax() {
279        return this.max;
280    }
281    
282    /** Setter for property max.
283     * @param max New value of property max.
284     *
285     * @throws ProfileException
286     */
287    public void setMax(short max) throws ProfileException {
288        short oldMax = this.max;
289        try {
290            vetoableChangeSupport.fireVetoableChange("max", new Short(oldMax), new Short(max));
291        } catch (Exception e) {
292            throw new ProfileException(null, e);
293        }
294        this.max = max;
295        propertyChangeSupport.firePropertyChange("max", new Short(oldMax), new Short(max));
296    }
297    
298    /** Makes child list long enough to accommodate setter.  */
299    private void extendChildList(int index) {
300        if (index >= this.fields.length) {
301            Field[] newCopy = new Field[index + 1];
302            System.arraycopy(this.fields, 0, newCopy, 0, this.fields.length);
303            this.fields = newCopy;
304        }        
305    }    
306
307    /** Returns the number of fields in the segment */
308    public int getFields() {
309        return this.fields.length;
310    }
311    
312}