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 "ConformanceSegGroupBuilder.java".  Description: 
10  "This Class builds Conformance SegGroup Classes" 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2001.  All Rights Reserved. 
14  
15  Contributor(s): James Agnew
16                  Paul Brohman
17                  Mitch Delachevrotiere
18                  Shawn Dyck
19    				Cory Metcalf
20    				
21  Alternatively, the contents of this file may be used under the terms of the 
22  GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
23  applicable instead of those above.  If you wish to allow use of your version of this 
24  file only under the terms of the GPL and not to allow others to use your version 
25  of this file under the MPL, indicate your decision by deleting  the provisions above 
26  and replace  them with the notice and other provisions required by the GPL License.  
27  If you do not delete the provisions above, a recipient may use your version of 
28  this file under either the MPL or the GPL. 
29  
30  */
31  package ca.uhn.hl7v2.conf.classes.generator.builders;
32  
33  import ca.uhn.hl7v2.conf.classes.generator.genclasses.GeneratedConformanceContainer;
34  import ca.uhn.hl7v2.conf.classes.generator.genclasses.GeneratedMethod;
35  import ca.uhn.hl7v2.conf.classes.generator.genclasses.GeneratedRepGetter;
36  import ca.uhn.hl7v2.conf.spec.message.ProfileStructure;
37  import ca.uhn.hl7v2.conf.spec.message.Seg;
38  import ca.uhn.hl7v2.conf.spec.message.SegGroup;
39  
40  /** This Class builds Conformance SegGroup Classes
41   * @author <table><tr>James Agnew</tr>
42   *                <tr>Paul Brohman</tr>
43   *                <tr>Mitch Delachevrotiere</tr>
44   *                <tr>Shawn Dyck</tr>
45   * 				  <tr>Cory Metcalf</tr></table>
46   */
47  public class ConformanceSegGroupBuilder {
48  
49     private final DeploymentManager depManager; // The deployment manager
50     private final DocumentationBuilder docBuilder; // The documentation builder
51  
52     private final String packageName; // Represents the Package that this Segment will go in
53     private final String structID; // The struct ID of the message type
54     private final String version; // The HAPI version
55  
56     /** This constructor will create a new ConformanceSegmentBuilder
57      * @param packageName the name of the package
58      * @param version the version of HL7 which these classes are conforming to
59      * @param depManager the instance of DeploymentManager
60      */
61     public ConformanceSegGroupBuilder(String packageName, String version, DeploymentManager depManager, String structID) {
62        super();
63        this.packageName = packageName;
64        this.version = version;
65        this.docBuilder = DocumentationBuilder.getDocumentationBuilder();
66        this.depManager = depManager;
67        this.structID = structID;
68     }
69  
70     /** This method will build all the field children for this Segment
71      * @param segGroup the SegGroup to build
72      * @param parentUnderlyingType the data type of the parent Message for this field
73      *        example "ca.uhn.hl7v2.model.v24.group.ADR_A19_..."  
74      * @param profileName this is the profile name associated with this Class
75      */
76     public void buildClass(SegGroup segGroup, String parentUnderlyingType, ProfileName profileName) {
77        GeneratedConformanceContainerclasses/GeneratedConformanceContainer.html#GeneratedConformanceContainer">GeneratedConformanceContainer gcc = new GeneratedConformanceContainer();
78        ConformanceSegmentBuilderonformanceSegmentBuilder.html#ConformanceSegmentBuilder">ConformanceSegmentBuilder confSegBuilder = new ConformanceSegmentBuilder(packageName + "." + profileName.getPackageName(), version, depManager);
79        ConformanceSegGroupBuilderanceSegGroupBuilder.html#ConformanceSegGroupBuilder">ConformanceSegGroupBuilder confSegGroupBuilder = new ConformanceSegGroupBuilder(packageName + "." + profileName.getPackageName(), version, depManager, structID);
80  
81        String underlyingDataType = structID + "_" + generateSegGroupName(segGroup);
82        String underlyingPackageType = "ca.uhn.hl7v2.model." + version + ".group." + underlyingDataType;
83  
84        // Set up class
85        gcc.setClassPackage(packageName);
86        gcc.addClassImport("ca.uhn.hl7v2.conf.classes.abs.*");
87        gcc.addClassImport("ca.uhn.hl7v2.conf.classes.exceptions.*");
88        gcc.addClassImport("ca.uhn.hl7v2.*");
89        gcc.addClassImport(packageName + "." + profileName.getPackageName() + ".*");
90  
91        gcc.setName(profileName.getClassName());
92        gcc.setProperties("extends AbstractConformanceContainer implements Repeatable");
93        gcc.setMinMaxReps(segGroup.getMin(), segGroup.getMax());
94        gcc.addMemberVariable("private " + underlyingPackageType + " hapiSegGroup;");
95  
96        // Set up underlying Segment type
97        GeneratedMethod theConstructor = gcc.getConstructor();
98  	  docBuilder.decorateConstructor( theConstructor, profileName.getClassName() );
99        theConstructor.addParam(parentUnderlyingType + " underlyingMessage", "The underlying message object");
100       theConstructor.addParam("int rep", "The underlying rep number");
101       theConstructor.addToThrows("HL7Exception");
102 
103       UnderlyingAccessors/UnderlyingAccessor.html#UnderlyingAccessor">UnderlyingAccessor underlyingAccessor = new UnderlyingAccessor(parentUnderlyingType, "get" + underlyingDataType);
104       theConstructor.addToBody("this.hapiSegGroup = underlyingMessage." + underlyingAccessor.toString() + ";");
105 
106       for (int i = 1; i <= segGroup.getChildren(); i++) {
107 		 //don't build not supported, backward, or unknown types
108 		 String usage = segGroup.getChild(i).getUsage();
109 		 if( usage != null && (usage.equals("X") || usage.equals("B") || usage.equals("U")) )
110 			continue;
111 			
112          if (segGroup.getChild(i) instanceof Seg) {
113             ProfileName childProfileName = profileName.newInstance(segGroup.getChild(i).getName(), ProfileName.PS_SEG);
114 
115             // Add the member variable vector to hold them
116             gcc.addMemberVariable("private FiniteList " + childProfileName.getMemberName() + ";");
117             theConstructor.addToBody(childProfileName.getMemberName() + " = new FiniteList( " + childProfileName.getClassName() + ".class, hapiSegGroup );");
118 				UnderlyingAccessor childAccessor = new UnderlyingAccessor(underlyingPackageType, childProfileName.getAccessorName());
119             GeneratedRepGetterr/genclasses/GeneratedRepGetter.html#GeneratedRepGetter">GeneratedRepGetter repGetter = new GeneratedRepGetter(childProfileName, childAccessor.getAcceptsRep());
120 
121             docBuilder.decorateRepGetter(repGetter, segGroup.getChild(i), childProfileName.getOriginalName());
122             gcc.addMethod(repGetter);
123             if (depManager.getVerbose())
124                System.out.println("Generating Segment: " + packageName + "." + gcc.getName());
125 
126             confSegBuilder.buildClass((Seg) segGroup.getChild(i), underlyingPackageType, childProfileName.clearNameMap());
127 
128          } else
129             if (segGroup.getChild(i) instanceof SegGroup) {
130                ProfileName childProfileName = profileName.newInstance(segGroup.getChild(i).getName(), ProfileName.PS_SEGG);
131 
132                // Add the member variable vector to hold them
133                gcc.addMemberVariable("private FiniteList " + childProfileName.getMemberName() + ";");
134                theConstructor.addToBody(childProfileName.getMemberName() + " = new FiniteList( " + childProfileName.getClassName() + ".class, hapiSegGroup );");
135 
136 					String underlyingAccessorName = "get" + structID + "_" + ConformanceSegGroupBuilder.generateSegGroupName((SegGroup)segGroup.getChild(i));
137 					UnderlyingAccessor childAccessor = new UnderlyingAccessor(underlyingPackageType, underlyingAccessorName);
138                GeneratedRepGetterr/genclasses/GeneratedRepGetter.html#GeneratedRepGetter">GeneratedRepGetter repGetter = new GeneratedRepGetter(childProfileName, childAccessor.getAcceptsRep());
139 
140                docBuilder.decorateRepGetter(repGetter, segGroup.getChild(i), childProfileName.getOriginalName());
141                gcc.addMethod(repGetter);
142                if (depManager.getVerbose())
143                   System.out.println("Generating SegGroup: " + packageName + "." + gcc.getName());
144 
145                confSegGroupBuilder.buildClass((SegGroup) segGroup.getChild(i), underlyingPackageType, childProfileName.clearNameMap());
146             }
147 
148       }
149 
150       depManager.generateFile(gcc, packageName, profileName.getClassName());
151 
152    }
153 
154 	/** This method creates the segment group name which is comprised of all the 
155 	 * underlying segments names put together
156 	 * @param segGroup the SegGroup to build
157 	 */
158 	public static String generateSegGroupName(SegGroup segGroup) {
159 		StringBuilder name = new StringBuilder();
160 
161 		for (int i = 1; i <= segGroup.getChildren(); i++) {
162 			ProfileStructure child = segGroup.getChild(i);
163 			if (child instanceof Seg)
164 				name.append(child.getName());
165 			else
166 				name.append(generateSegGroupName((SegGroup) child));
167 		}
168 
169 		return name.toString();
170 	}
171 
172 }