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 "GroupDef.java".  Description: 
10  "Contains the information needed to create source code for a Group (a 
11    Group is a part of a message that may repeat, and that contains two or 
12    more segments or other groups)" 
13  
14  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
15  2001.  All Rights Reserved. 
16  
17  Contributor(s): ______________________________________. 
18  
19  Alternatively, the contents of this file may be used under the terms of the 
20  GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
21  applicable instead of those above.  If you wish to allow use of your version of this 
22  file only under the terms of the GPL and not to allow others to use your version 
23  of this file under the MPL, indicate your decision by deleting  the provisions above 
24  and replace  them with the notice and other provisions required by the GPL License.  
25  If you do not delete the provisions above, a recipient may use your version of 
26  this file under either the MPL or the GPL. 
27  
28  */
29  
30  package ca.uhn.hl7v2.sourcegen;
31  
32  import java.util.ArrayList;
33  import java.util.HashMap;
34  import java.util.List;
35  
36  /**
37   * Contains the information needed to create source code for a Group (a 
38   * Group is a part of a message that may repeat, and that contains two or 
39   * more segments or other groups).   
40   * @author Bryan Tripp (bryan_tripp@sourceforge.net)
41   */
42  public class GroupDef implements StructureDef {
43  
44  	private ArrayList<StructureDef> elements;
45      private String messageName;
46      private String groupName;
47      private String description;
48      private boolean required;
49      private boolean repeating;
50      private HashMap<String, String> existingNames;
51      private String myIndexName;
52  	private List<String> associatedStructures;
53      
54      
55      /** Creates new GroupDef */
56      public GroupDef(String messageName, String groupName, boolean required, boolean repeating, String description) {
57          this.messageName = messageName;
58          this.groupName = groupName;
59          this.elements = new ArrayList<StructureDef>();
60          this.required = required;
61          this.repeating = repeating;
62          this.description = description;
63          this.existingNames = new HashMap<String, String>();
64      }
65  
66      /**
67       * Returns the Java class name of this Group.  This is derived from the 
68       * message structure and the group elements.  This should only be called 
69       * after all the elements are added.  
70       */
71      public String getName() {
72          String result = null;
73          
74          if (groupName != null && groupName.length() > 0) {
75              result = messageName + "_" + groupName;
76          } else {
77              StringBuffer name = new StringBuffer();
78              name.append(messageName);
79              name.append("_");
80              String[] children = getChildSegments();
81              for (int i = 0; i < children.length; i++) {
82                  name.append(children[i]);
83              }        
84              result = name.toString();            
85          }
86          
87          return result;
88      }
89      
90      
91      public String getRawGroupName() {
92      	return groupName;
93      }
94      
95      public void setRawGroupName(String groupName) {
96      	this.groupName = groupName;
97      }
98      
99      
100     /**
101      * @return group name without message name prepended  
102      */
103     public String getUnqualifiedName() {
104         String name = getName();
105         return name.substring(messageName.length() + 1);
106     }
107         
108     /**
109      * Adds an element (segment or group) to this group.  
110      */
111     public void addStructure(StructureDef s) {
112         elements.add(s);
113         
114         String indexName = getIndexName(s.getUnqualifiedName());
115         s.setIndexName(indexName);
116     }
117     
118     /**
119      * Returns the structures in this group. 
120      */
121     public StructureDef[] getStructures() {
122         StructureDef[] ret = new StructureDef[elements.size()];
123         for (int i = 0; i < ret.length; i++) {
124             ret[i] = (StructureDef)elements.get(i);
125         }
126         return ret;
127     }
128     
129 
130     /**
131      * Returns true if this structure is required in the Group.  
132      */
133     public boolean isRequired() {
134         return this.required;
135     }
136 
137     
138     /**
139      * Returns true if this structure can repeat in the Group.  
140      */        
141     public boolean isRepeating() {
142         return this.repeating;
143     }
144 
145     /** 
146      * Returns the name by which a particular structure can be accessed (eg for use 
147      * in writing accessor source code).  This may differ from the class name of the 
148      * structure of there are >1 structures in the same group with the same class.  
149      * For example in ADT_A01 there are two ROL's that are not in sub-groups - AbstractGroup 
150      * stores the first one under the name ROL and the second under the name ROL2.  This 
151      * method returns names using the same naming scheme.  The order in which this 
152      * method is called matters: it should be called ONCE for each element of the group in the 
153      * order in which they appear.  
154      */
155     public String getIndexName(String name) {
156         //see if this name is already being used 
157         Object o = existingNames.get(name);
158         int c = 2;
159         String newName = name;
160         while (o != null) {
161             newName = name + c++;
162             o = existingNames.get(newName);
163         }
164         name = newName;
165         existingNames.put(name, name);
166         return name;
167     }
168      
169     /**
170      * Returns a text description of the structure.
171      */
172     public String getDescription() {
173         return this.description;
174     }
175     
176     /**
177      * Returns a list of the names of the segments that are children of this Structure.
178      * If the structure is a Segment, a 1-element array is returned containing the segment
179      * name.  If a Group, an array of all the segments in the Group, including those nested
180      * in subgroups (depth first).  This method is used to support the XML SIG's convention
181      * for deriving group names.
182      */
183     public String[] getChildSegments() {
184         ArrayList<String> deepChildList = new ArrayList<String>();
185         for (int i = 0; i < elements.size(); i++) {
186             StructureDef childStruct = (StructureDef) elements.get(i);
187             String[] childStructChildren = childStruct.getChildSegments();
188             for (int j = 0; j < childStructChildren.length; j++) {
189                 deepChildList.add(childStructChildren[j]);
190             }
191         }
192         String[] result = new String[deepChildList.size()];
193         for (int i = 0; i < result.length; i++) {
194             result[i] = (String) deepChildList.get(i);
195         }
196         return result; 
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202 	public String toString() {
203 		return "GroupDef[" + groupName +"]";
204 	}
205 
206     public boolean isGroup() {
207         return true;
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     public String getIndexName() {
214         return myIndexName;
215     }
216 
217     /**
218      * {@inheritDoc}
219      */
220     public void setIndexName(String theIndexName) {
221         myIndexName = theIndexName;
222     }
223 
224     /**
225      * {@inheritDoc}
226      */
227     public boolean isSegment() {
228         return false;
229     }
230 
231 	public void setRepeating(boolean theB) {
232 		repeating = theB;
233 	}
234 
235 	public boolean isChoice() {
236 		return false;
237 	}    
238 	
239     /**
240 	 * @return the associatedStructures
241 	 */
242 	public List<String> getAssociatedStructures() {
243 		if (associatedStructures == null) {
244 			associatedStructures = new ArrayList<String>();
245 		}
246 		return associatedStructures;
247 	}
248 
249 	/**
250 	 * @param theAssociatedStructures the associatedStructures to set
251 	 */
252 	public void addAssociatedStructure(String theAssociatedStructure) {
253 		getAssociatedStructures().add(theAssociatedStructure);
254 	}
255 
256     
257 }