KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jpa > verification > rules > entity > PersistenceUnitPresent


1 /*
2  * PersistenceUnitPresent.java
3  *
4  * Created on January 30, 2007, 3:17 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

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 JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.logging.Level JavaDoc;
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 /**
34  *
35  * @author Tomasz.Slota@Sun.COM
36  */

37 public class PersistenceUnitPresent extends JPAClassRule {
38     
39     /** Creates a new instance of PersistenceUnitPresent */
40     public PersistenceUnitPresent() {
41         setClassContraints(Arrays.asList(ClassConstraints.ENTITY));
42     }
43     
44     @Override JavaDoc 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                             // persistence unit found, no warning
59
return null;
60                         }
61                     }
62                 } catch (IOException JavaDoc 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 JavaDoc public Severity getSeverity(){
71         return Severity.WARNING;
72     }
73     
74     public String JavaDoc getDescription(){
75         return NbBundle.getMessage(IdDefinedInHierarchy.class, "MSG_MissingPersistenceUnitHint");
76     }
77 }
78
Popular Tags