KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.jmi.javamodel.Annotation;
26 import org.netbeans.jmi.javamodel.AttributeValue;
27 import org.netbeans.jmi.javamodel.Feature;
28 import org.netbeans.jmi.javamodel.JavaClass;
29 import org.netbeans.jmi.javamodel.Resource;
30 import org.netbeans.jmi.javamodel.StringLiteral;
31 import org.netbeans.modules.j2ee.common.JMIUtils;
32 import org.netbeans.modules.j2ee.refactoring.EntityAnnotationReference;
33 import org.netbeans.modules.j2ee.refactoring.EntityAssociationResolver;
34 import org.netbeans.modules.j2ee.refactoring.J2EERefactoring;
35 import org.netbeans.modules.j2ee.refactoring.PersistenceRefactoringUtil;
36 import org.netbeans.modules.j2ee.refactoring.rename.AbstractRenameRefactoringElement;
37 import org.netbeans.modules.javacore.api.JavaModel;
38 import org.netbeans.modules.javacore.internalapi.ExternalChange;
39 import org.netbeans.modules.refactoring.api.Problem;
40 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
41 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
42 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
43 import org.openide.filesystems.FileObject;
44 import org.openide.util.NbBundle;
45
46 /**
47  * This class provides support for safely deleting a property that
48  * is referred by 'mappedBy' annotation attribute value from
49  * other classes.
50  *
51  * @author Erno Mononen
52  */

53 public class EntitySafeDeleteRefactoring implements J2EERefactoring{
54     
55     private JavaClass javaClass;
56     private Feature property;
57     private SafeDeleteRefactoring safeDeleteRefactoring;
58     /**
59      * Creates a new instance of EntitySafeDeleteRefactoring
60      */

61     public EntitySafeDeleteRefactoring(JavaClass javaClass, Feature property,
62             SafeDeleteRefactoring safeDeleteRefactoring) {
63         this.javaClass = javaClass;
64         this.property = property;
65         this.safeDeleteRefactoring = safeDeleteRefactoring;
66     }
67     
68     public Problem prepare(RefactoringElementsBag refactoringElementsBag) {
69         
70         if (!PersistenceRefactoringUtil.isEntity(javaClass)){
71             return null;
72         }
73         
74         JMIUtils.beginJmiTransaction();
75         boolean rollback = true;
76         try{
77             List JavaDoc allEntities = PersistenceRefactoringUtil.getEntitiesInProject(javaClass);
78             EntityAssociationResolver resolver = new EntityAssociationResolver(javaClass, allEntities);
79             List JavaDoc<EntityAnnotationReference> references = resolver.getMappedByReferences(property);
80             
81             for (EntityAnnotationReference elem : references) {
82                 Resource res = elem.getReferring().getResource();
83                 FileObject fo = JavaModel.getFileObject(res);
84                 
85                 RefactoringElementImplementation refactoringElem =
86                         new EntitySafeDeleteRefactoringElement(elem, fo);
87                 refactoringElementsBag.add(safeDeleteRefactoring, refactoringElem);
88             }
89             rollback = false;
90         } finally {
91             JMIUtils.endJmiTransaction(rollback);
92         }
93         
94         return null;
95     }
96     
97     public Problem preCheck() {
98         return null;
99     }
100     
101     public Problem checkParameters() {
102         return null;
103     }
104     
105     public Problem fastCheckParameters() {
106         return null;
107     }
108     
109     /**
110      * Safe delete element for entity annotation.
111      */

112     private static class EntitySafeDeleteRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange {
113         
114         private EntityAnnotationReference reference;
115         
116         public EntitySafeDeleteRefactoringElement(EntityAnnotationReference reference, FileObject parentFile) {
117             this.parentFile = parentFile;
118             this.reference = reference;
119         }
120         
121           
122         public String JavaDoc getDisplayText() {
123             Object JavaDoc[] args = new Object JavaDoc [] {reference.getAnnotation().getType().getName(), reference.getReferringProperty().getName()};
124             return MessageFormat.format(NbBundle.getMessage(EntitySafeDeleteRefactoring.class, "TXT_EntitySafeDeleteAnnotation"), args);
125         }
126         
127         public void performExternalChange() {
128         }
129         
130         
131         public void undoExternalChange() {
132         }
133         
134         public void performChange() {
135             JMIUtils.beginJmiTransaction(true);
136             boolean rollback = true;
137             try{
138                 // removes the referencing annotation
139
reference.getReferringProperty().getAnnotations().remove(reference.getAnnotation());
140                 rollback = false;
141             } finally {
142                 JMIUtils.endJmiTransaction(rollback);
143             }
144         }
145         
146         private String JavaDoc getStringValue(AttributeValue attributeValue){
147             return ((StringLiteral) attributeValue.getValue()).getValue();
148         }
149     }
150 }
Popular Tags