KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

38     public DbEntityValidator() {
39         super();
40     }
41
42     public void validateObject(ProjectPath path, Validator validator) {
43         DbEntity ent = (DbEntity) path.getObject();
44         validateName(ent, path, validator);
45         validateAttributes(ent, path, validator);
46         validatePK(ent, path, validator);
47
48         if ((ent instanceof DerivedDbEntity)
49             && ((DerivedDbEntity) ent).getParentEntity() == null) {
50             validator.registerError(
51                 "No parent selected for derived entity \""
52                     + ent.getName()
53                     + "\".",
54                 path);
55         }
56     }
57
58     /**
59      * Validates the presence of the primary key. A warning is given only if the parent
60      * map also conatins an ObjEntity mapped to this entity, since unmapped primary key
61      * is ok if working with data rows.
62      */

63     protected void validatePK(
64         DbEntity ent,
65         ProjectPath path,
66         Validator validator) {
67         if (ent.getAttributes().size() > 0
68             && ent.getPrimaryKey().size() == 0) {
69             DataMap map = ent.getDataMap();
70             if (map != null && map.getMappedEntities(ent).size() > 0) {
71                 // there is an objentity, so complain about no pk
72
validator.registerWarning(
73                     "DbEntity \""
74                         + ent.getName()
75                         + "\" has no primary key attributes defined.",
76                     path);
77             }
78         }
79     }
80
81     /**
82      * Tables must have columns.
83      */

84     protected void validateAttributes(
85         DbEntity ent,
86         ProjectPath path,
87         Validator validator) {
88         if (ent.getAttributes().size() == 0) {
89             // complain about missing attributes
90
validator.registerWarning(
91                 "DbEntity \"" + ent.getName() + "\" has no attributes defined.",
92                 path);
93         }
94     }
95
96     protected void validateName(
97         DbEntity ent,
98         ProjectPath path,
99         Validator validator) {
100         String JavaDoc name = ent.getName();
101
102         // Must have name
103
if (Util.isEmptyString(name)) {
104             validator.registerError("Unnamed DbEntity.", path);
105             return;
106         }
107
108         DataMap map = (DataMap) path.getObjectParent();
109         if (map == null) {
110             return;
111         }
112
113         // check for duplicate names in the parent context
114
Iterator JavaDoc it = map.getDbEntities().iterator();
115         while (it.hasNext()) {
116             DbEntity otherEnt = (DbEntity) it.next();
117             if (otherEnt == ent) {
118                 continue;
119             }
120
121             if (name.equals(otherEnt.getName())) {
122                 validator.registerError(
123                     "Duplicate DbEntity name: " + name + ".",
124                     path);
125                 break;
126             }
127         }
128     }
129 }
130
Popular Tags