1 19 20 package org.netbeans.modules.j2ee.ejbcore.api.codegeneration; 21 22 import org.netbeans.modules.j2ee.ejbcore.EjbGenerationUtil; 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 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 44 public class EntityGenerator { 45 46 private static final String BMP_EJBCLASS = "Templates/J2EE/EJB21/BmpEjbClass.java"; private static final String BMP_LOCAL = "Templates/J2EE/EJB21/BmpLocal.java"; private static final String BMP_LOCALHOME = "Templates/J2EE/EJB21/BmpLocalHome.java"; private static final String BMP_REMOTE = "Templates/J2EE/EJB21/BmpRemote.java"; private static final String BMP_REMOTEHOME = "Templates/J2EE/EJB21/BmpRemoteHome.java"; 52 private static final String CMP_EJBCLASS = "Templates/J2EE/EJB21/CmpEjbClass.java"; private static final String CMP_LOCAL = "Templates/J2EE/EJB21/CmpLocal.java"; private static final String CMP_LOCALHOME = "Templates/J2EE/EJB21/CmpLocalHome.java"; private static final String CMP_REMOTE = "Templates/J2EE/EJB21/CmpRemote.java"; private static final String CMP_REMOTEHOME = "Templates/J2EE/EJB21/CmpRemoteHome.java"; 58 private final FileObject pkg; 60 private final boolean hasRemote; 61 private final boolean hasLocal; 62 private final boolean isCMP; 63 private final String primaryKeyClassName; 64 private final String wizardTargetName; 65 66 private final EJBNameOptions ejbNameOptions; 68 private final String ejbName; 69 private final String ejbClassName; 70 private final String remoteName; 71 private final String remoteHomeName; 72 private final String localName; 73 private final String localHomeName; 74 private final String displayName; 75 76 private final String packageName; 77 private final String packageNameWithDot; 78 79 private final Map <String , String > templateParameters; 80 81 public static EntityGenerator create(String wizardTargetName, FileObject pkg, boolean hasRemote, boolean hasLocal, 82 boolean isCMP, String primaryKeyClassName) { 83 return new EntityGenerator(wizardTargetName, pkg, hasRemote, hasLocal, isCMP, primaryKeyClassName); 84 } 85 86 private EntityGenerator(String wizardTargetName, FileObject pkg, boolean hasRemote, boolean hasLocal, 87 boolean isCMP, String 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 <String , String >(); 105 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 { 113 FileObject resultFileObject = null; 114 if (isCMP) { 115 resultFileObject = generateCmpClasses(); 116 } else { 117 resultFileObject = generateBmpClasses(); 118 } 119 120 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 { 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 { 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 { 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 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"); org.netbeans.modules.j2ee.dd.api.ejb.Method method = containerTransaction.newMethod(); 201 method.setEjbName(ejbName); 202 method.setMethodName("*"); containerTransaction.addMethod(method); 204 assemblyDescriptor.addContainerTransaction(containerTransaction); 205 ejbJar.write(ejbModule.getDeploymentDescriptor()); 206 } 207 208 } 209 | Popular Tags |