1 19 20 package org.netbeans.modules.j2ee.refactoring.moveclass; 21 22 import java.text.MessageFormat ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import org.netbeans.jmi.javamodel.JavaClass; 26 import org.netbeans.jmi.javamodel.Resource; 27 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 28 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 29 import org.netbeans.modules.j2ee.persistence.unit.PUDataObject; 30 import org.netbeans.modules.j2ee.refactoring.PersistenceRefactoringUtil; 31 import org.netbeans.modules.j2ee.refactoring.Utility; 32 import org.netbeans.modules.javacore.internalapi.ExternalChange; 33 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 34 import org.netbeans.modules.refactoring.api.MoveClassRefactoring; 35 import org.netbeans.modules.refactoring.api.Problem; 36 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 37 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 38 import org.openide.filesystems.FileObject; 39 import org.openide.util.NbBundle; 40 41 47 public class PersistenceXmlMoveClassRefactoring { 48 49 52 public PersistenceXmlMoveClassRefactoring() { 53 } 54 55 public Problem prepare(MoveClassRefactoring moveClassRefactor, 56 Collection resources, RefactoringElementsBag refactoringElements) { 57 58 Problem problem = null; 59 60 for (Iterator it = resources.iterator(); it.hasNext();) { 61 Resource elem = (Resource) it.next(); 62 63 for (Iterator it2 = elem.getClassifiers().iterator(); it2.hasNext();) { 64 JavaClass jClass = (JavaClass) it2.next(); 65 66 String newName = moveClassRefactor.getTargetPackageName(elem) + "." + jClass.getSimpleName(); 67 for (FileObject ddFile : PersistenceRefactoringUtil.getPersistence(jClass)) { 68 PUDataObject dataObject = ProviderUtil.getPUDataObject(ddFile); 69 70 if (!ProviderUtil.isValid(dataObject)){ 71 Problem newProblem = 72 new Problem(false, NbBundle.getMessage(PersistenceXmlMoveClassRefactoring.class, "TXT_PersistenceXmlInvalidProblem", ddFile.getPath())); 73 problem = Utility.addProblemsToEnd(problem, newProblem); 74 continue; 75 } 76 77 PersistenceUnit[] persistenceUnits = ProviderUtil.getPersistenceUnits(dataObject); 78 79 for (int i = 0; i < persistenceUnits.length; i++) { 80 PersistenceUnit unit = persistenceUnits[i]; 81 82 for (int j = 0; j < unit.getClass2().length; j++) { 83 84 if (unit.getClass2()[j].equals(jClass.getName())){ 85 86 RefactoringElementImplementation refactoringElem = 87 new PersistenceXmlMoveClassRefactoringElement(unit, jClass.getName(), newName, dataObject, ddFile); 88 89 refactoringElements.add(moveClassRefactor, refactoringElem); 90 } 91 92 } 93 } 94 } 95 } 96 97 } 98 return problem; 99 } 100 101 104 private static class PersistenceXmlMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange { 105 106 private PersistenceUnit persistenceUnit; 107 private PUDataObject puDataObject; 108 109 110 public PersistenceXmlMoveClassRefactoringElement(PersistenceUnit persistenceUnit, 111 String oldName, String newName, PUDataObject puDataObject, FileObject parentFile) { 112 this.oldName = oldName; 113 this.newName = newName; 114 this.persistenceUnit = persistenceUnit; 115 this.puDataObject = puDataObject; 116 this.parentFile = parentFile; 117 } 118 119 123 public String getDisplayText() { 124 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 125 return MessageFormat.format(NbBundle.getMessage(PersistenceXmlMoveClassRefactoring.class, "TXT_PersistenceXmlMoveClass"), args); 126 } 127 128 public void performExternalChange() { 129 ProviderUtil.renameManagedClass(persistenceUnit, newName, oldName, puDataObject); 130 } 131 132 public void undoExternalChange() { 133 ProviderUtil.renameManagedClass(persistenceUnit, oldName, newName, puDataObject); 134 } 135 136 138 public void performChange() { 139 JavaMetamodel.getManager().registerExtChange(this); 140 } 141 142 } 143 144 } 145 | Popular Tags |