1 19 20 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.action; 21 22 import java.io.IOException ; 23 import org.netbeans.modules.j2ee.common.method.MethodCustomizer; 24 import org.netbeans.modules.j2ee.common.method.MethodModel; 25 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider; 26 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb; 27 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 28 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans; 29 import org.netbeans.modules.j2ee.dd.api.ejb.EntityAndSession; 30 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.MethodType; 31 import org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.shared.MethodsNode; 32 import org.openide.ErrorManager; 33 import org.openide.filesystems.FileObject; 34 import org.openide.nodes.Node; 35 import org.openide.util.Lookup; 36 import org.openide.util.Utilities; 37 38 44 public abstract class AbstractAddMethodStrategy { 45 46 private final String name; 47 48 public AbstractAddMethodStrategy(String name) { 49 this.name = name; 50 } 51 52 protected abstract MethodModel getPrototypeMethod(); 53 54 55 public abstract MethodType.Kind getPrototypeMethodKind(); 56 57 protected abstract MethodCustomizer createDialog(FileObject fileObject, MethodModel methodModel) throws IOException ; 58 59 protected abstract void generateMethod(EntityAndSession entityAndSession, MethodModel method, boolean isOneReturn, 60 boolean publishToLocal, boolean publishToRemote, String ejbql, FileObject ejbClassFO, String className) throws IOException ; 61 62 public abstract boolean supportsEjb(FileObject fileObject, String className); 63 64 public String getTitle() { 65 return name; 66 } 67 68 public void addMethod(FileObject fileObject, String className) throws IOException { 69 if (className == null) { 70 return; 71 } 72 MethodModel methodModel = getPrototypeMethod(); 73 MethodCustomizer methodCustomizer = createDialog(fileObject, methodModel); 74 if (methodCustomizer.customizeMethod()) { 75 try { 76 MethodModel method = methodCustomizer.getMethodModel(); 77 boolean isOneReturn = methodCustomizer.finderReturnIsSingle(); 78 boolean publishToLocal = methodCustomizer.publishToLocal(); 79 boolean publishToRemote = methodCustomizer.publishToRemote(); 80 String ejbql = methodCustomizer.getEjbQL(); 81 EntityAndSession entityAndSession = getEntityAndSession(fileObject, className); 82 generateMethod(entityAndSession, method, isOneReturn, publishToLocal, publishToRemote, ejbql, fileObject, className); 83 } catch (IOException ioe) { 84 ErrorManager.getDefault().notify(ioe); 85 } 86 } 87 } 88 89 protected FileObject getDDFile(FileObject fileObject) { 90 return org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(fileObject).getDeploymentDescriptor(); 91 } 92 93 protected static MethodsNode getMethodsNode() { 94 Node[] nodes = Utilities.actionsGlobalContext().lookup(new Lookup.Template<Node>(Node.class)).allInstances().toArray(new Node[0]); 95 if (nodes.length != 1) { 96 return null; 97 } 98 return nodes[0].getLookup().lookup(MethodsNode.class); 99 } 100 101 protected static EntityAndSession getEntityAndSession(FileObject fileObject, String className) throws IOException { 102 org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(fileObject); 103 if (ejbModule != null) { 104 EjbJar ejbJar = DDProvider.getDefault().getMergedDDRoot(ejbModule.getMetadataUnit()); 105 if (ejbJar != null) { 106 EnterpriseBeans enterpriseBeans = ejbJar.getEnterpriseBeans(); 107 if (enterpriseBeans != null) { 108 EntityAndSession entityAndSession= (EntityAndSession) enterpriseBeans.findBeanByName( 109 EnterpriseBeans.SESSION, Ejb.EJB_CLASS, className); 110 if (entityAndSession == null) { 111 entityAndSession = (EntityAndSession) enterpriseBeans.findBeanByName( 112 EnterpriseBeans.ENTITY, Ejb.EJB_CLASS, className); 113 } 114 return entityAndSession; 115 } 116 } 117 } 118 return null; 119 } 120 121 } 122 | Popular Tags |