1 56 package org.objectstyle.cayenne.project.validator; 57 58 import org.objectstyle.cayenne.dba.TypesMapping; 59 import org.objectstyle.cayenne.map.DbAttribute; 60 import org.objectstyle.cayenne.map.DerivedDbAttribute; 61 import org.objectstyle.cayenne.project.ProjectPath; 62 import org.objectstyle.cayenne.util.Util; 63 64 67 public class DbAttributeValidator extends TreeNodeValidator { 68 69 72 public DbAttributeValidator() { 73 super(); 74 } 75 76 public void validateObject(ProjectPath path, Validator validator) { 77 DbAttribute attribute = (DbAttribute) path.getObject(); 78 if (Util.isEmptyString(attribute.getName())) { 80 validator.registerError("Unnamed DbAttribute.", path); 81 } 82 else { 83 MappingNamesHelper helper = MappingNamesHelper.getInstance(); 84 String invalidChars = helper.invalidCharsInDbPathComponent(attribute 85 .getName()); 86 87 if (invalidChars != null) { 88 validator.registerWarning("DbAttribute name contains invalid characters: " 89 + invalidChars, path); 90 } 91 } 92 93 if (attribute.getType() == TypesMapping.NOT_DEFINED) { 95 validator.registerWarning("DbAttribute has no type.", path); 96 } 97 98 if (attribute instanceof DerivedDbAttribute) { 99 DerivedDbAttribute derived = (DerivedDbAttribute) attribute; 100 int paramCount = derived.getParams().size(); 101 102 String spec = derived.getExpressionSpec(); 103 int paramsExpected = 0; 104 if (spec != null) { 105 int ind = -DerivedDbAttribute.ATTRIBUTE_TOKEN.length(); 107 while ((ind = spec.indexOf(DerivedDbAttribute.ATTRIBUTE_TOKEN, ind 108 + DerivedDbAttribute.ATTRIBUTE_TOKEN.length())) >= 0) { 109 paramsExpected++; 110 } 111 } 112 113 if (paramsExpected != paramCount) { 114 validator.registerWarning("Derived Attribute's \"" 115 + attribute.getName() 116 + "\" parameter mismatch.", path); 117 } 118 } 119 else if (attribute.getMaxLength() < 0 121 && (attribute.getType() == java.sql.Types.VARCHAR || attribute.getType() == java.sql.Types.CHAR)) { 122 123 validator.registerWarning( 124 "Character DbAttribute doesn't have max length.", 125 path); 126 } 127 } 128 } | Popular Tags |