KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > PersistenceRefactoringUtil


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;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.api.project.FileOwnerQuery;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.jmi.javamodel.AnnotableElement;
30 import org.netbeans.jmi.javamodel.Annotation;
31 import org.netbeans.jmi.javamodel.AnnotationType;
32 import org.netbeans.jmi.javamodel.JavaClass;
33 import org.netbeans.jmi.javamodel.Resource;
34 import org.netbeans.modules.j2ee.common.JMIUtils;
35 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope;
36 import org.netbeans.modules.j2ee.persistence.dd.PersistenceUtils;
37 import org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.Entity;
38 import org.netbeans.modules.javacore.api.JavaModel;
39 import org.openide.ErrorManager;
40 import org.openide.filesystems.FileObject;
41
42 /**
43  * Utility methods for persistence related refactorings.
44  * @author Erno Mononen
45  */

46 public class PersistenceRefactoringUtil {
47     
48     private PersistenceRefactoringUtil() {
49     }
50     
51     /**
52      * @return list of FileObjects representing persistence dd files
53      * in the project of the given <code>javaClass</code>.
54      */

55     public static List JavaDoc<FileObject> getPersistence(JavaClass javaClass){
56         
57         List JavaDoc<FileObject> result = new ArrayList JavaDoc<FileObject>();
58         
59         Project project = getProject(javaClass);
60         if (project != null){
61             PersistenceScope[] persistenceScopes = PersistenceUtils.getPersistenceScopes(project);
62             for (int i = 0; i < persistenceScopes.length; i++) {
63                 result.add(persistenceScopes[i].getPersistenceXml());
64             }
65         }
66         
67         return result;
68     }
69     
70     /**
71      * @return the project of the given <code>javaClass</code>.
72      */

73     public static Project getProject(JavaClass javaClass){
74         Resource res = javaClass.getResource();
75         FileObject fo = JavaModel.getFileObject(res);
76         return FileOwnerQuery.getOwner(fo);
77     }
78     
79     /**
80      * Gets all entities from the project to which given <code>javaClass</code>
81      * belongs to.
82      */

83     public static List JavaDoc<JavaClass> getEntitiesInProject(JavaClass javaClass){
84         List JavaDoc<JavaClass> result = new ArrayList JavaDoc<JavaClass>();
85         Project project = getProject(javaClass);
86         
87         Set JavaDoc<Entity> entities = null;
88         try {
89             entities = PersistenceUtils.getEntityClasses(project);
90         } catch (IOException JavaDoc e) {
91             ErrorManager.getDefault().notify(e);
92             return Collections.emptyList();
93         }
94         
95         JMIUtils.beginJmiTransaction();
96         boolean rollback = true;
97         try{
98             for (Entity entity : entities) {
99                 JavaClass jc = (JavaClass) JavaModel.getDefaultExtent().getType().resolve(entity.getClass2());
100                 result.add(jc);
101             }
102             rollback = false;
103         } finally {
104             JMIUtils.endJmiTransaction(rollback);
105         }
106         return result;
107     }
108     
109     /**
110      * @return true if given element represents an annotated entity.
111      */

112     public static boolean isEntity(AnnotableElement element){
113         
114         if (element == null || element.getAnnotations() == null){
115             return false;
116         }
117         
118         String JavaDoc entityAnnotation = "javax.persistence.Entity";
119         Annotation annots[] = (Annotation[]) element.getAnnotations().toArray(new Annotation[]{});
120         
121         for (Annotation ann : annots) {
122             AnnotationType atype = ann.getType();
123             if (atype != null && entityAnnotation.equals(atype.getName())){
124                 return true;
125             }
126         }
127         
128         return false;
129     }
130     
131 }
132
Popular Tags