KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > project > validator > ObjEntityValidator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.project.validator;
21
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.cayenne.access.DataDomain;
25 import org.apache.cayenne.map.DataMap;
26 import org.apache.cayenne.map.ObjEntity;
27 import org.apache.cayenne.project.ProjectPath;
28 import org.apache.cayenne.util.Util;
29
30 /**
31  * @author Andrus Adamchik
32  */

33 public class ObjEntityValidator extends TreeNodeValidator {
34
35     /**
36      * Constructor for ObjEntityValidator.
37      */

38     public ObjEntityValidator() {
39         super();
40     }
41
42     public void validateObject(ProjectPath path, Validator validator) {
43         ObjEntity ent = (ObjEntity) path.getObject();
44         validateName(ent, path, validator);
45         validateClassName(ent, path, validator);
46         validateSuperClassName(ent, path, validator);
47
48         // validate DbEntity presence
49
if (ent.getDbEntity() == null) {
50             validator.registerWarning("ObjEntity has no DbEntity mapping.", path);
51         }
52     }
53
54     private void validateClassName(ObjEntity ent, ProjectPath path, Validator validator) {
55         String JavaDoc className = ent.getClassName();
56
57         // if mapped to default class, ignore...
58
if (Util.isEmptyString(className)) {
59             return;
60         }
61
62         MappingNamesHelper helper = MappingNamesHelper.getInstance();
63         String JavaDoc invalidChars = helper.invalidCharsInJavaClassName(className);
64
65         if (invalidChars != null) {
66             validator.registerWarning(
67                     "ObjEntity Java class contains invalid characters: " + invalidChars,
68                     path);
69         }
70         else if (helper.invalidDataObjectClass(className)) {
71             validator.registerWarning(
72                     "ObjEntity Java class is invalid: " + className,
73                     path);
74         }
75         else if (className.indexOf('.') < 0) {
76             validator.registerWarning(
77                     "Placing Java class in default package is discouraged: " + className,
78                     path);
79         }
80     }
81
82     private void validateSuperClassName(
83             ObjEntity ent,
84             ProjectPath path,
85             Validator validator) {
86         String JavaDoc superClassName = ent.getSuperClassName();
87
88         if (Util.isEmptyString(superClassName)) {
89             return; // null is Ok
90
}
91
92         MappingNamesHelper helper = MappingNamesHelper.getInstance();
93         String JavaDoc invalidChars = helper.invalidCharsInJavaClassName(superClassName);
94
95         if (invalidChars != null) {
96             validator.registerWarning(
97                     "ObjEntity Java superclass contains invalid characters: "
98                             + invalidChars,
99                     path);
100         }
101         else if (helper.invalidDataObjectClass(superClassName)) {
102             validator.registerWarning("ObjEntity Java superclass is invalid: "
103                     + superClassName, path);
104         }
105
106         DataMap map = (DataMap) path.getObjectParent();
107         if (map == null) {
108             return;
109         }
110     }
111
112     protected void validateName(ObjEntity entity, ProjectPath path, Validator validator) {
113         String JavaDoc name = entity.getName();
114
115         // Must have name
116
if (Util.isEmptyString(name)) {
117             validator.registerError("Unnamed ObjEntity.", path);
118             return;
119         }
120
121         DataMap map = (DataMap) path.getObjectParent();
122         if (map == null) {
123             return;
124         }
125
126         // check for duplicate names in the parent context
127
Iterator JavaDoc it = map.getObjEntities().iterator();
128         while (it.hasNext()) {
129             ObjEntity otherEnt = (ObjEntity) it.next();
130             if (otherEnt == entity) {
131                 continue;
132             }
133
134             if (name.equals(otherEnt.getName())) {
135                 validator.registerError("Duplicate ObjEntity name: " + name + ".", path);
136                 break;
137             }
138         }
139
140         // check for dupliucates in other DataMaps
141
DataDomain domain = (DataDomain) path.firstInstanceOf(DataDomain.class);
142         if (domain != null) {
143             Iterator JavaDoc maps = domain.getDataMaps().iterator();
144             while (maps.hasNext()) {
145                 DataMap nextMap = (DataMap) maps.next();
146                 if (nextMap == map) {
147                     continue;
148                 }
149
150                 ObjEntity conflictingEntity = nextMap.getObjEntity(name);
151                 if (conflictingEntity != null) {
152
153                     if (!Util.nullSafeEquals(conflictingEntity.getClassName(), entity
154                             .getClassName())) {
155                         validator.registerWarning(
156                                 "Duplicate ObjEntity name in another DataMap: "
157                                         + name
158                                         + ".",
159                                 path);
160                         break;
161                     }
162                 }
163             }
164         }
165     }
166 }
167
Popular Tags