001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.ddloaders.multiview;
043:
044: import org.netbeans.modules.j2ee.dd.api.ejb.EntityAndSession;
045: import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.AbstractMethodController;
046: import org.openide.filesystems.FileObject;
047: import java.beans.PropertyChangeEvent;
048: import java.beans.PropertyChangeListener;
049: import java.io.IOException;
050: import java.util.Iterator;
051: import java.util.LinkedList;
052: import java.util.List;
053: import org.openide.nodes.Node;
054:
055: /**
056: * @author pfiala
057: */
058: public abstract class EntityAndSessionHelper implements
059: PropertyChangeListener, PropertyChangeSource {
060:
061: protected final EntityAndSession ejb;
062: protected final EjbJarMultiViewDataObject ejbJarMultiViewDataObject;
063: protected final FileObject ejbJarFile;
064:
065: // protected ClassPath sourceClassPath;
066:
067: private List listeners = new LinkedList();
068: public AbstractMethodController abstractMethodController;
069:
070: public EntityAndSessionHelper(
071: EjbJarMultiViewDataObject ejbJarMultiViewDataObject,
072: EntityAndSession ejb) {
073: this .ejb = ejb;
074: this .ejbJarMultiViewDataObject = ejbJarMultiViewDataObject;
075: this .ejbJarFile = ejbJarMultiViewDataObject.getPrimaryFile();
076: // sourceClassPath = Utils.getSourceClassPath(ejbJarFile);
077: ejbJarMultiViewDataObject.getEjbJar()
078: .addPropertyChangeListener(this );
079: }
080:
081: // public JavaClass getLocalBusinessInterfaceClass() {
082: // return abstractMethodController.getBeanInterface(true, true);
083: // }
084:
085: // public JavaClass getRemoteBusinessInterfaceClass() {
086: // return abstractMethodController.getBeanInterface(false, true);
087: // }
088:
089: public void removeInterfaces(boolean local) {
090: // JMIUtils.beginJmiTransaction(true);
091: // boolean rollback = true;
092: // try {
093: // if (local) {
094: // Utils.removeClass(sourceClassPath, ejb.getLocalHome());
095: // removeBeanInterface(ejb.getLocal());
096: // JavaClass businessInterfaceClass = getLocalBusinessInterfaceClass();
097: // if (businessInterfaceClass != null) {
098: // removeBeanInterface(businessInterfaceClass.getName());
099: // }
100: // } else {
101: // Utils.removeClass(sourceClassPath, ejb.getHome());
102: // removeBeanInterface(ejb.getRemote());
103: // JavaClass businessInterfaceClass = getRemoteBusinessInterfaceClass();
104: // if (businessInterfaceClass != null) {
105: // removeBeanInterface(businessInterfaceClass.getName());
106: // }
107: // }
108: // rollback = false;
109: // } finally {
110: // JMIUtils.endJmiTransaction(rollback);
111: // }
112: // if (local) {
113: // ejb.setLocal(null);
114: // ejb.setLocalHome(null);
115: // } else {
116: // ejb.setRemote(null);
117: // ejb.setHome(null);
118: // }
119: // modelUpdatedFromUI();
120: }
121:
122: public void modelUpdatedFromUI() {
123: ejbJarMultiViewDataObject.modelUpdatedFromUI();
124: }
125:
126: private void removeBeanInterface(String name) {
127: // JMIUtils.removeInterface(getBeanClass(), name);
128: // Utils.removeClass(sourceClassPath, name);
129: }
130:
131: public String getEjbClass() {
132: return ejb.getEjbClass();
133: }
134:
135: public String getLocal() {
136: return ejb.getLocal();
137: }
138:
139: public String getLocalHome() {
140: return ejb.getLocalHome();
141: }
142:
143: public String getRemote() {
144: return ejb.getRemote();
145: }
146:
147: public String getHome() {
148: return ejb.getHome();
149: }
150:
151: // public String getBusinessInterfaceName(boolean local) {
152: // JavaClass beanInterface = abstractMethodController.getBeanInterface(local, true);
153: // if (beanInterface == null) {
154: // return null;
155: // }
156: // String name = beanInterface.getName();
157: // String componentInterfaceName = local ? ejb.getLocal() : ejb.getRemote();
158: // if (componentInterfaceName == null || componentInterfaceName.equals(name)) {
159: // return null;
160: // } else {
161: // return name;
162: // }
163: // }
164:
165: public void addInterfaces(boolean local) {
166: // EntityAndSessionGenerator generator = getGenerator();
167: String packageName = Utils.getPackage(ejb.getEjbClass());
168: // FileObject packageFile = Utils.getPackageFile(sourceClassPath, packageName);
169: // String ejbName = ejb.getEjbName();
170: // try {
171: // JMIUtils.beginJmiTransaction(true);
172: // boolean rollback = true;
173: // String componentInterfaceName;
174: // String homeInterfaceName;
175: // String oppositeComponentInterfaceName;
176: // String oppositeHomeInterfaceName;
177: // try {
178: // if (local) {
179: // componentInterfaceName = EjbGenerationUtil.getLocalName(packageName, ejbName);
180: // homeInterfaceName = EjbGenerationUtil.getLocalHomeName(packageName, ejbName);
181: // oppositeComponentInterfaceName = ejb.getRemote();
182: // oppositeHomeInterfaceName = ejb.getHome();
183: // componentInterfaceName = generator.generateLocal(packageName, packageFile, componentInterfaceName, ejbName);
184: // if (oppositeComponentInterfaceName != null && oppositeHomeInterfaceName != null) {
185: // generateHomeInterface(oppositeHomeInterfaceName, homeInterfaceName, oppositeComponentInterfaceName, componentInterfaceName, false, packageFile);
186: // } else {
187: // homeInterfaceName = generator.generateLocalHome(packageName, packageFile, homeInterfaceName, componentInterfaceName, ejbName);
188: // String businessInterfaceName = EjbGenerationUtil.getLocalBusinessInterfaceName(packageName, ejbName);
189: // generator.generateBusinessInterfaces(packageName, packageFile, businessInterfaceName, ejbName, ejb.getEjbClass(), componentInterfaceName);
190: // }
191: // } else {
192: // componentInterfaceName = EjbGenerationUtil.getRemoteName(packageName, ejbName);
193: // homeInterfaceName = EjbGenerationUtil.getHomeName(packageName, ejbName);
194: // oppositeComponentInterfaceName = ejb.getLocal();
195: // oppositeHomeInterfaceName = ejb.getLocalHome();
196: // componentInterfaceName = generator.generateRemote(packageName, packageFile, componentInterfaceName, ejbName);
197: // if (oppositeComponentInterfaceName != null && oppositeHomeInterfaceName != null) {
198: // generateHomeInterface(oppositeHomeInterfaceName, homeInterfaceName, oppositeComponentInterfaceName, componentInterfaceName, true, packageFile);
199: // } else {
200: // homeInterfaceName = generator.generateHome(packageName, packageFile, homeInterfaceName, componentInterfaceName, ejbName);
201: // String businessInterfaceName = EjbGenerationUtil.getBusinessInterfaceName(packageName, ejbName);
202: // generator.generateBusinessInterfaces(packageName, packageFile, businessInterfaceName, ejbName, ejb.getEjbClass(), componentInterfaceName);
203: // }
204: // }
205: // if (local) {
206: // ejb.setLocal(componentInterfaceName);
207: // ejb.setLocalHome(homeInterfaceName);
208: // } else {
209: // ejb.setRemote(componentInterfaceName);
210: // ejb.setHome(homeInterfaceName);
211: // }
212: // rollback = false;
213: // } catch (JmiException jmie) {
214: // ErrorManager.getDefault().notify(jmie);
215: // } finally {
216: // JMIUtils.endJmiTransaction(rollback);
217: // }
218: // modelUpdatedFromUI();
219: // } catch (IOException e) {
220: // Utils.notifyError(e);
221: // }
222: }
223:
224: /**
225: * Generates local or remote home interface based on provided opposite existing remote or local interface.
226: * It will copy all create methods and findByPrimaryKey method from existing opposite home interface.
227: *
228: * @param oldHome name of home interface from which create and findByPrimary methods will be taken
229: * @param newHome name of home interface to create
230: * @param oldBusiness name of existing business interface (returned by create and findByPrimaryKey method of home interface)
231: * @param newBusiness name of already created new business interface (will be returned by create and findByPrimaryKey method of new home interface)
232: * @param generateRemote if new home interface is remote
233: * @param foForClasspath any file from same classpath as oldHome interface used just for resolving of class by name
234: * @throws java.io.IOException
235: */
236: private static void generateHomeInterface(String oldHome,
237: String newHome, String oldBusiness, String newBusiness,
238: boolean remote, FileObject foForClasspath)
239: throws IOException {
240: boolean rollback = true;
241: // try {
242: // JMIUtils.beginJmiTransaction(true);
243: // JavaClass oldIfClass = JMIUtils.findClass(oldHome, foForClasspath);
244: // if (oldIfClass != null) {
245: // FileObject oldIfFO = JavaModel.getFileObject(oldIfClass.getResource());
246: // if (oldIfFO != null) {
247: // JavaClass newIfClass = JMIUtils.createClass(oldIfClass, newHome);
248: // newIfClass.setModifiers(Modifier.PUBLIC | Modifier.INTERFACE);
249: // FileObject newIfFo = oldIfFO.getParent().createData(newIfClass.getSimpleName(), oldIfFO.getExt());
250: // Resource resource = JavaModel.getResource(newIfFo);
251: // resource.setPackageName(oldIfClass.getResource().getPackageName());
252: // resource.getClassifiers().add(newIfClass);
253: // for (Iterator it = oldIfClass.getResource().getImports().iterator(); it.hasNext();) {
254: // Import imp = (Import) it.next();
255: // resource.addImport((Import) imp.duplicate());
256: // }
257: // // add EJBHome or EJBLocalHome as supertype
258: // if (remote) {
259: // newIfClass.getInterfaceNames().add(JMIUtils.createMultipartId(newIfClass, "javax.ejb.EJBHome"));
260: // newIfClass.setJavadocText("This is the remote home interface for " + newIfClass.getSimpleName() + " enterprise bean");
261: // } else {
262: // newIfClass.getInterfaceNames().add(JMIUtils.createMultipartId(newIfClass, "javax.ejb.EJBLocalHome"));
263: // newIfClass.setJavadocText("This is the local home interface for " + newIfClass.getSimpleName() + " enterprise bean");
264: // }
265: // for (Iterator it = getImportantHomeMethods(oldIfClass).iterator(); it.hasNext();) {
266: // Method originalMethod = (Method) it.next();
267: // Method method = JMIUtils.duplicate(originalMethod);
268: // method.setModifiers(0);
269: // if (oldBusiness != null && newBusiness != null) {
270: // if (method.getType().getName().indexOf(oldBusiness.substring(oldBusiness.lastIndexOf(".") + 1)) != -1) {
271: // method.setTypeName(JMIUtils.createMultipartId(method, newBusiness));
272: // }
273: // }
274: // if (remote) {
275: // JMIUtils.addException(method, RemoteException.class.getName());
276: // }
277: // newIfClass.getContents().add(method);
278: // }
279: // }
280: // }
281: // rollback = false;
282: // } catch (JmiException jmie) {
283: // ErrorManager.getDefault().notify(jmie);
284: // } finally {
285: // JMIUtils.endJmiTransaction(rollback);
286: // }
287: }
288:
289: /**
290: * Fids all create methods and findByPrimaryKey method in home interface
291: *
292: * @param homeInterface
293: * @return collection of method or empty collection if no methods found
294: */
295: // private static Collection getImportantHomeMethods(JavaClass homeInterface) {
296: // Collection result = new ArrayList();
297: // for (Iterator it = homeInterface.getContents().iterator(); it.hasNext();) {
298: // ClassMember classMember = (ClassMember) ((ClassMember) it.next()).duplicate();
299: // if (classMember instanceof Method && Modifier.isInterface(homeInterface.getModifiers())) {
300: // Method method = (Method) classMember;
301: // if (method.getName().startsWith("create") || method.getName().equals("findByPrimaryKey")) {
302: // if (!result.contains(method)){
303: // result.add(method);
304: // }
305: // }
306: // }
307: // }
308: // return result;
309: // }
310: // protected abstract EntityAndSessionGenerator getGenerator();
311: // public JavaClass getLocalHomeInterfaceClass() {
312: // return abstractMethodController.getBeanInterface(true, false);
313: //
314: // }
315: // public JavaClass getHomeInterfaceClass() {
316: // return abstractMethodController.getBeanInterface(false, false);
317: // }
318: protected Node createEntityNode() {
319: // return Utils.createEntityNode(ejbJarFile, sourceClassPath, (Entity) ejb);
320: return null;
321: }
322:
323: public void addPropertyChangeListener(
324: PropertyChangeListener listener) {
325: listeners.add(listener);
326: }
327:
328: public void removePropertyChangeListener(
329: PropertyChangeListener listener) {
330: listeners.remove(listener);
331: }
332:
333: protected void firePropertyChange(PropertyChangeEvent evt) {
334: for (Iterator iterator = listeners.iterator(); iterator
335: .hasNext();) {
336: ((PropertyChangeListener) iterator.next())
337: .propertyChange(evt);
338: }
339: }
340:
341: public void propertyChange(PropertyChangeEvent evt) {
342: }
343:
344: // public JavaClass getBeanClass() {
345: // return abstractMethodController.getBeanClass();
346: // }
347: }
|