001/**
002The 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. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "CMPClassGenerator.java".  Description: 
010"Generates EJB code related to RIM classes, for an entity bean 
011  with container managed persistence" 
012
013The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0142001.  All Rights Reserved. 
015
016Contributor(s): ______________________________________. 
017
018Alternatively, the contents of this file may be used under the terms of the 
019GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
020applicable instead of those above.  If you wish to allow use of your version of this 
021file only under the terms of the GPL and not to allow others to use your version 
022of this file under the MPL, indicate your decision by deleting  the provisions above 
023and replace  them with the notice and other provisions required by the GPL License.  
024If you do not delete the provisions above, a recipient may use your version of 
025this file under either the MPL or the GPL. 
026
027*/
028
029package 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 * @deprecated
036 */
037public class CMPClassGenerator {
038
039    CMPAttributeGenerator attGen;
040    
041    /** Creates a new instance of CMPClassGenerator */
042    public CMPClassGenerator() {
043        attGen = new CMPAttributeGenerator();
044    }
045
046    public String makeRemoteCode(ClassDefinition cls) {
047        StringBuffer code = new StringBuffer();
048        code.append("package ");
049        code.append(SourceGenerator.getRIMPackage());
050        code.append("; \r\n\r\n");
051        code.append("import ");
052        code.append(SourceGenerator.getRIMDataTypePackage());
053        code.append(".*; \r\n"); 
054        code.append("import java.rmi.RemoteException; \r\n");
055        code.append("import javax.ejb.*; \r\n\r\n");
056        code.append(SourceGenerator.makeJavaDocComment(cls.getDescription(), 0));
057        code.append("public interface ");
058        code.append(cls.getName());
059        code.append(" extends ");
060        if (cls.getSuperClass() == null) {
061            code.append("EJBObject");
062        } else {
063            code.append(cls.getSuperClass());
064        }
065        code.append(" { \r\n\r\n");
066        AttributeDefinition[] atts = cls.getAttributes();
067        for (int i = 0; i < atts.length; i++) {
068            code.append(attGen.makeRemoteCode(atts[i]));
069            code.append("\r\n");
070        }
071        code.append("}\r\n");
072        return code.toString();
073    }
074    
075    public String makeHomeCode(ClassDefinition cls) {
076        StringBuffer code = new StringBuffer();
077        code.append("package ");
078        code.append(SourceGenerator.getRIMPackage());
079        code.append("; \r\n\r\n");
080        code.append("import javax.ejb.*; \r\n");
081        code.append("import java.rmi.RemoteException; \r\n");
082        code.append("\r\n");
083        code.append("/** Home interface for the RIM '");
084        code.append(cls.getName());
085        code.append("' Entity Bean. */ \r\n");
086        code.append("public interface ");
087        code.append(cls.getName());
088        code.append("Home extends EJBHome { \r\n");
089        code.append("    public ");
090        code.append(cls.getName());
091        code.append(" create(String UID) throws CreateException, RemoteException; \r\n");
092        code.append("    public ");
093        code.append(cls.getName());
094        code.append(" findByPrimaryKey(String UID) throws FinderException, RemoteException; \r\n");
095        code.append("} \r\n");
096        return code.toString();
097    }
098    
099    public String makeBeanCode(ClassDefinition cls) {
100        AttributeDefinition[] atts = cls.getAttributes();
101        StringBuffer code = new StringBuffer();
102        code.append("package ");
103        code.append(SourceGenerator.getRIMPackage());
104        code.append("; \r\n\r\n");
105        code.append("import javax.ejb.*; \r\n");
106        code.append("import java.rmi.RemoteException; \r\n");
107        code.append("import ");
108        code.append(SourceGenerator.getRIMDataTypePackage());
109        code.append(".*; \r\n\r\n");
110        code.append(SourceGenerator.makeJavaDocComment("Container-Managed Entity Bean for the RIM class '" + cls.getName() + "'. " + cls.getDescription(), 0));
111        code.append("public class ");
112        code.append(cls.getName());
113        code.append("Bean extends ");
114        if (cls.getSuperClass() == null) {
115            code.append("RIMClass");
116        } else {
117            code.append(cls.getSuperClass());
118            code.append("Bean");
119        }
120        code.append(" { \r\n\r\n");
121        for (int i = 0; i < atts.length; i++) {
122            code.append(attGen.makeBeanAttribute(atts[i]));
123        }
124        code.append("\r\n");
125        for (int i = 0; i < atts.length; i++) {
126            code.append(attGen.makeBeanCode(atts[i]));
127            code.append("\r\n");
128        }
129        code.append("} \r\n");        
130        return code.toString();
131    }
132    
133    public static void main(String args[]) {
134        ClassDefinition cls = new ClassDefinition();
135        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.");
136        cls.setIsAbstract(false);
137        cls.setName("Entity");
138        AttributeDefinition[] atts = new AttributeDefinition[2];
139        atts[0] = new AttributeDefinition();
140        atts[0].setDataType("CE");
141        atts[0].setName("cd");
142        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.");
143        atts[1] = new AttributeDefinition();
144        atts[1].setDataType("CE");
145        atts[1].setName("class_cd");
146        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.)");
147        cls.setAttributes(atts);
148        
149        CMPClassGenerator cgen = new CMPClassGenerator();
150        System.out.println(cgen.makeRemoteCode(cls));
151        System.out.println(cgen.makeBeanCode(cls));
152        System.out.println(cgen.makeHomeCode(cls));
153    }
154}