1 19 20 package org.netbeans.modules.j2ee.refactoring.rename; 21 22 import java.text.MessageFormat ; 23 import java.util.List ; 24 import javax.jmi.reflect.RefObject; 25 import org.netbeans.jmi.javamodel.AnnotableElement; 26 import org.netbeans.jmi.javamodel.Annotation; 27 import org.netbeans.jmi.javamodel.AnnotationType; 28 import org.netbeans.jmi.javamodel.AttributeValue; 29 import org.netbeans.jmi.javamodel.Feature; 30 import org.netbeans.jmi.javamodel.JavaClass; 31 import org.netbeans.jmi.javamodel.JavaModelPackage; 32 import org.netbeans.jmi.javamodel.Method; 33 import org.netbeans.jmi.javamodel.Resource; 34 import org.netbeans.jmi.javamodel.StringLiteral; 35 import org.netbeans.modules.j2ee.common.JMIUtils; 36 import org.netbeans.modules.j2ee.common.JMIGenerationUtil; 37 import org.netbeans.modules.j2ee.refactoring.EntityAnnotationReference; 38 import org.netbeans.modules.j2ee.refactoring.EntityAssociationResolver; 39 import org.netbeans.modules.j2ee.refactoring.PersistenceRefactoringUtil; 40 import org.netbeans.modules.j2ee.refactoring.Utility; 41 import org.netbeans.modules.javacore.api.JavaModel; 42 import org.netbeans.modules.javacore.internalapi.ExternalChange; 43 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 44 import org.netbeans.modules.refactoring.api.AbstractRefactoring; 45 import org.netbeans.modules.refactoring.api.Problem; 46 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 47 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 48 import org.openide.filesystems.FileObject; 49 import org.openide.util.NbBundle; 50 51 56 public class EntityRenameRefactoring { 57 58 61 public EntityRenameRefactoring() { 62 } 63 64 public Problem prepare(AbstractRefactoring refactoring, RefObject refObject, 65 String newName, RefactoringElementsBag refactoringElements) { 66 67 if (!(refObject instanceof Feature)){ 68 return null; 69 } 70 71 JMIUtils.beginJmiTransaction(); 72 boolean rollback = true; 73 try{ 74 Feature property = (Feature) refObject; 75 JavaClass jc = null; 76 if (property.refImmediateComposite() instanceof JavaClass){ 77 jc = (JavaClass) property.refImmediateComposite(); 78 } 79 80 if (jc != null && PersistenceRefactoringUtil.isEntity(jc)){ 81 List allEntities = PersistenceRefactoringUtil.getEntitiesInProject(jc); 82 EntityAssociationResolver resolver = new EntityAssociationResolver(jc, allEntities); 83 List <EntityAnnotationReference> references = resolver.getMappedByReferences(property); 84 85 for (EntityAnnotationReference elem : references) { 86 Resource res = elem.getReferring().getResource(); 87 FileObject fo = JavaModel.getFileObject(res); 88 89 RefactoringElementImplementation refactoringElem = 90 new EntityRenameRefactoringElement(elem, property.getName(), newName, fo); 91 refactoringElements.add(refactoring, refactoringElem); 92 93 } 94 } 95 rollback = false; 96 } finally { 97 JMIUtils.endJmiTransaction(rollback); 98 } 99 100 return null; 101 } 102 103 106 private static class EntityRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 107 108 private EntityAnnotationReference reference; 109 113 private String newPropertyName; 114 115 public EntityRenameRefactoringElement(EntityAnnotationReference reference, 116 String oldName, String newName, FileObject parentFile) { 117 this.oldName = oldName; 118 this.newName = newName; 119 this.parentFile = parentFile; 120 this.reference = reference; 121 this.newPropertyName = Utility.getPropertyName(newName); 122 } 123 124 128 private String getOldValue(){ 129 JMIUtils.beginJmiTransaction(); 130 boolean rollback = true; 131 String oldValue = null; 132 try{ 133 oldValue = ((StringLiteral) reference.getAttributeValue().getValue()).getValue(); 134 rollback = false; 135 } finally { 136 JMIUtils.endJmiTransaction(rollback); 137 } 138 return oldValue; 139 } 140 141 public String getDisplayText() { 142 Object [] args = new Object [] {parentFile.getNameExt(), getOldValue(), newPropertyName}; 143 return MessageFormat.format(NbBundle.getMessage(EntityRenameRefactoring.class, "TXT_EntityAnnotationRename"), args); 144 } 145 146 public void performExternalChange() { 147 } 148 149 150 public void undoExternalChange() { 151 } 152 153 public void performChange() { 154 JMIUtils.beginJmiTransaction(true); 155 boolean rollback = true; 156 StringLiteral stringLiteral = null; 157 try{ 158 String newPropertyName = Utility.getPropertyName(newName); 159 stringLiteral = ((JavaModelPackage) reference.getAnnotation().refImmediatePackage()).getStringLiteral().createStringLiteral(newPropertyName); 160 reference.getAttributeValue().setValue(stringLiteral); 161 rollback = false; 162 } finally { 163 JMIUtils.endJmiTransaction(rollback); 164 } 165 } 166 } 167 } 168 | Popular Tags |