KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.cayenne.map.ObjAttribute;
23 import org.apache.cayenne.project.ProjectPath;
24 import org.apache.cayenne.util.Util;
25
26 /**
27  * @author Andrus Adamchik
28  */

29 public class ObjAttributeValidator extends TreeNodeValidator {
30
31     /**
32      * Constructor for ObjAttributeValidator.
33      */

34     public ObjAttributeValidator() {
35         super();
36     }
37
38     public void validateObject(ProjectPath path, Validator validator) {
39         ObjAttribute attribute = (ObjAttribute) path.getObject();
40
41         // skip validation of inherited attributes
42
if (path.getObjectParent() != null
43                 && path.getObjectParent() != attribute.getEntity()) {
44             return;
45         }
46
47         // Must have name
48
if (Util.isEmptyString(attribute.getName())) {
49             validator.registerError("Unnamed ObjAttribute.", path);
50         }
51         else {
52             MappingNamesHelper helper = MappingNamesHelper.getInstance();
53             String JavaDoc invalidChars = helper.invalidCharsInObjPathComponent(attribute
54                     .getName());
55
56             if (invalidChars != null) {
57                 validator.registerWarning(
58                         "ObjAttribute name contains invalid characters: " + invalidChars,
59                         path);
60             }
61             else if (helper.invalidDataObjectProperty(attribute.getName())) {
62                 validator.registerWarning("ObjAttribute name is invalid: "
63                         + attribute.getName(), path);
64             }
65         }
66
67         // all attributes must have type
68
if (Util.isEmptyString(attribute.getType())) {
69             validator.registerWarning("ObjAttribute has no type.", path);
70         }
71
72         if (attribute.getDbAttribute() == null) {
73             validator.registerWarning("ObjAttribute has no DbAttribute mapping.", path);
74         }
75         // can't support generated meaningful attributes for now; besides they don't make
76
// sense.
77
else if (attribute.getDbAttribute().isPrimaryKey()
78                 && attribute.getDbAttribute().isGenerated()) {
79             validator.registerWarning("ObjAttribute is mapped to a generated PK: "
80                     + attribute.getDbAttributeName(), path);
81         }
82     }
83 }
84
Popular Tags