001    /**
002    The contents of this file are subject to the Mozilla Public License Version 1.1 
003    (the "License"); you may not use this file except in compliance with the License. 
004    You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005    Software distributed under the License is distributed on an "AS IS" basis, 
006    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007    specific language governing rights and limitations under the License. 
008    
009    The Original Code is "CMPClassGenerator.java".  Description: 
010    "Generates EJB code related to RIM classes, for an entity bean 
011      with container managed persistence" 
012    
013    The Initial Developer of the Original Code is University Health Network. Copyright (C) 
014    2001.  All Rights Reserved. 
015    
016    Contributor(s): ______________________________________. 
017    
018    Alternatively, the contents of this file may be used under the terms of the 
019    GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
020    applicable instead of those above.  If you wish to allow use of your version of this 
021    file only under the terms of the GPL and not to allow others to use your version 
022    of this file under the MPL, indicate your decision by deleting  the provisions above 
023    and replace  them with the notice and other provisions required by the GPL License.  
024    If you do not delete the provisions above, a recipient may use your version of 
025    this file under either the MPL or the GPL. 
026    
027    */
028    
029    package ca.uhn.hl7v3.sourcegen;
030    
031    /**
032     * Generates EJB code related to RIM classes, for an entity bean 
033     * with container managed persistence.    
034     * @author Bryan Tripp
035     */
036    public class CMPClassGenerator {
037    
038        CMPAttributeGenerator attGen;
039        
040        /** Creates a new instance of CMPClassGenerator */
041        public CMPClassGenerator() {
042            attGen = new CMPAttributeGenerator();
043        }
044    
045        public String makeRemoteCode(ClassDefinition cls) {
046            StringBuffer code = new StringBuffer();
047            code.append("package ");
048            code.append(SourceGenerator.getRIMPackage());
049            code.append("; \r\n\r\n");
050            code.append("import ");
051            code.append(SourceGenerator.getRIMDataTypePackage());
052            code.append(".*; \r\n"); 
053            code.append("import java.rmi.RemoteException; \r\n");
054            code.append("import javax.ejb.*; \r\n\r\n");
055            code.append(SourceGenerator.makeJavaDocComment(cls.getDescription(), 0));
056            code.append("public interface ");
057            code.append(cls.getName());
058            code.append(" extends ");
059            if (cls.getSuperClass() == null) {
060                code.append("EJBObject");
061            } else {
062                code.append(cls.getSuperClass());
063            }
064            code.append(" { \r\n\r\n");
065            AttributeDefinition[] atts = cls.getAttributes();
066            for (int i = 0; i < atts.length; i++) {
067                code.append(attGen.makeRemoteCode(atts[i]));
068                code.append("\r\n");
069            }
070            code.append("}\r\n");
071            return code.toString();
072        }
073        
074        public String makeHomeCode(ClassDefinition cls) {
075            StringBuffer code = new StringBuffer();
076            code.append("package ");
077            code.append(SourceGenerator.getRIMPackage());
078            code.append("; \r\n\r\n");
079            code.append("import javax.ejb.*; \r\n");
080            code.append("import java.rmi.RemoteException; \r\n");
081            code.append("\r\n");
082            code.append("/** Home interface for the RIM '");
083            code.append(cls.getName());
084            code.append("' Entity Bean. */ \r\n");
085            code.append("public interface ");
086            code.append(cls.getName());
087            code.append("Home extends EJBHome { \r\n");
088            code.append("    public ");
089            code.append(cls.getName());
090            code.append(" create(String UID) throws CreateException, RemoteException; \r\n");
091            code.append("    public ");
092            code.append(cls.getName());
093            code.append(" findByPrimaryKey(String UID) throws FinderException, RemoteException; \r\n");
094            code.append("} \r\n");
095            return code.toString();
096        }
097        
098        public String makeBeanCode(ClassDefinition cls) {
099            AttributeDefinition[] atts = cls.getAttributes();
100            StringBuffer code = new StringBuffer();
101            code.append("package ");
102            code.append(SourceGenerator.getRIMPackage());
103            code.append("; \r\n\r\n");
104            code.append("import javax.ejb.*; \r\n");
105            code.append("import java.rmi.RemoteException; \r\n");
106            code.append("import ");
107            code.append(SourceGenerator.getRIMDataTypePackage());
108            code.append(".*; \r\n\r\n");
109            code.append(SourceGenerator.makeJavaDocComment("Container-Managed Entity Bean for the RIM class '" + cls.getName() + "'. " + cls.getDescription(), 0));
110            code.append("public class ");
111            code.append(cls.getName());
112            code.append("Bean extends ");
113            if (cls.getSuperClass() == null) {
114                code.append("RIMClass");
115            } else {
116                code.append(cls.getSuperClass());
117                code.append("Bean");
118            }
119            code.append(" { \r\n\r\n");
120            for (int i = 0; i < atts.length; i++) {
121                code.append(attGen.makeBeanAttribute(atts[i]));
122            }
123            code.append("\r\n");
124            for (int i = 0; i < atts.length; i++) {
125                code.append(attGen.makeBeanCode(atts[i]));
126                code.append("\r\n");
127            }
128            code.append("} \r\n");        
129            return code.toString();
130        }
131        
132        public static void main(String args[]) {
133            ClassDefinition cls = new ClassDefinition();
134            cls.setDescription("Entities are physical things or organizations and groupings of physical things. A physical thing is anything that has extent in space, and has mass. This hierarchy encompasses human beings, organizations, living organisms, devices, pharmaceutical substances, etc. This does not include events/acts/actions, the definition of things, the roles that things can play (e.g. patient, provider), nor the relationships among things.");
135            cls.setIsAbstract(false);
136            cls.setName("Entity");
137            AttributeDefinition[] atts = new AttributeDefinition[2];
138            atts[0] = new AttributeDefinition();
139            atts[0].setDataType("CE");
140            atts[0].setName("cd");
141            atts[0].setDescription("This is the main classifying attribute of the Entity class and all of its subclasses.  This code indicates what kind of Entity is meant using a code from one of several coding systems depending on the class of entities, such as living subjects (typed by animal and plant taxonomies), chemical substance (e.g., IUPAC code), organizations, insurance company, government agency, hospital, park, lake, syringe, etc. Note that the entity type code may be so fine grained that some types may only have one known instance.  Types with an extension of one instance are very similar to names. An example is the CDC vaccine manufacturer code, which is modeled as a concept vocabulary, when in fact each concept refers to only one instance. However, type codes SHOULD NOT normally be so fine grained as of overlap with instance identification.");
142            atts[1] = new AttributeDefinition();
143            atts[1].setDataType("CE");
144            atts[1].setName("class_cd");
145            atts[1].setDescription("A code specifying on a high, technical, and tightly controlled level the kind of entity. This code is similar in nature as the names of the classes derived from entity in a refined message information model (R-MIM.)");
146            cls.setAttributes(atts);
147            
148            CMPClassGenerator cgen = new CMPClassGenerator();
149            System.out.println(cgen.makeRemoteCode(cls));
150            System.out.println(cgen.makeBeanCode(cls));
151            System.out.println(cgen.makeHomeCode(cls));
152        }
153    }