2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.j2ee.jpa.verification.rules.entity;
43
import java.util.ArrayList;
44
import java.util.Arrays;
45
import java.util.List;
46
import javax.lang.model.element.TypeElement;
47
import org.netbeans.modules.j2ee.jpa.model.AccessType;
48
import org.netbeans.modules.j2ee.jpa.model.AttributeWrapper;
49
import org.netbeans.modules.j2ee.jpa.model.ModelUtils;
50
import org.netbeans.modules.j2ee.jpa.verification.JPAClassRule;
51
import org.netbeans.modules.j2ee.jpa.verification.JPAEntityAttributeCheck;
52
import org.netbeans.modules.j2ee.jpa.verification.JPAProblemContext;
53
import org.netbeans.modules.j2ee.jpa.verification.JPAProblemFinder;
54
import org.netbeans.modules.j2ee.jpa.verification.common.ProblemContext;
55
import org.netbeans.modules.j2ee.jpa.verification.rules.attribute.AttrIsEncapsulated;
56
import org.netbeans.modules.j2ee.jpa.verification.rules.attribute.GeneratedValueIsId;
57
import org.netbeans.modules.j2ee.jpa.verification.rules.attribute.MVRelationshipForEntityTypeAttrDefined;
58
import org.netbeans.modules.j2ee.jpa.verification.rules.attribute.RelationshipForEntityTypeAttrDefined;
59
import org.netbeans.modules.j2ee.jpa.verification.rules.attribute.TemporalFieldsAnnotated;
60
import org.netbeans.modules.j2ee.jpa.verification.rules.attribute.ValidBasicType;
61
import org.netbeans.modules.j2ee.jpa.verification.rules.attribute.ValidColumnName;
62
import org.netbeans.modules.j2ee.jpa.verification.rules.attribute.ValidModifiers;
63
import org.netbeans.modules.j2ee.jpa.verification.rules.attribute.ValidVersionType;
64
import org.netbeans.modules.j2ee.persistence.api.metadata.orm.Basic;
65
import org.netbeans.modules.j2ee.persistence.api.metadata.orm.Embeddable;
66
import org.netbeans.modules.j2ee.persistence.api.metadata.orm.Entity;
67
import org.netbeans.modules.j2ee.persistence.api.metadata.orm.Id;
68
import org.netbeans.modules.j2ee.persistence.api.metadata.orm.MappedSuperclass;
69
import org.netbeans.modules.j2ee.persistence.api.metadata.orm.Version;
70
import org.netbeans.spi.editor.hints.ErrorDescription;
73
* Perform checks on the attributes
75
* @author Tomasz.Slota@Sun.COM
77
public class ValidAttributes extends JPAClassRule {
78
private static JPAEntityAttributeCheck[] attribChecks = new JPAEntityAttributeCheck[]{
79
new ValidColumnName(),
82
new RelationshipForEntityTypeAttrDefined(),
83
new MVRelationshipForEntityTypeAttrDefined(),
84
new AttrIsEncapsulated(),
85
new ValidVersionType(),
86
new GeneratedValueIsId(),
87
new TemporalFieldsAnnotated()
90
public ValidAttributes() {
91
setClassContraints(Arrays.asList(ClassConstraints.ENTITY,
92
ClassConstraints.EMBEDDABLE,
93
ClassConstraints.MAPPED_SUPERCLASS));
96
@Override public ErrorDescription[] apply(TypeElement subject, ProblemContext ctx){
97
List<ErrorDescription> problemsFound = new ArrayList<ErrorDescription>();
98
List <AttributeWrapper> attrs = new ArrayList<AttributeWrapper>();
99
Basic basicFields[] = null;
100
Id idFields[] = null;
101
Version versionFields[] = null;
103
if (ctx.getModelElement() instanceof Entity){
104
Entity entity = (Entity)ctx.getModelElement();
105
basicFields = entity.getAttributes().getBasic();
107
idFields = entity.getAttributes().getId();
108
versionFields = entity.getAttributes().getVersion();
110
else if (ctx.getModelElement() instanceof Embeddable){
111
Embeddable embeddable = (Embeddable)ctx.getModelElement();
112
basicFields = embeddable.getAttributes().getBasic();
114
} else if (ctx.getModelElement() instanceof MappedSuperclass){
115
MappedSuperclass mappedSuperclass = (MappedSuperclass)ctx.getModelElement();
116
basicFields = mappedSuperclass.getAttributes().getBasic();
118
idFields = mappedSuperclass.getAttributes().getId();
119
versionFields = mappedSuperclass.getAttributes().getVersion();
122
if (basicFields != null){
123
for (Basic basic: basicFields){
124
attrs.add(new AttributeWrapper(basic));
128
if (idFields != null){
129
for (Id id: idFields){
130
attrs.add(new AttributeWrapper(id));
134
if (versionFields != null){
135
for (Version version : versionFields){
136
attrs.add(new AttributeWrapper(version));
140
JPAProblemContext jpaCtx = (JPAProblemContext)ctx;
142
if (jpaCtx.getAccessType() == AccessType.INCONSISTENT){
143
JPAProblemFinder.LOG.fine("Skipping attribute checks due to inconsistent access type");
145
for (AttributeWrapper attr : attrs){
146
ModelUtils.resolveJavaElementFromModel(jpaCtx, attr);
148
if (attr.getJavaElement() == null){
149
JPAProblemFinder.LOG.severe("Failed to resolve java model element for JPA merged model element "
150
+ subject.getSimpleName() + "." + attr.getName());
152
for (JPAEntityAttributeCheck check : attribChecks){
153
ErrorDescription[] attrProblems = check.check(jpaCtx, attr);
155
if (attrProblems != null){
156
for (ErrorDescription err : attrProblems){
157
problemsFound.add(err);
165
return problemsFound.toArray(new ErrorDescription[problemsFound.size()]);