KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.List JavaDoc;
24
25 import org.apache.cayenne.map.DbEntity;
26 import org.apache.cayenne.map.DbJoin;
27 import org.apache.cayenne.map.DbRelationship;
28 import org.apache.cayenne.map.DeleteRule;
29 import org.apache.cayenne.map.ObjEntity;
30 import org.apache.cayenne.map.ObjRelationship;
31 import org.apache.cayenne.project.ProjectPath;
32 import org.apache.cayenne.util.Util;
33
34 /**
35  * @author Andrus Adamchik
36  */

37 public class ObjRelationshipValidator extends TreeNodeValidator {
38
39     /**
40      * Constructor for ObjRelationshipValidator.
41      */

42     public ObjRelationshipValidator() {
43         super();
44     }
45
46     public void validateObject(ProjectPath path, Validator validator) {
47         ObjRelationship rel = (ObjRelationship) path.getObject();
48
49         // skip validation of inherited relationships
50
if (path.getObjectParent() != null
51                 && path.getObjectParent() != rel.getSourceEntity()) {
52             return;
53         }
54
55         if (Util.isEmptyString(rel.getName())) {
56             validator.registerError("Unnamed ObjRelationship.", path);
57         }
58         // check if there are attributes having the same name
59
else if (rel.getSourceEntity().getAttribute(rel.getName()) != null) {
60             validator.registerWarning("ObjRelationship "
61                     + objRelationshipIdentifier(rel)
62                     + " has the same name as one of ObjAttributes", path);
63         }
64         else {
65             MappingNamesHelper helper = MappingNamesHelper.getInstance();
66             String JavaDoc invalidChars = helper.invalidCharsInObjPathComponent(rel.getName());
67
68             if (invalidChars != null) {
69                 validator.registerWarning("ObjRelationship "
70                         + objRelationshipIdentifier(rel)
71                         + " name contains invalid characters: "
72                         + invalidChars, path);
73             }
74             else if (helper.invalidDataObjectProperty(rel.getName())) {
75                 validator.registerWarning("ObjRelationship "
76                         + objRelationshipIdentifier(rel)
77                         + " name is invalid.", path);
78             }
79         }
80
81         if (rel.getTargetEntity() == null) {
82             validator.registerWarning("ObjRelationship "
83                     + objRelationshipIdentifier(rel)
84                     + " has no target entity.", path);
85         }
86         else {
87             // check for missing DbRelationship mappings
88
List JavaDoc dbRels = rel.getDbRelationships();
89             if (dbRels.size() == 0) {
90                 validator.registerWarning("ObjRelationship "
91                         + objRelationshipIdentifier(rel)
92                         + " has no DbRelationship mapping.", path);
93             }
94             else {
95                 DbEntity expectedSrc = ((ObjEntity) rel.getSourceEntity()).getDbEntity();
96                 DbEntity expectedTarget = ((ObjEntity) rel.getTargetEntity())
97                         .getDbEntity();
98
99                 if (((DbRelationship) dbRels.get(0)).getSourceEntity() != expectedSrc
100                         || ((DbRelationship) dbRels.get(dbRels.size() - 1))
101                                 .getTargetEntity() != expectedTarget) {
102                     validator.registerWarning("ObjRelationship "
103                             + objRelationshipIdentifier(rel)
104                             + " has incomplete DbRelationship mapping.", path);
105                 }
106             }
107         }
108
109         // Disallow a Nullify delete rule where the relationship is toMany and the
110
// foreign key attributes are mandatory.
111
if (rel.isToMany()
112                 && !rel.isFlattened()
113                 && (rel.getDeleteRule() == DeleteRule.NULLIFY)) {
114             ObjRelationship inverse = rel.getReverseRelationship();
115             if (inverse != null) {
116                 DbRelationship firstRel = (DbRelationship) inverse
117                         .getDbRelationships()
118                         .get(0);
119                 Iterator JavaDoc attributePairIterator = firstRel.getJoins().iterator();
120                 // by default, the relation will be check for mandatory.
121
boolean check = true;
122                 while (attributePairIterator.hasNext()) {
123                     DbJoin pair = (DbJoin) attributePairIterator.next();
124                     if (!pair.getSource().isMandatory()) {
125                         // a field of the fk can be nullable, cancel the check.
126
check = false;
127                         break;
128                     }
129                 }
130                 
131                 if (check) {
132                     validator
133                             .registerWarning(
134                                     "ObjRelationship "
135                                             + objRelationshipIdentifier(rel)
136                                             + " has a Nullify delete rule and a mandatory reverse relationship ",
137                                     path);
138                 }
139             }
140         }
141     }
142
143     public String JavaDoc objRelationshipIdentifier(ObjRelationship rel) {
144         if (null == rel.getSourceEntity()) {
145             return "<[null source entity]." + rel.getName() + ">";
146         }
147         return "<" + rel.getSourceEntity().getName() + "." + rel.getName() + ">";
148     }
149 }
150
Popular Tags