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.*;
34  import ca.uhn.hl7v2.conf.spec.message.*;
35  
36  /** This Class builds Conformance SegGroup Classes
37   * @author <table><tr>James Agnew</tr>
38   *                <tr>Paul Brohman</tr>
39   *                <tr>Mitch Delachevrotiere</tr>
40   *                <tr>Shawn Dyck</tr>
41   * 				  <tr>Cory Metcalf</tr></table>
42   */
43  public class ConformanceSegGroupBuilder {
44  
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     private String structID; // The struct ID of the message type
50     private String version; // The HAPI version
51  
52     /** This constructor will create a new ConformanceSegmentBuilder
53      * @param packageName the name of the package
54      * @param versionString the version of HL7 which these classes are conforming to
55      * @param depManager the instance of DeploymentManager
56      */
57     public ConformanceSegGroupBuilder(String packageName, String version, DeploymentManager depManager, String structID) {
58        super();
59        this.packageName = packageName;
60        this.version = version;
61        this.docBuilder = DocumentationBuilder.getDocumentationBuilder();
62        this.depManager = depManager;
63        this.structID = structID;
64     }
65  
66     /** This method will build all the field children for this Segment
67      * @param segGroup the SegGroup to build
68      * @param parentUnderlyingType the data type of the parent Message for this field
69      *        example "ca.uhn.hl7v2.model.v24.group.ADR_A19_..."  
70      * @param profileName this is the profile name associated with this Class
71      */
72     public void buildClass(SegGroup segGroup, String parentUnderlyingType, ProfileName profileName) {
73        GeneratedConformanceContainer gcc = new GeneratedConformanceContainer();
74        ConformanceSegmentBuilder confSegBuilder = new ConformanceSegmentBuilder(packageName + "." + profileName.getPackageName(), version, depManager);
75        ConformanceSegGroupBuilder confSegGroupBuilder = new ConformanceSegGroupBuilder(packageName + "." + profileName.getPackageName(), version, depManager, structID);
76  
77        String underlyingDataType = structID + "_" + generateSegGroupName(segGroup);
78        String underlyingPackageType = "ca.uhn.hl7v2.model." + version + ".group." + underlyingDataType;
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.*");
85        gcc.addClassImport(packageName + "." + profileName.getPackageName() + ".*");
86  
87        gcc.setName(profileName.getClassName());
88        gcc.setProperties("extends AbstractConformanceContainer implements Repeatable");
89        gcc.setMinMaxReps(segGroup.getMin(), segGroup.getMax());
90        gcc.addMemberVariable("private " + underlyingPackageType + " hapiSegGroup;");
91  
92        // Set up underlying Segment type
93        GeneratedMethod theConstructor = gcc.getConstructor();
94  	  docBuilder.decorateConstructor( theConstructor, profileName.getClassName() );
95        theConstructor.addParam(parentUnderlyingType + " underlyingMessage", "The underlying message object");
96        theConstructor.addParam("int rep", "The underlying rep number");
97        theConstructor.addToThrows("HL7Exception");
98  
99        UnderlyingAccessor underlyingAccessor = new UnderlyingAccessor(parentUnderlyingType, "get" + underlyingDataType);
100       theConstructor.addToBody("this.hapiSegGroup = underlyingMessage." + underlyingAccessor.toString() + ";");
101 
102       for (int i = 1; i <= segGroup.getChildren(); i++) {
103 		 //don't build not supported, backward, or unknown types
104 		 String usage = segGroup.getChild(i).getUsage();
105 		 if( usage != null && (usage.equals("X") || usage.equals("B") || usage.equals("U")) )
106 			continue;
107 			
108          if (segGroup.getChild(i) instanceof Seg) {
109             ProfileName childProfileName = profileName.newInstance(segGroup.getChild(i).getName(), ProfileName.PS_SEG);
110 
111             // Add the member variable vector to hold them
112             gcc.addMemberVariable("private FiniteList " + childProfileName.getMemberName() + ";");
113             theConstructor.addToBody(childProfileName.getMemberName() + " = new FiniteList( " + childProfileName.getClassName() + ".class, hapiSegGroup );");
114 				UnderlyingAccessor childAccessor = new UnderlyingAccessor(underlyingPackageType, childProfileName.getAccessorName());
115             GeneratedRepGetter repGetter = new GeneratedRepGetter(childProfileName, childAccessor.getAcceptsRep());
116 
117             docBuilder.decorateRepGetter(repGetter, (Seg) segGroup.getChild(i), childProfileName.getOriginalName());
118             gcc.addMethod(repGetter);
119             if (depManager.getVerbose())
120                System.out.println("Generating Segment: " + packageName + "." + gcc.getName());
121 
122             confSegBuilder.buildClass((Seg) segGroup.getChild(i), underlyingPackageType, childProfileName.clearNameMap());
123 
124          } else
125             if (segGroup.getChild(i) instanceof SegGroup) {
126                ProfileName childProfileName = profileName.newInstance(segGroup.getChild(i).getName(), ProfileName.PS_SEGG);
127 
128                // Add the member variable vector to hold them
129                gcc.addMemberVariable("private FiniteList " + childProfileName.getMemberName() + ";");
130                theConstructor.addToBody(childProfileName.getMemberName() + " = new FiniteList( " + childProfileName.getClassName() + ".class, hapiSegGroup );");
131 
132 					String underlyingAccessorName = "get" + structID + "_" + ConformanceSegGroupBuilder.generateSegGroupName((SegGroup)segGroup.getChild(i));
133 					UnderlyingAccessor childAccessor = new UnderlyingAccessor(underlyingPackageType, underlyingAccessorName);
134                GeneratedRepGetter repGetter = new GeneratedRepGetter(childProfileName, childAccessor.getAcceptsRep());
135 
136                docBuilder.decorateRepGetter(repGetter, (SegGroup) segGroup.getChild(i), childProfileName.getOriginalName());
137                gcc.addMethod(repGetter);
138                if (depManager.getVerbose())
139                   System.out.println("Generating SegGroup: " + packageName + "." + gcc.getName());
140 
141                confSegGroupBuilder.buildClass((SegGroup) segGroup.getChild(i), underlyingPackageType, childProfileName.clearNameMap());
142             }
143 
144       }
145 
146       depManager.generateFile(gcc, packageName, profileName.getClassName());
147 
148    }
149 
150 	/** This method creates the segment group name which is comprised of all the 
151 	 * underlying segments names put together
152 	 * @param segGroup the SegGroup to build
153 	 */
154 	public static String generateSegGroupName(SegGroup segGroup) {
155 		String name = new String();
156 
157 		for (int i = 1; i <= segGroup.getChildren(); i++) {
158 			ProfileStructure child = segGroup.getChild(i);
159 			if (child instanceof Seg)
160 				name += child.getName();
161 			else
162 				name += generateSegGroupName((SegGroup) child);
163 		}
164 
165 		return name;
166 	}
167 
168 }