KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > rename > PersistenceXmlRenameRefactoring


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.rename;
21
22 import java.text.MessageFormat JavaDoc;
23 import javax.jmi.reflect.RefObject;
24 import org.netbeans.jmi.javamodel.JavaClass;
25 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
26 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil;
27 import org.netbeans.modules.j2ee.persistence.unit.PUDataObject;
28 import org.netbeans.modules.j2ee.refactoring.PersistenceRefactoringUtil;
29 import org.netbeans.modules.j2ee.refactoring.Utility;
30 import org.netbeans.modules.javacore.internalapi.ExternalChange;
31 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
32 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
33 import org.netbeans.modules.refactoring.api.Problem;
34 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
35 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
36 import org.openide.filesystems.FileObject;
37 import org.openide.util.NbBundle;
38
39 /**
40  * This class handles renaming of managed classes listed in persistence.xml.
41  *
42  * @author Erno Mononen
43  */

44 public class PersistenceXmlRenameRefactoring {
45     
46     /** Creates a new instance of PersistenceXmlRenameRefactoring */
47     public PersistenceXmlRenameRefactoring() {
48     }
49     
50     /**
51      * Finds usages in persistence.xml.
52      */

53     public Problem prepare(AbstractRefactoring refactoring, RefObject refObject,
54             String JavaDoc newName, RefactoringElementsBag refactoringElements) {
55         
56         Problem problem = null;
57         if (!(refObject instanceof JavaClass)) {
58             return null;
59         }
60         
61         JavaClass jClass = (JavaClass)refObject;
62         newName = Utility.renameClass(jClass.getName(), newName);
63         
64         for (FileObject ddFile : PersistenceRefactoringUtil.getPersistence(jClass)) {
65             
66             PUDataObject dataObject = ProviderUtil.getPUDataObject(ddFile);
67             
68             if (!ProviderUtil.isValid(dataObject)){
69                 Problem newProblem =
70                         new Problem(false, NbBundle.getMessage(PersistenceXmlRenameRefactoring.class, "TXT_PersistenceXmlInvalidProblem", ddFile.getPath()));
71                 problem = Utility.addProblemsToEnd(problem, newProblem);
72                 continue;
73             }
74
75             PersistenceUnit[] persistenceUnits = ProviderUtil.getPersistenceUnits(dataObject);
76             
77             
78             for (int i = 0; i < persistenceUnits.length; i++) {
79                 PersistenceUnit unit = persistenceUnits[i];
80                 
81                 for (int j = 0; j < unit.getClass2().length; j++) {
82                     
83                     if (unit.getClass2()[j].equals(jClass.getName())){
84                         
85                         RefactoringElementImplementation refactoringElem =
86                                 new PersistenceXmlClassRenameRefactoringElement(unit, jClass.getName(), newName, dataObject, ddFile);
87                         
88                         refactoringElements.add(refactoring, refactoringElem);
89                     }
90                     
91                 }
92             }
93         }
94         
95         return problem;
96     }
97     
98     /**
99      * Rename element for persistence.xml
100      */

101     private static class PersistenceXmlClassRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange {
102         
103         private PersistenceUnit persistenceUnit;
104         private PUDataObject puDataObject;
105         
106         /** Creates a new instance of WebXmlServletRenameRefactoringElement */
107         public PersistenceXmlClassRenameRefactoringElement(PersistenceUnit persistenceUnit,
108                 String JavaDoc oldName, String JavaDoc newName, PUDataObject puDataObject, FileObject parentFile) {
109             this.oldName = oldName;
110             this.newName = newName;
111             this.persistenceUnit = persistenceUnit;
112             this.puDataObject = puDataObject;
113             this.parentFile = parentFile;
114         }
115         
116         /**
117          * Returns text describing the refactoring formatted for display (using HTML tags).
118          * @return Formatted text.
119          */

120         public String JavaDoc getDisplayText() {
121             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
122             return MessageFormat.format(NbBundle.getMessage(PersistenceXmlRenameRefactoring.class, "TXT_PersistenceXmlRename"), args);
123         }
124         
125         public void performExternalChange() {
126             ProviderUtil.renameManagedClass(persistenceUnit, newName, oldName, puDataObject);
127         }
128         
129         public void undoExternalChange() {
130             ProviderUtil.renameManagedClass(persistenceUnit, oldName, newName, puDataObject);
131         }
132         
133         /** Performs the change represented by this refactoring element.
134          */

135         public void performChange() {
136             JavaMetamodel.getManager().registerExtChange(this);
137         }
138
139     }
140 }
141
Popular Tags