KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
24 import javax.jmi.reflect.RefObject;
25 import org.netbeans.jmi.javamodel.AnnotableElement;
26 import org.netbeans.jmi.javamodel.Annotation;
27 import org.netbeans.jmi.javamodel.AnnotationType;
28 import org.netbeans.jmi.javamodel.AttributeValue;
29 import org.netbeans.jmi.javamodel.Feature;
30 import org.netbeans.jmi.javamodel.JavaClass;
31 import org.netbeans.jmi.javamodel.JavaModelPackage;
32 import org.netbeans.jmi.javamodel.Method;
33 import org.netbeans.jmi.javamodel.Resource;
34 import org.netbeans.jmi.javamodel.StringLiteral;
35 import org.netbeans.modules.j2ee.common.JMIUtils;
36 import org.netbeans.modules.j2ee.common.JMIGenerationUtil;
37 import org.netbeans.modules.j2ee.refactoring.EntityAnnotationReference;
38 import org.netbeans.modules.j2ee.refactoring.EntityAssociationResolver;
39 import org.netbeans.modules.j2ee.refactoring.PersistenceRefactoringUtil;
40 import org.netbeans.modules.j2ee.refactoring.Utility;
41 import org.netbeans.modules.javacore.api.JavaModel;
42 import org.netbeans.modules.javacore.internalapi.ExternalChange;
43 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
44 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
45 import org.netbeans.modules.refactoring.api.Problem;
46 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
47 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
48 import org.openide.filesystems.FileObject;
49 import org.openide.util.NbBundle;
50
51 /**
52  * This class provides rename refactoring support for entity annotations.
53  *
54  * @author Erno Mononen
55  */

56 public class EntityRenameRefactoring {
57     
58     /**
59      * Creates a new instance of EntityRenameRefactoring
60      */

61     public EntityRenameRefactoring() {
62     }
63     
64     public Problem prepare(AbstractRefactoring refactoring, RefObject refObject,
65             String JavaDoc newName, RefactoringElementsBag refactoringElements) {
66         
67         if (!(refObject instanceof Feature)){
68             return null;
69         }
70         
71         JMIUtils.beginJmiTransaction();
72         boolean rollback = true;
73         try{
74             Feature property = (Feature) refObject;
75             JavaClass jc = null;
76             if (property.refImmediateComposite() instanceof JavaClass){
77                 jc = (JavaClass) property.refImmediateComposite();
78             }
79             
80             if (jc != null && PersistenceRefactoringUtil.isEntity(jc)){
81                 List JavaDoc allEntities = PersistenceRefactoringUtil.getEntitiesInProject(jc);
82                 EntityAssociationResolver resolver = new EntityAssociationResolver(jc, allEntities);
83                 List JavaDoc<EntityAnnotationReference> references = resolver.getMappedByReferences(property);
84                 
85                 for (EntityAnnotationReference elem : references) {
86                     Resource res = elem.getReferring().getResource();
87                     FileObject fo = JavaModel.getFileObject(res);
88                     
89                     RefactoringElementImplementation refactoringElem =
90                             new EntityRenameRefactoringElement(elem, property.getName(), newName, fo);
91                     refactoringElements.add(refactoring, refactoringElem);
92                     
93                 }
94             }
95             rollback = false;
96         } finally {
97             JMIUtils.endJmiTransaction(rollback);
98         }
99         
100         return null;
101     }
102     
103     /**
104      * Rename element for entity annotation.
105      */

106     private static class EntityRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange {
107         
108         private EntityAnnotationReference reference;
109         /**
110          * New value for the annotation attribute value, i.e. new name of the method
111          * that's being renamed without 'get' prefix.
112          */

113         private String JavaDoc newPropertyName;
114         
115         public EntityRenameRefactoringElement(EntityAnnotationReference reference,
116                 String JavaDoc oldName, String JavaDoc newName, FileObject parentFile) {
117             this.oldName = oldName;
118             this.newName = newName;
119             this.parentFile = parentFile;
120             this.reference = reference;
121             this.newPropertyName = Utility.getPropertyName(newName);
122         }
123         
124         /**
125          * @return the old value of the annotation attribute value that is to
126          * be renamed.
127          */

128         private String JavaDoc getOldValue(){
129             JMIUtils.beginJmiTransaction();
130             boolean rollback = true;
131             String JavaDoc oldValue = null;
132             try{
133                 oldValue = ((StringLiteral) reference.getAttributeValue().getValue()).getValue();
134                 rollback = false;
135             } finally {
136                 JMIUtils.endJmiTransaction(rollback);
137             }
138             return oldValue;
139         }
140         
141         public String JavaDoc getDisplayText() {
142             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), getOldValue(), newPropertyName};
143             return MessageFormat.format(NbBundle.getMessage(EntityRenameRefactoring.class, "TXT_EntityAnnotationRename"), args);
144         }
145         
146         public void performExternalChange() {
147         }
148         
149         
150         public void undoExternalChange() {
151         }
152         
153         public void performChange() {
154             JMIUtils.beginJmiTransaction(true);
155             boolean rollback = true;
156             StringLiteral stringLiteral = null;
157             try{
158                 String JavaDoc newPropertyName = Utility.getPropertyName(newName);
159                 stringLiteral = ((JavaModelPackage) reference.getAnnotation().refImmediatePackage()).getStringLiteral().createStringLiteral(newPropertyName);
160                 reference.getAttributeValue().setValue(stringLiteral);
161                 rollback = false;
162             } finally {
163                 JMIUtils.endJmiTransaction(rollback);
164             }
165         }
166     }
167 }
168
Popular Tags