KickJava   Java API By Example, From Geeks To Geeks.

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


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

31 public class DbAttributeValidator extends TreeNodeValidator {
32
33     /**
34      * Constructor for DbAttributeValidator.
35      */

36     public DbAttributeValidator() {
37         super();
38     }
39
40     public void validateObject(ProjectPath path, Validator validator) {
41         DbAttribute attribute = (DbAttribute) path.getObject();
42         // Must have name
43
if (Util.isEmptyString(attribute.getName())) {
44             validator.registerError("Unnamed DbAttribute.", path);
45         }
46         else {
47             MappingNamesHelper helper = MappingNamesHelper.getInstance();
48             String JavaDoc invalidChars = helper.invalidCharsInDbPathComponent(attribute
49                     .getName());
50             
51             if (invalidChars != null) {
52                 validator.registerWarning("DbAttribute name contains invalid characters: "
53                         + invalidChars, path);
54             }
55         }
56
57         // all attributes must have type
58
if (attribute.getType() == TypesMapping.NOT_DEFINED) {
59             validator.registerWarning("DbAttribute has no type.", path);
60         }
61
62         if (attribute instanceof DerivedDbAttribute) {
63             DerivedDbAttribute derived = (DerivedDbAttribute) attribute;
64             int paramCount = derived.getParams().size();
65
66             String JavaDoc spec = derived.getExpressionSpec();
67             int paramsExpected = 0;
68             if (spec != null) {
69                 // count tokens
70
int ind = -DerivedDbAttribute.ATTRIBUTE_TOKEN.length();
71                 while ((ind = spec.indexOf(DerivedDbAttribute.ATTRIBUTE_TOKEN, ind
72                         + DerivedDbAttribute.ATTRIBUTE_TOKEN.length())) >= 0) {
73                     paramsExpected++;
74                 }
75             }
76
77             if (paramsExpected != paramCount) {
78                 validator.registerWarning("Derived Attribute's \""
79                         + attribute.getName()
80                         + "\" parameter mismatch.", path);
81             }
82         }
83         // VARCHAR and CHAR attributes must have max length
84
else if (attribute.getMaxLength() < 0
85                 && (attribute.getType() == java.sql.Types.VARCHAR || attribute.getType() == java.sql.Types.CHAR)) {
86
87             validator.registerWarning(
88                     "Character DbAttribute doesn't have max length.",
89                     path);
90         }
91     }
92 }
93
Popular Tags