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 "ConformanceFieldBuilder.java".  Description: 
10  "/** This class is builds Conformance Field 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  
32  package ca.uhn.hl7v2.conf.classes.generator.builders;
33  
34  import ca.uhn.hl7v2.conf.classes.generator.genclasses.*;
35  import ca.uhn.hl7v2.conf.classes.exceptions.*;
36  import ca.uhn.hl7v2.conf.spec.message.*;
37  
38  /** This class builds Conformance Field Classes
39   * @author <table><tr>James Agnew</tr>
40   *                <tr>Paul Brohman</tr>
41   *                <tr>Mitch Delachevrotiere</tr>
42   *                <tr>Shawn Dyck</tr>
43   * 				  <tr>Cory Metcalf</tr></table>
44   */
45  public class ConformanceFieldBuilder {
46     private final DeploymentManager depManager;
47     private final DocumentationBuilder docBuilder; // The documentation builder
48  
49     private final String packageName; // Represents the Package that this Segment will go in
50     private String underlyingType; // The underlying HAPI Type
51     private final String versionString; // The HAPI version
52  
53     /** This constructor will create a new ConformanceFieldBuilder
54      * @param packageName the name of the package
55      * @param versionString the version of HL7 which these classes are conforming to
56      * @param depManager the instance of DeploymentManager
57      */
58     public ConformanceFieldBuilder(String packageName, String versionString, DeploymentManager depManager) {
59        super();
60        this.packageName = packageName;
61        this.docBuilder = DocumentationBuilder.getDocumentationBuilder();
62        this.depManager = depManager;
63        this.versionString = versionString;
64     }
65  
66     /** This method builds a Conformance Field Class
67      * @param field the Field to build
68      * @param parentUnderlyingType the data type of the parent Segment for this field
69      *        example "ca.uhn.hl7v2.model.v24.segment.MSH"  
70      * @param profileName  ProfileName
71      */
72     public void buildClass(Field field, String parentUnderlyingType, ProfileName profileName) {
73        GeneratedConformanceContainerclasses/GeneratedConformanceContainer.html#GeneratedConformanceContainer">GeneratedConformanceContainer gcc = new GeneratedConformanceContainer();
74        GeneratedMethods/generator/genclasses/GeneratedMethod.html#GeneratedMethod">GeneratedMethod gm = new GeneratedMethod();
75  
76        // Check for possible snags in the Runtime Profile Segment
77        if (field.getName() == null || field.getName().length() < 1)
78           throw new ConformanceError("Error building ConformanceField: Runtime Field does not contain a name.");
79  
80        // Set up class
81        gcc.setClassPackage(packageName);
82        gcc.addClassImport("ca.uhn.hl7v2.conf.classes.abs.*");
83        gcc.addClassImport("ca.uhn.hl7v2.conf.classes.exceptions.*");
84        gcc.addClassImport("ca.uhn.hl7v2.model.*");
85        gcc.addClassImport("ca.uhn.hl7v2.*");
86  
87        if (field.getComponents() > 0)
88           gcc.addClassImport(packageName + "." + profileName.getPackageName() + ".*");
89  
90        gcc.setName(profileName.getClassName());
91  
92        gcc.setProperties("extends AbstractConformanceContainer implements Repeatable");
93        gcc.setMinMaxReps(field.getMin(), field.getMax());
94        underlyingType = "ca.uhn.hl7v2.model." + versionString + ".datatype." + field.getDatatype();
95        gcc.addMemberVariable(underlyingType + " hapiType;");
96        gcc.addMemberVariable("private final short MAX_LENGTH = " + field.getLength() + ";");
97        gm.setReturnType("long");
98        gm.setVisibility("public");
99        gm.setName("getMaxLength");
100       gm.addToBody("return this.MAX_LENGTH;");
101 	  docBuilder.decorateMaxLength(gm);
102       gcc.addMethod(gm);
103 
104       // Set up underlying Field type
105       gcc.getConstructor().addParam(parentUnderlyingType + " hapiSegment", "The underlying HAPI field object");
106 
107       gcc.getConstructor().addParam("int rep", "The desired repetition");
108       gcc.getConstructor().addToBody("try {");
109 
110       UnderlyingAccessors/UnderlyingAccessor.html#UnderlyingAccessor">UnderlyingAccessor underlyingAccessor = new UnderlyingAccessor(parentUnderlyingType, profileName.getAccessorName());
111       gcc.getConstructor().addToBody("   this.hapiType = hapiSegment." + underlyingAccessor + ";");
112 
113 	  docBuilder.decorateConstructor( gcc.getConstructor(), profileName.getClassName() );
114 	  
115       // Create the getters and member variables associated with each child
116       for (int i = 1; i <= field.getComponents(); i++) {
117 		 //don't build not supported, backward, or unknown types
118 		 String usage = field.getComponent(i).getUsage();
119 		 if( usage != null && (usage.equals("X") || usage.equals("B") || usage.equals("U")) )
120 			continue;
121 			
122          boolean hasChildren = field.getComponent(i).getSubComponents() > 0;
123          ProfileNamer/builders/ProfileName.html#ProfileName">ProfileName childProfileName = new ProfileName(field.getComponent(i).getName(), ProfileName.PS_COMP);
124          gcc.addComponent(childProfileName, (short) (i - 1), hasChildren);
125       }
126 
127       gcc.getConstructor().addToBody("} catch ( HL7Exception e ) {");
128       gcc.getConstructor().addToBody("   throw new ConformanceError( \"Invalid Attempt to access a rep. This is a bug.\" );");
129       gcc.getConstructor().addToBody("}");
130 
131       // Decorate with comments
132       docBuilder.decorateField(gcc, field);
133 
134       if (depManager.getVerbose())
135          System.out.println("Generating Field: " + packageName + "." + gcc.getName());
136 
137       // Create the components
138       for (int i = 1; i <= field.getComponents(); i++) {
139          if (field.getComponent(i).getSubComponents() == 0) {
140             ConformancePrimitiveBuilderonformancePrimitiveBuilder.html#ConformancePrimitiveBuilder">ConformancePrimitiveBuilder childBuilder = new ConformancePrimitiveBuilder(packageName + "." + profileName.getPackageName(), depManager);
141             childBuilder.buildClass(field.getComponent(i), ProfileName.PS_COMP);
142          } else {
143             ConformanceComponentBuilderonformanceComponentBuilder.html#ConformanceComponentBuilder">ConformanceComponentBuilder childBuilder = new ConformanceComponentBuilder(packageName + "." + profileName.getPackageName(), depManager, versionString);
144             childBuilder.buildClass(field.getComponent(i));
145          }
146 
147       }
148       depManager.generateFile(gcc, packageName, gcc.getName());
149    }
150 }