KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > api > codegeneration > EntityGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbcore.api.codegeneration;
21
22 import org.netbeans.modules.j2ee.ejbcore.EjbGenerationUtil;
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.netbeans.api.project.FileOwnerQuery;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.modules.j2ee.common.source.GenerationUtils;
29 import org.netbeans.modules.j2ee.dd.api.ejb.AssemblyDescriptor;
30 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField;
31 import org.netbeans.modules.j2ee.dd.api.ejb.ContainerTransaction;
32 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
33 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
34 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
35 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
36 import org.netbeans.modules.j2ee.ejbcore.naming.EJBNameOptions;
37 import org.openide.filesystems.FileObject;
38
39 /**
40  *
41  * @author Chris Webster
42  * @author Martin Adamek
43  */

44 public class EntityGenerator {
45
46     private static final String JavaDoc BMP_EJBCLASS = "Templates/J2EE/EJB21/BmpEjbClass.java"; // NOI18N
47
private static final String JavaDoc BMP_LOCAL = "Templates/J2EE/EJB21/BmpLocal.java"; // NOI18N
48
private static final String JavaDoc BMP_LOCALHOME = "Templates/J2EE/EJB21/BmpLocalHome.java"; // NOI18N
49
private static final String JavaDoc BMP_REMOTE = "Templates/J2EE/EJB21/BmpRemote.java"; // NOI18N
50
private static final String JavaDoc BMP_REMOTEHOME = "Templates/J2EE/EJB21/BmpRemoteHome.java"; // NOI18N
51

52     private static final String JavaDoc CMP_EJBCLASS = "Templates/J2EE/EJB21/CmpEjbClass.java"; // NOI18N
53
private static final String JavaDoc CMP_LOCAL = "Templates/J2EE/EJB21/CmpLocal.java"; // NOI18N
54
private static final String JavaDoc CMP_LOCALHOME = "Templates/J2EE/EJB21/CmpLocalHome.java"; // NOI18N
55
private static final String JavaDoc CMP_REMOTE = "Templates/J2EE/EJB21/CmpRemote.java"; // NOI18N
56
private static final String JavaDoc CMP_REMOTEHOME = "Templates/J2EE/EJB21/CmpRemoteHome.java"; // NOI18N
57

58     // informations collected in wizard
59
private final FileObject pkg;
60     private final boolean hasRemote;
61     private final boolean hasLocal;
62     private final boolean isCMP;
63     private final String JavaDoc primaryKeyClassName;
64     private final String JavaDoc wizardTargetName;
65     
66     // EJB naming options
67
private final EJBNameOptions ejbNameOptions;
68     private final String JavaDoc ejbName;
69     private final String JavaDoc ejbClassName;
70     private final String JavaDoc remoteName;
71     private final String JavaDoc remoteHomeName;
72     private final String JavaDoc localName;
73     private final String JavaDoc localHomeName;
74     private final String JavaDoc displayName;
75     
76     private final String JavaDoc packageName;
77     private final String JavaDoc packageNameWithDot;
78     
79     private final Map JavaDoc<String JavaDoc, String JavaDoc> templateParameters;
80
81     public static EntityGenerator create(String JavaDoc wizardTargetName, FileObject pkg, boolean hasRemote, boolean hasLocal,
82             boolean isCMP, String JavaDoc primaryKeyClassName) {
83         return new EntityGenerator(wizardTargetName, pkg, hasRemote, hasLocal, isCMP, primaryKeyClassName);
84     }
85     
86     private EntityGenerator(String JavaDoc wizardTargetName, FileObject pkg, boolean hasRemote, boolean hasLocal,
87             boolean isCMP, String JavaDoc primaryKeyClassName) {
88         this.pkg = pkg;
89         this.hasRemote = hasRemote;
90         this.hasLocal = hasLocal;
91         this.isCMP = isCMP;
92         this.primaryKeyClassName = primaryKeyClassName;
93         this.wizardTargetName = wizardTargetName;
94         this.ejbNameOptions = new EJBNameOptions();
95         this.ejbName = ejbNameOptions.getEntityEjbNamePrefix() + wizardTargetName + ejbNameOptions.getEntityEjbNameSuffix();
96         this.ejbClassName = ejbNameOptions.getEntityEjbClassPrefix() + wizardTargetName + ejbNameOptions.getEntityEjbClassSuffix();
97         this.remoteName = ejbNameOptions.getEntityRemotePrefix() + wizardTargetName + ejbNameOptions.getEntityRemoteSuffix();
98         this.remoteHomeName = ejbNameOptions.getEntityRemoteHomePrefix() + wizardTargetName + ejbNameOptions.getEntityRemoteHomeSuffix();
99         this.localName = ejbNameOptions.getEntityLocalPrefix() + wizardTargetName + ejbNameOptions.getEntityLocalSuffix();
100         this.localHomeName = ejbNameOptions.getEntityLocalHomePrefix() + wizardTargetName + ejbNameOptions.getEntityLocalHomeSuffix();
101         this.displayName = ejbNameOptions.getEntityDisplayNamePrefix() + wizardTargetName + ejbNameOptions.getEntityDisplayNameSuffix();
102         this.packageName = EjbGenerationUtil.getSelectedPackageName(pkg);
103         this.packageNameWithDot = packageName + ".";
104         this.templateParameters = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
105         // fill all possible template parameters
106
this.templateParameters.put("package", packageName);
107         this.templateParameters.put("primaryKey", primaryKeyClassName);
108         this.templateParameters.put("localInterface", packageNameWithDot + localName);
109         this.templateParameters.put("remoteInterface", packageNameWithDot + remoteName);
110     }
111
112     public FileObject generate() throws IOException JavaDoc {
113         FileObject resultFileObject = null;
114         if (isCMP) {
115             resultFileObject = generateCmpClasses();
116         } else {
117             resultFileObject = generateBmpClasses();
118         }
119
120         //put these lines in a common function at the appropriate place after EA1
121
//something like public EjbJar getEjbJar()
122
//This method will be used whereever we construct/get DD object graph to ensure
123
//corresponding config listners attached to it.
124
Project project = FileOwnerQuery.getOwner(pkg);
125         J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class);
126         j2eeModuleProvider.getConfigSupport().ensureConfigurationReady();
127
128         generateXml();
129         
130         return resultFileObject;
131     }
132     
133     private FileObject generateBmpClasses() throws IOException JavaDoc {
134         FileObject ejbClassFO = GenerationUtils.createClass(BMP_EJBCLASS, pkg, ejbClassName, null, templateParameters);
135         if (hasRemote) {
136             GenerationUtils.createClass(BMP_REMOTE, pkg, remoteName, null, templateParameters);
137             GenerationUtils.createClass(BMP_REMOTEHOME, pkg, remoteHomeName, null, templateParameters);
138         }
139         if (hasLocal) {
140             GenerationUtils.createClass(BMP_LOCAL, pkg, localName, null, templateParameters);
141             GenerationUtils.createClass(BMP_LOCALHOME, pkg, localHomeName, null, templateParameters);
142         }
143         return ejbClassFO;
144     }
145
146     private FileObject generateCmpClasses() throws IOException JavaDoc {
147         FileObject ejbClassFO = GenerationUtils.createClass(CMP_EJBCLASS, pkg, ejbClassName, null, templateParameters);
148         if (hasRemote) {
149             GenerationUtils.createClass(CMP_REMOTE, pkg, remoteName, null, templateParameters);
150             GenerationUtils.createClass(CMP_REMOTEHOME, pkg, remoteHomeName, null, templateParameters);
151         }
152         if (hasLocal) {
153             GenerationUtils.createClass(CMP_LOCAL, pkg, localName, null, templateParameters);
154             GenerationUtils.createClass(CMP_LOCALHOME, pkg, localHomeName, null, templateParameters);
155         }
156         return ejbClassFO;
157     }
158
159     private void generateXml() throws IOException JavaDoc {
160         org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(pkg);
161         org.netbeans.modules.j2ee.dd.api.ejb.EjbJar ejbJar = DDProvider.getDefault().getMergedDDRoot(ejbModule.getMetadataUnit());
162         EnterpriseBeans enterpriseBeans = ejbJar.getEnterpriseBeans();
163         if (enterpriseBeans == null) {
164             enterpriseBeans = ejbJar.newEnterpriseBeans();
165             ejbJar.setEnterpriseBeans(enterpriseBeans);
166         }
167         Entity entity = enterpriseBeans.newEntity();
168         entity.setEjbName(ejbName);
169         entity.setEjbClass(packageNameWithDot + ejbClassName);
170         entity.setPrimKeyClass(primaryKeyClassName);
171         entity.setReentrant(false);
172         entity.setDisplayName(displayName);
173         if (hasRemote) {
174             entity.setRemote(packageNameWithDot + remoteName);
175             entity.setHome(packageNameWithDot + remoteHomeName);
176         }
177         if (hasLocal) {
178             entity.setLocal(packageNameWithDot + localName);
179             entity.setLocalHome(packageNameWithDot + localHomeName);
180         }
181         if (isCMP) {
182             entity.setPersistenceType(Entity.PERSISTENCE_TYPE_CONTAINER);
183             entity.setAbstractSchemaName(wizardTargetName);
184             CmpField cmpField = entity.newCmpField();
185             cmpField.setFieldName("key");
186             entity.addCmpField(cmpField);
187             entity.setPrimkeyField("key");
188         } else {
189             entity.setPersistenceType(Entity.PERSISTENCE_TYPE_BEAN);
190         }
191         enterpriseBeans.addEntity(entity);
192         // add transaction requirements
193
AssemblyDescriptor assemblyDescriptor = ejbJar.getSingleAssemblyDescriptor();
194         if (assemblyDescriptor == null) {
195             assemblyDescriptor = ejbJar.newAssemblyDescriptor();
196             ejbJar.setAssemblyDescriptor(assemblyDescriptor);
197         }
198         ContainerTransaction containerTransaction = assemblyDescriptor.newContainerTransaction();
199         containerTransaction.setTransAttribute("Required"); //NOI18N;
200
org.netbeans.modules.j2ee.dd.api.ejb.Method method = containerTransaction.newMethod();
201         method.setEjbName(ejbName);
202         method.setMethodName("*"); //NOI18N;
203
containerTransaction.addMethod(method);
204         assemblyDescriptor.addContainerTransaction(containerTransaction);
205         ejbJar.write(ejbModule.getDeploymentDescriptor());
206     }
207
208 }
209
Popular Tags