1 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 31 public class DbAttributeValidator extends TreeNodeValidator { 32 33 36 public DbAttributeValidator() { 37 super(); 38 } 39 40 public void validateObject(ProjectPath path, Validator validator) { 41 DbAttribute attribute = (DbAttribute) path.getObject(); 42 if (Util.isEmptyString(attribute.getName())) { 44 validator.registerError("Unnamed DbAttribute.", path); 45 } 46 else { 47 MappingNamesHelper helper = MappingNamesHelper.getInstance(); 48 String 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 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 spec = derived.getExpressionSpec(); 67 int paramsExpected = 0; 68 if (spec != null) { 69 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 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 |