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 "DBTableRepository.java".  Description: 
10  "Implements TableRepository by looking up values from the default HL7
11    normative database" 
12  
13  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
14  2001.  All Rights Reserved. 
15  
16  Contributor(s): James Agnew
17  				Paul Brohman
18  				Mitch Delachevrotiere
19  				Shawn Dyck
20  				Cory Metcalf
21    				
22  Alternatively, the contents of this file may be used under the terms of the 
23  GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
24  applicable instead of those above.  If you wish to allow use of your version of this 
25  file only under the terms of the GPL and not to allow others to use your version 
26  of this file under the MPL, indicate your decision by deleting  the provisions above 
27  and replace  them with the notice and other provisions required by the GPL License.  
28  If you do not delete the provisions above, a recipient may use your version of 
29  this file under either the MPL or the GPL. 
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  
37  /** This Class builds Conformance Primitive 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 ConformancePrimitiveBuilder {
45     private DeploymentManager depManager; // The deployment manager
46     private DocumentationBuilder docBuilder; // The documentation builder
47  
48     private String packageName; // Represents the Package that this Segment will go in
49  
50     /** This constructor will create a new ConformancePrimitiveBuilder
51      */
52     public ConformancePrimitiveBuilder(String packageName, DeploymentManager depManager) {
53        super();
54        this.packageName = packageName;
55        this.docBuilder = DocumentationBuilder.getDocumentationBuilder();
56        this.depManager = depManager;
57     }
58  
59     /** This method will build a primitive conformance class (ST, NM, etc) which is
60      * a Component or Subcomponent. 
61      */
62     public void buildClass(ca.uhn.hl7v2.conf.spec.message.AbstractComponent primitive, int profileType) {
63        GeneratedPrimitive genClass = new GeneratedPrimitive();
64        ProfileName profileName = new ProfileName(primitive.getName(), profileType);
65  
66        // Set up class
67        genClass.setClassPackage(packageName);
68        genClass.addClassImport("ca.uhn.hl7v2.model.*");
69        genClass.addClassImport("ca.uhn.hl7v2.conf.classes.abs.*");
70        genClass.setProperties("extends AbstractConformanceDataType");
71  
72        genClass.setName(profileName.getClassName());
73  	  docBuilder.decorateConstructor( genClass.getConstructor(), profileName.getClassName() );
74  	  	  
75        if (primitive.getConstantValue() != null && primitive.getConstantValue().length() > 0) {
76  			// Add constant value constraints if there are any
77           genClass.addConstantValue(primitive.getConstantValue());
78        } else {
79        	// if no constant value, then we add a setter method
80           GeneratedMethod setter = new GeneratedMethod();
81           setter.addParam("java.lang.String value");
82           setter.addToThrows("ConfDataException");
83           setter.addToBody("super.setValue( value );");
84           setter.setReturnType("void");
85           setter.setVisibility("public");
86           setter.setName("setValue");
87           docBuilder.decorateSetValue(setter, primitive.getLength());
88           genClass.addMethod(setter);
89  
90           genClass.addClassImport("ca.uhn.hl7v2.conf.classes.exceptions.*");
91        }
92        genClass.addMaxLength(primitive.getLength());
93  
94        // Decorate with comments
95        docBuilder.decoratePrimitive(genClass, primitive);
96        if (depManager.getVerbose())
97           System.out.println("Generating Primitive: " + packageName + "." + genClass.getName());
98  
99        depManager.generateFile(genClass, packageName, genClass.getName());
100 
101    }
102 
103    /** This method will build a primitive conformance class (ST, NM, etc) which is
104     * a Field. 
105     */
106    public void buildClass(ca.uhn.hl7v2.conf.spec.message.Field primitive, String parentUnderlyingType, ProfileName profileName) {
107       GeneratedPrimitive genClass = new GeneratedPrimitive();
108 
109       // Check for possible snags in the Runtime Profile Component
110       if (primitive.getName() == null || primitive.getName().length() < 1)
111          throw new ConformanceError("Error building ConformanceSegment: Runtime AbstractComponent does not contain a name.");
112 
113       GeneratedMethod theConstructor = new GeneratedMethod();
114       genClass.setConstructor(theConstructor);
115       genClass.addClassImport("ca.uhn.hl7v2.model.*");
116 
117       UnderlyingAccessor underlyingAccessor = new UnderlyingAccessor(parentUnderlyingType, profileName.getAccessorName());
118       theConstructor.addParam(parentUnderlyingType + " parentSeg", "The parent underlying data type");
119       theConstructor.addParam("int rep", "The desired repetition");
120       theConstructor.setName(profileName.getClassName());
121       theConstructor.setVisibility("public ");
122       theConstructor.addToThrows("ca.uhn.hl7v2.HL7Exception");
123       theConstructor.addToBody("super( (Primitive)parentSeg." + underlyingAccessor + " );");
124       theConstructor.addToBody("if ( parentSeg." + underlyingAccessor + " == null )");
125       theConstructor.addToBody("   throw new ca.uhn.hl7v2.HL7Exception( \"Error accussing underlying object. This is a bug.\", 0 );");
126 
127       // Set up class
128       genClass.setClassPackage(packageName);
129       //genClass.addClassImport("ca.uhn.hl7v2.model.*");
130       genClass.addClassImport("ca.uhn.hl7v2.conf.classes.abs.*");
131       //genClass.addClassImport( "ca.uhn.hl7v2.conf.classes.exceptions.*" );
132       genClass.setProperties("extends AbstractConformanceDataType implements Repeatable");
133 
134       // Add min and max reps stuff
135       genClass.setMinMaxReps(primitive.getMin(), primitive.getMax());
136 
137       genClass.setName(profileName.getClassName());
138 	  docBuilder.decorateConstructor( genClass.getConstructor(), profileName.getClassName() );
139 	  
140       // Add constant value constraints if there are any, if not, add a setter method
141       if (primitive.getConstantValue() != null && primitive.getConstantValue().length() > 0) {
142          genClass.addConstantValue(primitive.getConstantValue());
143       } else {
144          GeneratedMethod setter = new GeneratedMethod();
145          setter.addParam("java.lang.String value");
146          setter.addToThrows("ConfDataException");
147          setter.addToBody("super.setValue( value );");
148          setter.setReturnType("void");
149          setter.setVisibility("public");
150          setter.setName("setValue");
151          docBuilder.decorateSetValue(setter, primitive.getLength());
152          genClass.addMethod(setter);
153 
154          genClass.addClassImport("ca.uhn.hl7v2.conf.classes.exceptions.*");
155       }
156       genClass.addMaxLength(primitive.getLength());
157 
158       // Decorate with comments
159       docBuilder.decoratePrimitive(genClass, primitive);
160       if (depManager.getVerbose())
161          System.out.println("Generating Primitive: " + packageName + "." + genClass.getName());
162 
163       depManager.generateFile(genClass, packageName, genClass.getName());
164    }
165 
166 }