Search in sources :

Example 1 with CodeTableProperty

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;
    }
}
Also used : CodeTable(com.revolsys.record.code.CodeTable) CodeTableProperty(com.revolsys.record.code.CodeTableProperty)

Example 2 with CodeTableProperty

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);
                        }
                    }
                }
            }
        }
    }
}
Also used : CodeTable(com.revolsys.record.code.CodeTable) TypedIdentifier(com.revolsys.identifier.TypedIdentifier) Identifier(com.revolsys.identifier.Identifier) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) CodeTableProperty(com.revolsys.record.code.CodeTableProperty) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 3 with CodeTableProperty

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;
}
Also used : CodeTable(com.revolsys.record.code.CodeTable) Geometry(com.revolsys.geometry.model.Geometry) Identifier(com.revolsys.identifier.Identifier) IsValidOp(com.revolsys.geometry.operation.valid.IsValidOp) BigDecimal(java.math.BigDecimal) CodeTableProperty(com.revolsys.record.code.CodeTableProperty)

Aggregations

CodeTable (com.revolsys.record.code.CodeTable)3 CodeTableProperty (com.revolsys.record.code.CodeTableProperty)3 Identifier (com.revolsys.identifier.Identifier)2 Geometry (com.revolsys.geometry.model.Geometry)1 IsValidOp (com.revolsys.geometry.operation.valid.IsValidOp)1 TypedIdentifier (com.revolsys.identifier.TypedIdentifier)1 JdbcFieldDefinition (com.revolsys.jdbc.field.JdbcFieldDefinition)1 RecordDefinition (com.revolsys.record.schema.RecordDefinition)1 BigDecimal (java.math.BigDecimal)1