1 19 20 package org.netbeans.modules.j2ee.refactoring; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import java.util.Set ; 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 46 public class PersistenceRefactoringUtil { 47 48 private PersistenceRefactoringUtil() { 49 } 50 51 55 public static List <FileObject> getPersistence(JavaClass javaClass){ 56 57 List <FileObject> result = new ArrayList <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 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 83 public static List <JavaClass> getEntitiesInProject(JavaClass javaClass){ 84 List <JavaClass> result = new ArrayList <JavaClass>(); 85 Project project = getProject(javaClass); 86 87 Set <Entity> entities = null; 88 try { 89 entities = PersistenceUtils.getEntityClasses(project); 90 } catch (IOException 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 112 public static boolean isEntity(AnnotableElement element){ 113 114 if (element == null || element.getAnnotations() == null){ 115 return false; 116 } 117 118 String 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 |