KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > action > FinderMethodGenerator


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.action;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.lang.model.element.Modifier;
27 import org.netbeans.modules.j2ee.common.method.MethodModel;
28 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
29 import org.netbeans.modules.j2ee.dd.api.ejb.MethodParams;
30 import org.netbeans.modules.j2ee.dd.api.ejb.Query;
31 import org.netbeans.modules.j2ee.dd.api.ejb.QueryMethod;
32 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil;
33 import org.openide.filesystems.FileObject;
34
35 /**
36  *
37  * @author Martin Adamek
38  */

39 public class FinderMethodGenerator extends AbstractMethodGenerator {
40     
41     private FinderMethodGenerator(Entity ejb, FileObject ejbClassFileObject, FileObject ddFileObject) {
42         super(ejb, ejbClassFileObject, ddFileObject);
43     }
44     
45     public static FinderMethodGenerator create(Entity ejb, FileObject ejbClassFileObject, FileObject ddFileObject) {
46         return new FinderMethodGenerator(ejb, ejbClassFileObject, ddFileObject);
47     }
48     
49     public void generate(MethodModel methodModel, boolean generateLocal, boolean generateRemote,
50             boolean isOneReturn, String JavaDoc ejbql) throws IOException JavaDoc {
51         Entity entity = (Entity) ejb;
52         if (Entity.PERSISTENCE_TYPE_CONTAINER.equals(entity.getPersistenceType())) {
53             generateCmp(methodModel, generateLocal, generateRemote, isOneReturn, ejbql);
54         } else if (Entity.PERSISTENCE_TYPE_BEAN.equals(entity.getPersistenceType())) {
55             generateBmp(methodModel, generateLocal, generateRemote, isOneReturn, ejbql);
56         }
57     }
58     
59     private void generateCmp(MethodModel methodModel, boolean generateLocal, boolean generateRemote,
60             boolean isOneReturn, String JavaDoc ejbql) throws IOException JavaDoc {
61         
62         if (!methodModel.getName().startsWith("find")) {
63             throw new IllegalArgumentException JavaDoc("The finder method name must have find as its prefix.");
64         }
65         
66         // local interface EJB 2.1 spec 10.6.12
67
if (generateLocal && ejb.getLocal() != null && ejb.getLocalHome() != null) {
68             List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
69             if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) {
70                 exceptions.add("javax.ejb.FinderException");
71             }
72             MethodModel methodModelCopy = MethodModel.create(
73                     methodModel.getName(),
74                     isOneReturn ? ejb.getLocal() : "java.util.Collection",
75                     null,
76                     methodModel.getParameters(),
77                     exceptions,
78                     Collections.<Modifier>emptySet()
79                     );
80             FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getLocalHome());
81             addMethod(methodModelCopy, fileObject, ejb.getLocalHome());
82             
83         }
84         
85         // remote interface
86
if (generateRemote && ejb.getRemote() != null && ejb.getHome() != null) {
87             List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
88             if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) {
89                 exceptions.add("javax.ejb.FinderException");
90             }
91             if (!methodModel.getExceptions().contains("java.rmi.RemoteException")) {
92                 exceptions.add("java.rmi.RemoteException");
93             }
94             MethodModel methodModelCopy = MethodModel.create(
95                     methodModel.getName(),
96                     isOneReturn ? ejb.getRemote() : "java.util.Collection",
97                     null,
98                     methodModel.getParameters(),
99                     exceptions,
100                     Collections.<Modifier>emptySet()
101                     );
102             FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getHome());
103             addMethod(methodModelCopy, fileObject, ejb.getHome());
104         }
105         
106         // write query to deplyment descriptor
107
addQueryToXml(methodModel, ejbql);
108         
109     }
110     
111     private void generateBmp(MethodModel methodModel, boolean generateLocal, boolean generateRemote,
112             boolean isOneReturn, String JavaDoc ejbql) throws IOException JavaDoc {
113         
114         if (!methodModel.getName().startsWith("find")) {
115             throw new IllegalArgumentException JavaDoc("The finder method name must have find as its prefix.");
116         }
117         
118         // local interface EJB 2.1 spec 10.6.12
119
if (generateLocal && ejb.getLocal() != null && ejb.getLocalHome() != null) {
120             List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
121             if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) {
122                 exceptions.add("javax.ejb.FinderException");
123             }
124             // find method in LocalHome interface
125
MethodModel methodModelCopy = MethodModel.create(
126                     methodModel.getName(),
127                     isOneReturn ? ejb.getLocal() : "java.util.Collection",
128                     null,
129                     methodModel.getParameters(),
130                     exceptions,
131                     Collections.<Modifier>emptySet()
132                     );
133             FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getLocalHome());
134             addMethod(methodModelCopy, fileObject, ejb.getLocalHome());
135         }
136         
137         // remote interface
138
if (generateRemote && ejb.getRemote() != null && ejb.getHome() != null) {
139             List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
140             if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) {
141                 exceptions.add("javax.ejb.FinderException");
142             }
143             if (!methodModel.getExceptions().contains("java.rmi.RemoteException")) {
144                 exceptions.add("java.rmi.RemoteException");
145             }
146             // find method in RemoteHome interface
147
MethodModel methodModelCopy = MethodModel.create(
148                     'c' + methodModel.getName().substring(4),
149                     isOneReturn ? ejb.getRemote() : "java.util.Collection",
150                     null,
151                     methodModel.getParameters(),
152                     exceptions,
153                     Collections.<Modifier>emptySet()
154                     );
155             FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getHome());
156             addMethod(methodModelCopy, fileObject, ejb.getHome());
157             
158             // ejbFind method in ejb class
159
Entity entity = (Entity) ejb;
160             methodModelCopy = MethodModel.create(
161                     "ejbF" + methodModelCopy.getName().substring(1),
162                     isOneReturn ? entity.getPrimKeyClass() : "java.util.Collection",
163                     methodModelCopy.getBody(),
164                     methodModelCopy.getParameters(),
165                     methodModelCopy.getExceptions(),
166                     Collections.singleton(Modifier.PUBLIC)
167                     );
168             addMethod(methodModelCopy, ejbClassFileObject, ejb.getEjbClass());
169             
170         }
171         
172         // ejbFind method in ejb class
173
Entity entity = (Entity) ejb;
174         List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
175         if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) {
176             exceptions.add("javax.ejb.FinderException");
177         }
178         MethodModel methodModelCopy = MethodModel.create(
179                 "ejbF" + methodModel.getName().substring(1),
180                 isOneReturn ? entity.getPrimKeyClass() : "java.util.Collection",
181                 "return null;",
182                 methodModel.getParameters(),
183                 exceptions,
184                 Collections.singleton(Modifier.PUBLIC)
185                 );
186         addMethod(methodModelCopy, ejbClassFileObject, ejb.getEjbClass());
187         
188     }
189     
190     private void addQueryToXml(MethodModel methodModel, String JavaDoc ejbql) throws IOException JavaDoc {
191         Entity entity = (Entity) ejb;
192         Query query = entity.newQuery();
193         QueryMethod queryMethod = query.newQueryMethod();
194         queryMethod.setMethodName(methodModel.getName());
195         MethodParams methodParams = queryMethod.newMethodParams();
196         for (MethodModel.Variable parameter : methodModel.getParameters()) {
197             methodParams.addMethodParam(parameter.getType());
198         }
199         queryMethod.setMethodParams(methodParams);
200         query.setQueryMethod(queryMethod);
201         query.setEjbQl(ejbql);
202         entity.addQuery(query);
203         saveXml();
204     }
205     
206 }
207
Popular Tags