1 19 20 package org.netbeans.modules.j2ee.refactoring.safedelete; 21 22 import java.text.MessageFormat ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import org.netbeans.jmi.javamodel.Annotation; 26 import org.netbeans.jmi.javamodel.AttributeValue; 27 import org.netbeans.jmi.javamodel.Feature; 28 import org.netbeans.jmi.javamodel.JavaClass; 29 import org.netbeans.jmi.javamodel.Resource; 30 import org.netbeans.jmi.javamodel.StringLiteral; 31 import org.netbeans.modules.j2ee.common.JMIUtils; 32 import org.netbeans.modules.j2ee.refactoring.EntityAnnotationReference; 33 import org.netbeans.modules.j2ee.refactoring.EntityAssociationResolver; 34 import org.netbeans.modules.j2ee.refactoring.J2EERefactoring; 35 import org.netbeans.modules.j2ee.refactoring.PersistenceRefactoringUtil; 36 import org.netbeans.modules.j2ee.refactoring.rename.AbstractRenameRefactoringElement; 37 import org.netbeans.modules.javacore.api.JavaModel; 38 import org.netbeans.modules.javacore.internalapi.ExternalChange; 39 import org.netbeans.modules.refactoring.api.Problem; 40 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring; 41 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 42 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 43 import org.openide.filesystems.FileObject; 44 import org.openide.util.NbBundle; 45 46 53 public class EntitySafeDeleteRefactoring implements J2EERefactoring{ 54 55 private JavaClass javaClass; 56 private Feature property; 57 private SafeDeleteRefactoring safeDeleteRefactoring; 58 61 public EntitySafeDeleteRefactoring(JavaClass javaClass, Feature property, 62 SafeDeleteRefactoring safeDeleteRefactoring) { 63 this.javaClass = javaClass; 64 this.property = property; 65 this.safeDeleteRefactoring = safeDeleteRefactoring; 66 } 67 68 public Problem prepare(RefactoringElementsBag refactoringElementsBag) { 69 70 if (!PersistenceRefactoringUtil.isEntity(javaClass)){ 71 return null; 72 } 73 74 JMIUtils.beginJmiTransaction(); 75 boolean rollback = true; 76 try{ 77 List allEntities = PersistenceRefactoringUtil.getEntitiesInProject(javaClass); 78 EntityAssociationResolver resolver = new EntityAssociationResolver(javaClass, allEntities); 79 List <EntityAnnotationReference> references = resolver.getMappedByReferences(property); 80 81 for (EntityAnnotationReference elem : references) { 82 Resource res = elem.getReferring().getResource(); 83 FileObject fo = JavaModel.getFileObject(res); 84 85 RefactoringElementImplementation refactoringElem = 86 new EntitySafeDeleteRefactoringElement(elem, fo); 87 refactoringElementsBag.add(safeDeleteRefactoring, refactoringElem); 88 } 89 rollback = false; 90 } finally { 91 JMIUtils.endJmiTransaction(rollback); 92 } 93 94 return null; 95 } 96 97 public Problem preCheck() { 98 return null; 99 } 100 101 public Problem checkParameters() { 102 return null; 103 } 104 105 public Problem fastCheckParameters() { 106 return null; 107 } 108 109 112 private static class EntitySafeDeleteRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 113 114 private EntityAnnotationReference reference; 115 116 public EntitySafeDeleteRefactoringElement(EntityAnnotationReference reference, FileObject parentFile) { 117 this.parentFile = parentFile; 118 this.reference = reference; 119 } 120 121 122 public String getDisplayText() { 123 Object [] args = new Object [] {reference.getAnnotation().getType().getName(), reference.getReferringProperty().getName()}; 124 return MessageFormat.format(NbBundle.getMessage(EntitySafeDeleteRefactoring.class, "TXT_EntitySafeDeleteAnnotation"), args); 125 } 126 127 public void performExternalChange() { 128 } 129 130 131 public void undoExternalChange() { 132 } 133 134 public void performChange() { 135 JMIUtils.beginJmiTransaction(true); 136 boolean rollback = true; 137 try{ 138 reference.getReferringProperty().getAnnotations().remove(reference.getAnnotation()); 140 rollback = false; 141 } finally { 142 JMIUtils.endJmiTransaction(rollback); 143 } 144 } 145 146 private String getStringValue(AttributeValue attributeValue){ 147 return ((StringLiteral) attributeValue.getValue()).getValue(); 148 } 149 } 150 } | Popular Tags |