1 9 10 package org.netbeans.modules.j2ee.jpa.verification.rules.entity; 11 12 import org.netbeans.modules.j2ee.jpa.verification.rules.entity.IdDefinedInHierarchy; 13 import java.io.IOException ; 14 import java.util.Arrays ; 15 import java.util.logging.Level ; 16 import javax.lang.model.element.TypeElement; 17 import org.netbeans.api.project.FileOwnerQuery; 18 import org.netbeans.api.project.Project; 19 import org.netbeans.modules.j2ee.jpa.verification.JPAClassRule; 20 import org.netbeans.modules.j2ee.jpa.verification.JPAClassRule.ClassConstraints; 21 import org.netbeans.modules.j2ee.jpa.verification.JPAProblemFinder; 22 import org.netbeans.modules.j2ee.jpa.verification.common.ProblemContext; 23 import org.netbeans.modules.j2ee.jpa.verification.fixes.CreatePersistenceUnit; 24 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 25 import org.netbeans.modules.j2ee.persistence.dd.PersistenceMetadata; 26 import org.netbeans.modules.j2ee.persistence.dd.PersistenceUtils; 27 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 28 import org.netbeans.spi.editor.hints.ErrorDescription; 29 import org.netbeans.spi.editor.hints.Severity; 30 import org.openide.filesystems.FileObject; 31 import org.openide.util.NbBundle; 32 33 37 public class PersistenceUnitPresent extends JPAClassRule { 38 39 40 public PersistenceUnitPresent() { 41 setClassContraints(Arrays.asList(ClassConstraints.ENTITY)); 42 } 43 44 @Override public ErrorDescription[] apply(TypeElement subject, ProblemContext ctx){ 45 Project project = FileOwnerQuery.getOwner(ctx.getFileObject()); 46 PersistenceScope[] scopes = PersistenceUtils.getPersistenceScopes(project); 47 48 for (PersistenceScope scope : scopes){ 49 if (scope.getClassPath().contains(ctx.getFileObject())){ 50 51 try{ 52 FileObject persistenceXML = scope.getPersistenceXml(); 53 54 if (persistenceXML != null){ 55 PersistenceUnit pus[] = PersistenceMetadata.getDefault().getRoot(persistenceXML).getPersistenceUnit(); 56 57 if (pus != null && pus.length > 0){ 58 return null; 60 } 61 } 62 } catch (IOException e){ 63 JPAProblemFinder.LOG.log(Level.SEVERE, e.getMessage(), e); 64 } 65 } 66 } 67 return new ErrorDescription[]{createProblem(subject, ctx, new CreatePersistenceUnit(project))}; 68 } 69 70 @Override public Severity getSeverity(){ 71 return Severity.WARNING; 72 } 73 74 public String getDescription(){ 75 return NbBundle.getMessage(IdDefinedInHierarchy.class, "MSG_MissingPersistenceUnitHint"); 76 } 77 } 78 | Popular Tags |