1 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 ; 36 import java.util.LinkedList ; 37 38 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 ejbName = ejb.getEjbName(); 57 if (isEqualClass(ejb.getEjbClass(), javaClass)) { 58 return new Problem(false, NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class, 59 "TXT_EjbJarBeanClassSafeDelete", javaClass.getName(), ejbName)); } 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)); } else if (javaClass.equals(local)) { 74 return new Problem(false, NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class, 75 "TXT_EjbJarInterfaceClassSafeDelete", javaClass.getName(), ejbName)); } else if (javaClass.equals(home)) { 79 return new Problem(false, NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class, 80 "TXT_EjbJarInterfaceClassSafeDelete", javaClass.getName(), ejbName)); } else if (javaClass.equals(remote)) { 84 return new Problem(false, NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class, 85 "TXT_EjbJarInterfaceClassSafeDelete", javaClass.getName(), ejbName)); } 89 if (removeLocal) { 90 addChildRefactoring(getBusinessInterface(entityAndSession.getLocal())); 91 String 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 msg = NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class, 97 "TXT_EjbJarRemoteInterfacesSafeDelete", ejbName); return new Problem(false, msg); 99 } 100 } 101 return null; 102 } 103 104 private JavaClass getBusinessInterface(String 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 List compInterfaces = new LinkedList (compInterface.getInterfaces()); 112 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 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 homeInterface; 141 private String 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 156 public String getDisplayText() { 157 final String resName = local ? "TXT_EjbJarLocalInterfacesSafeDeleteDisplayText" : 158 "TXT_EjbJarRemoteInterfacesSafeDeleteDisplayText"; return NbBundle.getMessage(EjbClassSafeDeleteRefactoring.class, resName); 160 } 161 162 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 |