KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > ejb > action > AbstractAddMethodStrategy


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.ui.logicalview.ejb.action;
21
22 import java.io.IOException JavaDoc;
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 /**
39  * Strategy for visual support for adding various methods into an EJB.
40  *
41  * @author Pavel Buzek
42  * @author Martin Adamek
43  */

44 public abstract class AbstractAddMethodStrategy {
45     
46     private final String JavaDoc name;
47     
48     public AbstractAddMethodStrategy(String JavaDoc name) {
49         this.name = name;
50     }
51     
52     protected abstract MethodModel getPrototypeMethod();
53     
54     /** Describes method type handled by this action. */
55     public abstract MethodType.Kind getPrototypeMethodKind();
56     
57     protected abstract MethodCustomizer createDialog(FileObject fileObject, MethodModel methodModel) throws IOException JavaDoc;
58
59     protected abstract void generateMethod(EntityAndSession entityAndSession, MethodModel method, boolean isOneReturn,
60             boolean publishToLocal, boolean publishToRemote, String JavaDoc ejbql, FileObject ejbClassFO, String JavaDoc className) throws IOException JavaDoc;
61     
62     public abstract boolean supportsEjb(FileObject fileObject, String JavaDoc className);
63
64     public String JavaDoc getTitle() {
65         return name;
66     }
67     
68     public void addMethod(FileObject fileObject, String JavaDoc className) throws IOException JavaDoc {
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 JavaDoc ejbql = methodCustomizer.getEjbQL();
81                 EntityAndSession entityAndSession = getEntityAndSession(fileObject, className);
82                 generateMethod(entityAndSession, method, isOneReturn, publishToLocal, publishToRemote, ejbql, fileObject, className);
83             } catch (IOException JavaDoc 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 JavaDoc className) throws IOException JavaDoc {
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