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 java.util.Objects;
35  import java.util.regex.Pattern;
36  
37  import ca.uhn.hl7v2.conf.spec.message.*;
38  import ca.uhn.hl7v2.conf.classes.generator.genclasses.*;
39  import ca.uhn.hl7v2.conf.spec.*;
40  
41  /** This Class contains some of the Documentation for the Class to be generated
42   * @author <table><tr>James Agnew</tr>
43   *                 <tr>Paul Brohman</tr>
44   *                 <tr>Mitch Delachevrotiere</tr>
45   *                 <tr>Shawn Dyck</tr>
46   * 				   <tr>Cory Metcalf</tr></table>
47   */
48  public final class DocumentationBuilder {
49  
50  	private static DocumentationBuilder documentationBuilder;
51  
52  	/** Returns the singleton instance of the DocumentationBuilder
53  	 * @return the singleton instance of the DocumentationBuilder
54  	 */
55  	public static DocumentationBuilder getDocumentationBuilder() {
56  		if (documentationBuilder == null)
57  			documentationBuilder = new DocumentationBuilder();
58  
59  		return documentationBuilder;
60  	}
61  
62  	/** Default Constructor */
63  	private DocumentationBuilder() {
64  	}
65  
66  	/** Adds a label and it data value to GeneratedClass Comments in a table format
67  	 * @param genClass a GeneratedConformanceContainer
68  	 * @param label optional comment label to add to the class header document table
69  	 * @param data the data value to add to the class header document table
70  	 */
71  	private void addOptionalComment(
72  		GeneratedConformanceContainer genClass,
73  		String label,
74  		String data) {
75  		if (data != null && !Objects.equals(data, ""))
76  			genClass.addClassComment(
77  				"   <tr><td>" + label + "</td><td>" + data + "</td></tr>");
78  	}
79  
80  	/** Adds a label and it data value to Primitive GeneratedClass Comments in a table format
81  	 * @param genClass a GeneratedPrimitive
82  	 * @param label optional comment label to add to the class header document table
83  	 * @param data the data value to add to the class header document table
84  	 */
85  	private void addOptionalComment(
86  		GeneratedPrimitive genClass,
87  		String label,
88  		String data) {
89  		if (data != null && !Objects.equals(data, ""))
90  			genClass.addClassComment(
91  				"   <tr><td>" + label + "</td><td>" + data + "</td></tr>");
92  	}
93  
94  	/** Adds a table of comments to a Generated Component Class
95  	 * @param genClass a GeneratedConformanceContainer
96  	 * @param comp the component for which the comments are being added to
97  	 */
98  	public void decorateComponent(
99  		GeneratedConformanceContainer genClass,
100 		Component comp) {
101 		genClass.addClassComment(
102 			"This class contains and provides access to all SubComponents");
103 		genClass.addClassComment("<table>");
104 		addOptionalComment(genClass, "Name", comp.getName());
105 		addOptionalComment(genClass, "Usage", comp.getUsage());
106 		addOptionalComment(genClass, "Data Type", comp.getDatatype());
107 		addOptionalComment(genClass, "Constant Value", comp.getConstantValue());
108 		addOptionalComment(genClass, "Description", comp.getDescription());
109 		addOptionalComment(genClass, "Implementation Note", comp.getImpNote());
110 		addOptionalComment(genClass, "Predicate", comp.getPredicate());
111 		addOptionalComment(genClass, "Reference", comp.getReference());
112 		addOptionalComment(genClass, "Length", comp.getLength() + "");
113 		if (comp.getLength() != 0)
114 			addOptionalComment(genClass, "Table", comp.getTable() + "");
115 		genClass.addClassComment("</table>");
116 	}
117 
118 	/** Adds a table of comments to a Conformance Message Class
119 	 * @param genClass a GeneratedConformanceMessage
120 	 * @param runtimeProfile the runtimeProfile for which the comments are being added to
121 	 */
122 	public void decorateConformanceMessage(
123 		GeneratedConformanceMessage genClass,
124 		RuntimeProfile runtimeProfile) {
125 		genClass.addToClassComments(
126 			"This class is the primary access class for the HL7 "
127 				+ runtimeProfile.getHL7Version()
128 				+ " "
129 				+ runtimeProfile.getMessage().getMsgType()
130 				+ " Conformance");
131 		genClass.addToClassComments(
132 			"Class Set. This message has the following attributes:<br>");
133 		genClass.addToClassComments("<table>");
134 		genClass.addToClassComments(
135 			"<tr><td>Message Type</td><td>"
136 				+ runtimeProfile.getMessage().getMsgType()
137 				+ "</td></tr>");
138 		genClass.addToClassComments(
139 			"<tr><td>Event Type</td><td>"
140 				+ runtimeProfile.getMessage().getEventType()
141 				+ "</td></tr>");
142 		genClass.addToClassComments(
143 			"<tr><td>HL7 Version</td><td>"
144 				+ runtimeProfile.getHL7Version()
145 				+ "</td></tr>");
146 		genClass.addToClassComments("</table><br><br>");
147 		genClass.addToClassComments("<b>Purpose</b><br>");
148 		genClass.addToClassComments(runtimeProfile.getMessage().getEventDesc());
149 	}
150 
151 	/** Adds Class Comments to a getMaxLength method
152 	 * @param genMethod the Generated Method to add the constant value comments to
153 	 */
154 	public void decorateConstantValue(GeneratedMethod genMethod) {
155 		genMethod.addToDescription(
156 			"Returns the constant value associated with this Data Type");
157 		genMethod.addToComments(
158 			"@return the constant value associated with this Data Type, or <code>null</code> if there is none");
159 
160 	}
161 
162 	/** Adds comments to a generated constructor
163 	 * @param constructor a constructor to add comments to
164 	 * @param className class name
165 	 */
166 	public void decorateConstructor(GeneratedMethod constructor, String className) {
167 		constructor.addToDescription("Default constructor for Conformance_HL7_" + className + ". Has the");
168 		constructor.addToDescription("following functions:");
169 		constructor.addToDescription("<ul><li>Instantiating all required Conformance Classes in the structure");
170 		constructor.addToDescription("    <li>Instantiating the underlying HAPI Message object");
171 		constructor.addToDescription("    <li>Populating any constant values");
172 		constructor.addToDescription("</ul>");
173 
174 	}
175 
176 	/** Adds Class Comments to a Generated Field Class
177 	 * @param genClass a GeneratedConformanceContainer
178 	 * @param field the field for which the comments are being added to
179 	 */
180 	public void decorateField(
181 		GeneratedConformanceContainer genClass,
182 		Field field) {
183 		genClass.addClassComment(
184 			"This represents a constrained "
185 				+ field.getName()
186 				+ " field, with the following proerties:");
187 		genClass.addClassComment("<table>");
188 		addOptionalComment(genClass, "Name", field.getName());
189 		addOptionalComment(genClass, "Usage", field.getUsage());
190 		addOptionalComment(genClass, "Description", field.getDescription());
191 		addOptionalComment(genClass, "Implementation Note", field.getImpNote());
192 		addOptionalComment(genClass, "Predicate", field.getPredicate());
193 		addOptionalComment(genClass, "Reference", field.getReference());
194 		addOptionalComment(
195 			genClass,
196 			"Minimum Repetitions ",
197 			field.getMin() + "");
198 		addOptionalComment(
199 			genClass,
200 			"Maximum Repetitions ",
201 			field.getMax() + "");
202 		genClass.addClassComment("</table>");
203 	}
204 
205 	/** Adds Class Comments to a getMaxLength method
206 	 * @param genMethod a GeneratedMethod
207 	 */
208 	public void decorateMaxLength(GeneratedMethod genMethod) {
209 		genMethod.addToDescription(
210 			"Returns the Maximum allowable length for the Data Type");
211 		genMethod.addToComments(
212 			"@return the maximum allowable length of the Data Type");
213 
214 	}
215 
216 	/** Adds Class Comments to a Maximum Repition Method
217 	 * @param genMethod the GeneratedMethod for which the comments are being added to
218 	 */
219 	public void decorateMaxReps(GeneratedMethod genMethod) {
220 
221 		genMethod.addToDescription("This method returns the maximum allowable");
222 		genMethod.addToComments("repetitions of this object.");
223 		genMethod.addToComments("@return maximum allowable repetitions");
224 
225 	}
226 
227 	/** Adds Class Comments to a Minimum Repition Method
228 	 * @param genMethod the GeneratedMethod for which the comments are being added to
229 	 */
230 	public void decorateMinReps(GeneratedMethod genMethod) {
231 
232 		genMethod.addToDescription("This method returns the minimum allowable");
233 		genMethod.addToComments("repetitions of this object.");
234 		genMethod.addToComments("@return minimum allowable repetitions");
235 
236 	}
237 
238 	/** Adds Class Comments to a Generated Primitive Class
239 	 * @param genClass a GeneratedConformanceContainer
240 	 * @param ac the AbstractComponent for which the comments are being added to
241 	 */
242 	public void decoratePrimitive(
243 		GeneratedPrimitive genClass,
244 		AbstractComponent ac) {
245 		genClass.addClassComment(
246 			"This represents a constrained "
247 				+ ac.getName()
248 				+ " field, with the following properties:");
249 		genClass.addClassComment("<table>");
250 		addOptionalComment(genClass, "Name", ac.getName());
251 		addOptionalComment(genClass, "Usage", ac.getUsage());
252 		addOptionalComment(genClass, "Data Type", ac.getDatatype());
253 		addOptionalComment(genClass, "Constant Value", ac.getConstantValue());
254 		addOptionalComment(genClass, "Description", ac.getDescription());
255 		addOptionalComment(genClass, "Implementation Note", ac.getImpNote());
256 		addOptionalComment(genClass, "Predicate", ac.getPredicate());
257 		addOptionalComment(genClass, "Reference", ac.getReference());
258 		addOptionalComment(genClass, "Length", ac.getLength() + "");
259 		if (ac.getTable() != null && Pattern.matches("[^0]", ac.getTable()))
260 			addOptionalComment(genClass, "Table", ac.getTable() + "");
261 		genClass.addClassComment("</table>");
262 	}
263 
264 	/** Adds Class Comments to a Generated Class
265 	 * @param genClass a GeneratedConformanceContainer
266 	 * @param ps the Profile Structure for which the comments are being added to
267 	 */
268 	public void decorateProfileStructure(
269 		GeneratedConformanceContainer genClass,
270 		ProfileStructure ps) {
271 		genClass.addClassComment(
272 			"This represents a constrained "
273 				+ ps.getName()
274 				+ " segment, with the following properties:");
275 		genClass.addClassComment("<table>");
276 		addOptionalComment(genClass, "Name", ps.getName());
277 		addOptionalComment(genClass, "Usage", ps.getUsage());
278 		addOptionalComment(genClass, "Description", ps.getDescription());
279 		addOptionalComment(genClass, "Implementation Note", ps.getImpNote());
280 		addOptionalComment(genClass, "Predicate", ps.getPredicate());
281 		addOptionalComment(genClass, "Reference", ps.getReference());
282 		addOptionalComment(genClass, "Minimum Repetitions ", ps.getMin() + "");
283 		addOptionalComment(genClass, "Maximum Repetitions ", ps.getMax() + "");
284 		genClass.addClassComment("</table>");
285 	}
286 
287 	/** Adds Class Comments to a Generated Method
288 	 * @param genMethod a GeneratedRepGetter
289 	 * @param field the field for which the comments are being added to
290 	 * @param repType the repetition Type
291 	 */
292 	public void decorateRepGetter(
293 		GeneratedRepGetter genMethod,
294 		Field field,
295 		String repType) {
296 
297 		genMethod.addToDescription(
298 			"This method returns a reference to the "
299 				+ field.getName()
300 				+ " field belonging to this segment.");
301 		genMethod.addToComments("Min Repetitions " + field.getMin());
302 		genMethod.addToComments("Max Repetitions " + field.getMax());
303 		if (field.getMax() == -1)
304 			genMethod.addToComments("@param rep The desired repetition number");
305 		genMethod.addToComments(
306 			"@return the child object reference. <b>rep must fall between "
307 				+ field.getMin()
308 				+ " and "
309 				+ field.getMax()
310 				+ "</b>");
311 		genMethod.addToComments(
312 			"@throws ca.uhn.hl7v2.conf.classes.exceptions.ConfRepException");
313 
314 	}
315 
316 	/** Adds Class Comments to a Generated Method
317 	 * @param genMethod a GeneratedRepGetter
318 	 * @param profileStructure the Profile Structure for which the comments are being added to
319 	 * @param repType the repetition Type
320 	 */
321 	public void decorateRepGetter(
322 		GeneratedRepGetter genMethod,
323 		ProfileStructure profileStructure,
324 		String repType) {
325 
326 		genMethod.addToDescription(
327 			"This method returns a reference to the "
328 				+ profileStructure.getName()
329 				+ " child.");
330 		genMethod.addToComments("Min Repetitions" + profileStructure.getMin());
331 		genMethod.addToComments("Max Repetitions" + profileStructure.getMax());
332 		if (profileStructure.getMax() == -1)
333 			genMethod.addToComments("@param rep The desired repetition number");
334 		genMethod.addToComments(
335 			"@return the child object reference. <b>rep must fall between "
336 				+ profileStructure.getMin()
337 				+ " and "
338 				+ profileStructure.getMax()
339 				+ "</b>");
340 		genMethod.addToComments(
341 			"@throws ca.uhn.hl7v2.conf.classes.exceptions.ConfRepException");
342 
343 	}
344 
345 	/** Adds Class Comments to a Generated SegGroup Class
346 	 * @param genClass a GeneratedConformanceContainer
347 	 * @param segGroup the SegGroup for which the comments are being added to
348 	 */
349 	public void decorateSegGroup(
350 		GeneratedConformanceContainer genClass,
351 		SegGroup segGroup) {
352 		this.decorateProfileStructure(genClass, segGroup);
353 	}
354 
355 	/** Adds Class Comments to a Generated Segment Class
356 	 * @param genClass a GeneratedConformanceContainer
357 	 * @param seg the Segment for which the comments are being added to
358 	 */
359 	public void decorateSegment(
360 		GeneratedConformanceContainer genClass,
361 		Seg seg) {
362 		this.decorateProfileStructure(genClass, seg);
363 	}
364 
365 	public void decorateSetValue(GeneratedMethod genMethod, long maxLength) {
366 		genMethod.addToDescription(
367 			"This method validates the <code>String</code> value passed in to be no greater then the");
368 		genMethod.addToDescription(
369 			"maximum allowable length for the extending class.  If the <code>String</code> value is valid, this");
370 		genMethod.addToDescription(
371 			"method will set the underlying HAPI class's value. If the data passed in is invalid for the given");
372 		genMethod.addToDescription(
373 			"data type, a ConformanceException is thrown.");
374 		genMethod.addToDescription(
375 			"@throws ConfDataException if the data is invalid for the given datatype");
376 		//TODO: this probably isn't the right exception type
377 		genMethod.addToDescription("@param value The value to be set.");
378 		if (maxLength > 0)
379 			genMethod.addToDescription(
380 				"Note: The value has a maximum length of " + maxLength);
381 	}
382 
383 	/** Contains the License agreement for the Generated Classes
384 	 * @return Generated Class Lincense agreement
385 	 */
386 	public String getGeneratedClassHeader() {
387 		return (
388 			"/**\n"
389 				+ "The following code was automatically genearated by the Conformance Class Generation System\n"
390 				+ "Contributor(s):The Conformance Class Generation System was developed by\n"
391 				+ "                --------------.:JH6 Solutions:.--------------\n"
392 				+ "                James Agnew - jait214@yahoo.ca\n"
393 				+ "                Paul Brohman - pbrohman@rogers.com\n"
394 				+ "                Mitch Delachevrotiere - hapimgd@hotmail.com\n"
395 				+ "                Shawn Dyck - sdyck613@yahoo.ca\n"
396 				+ "                Cory Metcalf - cory_metcalf@hotmail.com\n"
397 				+ "\n"
398 				+ "*/");
399 	}
400 
401 }