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 "ConformanceSegmentBuilder.java".  Description: 
10  "This Class builds Conformance Segment 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.*;
34  import ca.uhn.hl7v2.conf.classes.exceptions.*;
35  import ca.uhn.hl7v2.conf.spec.message.*;
36  
37  /** This Class builds Conformance Segment Classes
38   * @author <table><tr>James Agnew</tr>
39   *                <tr>Paul Brohman</tr>
40   *                <tr>Mitch Delachevrotiere</tr>
41   *                <tr>Shawn Dyck</tr>
42   * 				  <tr>Cory Metcalf</tr></table>
43   */
44  public class ConformanceSegmentBuilder {
45     private final DeploymentManager depManager; // The deployment manager
46     private final DocumentationBuilder docBuilder; // The documentation builder
47     private final String packageName; // Represents the Package that this Segment will go in
48  
49     private final String versionString; // The HAPI version
50  
51     /** This constructor will create a new ConformanceSegmentBuilder
52      * @param packageName the name of the package
53      * @param versionString the version of HL7 which these classes are conforming to
54      * @param depManager the instance of DeploymentManager
55      */
56     public ConformanceSegmentBuilder(String packageName, String versionString, DeploymentManager depManager) {
57        super();
58        this.packageName = packageName;
59        this.versionString = versionString;
60        this.docBuilder = DocumentationBuilder.getDocumentationBuilder();
61        this.depManager = depManager;
62     }
63  
64     /** This method builds a Conformance Segment Class
65      * @param seg the Segment to build
66      * @param parentUnderlyingType the data type of the parent Message or SegGroup for this segment
67      *        example "ca.uhn.hl7v2.model.v24.group.ADR_A19_..."    
68      * @param profileName this is the profile name associated with this Class
69      */
70     public void buildClass(Seg seg, String parentUnderlyingType, ProfileName profileName) {
71        GeneratedConformanceContainerclasses/GeneratedConformanceContainer.html#GeneratedConformanceContainer">GeneratedConformanceContainer gcc = new GeneratedConformanceContainer();
72  		String underlyingDataType; // The underlying HAPI Type
73  
74        // Check for possible snags in the Runtime Profile Segment
75        if (seg.getName() == null || seg.getName().length() < 1)
76           throw new ConformanceError("Error building ConformanceSegment: Runtime Segment does not contain a name.");
77  
78        // Set up class
79        gcc.setClassPackage(packageName);
80        gcc.addClassImport("ca.uhn.hl7v2.conf.classes.abs.*");
81        gcc.addClassImport("ca.uhn.hl7v2.conf.classes.exceptions.*");
82        gcc.addClassImport("ca.uhn.hl7v2.*");
83        gcc.addClassImport(packageName + "." + profileName.getPackageName() + ".*");
84  
85        gcc.setName(profileName.getClassName());
86        gcc.setProperties("extends AbstractConformanceContainer implements Repeatable");
87        gcc.setMinMaxReps(seg.getMin(), seg.getMax());
88        underlyingDataType = "ca.uhn.hl7v2.model." + versionString + ".segment." + seg.getName();
89  
90        String segClassName = UnderlyingAccessor.getHapiModelClass(underlyingDataType).getName();
91        gcc.addMemberVariable("private " + segClassName + " hapiSegment;");
92  
93  	  docBuilder.decorateConstructor( gcc.getConstructor(), profileName.getClassName() );
94  	  
95        // Set up underlying Segment type
96        GeneratedMethod theConstructor = gcc.getConstructor();
97        theConstructor.addParam(parentUnderlyingType + " underlyingMessage", "The underlying message object");
98        theConstructor.addParam("int rep", "The underlying rep number");
99        UnderlyingAccessors/UnderlyingAccessor.html#UnderlyingAccessor">UnderlyingAccessor underlyingAccessor = new UnderlyingAccessor(parentUnderlyingType, profileName.getAccessorName());
100       theConstructor.addToBody("this.hapiSegment = (" + segClassName + ") underlyingMessage." + underlyingAccessor.toString() + ";");
101       theConstructor.addToThrows("HL7Exception");
102 
103       // Loop through each child. Note that array is 1-offset
104       for (int i = 1; i <= seg.getFields(); i++) {
105 		 //don't build not supported, backward, or unknown types
106 		 String usage = seg.getField(i).getUsage();
107 		 if( usage != null && (usage.equals("X") || usage.equals("B") || usage.equals("U")) )
108 			continue;
109 			
110          // Create the names for each type of child
111 //         //CHANGED to use field # instead of name, in support of Z-segments 
112 //         ProfileName childProfileName = profileName.newInstance(String.valueOf(i), ProfileName.PS_FIELD);
113          ProfileName childProfileName = profileName.newInstance(seg.getField(i).getName(), ProfileName.PS_FIELD);
114          
115          // Add the member variable vector to hold them
116          gcc.addMemberVariable("private FiniteList " + childProfileName.getMemberName() + ";");
117 
118          // Set up the constructor
119          theConstructor.addToBody(childProfileName.getMemberName() + " = new FiniteList( " + childProfileName.getClassName() + ".class, hapiSegment );");
120 
121          // Add the getter
122 			UnderlyingAccessor childAccessor = new UnderlyingAccessor(underlyingDataType, childProfileName.getAccessorName());
123          GeneratedRepGetterr/genclasses/GeneratedRepGetter.html#GeneratedRepGetter">GeneratedRepGetter repGetter = new GeneratedRepGetter(childProfileName, childAccessor.getAcceptsRep());
124          docBuilder.decorateRepGetter(repGetter, seg.getField(i), childProfileName.getClassName());
125          gcc.addMethod(repGetter);
126 
127          // If the field has no components it is a primitive, so build as such.
128          if (seg.getField(i).getComponents() > 0) {
129             ConformanceFieldBuilderrs/ConformanceFieldBuilder.html#ConformanceFieldBuilder">ConformanceFieldBuilder childBuilder = new ConformanceFieldBuilder(packageName + "." + profileName.getPackageName(), versionString, depManager);
130             childBuilder.buildClass(seg.getField(i), segClassName, childProfileName.clearNameMap());
131          } else {
132             ConformancePrimitiveBuilderonformancePrimitiveBuilder.html#ConformancePrimitiveBuilder">ConformancePrimitiveBuilder childBuilder = new ConformancePrimitiveBuilder(packageName + "." + profileName.getPackageName(), depManager);
133             childBuilder.buildClass(seg.getField(i), segClassName, childProfileName.clearNameMap());
134          }
135 
136       }
137 
138       // Decorate with comments
139       docBuilder.decorateSegment(gcc, seg);
140 
141       if (depManager.getVerbose())
142          System.out.println("Generating Segment: " + packageName + "." + gcc.getName());
143 
144       depManager.generateFile(gcc, packageName, profileName.getClassName());
145 
146    }
147 }