* @param type the AttributeType
*
* @return an int indicating the max length of field in characters, or ANY_LENGTH
*/
public static int getFieldLength( PropertyDescriptor descriptor ) {
PropertyType type = descriptor.getType();
Integer length = null;
while( type != null ){
// TODO: We should really go through all the restrictions and find
// the minimum of all the length restrictions; for now we assume an
// override behavior.
for ( Filter f : type.getRestrictions()) {
Integer filterLength = null;
try {
if(f == null) {
continue;
}
if (f instanceof PropertyIsLessThan) {
BinaryComparisonOperator cf = (BinaryComparisonOperator) f;
if (cf.getExpression1() instanceof LengthFunction) {
filterLength = cf.getExpression2().evaluate(null, Integer.class) - 1;
}
} else if (f instanceof PropertyIsLessThanOrEqualTo) {
BinaryComparisonOperator cf = (BinaryComparisonOperator) f;
if (cf.getExpression1() instanceof LengthFunction) {
filterLength = cf.getExpression2().evaluate(null, Integer.class);
}
} else if(f instanceof PropertyIsGreaterThan) {
BinaryComparisonOperator cf = (BinaryComparisonOperator) f;
if (cf.getExpression2() instanceof LengthFunction) {
filterLength = cf.getExpression1().evaluate(null, Integer.class) - 1;
}
} else if (f instanceof PropertyIsGreaterThanOrEqualTo) {
BinaryComparisonOperator cf = (BinaryComparisonOperator) f;
if (cf.getExpression2() instanceof LengthFunction) {
filterLength = cf.getExpression1().evaluate(null, Integer.class);
}
}
} catch (NullPointerException e) {
// was not an integer eh? Continue, worst case we'll return ANY_LENGTH
}
if(filterLength != null) {
if(length == null || filterLength < length) {
length = filterLength;
}
}
}
type = type.getSuper();
}
return length != null ? length : ANY_LENGTH;
}