use of com.revolsys.record.code.CodeTableProperty in project com.revolsys.open by revolsys.
the class RecordDefinitionImpl method getCodeTableByFieldName.
@Override
public CodeTable getCodeTableByFieldName(final CharSequence fieldName) {
if (fieldName == null) {
return null;
} else {
final RecordStore recordStore = getRecordStore();
CodeTable codeTable;
final FieldDefinition fieldDefinition = getField(fieldName);
if (fieldDefinition != null) {
codeTable = fieldDefinition.getCodeTable();
if (codeTable != null) {
return codeTable;
}
}
codeTable = this.codeTableByFieldNameMap.get(fieldName.toString().toUpperCase());
if (codeTable == null && recordStore != null) {
codeTable = recordStore.getCodeTableByFieldName(fieldName);
}
if (codeTable instanceof CodeTableProperty) {
@SuppressWarnings("resource") final CodeTableProperty property = (CodeTableProperty) codeTable;
if (property.getRecordDefinition() == this) {
return null;
}
}
if (fieldDefinition != null && codeTable != null) {
fieldDefinition.setCodeTable(codeTable);
}
return codeTable;
}
}
use of com.revolsys.record.code.CodeTableProperty in project com.revolsys.open by revolsys.
the class Value method setFieldDefinition.
@Override
public void setFieldDefinition(final FieldDefinition field) {
if (field != null) {
this.fieldDefinition = field;
if (field instanceof JdbcFieldDefinition) {
this.jdbcField = (JdbcFieldDefinition) field;
} else {
this.jdbcField = JdbcFieldDefinition.newFieldDefinition(this.queryValue);
}
CodeTable codeTable = null;
if (field != null) {
final RecordDefinition recordDefinition = field.getRecordDefinition();
if (recordDefinition != null) {
final String fieldName = field.getName();
codeTable = recordDefinition.getCodeTableByFieldName(fieldName);
if (codeTable instanceof CodeTableProperty) {
final CodeTableProperty codeTableProperty = (CodeTableProperty) codeTable;
if (codeTableProperty.getRecordDefinition() == recordDefinition) {
codeTable = null;
}
}
if (codeTable != null) {
final Identifier id = codeTable.getIdentifier(this.queryValue);
if (id == null) {
this.displayValue = this.queryValue;
} else {
setQueryValue(id);
final List<Object> values = codeTable.getValues(id);
if (values.size() == 1) {
this.displayValue = values.get(0);
} else {
this.displayValue = Strings.toString(":", values);
}
}
}
}
}
}
}
use of com.revolsys.record.code.CodeTableProperty in project com.revolsys.open by revolsys.
the class FieldDefinition method validate.
public Object validate(Object value) {
final String fieldName = getName();
value = toFieldValueException(value);
if (value == null) {
if (isRequired()) {
throw new IllegalArgumentException(fieldName + " is required");
}
} else {
final RecordDefinition recordDefinition = getRecordDefinition();
final CodeTable codeTable = recordDefinition.getCodeTableByFieldName(fieldName);
if (codeTable == null) {
final int maxLength = getLength();
if (value instanceof Number) {
final Number number = (Number) value;
final BigDecimal bigNumber = new BigDecimal(number.toString());
final int length = bigNumber.precision();
if (maxLength > 0) {
if (length > maxLength) {
throw new IllegalArgumentException(fieldName + "=" + value + " length " + length + " > " + maxLength);
}
}
final int scale = bigNumber.scale();
final int maxScale = getScale();
if (maxScale > 0) {
if (scale > maxScale) {
throw new IllegalArgumentException(fieldName + "=" + value + " scale " + scale + " > " + maxScale);
}
}
final Number minValue = getMinValue();
if (minValue != null) {
if (NumericComparator.numericCompare(number, minValue) < 0) {
throw new IllegalArgumentException(fieldName + "=" + value + " > " + minValue);
}
}
final Number maxValue = getMaxValue();
if (maxValue != null) {
if (NumericComparator.numericCompare(number, maxValue) > 0) {
throw new IllegalArgumentException(fieldName + "=" + value + " < " + maxValue);
}
}
} else if (value instanceof String) {
final String string = (String) value;
final int length = string.length();
if (maxLength > 0) {
if (length > maxLength) {
throw new IllegalArgumentException(fieldName + "=" + value + " length " + length + " > " + maxLength);
}
}
} else if (value instanceof Geometry) {
final Geometry geometry = (Geometry) value;
final IsValidOp validOp = new IsValidOp(geometry, false);
if (!validOp.isValid()) {
final String errors = Strings.toString(validOp.getErrors());
throw new IllegalArgumentException("Geometry not valid: " + errors);
}
}
if (!this.allowedValues.isEmpty()) {
if (!this.allowedValues.containsKey(value)) {
throw new IllegalArgumentException(fieldName + "=" + value + " not in (" + Strings.toString(",", this.allowedValues) + ")");
}
}
} else {
final Identifier id = codeTable.getIdentifier(value);
if (id == null) {
String codeTableName;
if (codeTable instanceof CodeTableProperty) {
@SuppressWarnings("resource") final CodeTableProperty property = (CodeTableProperty) codeTable;
codeTableName = property.getTypeName();
} else {
codeTableName = codeTable.toString();
}
throw new IllegalArgumentException("Unable to find code for '" + value + "' in " + codeTableName);
}
}
}
return value;
}
Aggregations