1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package ca.uhn.hl7v2.conf.classes.generator.builders;
32
33 import java.lang.reflect.Constructor;
34 import java.lang.reflect.Method;
35
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import ca.uhn.hl7v2.conf.ProfileException;
40 import ca.uhn.hl7v2.conf.classes.exceptions.ConformanceError;
41 import ca.uhn.hl7v2.conf.classes.generator.genclasses.GeneratedConformanceContainer;
42 import ca.uhn.hl7v2.conf.spec.message.Component;
43 import ca.uhn.hl7v2.conf.spec.message.SubComponent;
44 import ca.uhn.hl7v2.model.Composite;
45 import ca.uhn.hl7v2.model.Message;
46
47
48
49
50
51
52
53
54 public class ConformanceComponentBuilder {
55
56 private static final Logger ourLog = LoggerFactory.getLogger(ConformanceComponentBuilder.class);
57
58 private final DeploymentManager depManager;
59 private final DocumentationBuilder docBuilder;
60 private final String packageName;
61 private final String versionString;
62
63
64
65
66
67
68 public ConformanceComponentBuilder(String packageName, DeploymentManager depManager, String versionString) {
69 this.packageName = packageName;
70 this.docBuilder = DocumentationBuilder.getDocumentationBuilder();
71 this.depManager = depManager;
72 this.versionString = versionString;
73 }
74
75
76
77
78 public void buildClass(Component comp) {
79
80 ProfileNameerator/builders/ProfileName.html#ProfileName">ProfileName profileName = new ProfileName(comp.getName(), ProfileName.PS_COMP);
81 GeneratedConformanceContainerclasses/GeneratedConformanceContainer.html#GeneratedConformanceContainer">GeneratedConformanceContainer gcc = new GeneratedConformanceContainer();
82
83 gcc.setClassPackage(packageName);
84
85 gcc.addClassImport("ca.uhn.hl7v2.conf.classes.abs.*");
86 gcc.addClassImport("ca.uhn.hl7v2.conf.classes.exceptions.*");
87 gcc.addClassImport("ca.uhn.hl7v2.model.*");
88 gcc.addClassImport(packageName + "." + profileName.getPackageName() + ".*");
89
90 gcc.setName(profileName.getClassName());
91 gcc.setProperties("extends AbstractConformanceContainer");
92 gcc.addMemberVariable("private Composite hapiType;");
93
94 gcc.getConstructor().addToComments("Creates new instances of all SubComponents contained in this Component");
95 gcc.getConstructor().addToBody("this.hapiType = hapiType;");
96
97 docBuilder.decorateConstructor( gcc.getConstructor(), profileName.getClassName() );
98
99 gcc.getConstructor().addToBody("try{");
100 for (int i = 1; i <= comp.getSubComponents(); i++) {
101
102 String usage = comp.getSubComponent(i).getUsage();
103 if( usage != null && (usage.equals("X") || usage.equals("B") || usage.equals("U")) )
104 continue;
105
106
107 Class<?> c;
108 Object instance;
109 try {
110
111 String className = "ca.uhn.hl7v2.model." + versionString + ".datatype." + comp.getSubComponent(i).getDatatype();
112 ourLog.info("Analyzing class: "+ className);
113
114 c = Class.forName(className);
115 Constructor<?> constructor = c.getConstructors()[0];
116 if (constructor.getParameterTypes().length == 1) {
117 instance = constructor.newInstance((Message)null);
118 } else {
119 instance = constructor.newInstance((Message)null, 0);
120 }
121 } catch ( Exception e ) {
122 throw new ConformanceError("Could not find underlying SubComponent datatype. This is a bug. Exception: " + e.toString(), e);
123 }
124
125 if ( instance instanceof Composite ) {
126
127 @SuppressWarnings("unchecked")
128 Class<? extends Composite> cComp = (Class<? extends Composite>) c;
129
130
131 Component generatedComponent = createSubComponentProfile(cComp, comp.getSubComponent(i).getName());
132 gcc.addComponent(new ProfileName(comp.getSubComponent(i).getName(), ProfileName.PS_COMP), i - 1, true);
133 ConformanceComponentBuilder childBuilder = new ConformanceComponentBuilder(packageName + "." + profileName.getPackageName(), depManager, versionString);
134 childBuilder.buildClass(generatedComponent);
135 } else {
136
137 gcc.addSubComponent(new ProfileName(comp.getSubComponent(i).getName(), ProfileName.PS_SUBC), i - 1);
138 ConformancePrimitiveBuilder childBuilder = new ConformancePrimitiveBuilder(packageName + "." + profileName.getPackageName(), depManager);
139 childBuilder.buildClass(comp.getSubComponent(i), ProfileName.PS_SUBC);
140 }
141
142 }
143
144 gcc.getConstructor().addParam("Composite hapiType", "Reference to the underlying HAPI Composite data type.");
145 gcc.getConstructor().addToBody("} catch ( DataTypeException e ) {");
146 gcc.getConstructor().addToBody(" throw new ConformanceError(\"Invalid Attempt to access a rep. This is a Conformance Class Bug\");");
147 gcc.getConstructor().addToBody("}");
148
149
150 docBuilder.decorateComponent(gcc, comp);
151
152 if (depManager.getVerbose())
153 System.out.println("Generating Component: " + packageName + "." + gcc.getName());
154
155 depManager.generateFile(gcc, packageName, gcc.getName());
156 }
157
158
159
160
161
162
163 private Component createSubComponentProfile(Class<? extends Composite> compositeClass, String name) {
164 Componentge/Component.html#Component">Component theComponent = new Component();
165 int numSubComponents = 0;
166
167 try {
168 theComponent.setName(name);
169 theComponent.setDatatype(compositeClass.getName().substring(compositeClass.getName().lastIndexOf('.') + 1));
170 Method[] methods = compositeClass.getMethods();
171
172 for (Method method : methods) {
173
174 if (method.getReturnType().getName().length() > 18 && method.getReturnType().getName().startsWith("ca.uhn.hl7v2.model") && method.getParameterTypes().length == 0 && method.getName().subSequence(0, 3).equals("get")) {
175
176 SubComponentc/message/SubComponent.html#SubComponent">SubComponent sc = new SubComponent();
177 sc.setName(method.getName().substring(3));
178 sc.setDatatype(method.getReturnType().getName().substring(method.getReturnType().getName().lastIndexOf('.') + 1));
179 sc.setConstantValue("");
180
181 numSubComponents++;
182 theComponent.setSubComponent(numSubComponents, sc);
183 }
184
185 }
186
187
188
189 if (theComponent.getDatatype().equals("TS") && theComponent.getSubComponents() == 0) {
190 SubComponentc/message/SubComponent.html#SubComponent">SubComponent sc = new SubComponent();
191 sc.setName("DateTime");
192 sc.setDatatype("NM");
193 sc.setConstantValue("");
194 theComponent.setSubComponent(1, sc);
195
196 sc = new SubComponent();
197 sc.setName("DegreeOfPrecision");
198 sc.setDatatype("ST");
199 sc.setConstantValue("");
200 theComponent.setSubComponent(2, sc);
201 }
202
203 } catch (ProfileException e) {
204 e.printStackTrace();
205 }
206
207 return theComponent;
208 }
209 }