KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > EntityAndSessionHelper


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.ddloaders.multiview;
21
22 import org.netbeans.modules.j2ee.dd.api.ejb.EntityAndSession;
23 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.AbstractMethodController;
24 import org.openide.filesystems.FileObject;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import org.openide.nodes.Node;
32
33 /**
34  * @author pfiala
35  */

36 public abstract class EntityAndSessionHelper implements PropertyChangeListener JavaDoc, PropertyChangeSource {
37     
38     protected final EntityAndSession ejb;
39     protected final EjbJarMultiViewDataObject ejbJarMultiViewDataObject;
40     protected final FileObject ejbJarFile;
41     
42 // protected ClassPath sourceClassPath;
43

44     private List JavaDoc listeners = new LinkedList JavaDoc();
45     public AbstractMethodController abstractMethodController;
46     
47     public EntityAndSessionHelper(EjbJarMultiViewDataObject ejbJarMultiViewDataObject, EntityAndSession ejb) {
48         this.ejb = ejb;
49         this.ejbJarMultiViewDataObject = ejbJarMultiViewDataObject;
50         this.ejbJarFile = ejbJarMultiViewDataObject.getPrimaryFile();
51 // sourceClassPath = Utils.getSourceClassPath(ejbJarFile);
52
ejbJarMultiViewDataObject.getEjbJar().addPropertyChangeListener(this);
53     }
54     
55 // public JavaClass getLocalBusinessInterfaceClass() {
56
// return abstractMethodController.getBeanInterface(true, true);
57
// }
58

59 // public JavaClass getRemoteBusinessInterfaceClass() {
60
// return abstractMethodController.getBeanInterface(false, true);
61
// }
62

63     public void removeInterfaces(boolean local) {
64 // JMIUtils.beginJmiTransaction(true);
65
// boolean rollback = true;
66
// try {
67
// if (local) {
68
// Utils.removeClass(sourceClassPath, ejb.getLocalHome());
69
// removeBeanInterface(ejb.getLocal());
70
// JavaClass businessInterfaceClass = getLocalBusinessInterfaceClass();
71
// if (businessInterfaceClass != null) {
72
// removeBeanInterface(businessInterfaceClass.getName());
73
// }
74
// } else {
75
// Utils.removeClass(sourceClassPath, ejb.getHome());
76
// removeBeanInterface(ejb.getRemote());
77
// JavaClass businessInterfaceClass = getRemoteBusinessInterfaceClass();
78
// if (businessInterfaceClass != null) {
79
// removeBeanInterface(businessInterfaceClass.getName());
80
// }
81
// }
82
// rollback = false;
83
// } finally {
84
// JMIUtils.endJmiTransaction(rollback);
85
// }
86
// if (local) {
87
// ejb.setLocal(null);
88
// ejb.setLocalHome(null);
89
// } else {
90
// ejb.setRemote(null);
91
// ejb.setHome(null);
92
// }
93
// modelUpdatedFromUI();
94
}
95     
96     public void modelUpdatedFromUI() {
97         ejbJarMultiViewDataObject.modelUpdatedFromUI();
98     }
99     
100     private void removeBeanInterface(String JavaDoc name) {
101 // JMIUtils.removeInterface(getBeanClass(), name);
102
// Utils.removeClass(sourceClassPath, name);
103
}
104     
105     public String JavaDoc getEjbClass() {
106         return ejb.getEjbClass();
107     }
108     
109     public String JavaDoc getLocal() {
110         return ejb.getLocal();
111     }
112     
113     public String JavaDoc getLocalHome() {
114         return ejb.getLocalHome();
115     }
116     
117     public String JavaDoc getRemote() {
118         return ejb.getRemote();
119     }
120     
121     public String JavaDoc getHome() {
122         return ejb.getHome();
123     }
124     
125 // public String getBusinessInterfaceName(boolean local) {
126
// JavaClass beanInterface = abstractMethodController.getBeanInterface(local, true);
127
// if (beanInterface == null) {
128
// return null;
129
// }
130
// String name = beanInterface.getName();
131
// String componentInterfaceName = local ? ejb.getLocal() : ejb.getRemote();
132
// if (componentInterfaceName == null || componentInterfaceName.equals(name)) {
133
// return null;
134
// } else {
135
// return name;
136
// }
137
// }
138

139     public void addInterfaces(boolean local) {
140 // EntityAndSessionGenerator generator = getGenerator();
141
String JavaDoc packageName = Utils.getPackage(ejb.getEjbClass());
142 // FileObject packageFile = Utils.getPackageFile(sourceClassPath, packageName);
143
// String ejbName = ejb.getEjbName();
144
// try {
145
// JMIUtils.beginJmiTransaction(true);
146
// boolean rollback = true;
147
// String componentInterfaceName;
148
// String homeInterfaceName;
149
// String oppositeComponentInterfaceName;
150
// String oppositeHomeInterfaceName;
151
// try {
152
// if (local) {
153
// componentInterfaceName = EjbGenerationUtil.getLocalName(packageName, ejbName);
154
// homeInterfaceName = EjbGenerationUtil.getLocalHomeName(packageName, ejbName);
155
// oppositeComponentInterfaceName = ejb.getRemote();
156
// oppositeHomeInterfaceName = ejb.getHome();
157
// componentInterfaceName = generator.generateLocal(packageName, packageFile, componentInterfaceName, ejbName);
158
// if (oppositeComponentInterfaceName != null && oppositeHomeInterfaceName != null) {
159
// generateHomeInterface(oppositeHomeInterfaceName, homeInterfaceName, oppositeComponentInterfaceName, componentInterfaceName, false, packageFile);
160
// } else {
161
// homeInterfaceName = generator.generateLocalHome(packageName, packageFile, homeInterfaceName, componentInterfaceName, ejbName);
162
// String businessInterfaceName = EjbGenerationUtil.getLocalBusinessInterfaceName(packageName, ejbName);
163
// generator.generateBusinessInterfaces(packageName, packageFile, businessInterfaceName, ejbName, ejb.getEjbClass(), componentInterfaceName);
164
// }
165
// } else {
166
// componentInterfaceName = EjbGenerationUtil.getRemoteName(packageName, ejbName);
167
// homeInterfaceName = EjbGenerationUtil.getHomeName(packageName, ejbName);
168
// oppositeComponentInterfaceName = ejb.getLocal();
169
// oppositeHomeInterfaceName = ejb.getLocalHome();
170
// componentInterfaceName = generator.generateRemote(packageName, packageFile, componentInterfaceName, ejbName);
171
// if (oppositeComponentInterfaceName != null && oppositeHomeInterfaceName != null) {
172
// generateHomeInterface(oppositeHomeInterfaceName, homeInterfaceName, oppositeComponentInterfaceName, componentInterfaceName, true, packageFile);
173
// } else {
174
// homeInterfaceName = generator.generateHome(packageName, packageFile, homeInterfaceName, componentInterfaceName, ejbName);
175
// String businessInterfaceName = EjbGenerationUtil.getBusinessInterfaceName(packageName, ejbName);
176
// generator.generateBusinessInterfaces(packageName, packageFile, businessInterfaceName, ejbName, ejb.getEjbClass(), componentInterfaceName);
177
// }
178
// }
179
// if (local) {
180
// ejb.setLocal(componentInterfaceName);
181
// ejb.setLocalHome(homeInterfaceName);
182
// } else {
183
// ejb.setRemote(componentInterfaceName);
184
// ejb.setHome(homeInterfaceName);
185
// }
186
// rollback = false;
187
// } catch (JmiException jmie) {
188
// ErrorManager.getDefault().notify(jmie);
189
// } finally {
190
// JMIUtils.endJmiTransaction(rollback);
191
// }
192
// modelUpdatedFromUI();
193
// } catch (IOException e) {
194
// Utils.notifyError(e);
195
// }
196
}
197     
198     /**
199      * Generates local or remote home interface based on provided opposite existing remote or local interface.
200      * It will copy all create methods and findByPrimaryKey method from existing opposite home interface.
201      *
202      * @param oldHome name of home interface from which create and findByPrimary methods will be taken
203      * @param newHome name of home interface to create
204      * @param oldBusiness name of existing business interface (returned by create and findByPrimaryKey method of home interface)
205      * @param newBusiness name of already created new business interface (will be returned by create and findByPrimaryKey method of new home interface)
206      * @param generateRemote if new home interface is remote
207      * @param foForClasspath any file from same classpath as oldHome interface used just for resolving of class by name
208      * @throws java.io.IOException
209      */

210     private static void generateHomeInterface(String JavaDoc oldHome, String JavaDoc newHome, String JavaDoc oldBusiness, String JavaDoc newBusiness, boolean remote, FileObject foForClasspath) throws IOException JavaDoc {
211         boolean rollback = true;
212 // try {
213
// JMIUtils.beginJmiTransaction(true);
214
// JavaClass oldIfClass = JMIUtils.findClass(oldHome, foForClasspath);
215
// if (oldIfClass != null) {
216
// FileObject oldIfFO = JavaModel.getFileObject(oldIfClass.getResource());
217
// if (oldIfFO != null) {
218
// JavaClass newIfClass = JMIUtils.createClass(oldIfClass, newHome);
219
// newIfClass.setModifiers(Modifier.PUBLIC | Modifier.INTERFACE);
220
// FileObject newIfFo = oldIfFO.getParent().createData(newIfClass.getSimpleName(), oldIfFO.getExt());
221
// Resource resource = JavaModel.getResource(newIfFo);
222
// resource.setPackageName(oldIfClass.getResource().getPackageName());
223
// resource.getClassifiers().add(newIfClass);
224
// for (Iterator it = oldIfClass.getResource().getImports().iterator(); it.hasNext();) {
225
// Import imp = (Import) it.next();
226
// resource.addImport((Import) imp.duplicate());
227
// }
228
// // add EJBHome or EJBLocalHome as supertype
229
// if (remote) {
230
// newIfClass.getInterfaceNames().add(JMIUtils.createMultipartId(newIfClass, "javax.ejb.EJBHome"));
231
// newIfClass.setJavadocText("This is the remote home interface for " + newIfClass.getSimpleName() + " enterprise bean");
232
// } else {
233
// newIfClass.getInterfaceNames().add(JMIUtils.createMultipartId(newIfClass, "javax.ejb.EJBLocalHome"));
234
// newIfClass.setJavadocText("This is the local home interface for " + newIfClass.getSimpleName() + " enterprise bean");
235
// }
236
// for (Iterator it = getImportantHomeMethods(oldIfClass).iterator(); it.hasNext();) {
237
// Method originalMethod = (Method) it.next();
238
// Method method = JMIUtils.duplicate(originalMethod);
239
// method.setModifiers(0);
240
// if (oldBusiness != null && newBusiness != null) {
241
// if (method.getType().getName().indexOf(oldBusiness.substring(oldBusiness.lastIndexOf(".") + 1)) != -1) {
242
// method.setTypeName(JMIUtils.createMultipartId(method, newBusiness));
243
// }
244
// }
245
// if (remote) {
246
// JMIUtils.addException(method, RemoteException.class.getName());
247
// }
248
// newIfClass.getContents().add(method);
249
// }
250
// }
251
// }
252
// rollback = false;
253
// } catch (JmiException jmie) {
254
// ErrorManager.getDefault().notify(jmie);
255
// } finally {
256
// JMIUtils.endJmiTransaction(rollback);
257
// }
258
}
259     
260     /**
261      * Fids all create methods and findByPrimaryKey method in home interface
262      *
263      * @param homeInterface
264      * @return collection of method or empty collection if no methods found
265      */

266 // private static Collection getImportantHomeMethods(JavaClass homeInterface) {
267
// Collection result = new ArrayList();
268
// for (Iterator it = homeInterface.getContents().iterator(); it.hasNext();) {
269
// ClassMember classMember = (ClassMember) ((ClassMember) it.next()).duplicate();
270
// if (classMember instanceof Method && Modifier.isInterface(homeInterface.getModifiers())) {
271
// Method method = (Method) classMember;
272
// if (method.getName().startsWith("create") || method.getName().equals("findByPrimaryKey")) {
273
// if (!result.contains(method)){
274
// result.add(method);
275
// }
276
// }
277
// }
278
// }
279
// return result;
280
// }
281

282 // protected abstract EntityAndSessionGenerator getGenerator();
283

284 // public JavaClass getLocalHomeInterfaceClass() {
285
// return abstractMethodController.getBeanInterface(true, false);
286
//
287
// }
288

289 // public JavaClass getHomeInterfaceClass() {
290
// return abstractMethodController.getBeanInterface(false, false);
291
// }
292

293     protected Node createEntityNode() {
294 // return Utils.createEntityNode(ejbJarFile, sourceClassPath, (Entity) ejb);
295
return null;
296     }
297     
298     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
299         listeners.add(listener);
300     }
301     
302     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
303         listeners.remove(listener);
304     }
305     
306     protected void firePropertyChange(PropertyChangeEvent JavaDoc evt) {
307         for (Iterator JavaDoc iterator = listeners.iterator(); iterator.hasNext();) {
308             ((PropertyChangeListener JavaDoc) iterator.next()).propertyChange(evt);
309         }
310     }
311     
312     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
313     }
314     
315 // public JavaClass getBeanClass() {
316
// return abstractMethodController.getBeanClass();
317
// }
318
}
319
Popular Tags