KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > safedelete > EjbClassSafeDeleteRefactoring


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.refactoring.safedelete;
21
22 import org.netbeans.modules.refactoring.api.Problem;
23 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
24 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
25 import org.netbeans.modules.j2ee.refactoring.Utility;
26 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
27 import org.netbeans.modules.j2ee.dd.api.ejb.EntityAndSession;
28 import org.netbeans.modules.javacore.internalapi.ExternalChange;
29 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
30 import org.netbeans.jmi.javamodel.JavaClass;
31 import org.netbeans.jmi.javamodel.UnresolvedClass;
32 import org.netbeans.jmi.javamodel.Element;
33 import org.openide.util.NbBundle;
34
35 import java.util.List JavaDoc;
36 import java.util.LinkedList JavaDoc;
37
38 /**
39  * @author pfiala
40  */

41 public class EjbClassSafeDeleteRefactoring extends BaseRefactoring {
42     private final Ejb ejb;
43     private JavaClass javaClass;
44     private boolean removeLocal;
45     private boolean removeRemote;
46
47     public EjbClassSafeDeleteRefactoring(EjbHelper helper, Ejb ejb, JavaClass javaClass) {
48         super(helper);
49         this.ejb = ejb;
50         this.javaClass = javaClass;
51     }
52
53     protected Problem preCheckThis() {
54         removeLocal = false;
55         removeRemote = false;
56         final String JavaDoc ejbName = ejb.getEjbName();
57         if (isEqualClass(ejb.getEjbClass(), javaClass)) {
58             return new Problem(false, NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class,
59                     "TXT_EjbJarBeanClassSafeDelete", javaClass.getName(), ejbName)); //NOI18N
60
} else if (ejb instanceof EntityAndSession) {
61             final EntityAndSession entityAndSession = ((EntityAndSession) ejb);
62             final JavaClass localHome = Utility.resolveClass(entityAndSession.getLocalHome());
63             final JavaClass local = Utility.resolveClass(entityAndSession.getLocal());
64             final JavaClass home = Utility.resolveClass(entityAndSession.getHome());
65             final JavaClass remote = Utility.resolveClass(entityAndSession.getRemote());
66             if (javaClass.equals(localHome)) {
67                 return new Problem(false, NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class,
68                         "TXT_EjbJarInterfaceClassSafeDelete", javaClass.getName(), ejbName)); //NOI18N
69
// todo: Seek how to remove multiple classes. Now it does not work properly,
70
// so we do not support safe delete of bean interfaces
71
// removeLocal = true;
72
// addChildRefactoring(local);
73
} else if (javaClass.equals(local)) {
74                 return new Problem(false, NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class,
75                         "TXT_EjbJarInterfaceClassSafeDelete", javaClass.getName(), ejbName)); //NOI18N
76
// removeLocal = true;
77
// addChildRefactoring(localHome);
78
} else if (javaClass.equals(home)) {
79                 return new Problem(false, NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class,
80                         "TXT_EjbJarInterfaceClassSafeDelete", javaClass.getName(), ejbName)); //NOI18N
81
// removeRemote = true;
82
// addChildRefactoring(remote);
83
} else if (javaClass.equals(remote)) {
84                 return new Problem(false, NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class,
85                         "TXT_EjbJarInterfaceClassSafeDelete", javaClass.getName(), ejbName)); //NOI18N
86
// removeRemote = true;
87
// addChildRefactoring(home);
88
}
89             if (removeLocal) {
90                 addChildRefactoring(getBusinessInterface(entityAndSession.getLocal()));
91                 String JavaDoc msg = NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class,
92                         "TXT_EjbJarLocalInterfacesSafeDelete", ejbName);
93                 return new Problem(false, msg);
94             } else if (removeRemote) {
95                 addChildRefactoring(getBusinessInterface(entityAndSession.getRemote()));
96                 String JavaDoc msg = NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class,
97                         "TXT_EjbJarRemoteInterfacesSafeDelete", ejbName); //NOI18N
98
return new Problem(false, msg);
99             }
100         }
101         return null;
102     }
103
104     private JavaClass getBusinessInterface(String JavaDoc compInterfaceName) {
105         JavaClass compInterface = Utility.resolveClass(compInterfaceName);
106         JavaClass beanClass = Utility.resolveClass((ejb.getEjbClass()));
107         if (compInterface == null || beanClass == null) {
108             return null;
109         }
110         // get method interfaces
111
List JavaDoc/*<JavaClass>*/ compInterfaces = new LinkedList JavaDoc(compInterface.getInterfaces());
112         // intersection with bean interfaces
113
compInterfaces.retainAll(beanClass.getInterfaces());
114         return !compInterfaces.isEmpty() ? Utility.resolveClass(((JavaClass) compInterfaces.get(0)).getName()): null;
115     }
116
117
118     protected Problem prepareThis(RefactoringElementsBag refactoringElementsBag) {
119         if (removeLocal) {
120             helper.addRefactoringElement(refactoringElementsBag, new EjbJarRefactoringElement(true));
121         } else if (removeRemote) {
122             helper.addRefactoringElement(refactoringElementsBag, new EjbJarRefactoringElement(false));
123         }
124         return null;
125     }
126
127     private void addChildRefactoring(JavaClass javaClass) {
128         if (javaClass!= null && !(javaClass instanceof UnresolvedClass)) {
129             addChildRefactoring(new SafeDeleteRefactoring(new Element[]{javaClass}));
130         }
131     }
132
133     private static boolean isEqualClass(String JavaDoc className, JavaClass javaClass) {
134         return Utility.resolveClass(className).equals(javaClass);
135     }
136
137     public final class EjbJarRefactoringElement extends AbstractSafeDeleteRefactoringElement implements ExternalChange {
138         private final boolean local;
139         private EntityAndSession entityAndSession;
140         private String JavaDoc homeInterface;
141         private String JavaDoc componentInterface;
142
143         public EjbJarRefactoringElement(boolean local) {
144             super(helper.getFileObject());
145             this.local = local;
146             entityAndSession = ((EntityAndSession) ejb);
147             homeInterface = local ? entityAndSession.getLocalHome() : entityAndSession.getHome();
148             componentInterface = local ? entityAndSession.getLocal() : entityAndSession.getRemote();
149         }
150
151         /**
152          * Returns text describing the refactoring formatted for display (using HTML tags).
153          *
154          * @return Formatted text.
155          */

156         public String JavaDoc getDisplayText() {
157             final String JavaDoc resName = local ? "TXT_EjbJarLocalInterfacesSafeDeleteDisplayText" :
158                     "TXT_EjbJarRemoteInterfacesSafeDeleteDisplayText"; //NOI18N
159
return NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class, resName);
160         }
161
162         /**
163          * Performs the change represented by this refactoring element.
164          */

165         public void performChange() {
166             JavaMetamodel.getManager().registerExtChange(this);
167         }
168
169         public void performExternalChange() {
170             if (local) {
171                 entityAndSession.setLocalHome(null);
172                 entityAndSession.setLocal(null);
173             } else {
174                 entityAndSession.setHome(null);
175                 entityAndSession.setRemote(null);
176             }
177             helper.writeDD();
178         }
179
180         public void undoExternalChange() {
181             if (local) {
182                 entityAndSession.setLocalHome(homeInterface);
183                 entityAndSession.setLocal(componentInterface);
184             } else {
185                 entityAndSession.setHome(homeInterface);
186                 entityAndSession.setRemote(componentInterface);
187             }
188             helper.writeDD();
189         }
190     }
191 }
192
Popular Tags