KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > moveclass > PersistenceXmlMoveClassRefactoring


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.moveclass;
21
22 import java.text.MessageFormat JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
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 /**
42  * This class handles renaming managed entity class reference
43  * in persistence.xml when it is moved to an another package.
44  *
45  * @author Erno Mononen
46  */

47 public class PersistenceXmlMoveClassRefactoring {
48     
49     /**
50      * Creates a new instance of PersistenceXmlMoveClassRefactoring
51      */

52     public PersistenceXmlMoveClassRefactoring() {
53     }
54     
55     public Problem prepare(MoveClassRefactoring moveClassRefactor,
56             Collection JavaDoc resources, RefactoringElementsBag refactoringElements) {
57         
58         Problem problem = null;
59         
60         for (Iterator JavaDoc it = resources.iterator(); it.hasNext();) {
61             Resource elem = (Resource) it.next();
62             
63             for (Iterator JavaDoc it2 = elem.getClassifiers().iterator(); it2.hasNext();) {
64                 JavaClass jClass = (JavaClass) it2.next();
65                 
66                 String JavaDoc 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     /**
102      * Move class element for persistence.xml
103      */

104     private static class PersistenceXmlMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
105         
106         private PersistenceUnit persistenceUnit;
107         private PUDataObject puDataObject;
108         
109         /** Creates a new instance of PersistenceXmlMoveClassRefactoringElement */
110         public PersistenceXmlMoveClassRefactoringElement(PersistenceUnit persistenceUnit,
111                 String JavaDoc oldName, String JavaDoc 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         /**
120          * Returns text describing the refactoring formatted for display (using HTML tags).
121          * @return Formatted text.
122          */

123         public String JavaDoc getDisplayText() {
124             Object JavaDoc[] args = new Object JavaDoc [] {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         /** Performs the change represented by this refactoring element.
137          */

138         public void performChange() {
139             JavaMetamodel.getManager().registerExtChange(this);
140         }
141         
142     }
143     
144 }
145
Popular Tags