KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > safedelete > PersistenceXmlSafeDeleteRefactoring


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.safedelete;
21
22 import java.text.MessageFormat JavaDoc;
23 import org.netbeans.jmi.javamodel.Element;
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.DefaultPositionBoundsResolver;
29 import org.netbeans.modules.j2ee.refactoring.J2EERefactoring;
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.Problem;
35 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
36 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
37 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
38 import org.openide.ErrorManager;
39 import org.openide.filesystems.FileObject;
40 import org.openide.loaders.DataObject;
41 import org.openide.loaders.DataObjectNotFoundException;
42 import org.openide.text.PositionBounds;
43 import org.openide.util.NbBundle;
44
45 /**
46  * Safe delelete refactoring for entity classes listed
47  * in persistence.xml.
48  *
49  * @author Erno Mononen
50  */

51 public class PersistenceXmlSafeDeleteRefactoring implements J2EERefactoring {
52     
53     private Element element;
54     private SafeDeleteRefactoring safeDeleteRefactoring;
55     
56     /**
57      * Creates a new instance of PersistenceXmlSafeDeleteRefactoring
58      */

59     public PersistenceXmlSafeDeleteRefactoring(Element element, SafeDeleteRefactoring safeDeleteRefactoring) {
60         this.element = element;
61         this.safeDeleteRefactoring = safeDeleteRefactoring;
62     }
63     
64     public Problem preCheck() {
65         return null;
66     }
67     
68     public Problem checkParameters() {
69         return null;
70     }
71     
72     public Problem fastCheckParameters() {
73         return null;
74     }
75     
76     public Problem prepare(RefactoringElementsBag refactoringElementsBag) {
77         
78         Problem problem = null;
79         
80         for (Object JavaDoc elem : element.getResource().getClassifiers()) {
81             JavaClass jClass = (JavaClass) elem;
82             
83             for (FileObject ddFile : PersistenceRefactoringUtil.getPersistence(jClass)) {
84                 PUDataObject dataObject = ProviderUtil.getPUDataObject(ddFile);
85                 
86                 if (!ProviderUtil.isValid(dataObject)){
87                     Problem newProblem =
88                             new Problem(false, NbBundle.getMessage(PersistenceXmlSafeDeleteRefactoring.class, "TXT_PersistenceXmlInvalidProblem", ddFile.getPath()));
89                     problem = Utility.addProblemsToEnd(problem, newProblem);
90                     continue;
91                 }
92                 
93                 
94                 PersistenceUnit[] persistenceUnits = ProviderUtil.getPersistenceUnits(dataObject);
95                 
96                 for (int i = 0; i < persistenceUnits.length; i++) {
97                     PersistenceUnit unit = persistenceUnits[i];
98                     
99                     for (int j = 0; j < unit.getClass2().length; j++) {
100                         
101                         if (unit.getClass2()[j].equals(jClass.getName())){
102                             
103                             RefactoringElementImplementation refactoringElem =
104                                     new PersistenceXmlSafeDeleteRefactoringElement(unit, jClass.getName(), dataObject, ddFile);
105                             
106                             refactoringElementsBag.add(safeDeleteRefactoring, refactoringElem);
107                         }
108                         
109                     }
110                 }
111                 
112             }
113         }
114         return problem;
115     }
116     
117     /**
118      * Safely delete element for persistence.xml
119      */

120     private static class PersistenceXmlSafeDeleteRefactoringElement extends AbstractSafeDeleteRefactoringElement implements ExternalChange {
121         
122         private PersistenceUnit persistenceUnit;
123         private PUDataObject puDataObject;
124         private String JavaDoc clazz;
125         
126         /** Creates a new instance of PersistenceXmlSafeDeleteRefactoringElement */
127         public PersistenceXmlSafeDeleteRefactoringElement(PersistenceUnit persistenceUnit, String JavaDoc clazz, PUDataObject puDataObject, FileObject parentFile) {
128             this.parentFile = parentFile;
129             this.clazz = clazz;
130             this.persistenceUnit = persistenceUnit;
131             this.puDataObject = puDataObject;
132         }
133         
134         /**
135          * Returns text describing the refactoring formatted for display (using HTML tags).
136          * @return Formatted text.
137          */

138         public String JavaDoc getDisplayText() {
139             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), clazz};
140             return MessageFormat.format(NbBundle.getMessage(PersistenceXmlSafeDeleteRefactoringElement.class, "TXT_PersistenceXmlSafeDeleteClass"), clazz);
141         }
142         
143         public void performExternalChange() {
144             ProviderUtil.removeManagedClass(persistenceUnit, clazz, puDataObject);
145         }
146         
147         public void undoExternalChange() {
148             ProviderUtil.addManagedClass(persistenceUnit, clazz, puDataObject);
149         }
150         
151         /** Performs the change represented by this refactoring element.
152          */

153         public void performChange() {
154             JavaMetamodel.getManager().registerExtChange(this);
155         }
156         
157         public PositionBounds getPosition() {
158             try {
159                 return new DefaultPositionBoundsResolver(DataObject.find(parentFile), clazz).getPositionBounds();
160             } catch (DataObjectNotFoundException ex) {
161                 ErrorManager.getDefault().notify(ex);
162             }
163             return null;
164         }
165         
166         
167     }
168     
169     
170 }
171
Popular Tags