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 "SegmentDef.java".  Description: 
10  "Information about a message segment used in the creation of 
11    source code for a Group class" 
12  
13  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
14  2001.  All Rights Reserved. 
15  
16  Contributor(s): ______________________________________. 
17  
18  Alternatively, the contents of this file may be used under the terms of the 
19  GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
20  applicable instead of those above.  If you wish to allow use of your version of this 
21  file only under the terms of the GPL and not to allow others to use your version 
22  of this file under the MPL, indicate your decision by deleting  the provisions above 
23  and replace  them with the notice and other provisions required by the GPL License.  
24  If you do not delete the provisions above, a recipient may use your version of 
25  this file under either the MPL or the GPL. 
26  
27  */
28  
29  package ca.uhn.hl7v2.sourcegen;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import org.codehaus.plexus.util.StringUtils;
35  
36  import edu.emory.mathcs.backport.java.util.Collections;
37  
38  /**
39   * Information about a message segment used in the creation of 
40   * source code for a Group class.  SegmentDef is a slight misnomer because this 
41   * also includes group start/end indicators, with group names.  
42   *   
43   * @author Bryan Tripp (bryan_tripp@sourceforge.net)
44   */
45  public class SegmentDef implements StructureDef {
46  
47      private String name;
48      private String groupName;
49      private String description;
50      private boolean required;
51      private boolean repeating;
52      private String myIndexName;
53      private List<DatatypeDef> myFieldDefs = new ArrayList<DatatypeDef>();
54  	private boolean choice;
55  	private List<String> associatedStructures;
56      
57  	/** Creates new SegmentDef */
58      public SegmentDef(String name, String groupName, boolean required, boolean repeating, boolean choice, String description) {
59          this.name = name;
60          this.groupName = groupName;
61          this.required = required;
62          this.repeating = repeating;
63          this.description = StringUtils.defaultString(description);
64          this.choice = choice;
65      }
66  
67      /**
68  	 * @return the choice
69  	 */
70  	public boolean isChoice() {
71  		return choice;
72  	}
73  
74  	public void addFieldDef(DatatypeDef theFieldDef) {
75          myFieldDefs.add(theFieldDef);
76      }
77      
78      public List<DatatypeDef> getFieldDefs() {
79          return myFieldDefs;
80      }
81  
82      /**
83       * @return name of segment 
84       */
85      public String getName() {
86          String result = this.name;
87          if (result != null && result.equals("?")) {
88              result = "GenericSegment";
89          }
90          return result;
91      }
92      
93      /**
94       * @return name of group, if this is not really a segment but a group start indicator 
95       */
96      public String getGroupName() {
97          return this.groupName;
98      }
99      
100     /**
101      * Returns true if this structure is required in the Group.  
102      */
103     public boolean isRequired() {
104         return this.required;
105     }
106     
107     /**
108      * Returns true if this structure can repeat in the Group.  
109      */    
110     public boolean isRepeating() {
111         return this.repeating;
112     }
113     
114     /**
115      * Returns a text description of the structure.
116      */
117     public String getDescription() {
118         return this.description;
119     }
120     
121     /**
122      * Returns a list of the names of the segments that are children of this Structure.
123      * If the structure is a Segment, a 1-element array is returned containing the segment
124      * name.  If a Group, an array of all the segments in the Group, including those nested
125      * in subgroups (depth first).  This method is used to support the XML SIG's convention
126      * for deriving group names.
127      */
128     public String[] getChildSegments() {
129         String[] result = {getName()};
130         return result;
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     public String toString() {
137         return "SegmentDef[name=" + name + ", groupName=" + groupName + ", description=" + description + "]";
138     }
139 
140     public boolean isGroup() {
141         return false;
142     }
143     
144     /**
145      * {@inheritDoc}
146      */
147     public String getUnqualifiedName() {
148         return getName();
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     public String getIndexName() {
155         return myIndexName;
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     public void setIndexName(String theIndexName) {
162         myIndexName = theIndexName;
163     }    
164 
165     /**
166      * {@inheritDoc}
167      */
168     public boolean isSegment() {
169         return true;
170     }    
171     
172     /**
173 	 * @return the associatedStructures
174 	 */
175 	public List<String> getAssociatedStructures() {
176 		if (associatedStructures == null) {
177 			associatedStructures = new ArrayList<String>();
178 		}
179 		return associatedStructures;
180 	}
181 
182 	/**
183 	 * @param theAssociatedStructures the associatedStructures to set
184 	 */
185 	public void addAssociatedStructure(String theAssociatedStructure) {
186 		getAssociatedStructures().add(theAssociatedStructure);
187 		Collections.sort(getAssociatedStructures());
188 	}
189 
190     
191 }