KickJava   Java API By Example, From Geeks To Geeks.

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


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.map.DbJoin;
25 import org.apache.cayenne.map.DbRelationship;
26 import org.apache.cayenne.project.ProjectPath;
27 import org.apache.cayenne.util.Util;
28
29 /**
30  * @author Andrus Adamchik
31  */

32 public class DbRelationshipValidator extends TreeNodeValidator {
33
34     /**
35      * Constructor for DbRelationshipValidator.
36      */

37     public DbRelationshipValidator() {
38         super();
39     }
40
41     public void validateObject(ProjectPath path, Validator validator) {
42         DbRelationship rel = (DbRelationship) path.getObject();
43         if (rel.getTargetEntity() == null) {
44             validator.registerWarning("DbRelationship " + dbRelationshipIdentifier(rel) + " has no target entity.", path);
45         }
46         else if (rel.getJoins().size() == 0) {
47             validator.registerWarning("DbRelationship " + dbRelationshipIdentifier(rel) + " has no joins.", path);
48         }
49         else {
50             // validate joins
51
Iterator JavaDoc joins = rel.getJoins().iterator();
52             while (joins.hasNext()) {
53                 DbJoin join = (DbJoin) joins.next();
54                 if (join.getSource() == null && join.getTarget() == null) {
55                     validator
56                             .registerWarning(
57                                     "DbRelationship " + dbRelationshipIdentifier(rel) + " join has no source and target attributes selected.",
58                                     path);
59                 }
60                 else if (join.getSource() == null) {
61                     validator.registerWarning(
62                             "DbRelationship " + dbRelationshipIdentifier(rel) + " join has no source attribute selected.",
63                             path);
64                 }
65                 else if (join.getTarget() == null) {
66                     validator.registerWarning(
67                             "DbRelationship " + dbRelationshipIdentifier(rel) + " join has no target attribute selected.",
68                             path);
69                 }
70             }
71
72             // validate reverse
73
if (rel.getReverseRelationship() == null) {
74                 validator
75                         .registerWarning(
76                                 "Missing reverse DbRelationship " + dbRelationshipIdentifier(rel) + " (currently required by Cayenne).",
77                                 path);
78             }
79         }
80
81         if (Util.isEmptyString(rel.getName())) {
82             validator.registerError("Unnamed DbRelationship.", path);
83         }
84         // check if there are attributes having the same name
85
else if (rel.getSourceEntity().getAttribute(rel.getName()) != null) {
86             validator.registerError(
87                     "DbRelationship " + dbRelationshipIdentifier(rel) + " has the same name as one of DbAttributes",
88                     path);
89         }
90         else {
91             MappingNamesHelper helper = MappingNamesHelper.getInstance();
92             String JavaDoc invalidChars = helper.invalidCharsInDbPathComponent(rel.getName());
93
94             if (invalidChars != null) {
95                 validator
96                         .registerWarning(
97                                 "DbRelationship " + dbRelationshipIdentifier(rel) + " name contains invalid characters: "
98                                         + invalidChars,
99                                 path);
100             }
101         }
102     }
103     
104     public String JavaDoc dbRelationshipIdentifier(DbRelationship rel)
105     {
106         if (null == rel.getSourceEntity())
107         {
108             return "<[null source entity]." + rel.getName() + ">";
109         }
110         return "<" + rel.getSourceEntity().getName() + "." + rel.getName() + ">";
111     }
112 }
113
Popular Tags