use of com.revolsys.record.code.CodeTable in project com.revolsys.open by revolsys.
the class AbstractRecordTableModel method toObjectValue.
public Object toObjectValue(final String fieldName, final Object displayValue) {
if (!Property.hasValue(displayValue)) {
return null;
}
final RecordDefinition recordDefinition = getRecordDefinition();
final CodeTable codeTable = recordDefinition.getCodeTableByFieldName(fieldName);
if (codeTable == null) {
final FieldDefinition field = recordDefinition.getField(fieldName);
final Object recordValue = field.toFieldValue(displayValue);
return recordValue;
} else {
if (displayValue instanceof Identifier) {
final Identifier identifier = (Identifier) displayValue;
return identifier;
} else {
final Object objectValue = codeTable.getIdentifier(displayValue);
return objectValue;
}
}
}
use of com.revolsys.record.code.CodeTable in project com.revolsys.open by revolsys.
the class RecordRowTableModel method getPrototypeValue.
@Override
public Object getPrototypeValue(final int columnIndex) {
FieldDefinition fieldDefinition = getColumnFieldDefinition(columnIndex);
if (fieldDefinition == null) {
return null;
} else {
final CodeTable codeTable = fieldDefinition.getCodeTable();
if (codeTable != null) {
final FieldDefinition valueFieldDefinition = codeTable.getValueFieldDefinition();
if (valueFieldDefinition != null) {
fieldDefinition = valueFieldDefinition;
} else {
Object maxValue = "";
int maxLength = 0;
for (final Object value : codeTable.getValues()) {
if (value != null) {
final int length = DataTypes.toString(value).length();
if (length > maxLength) {
maxValue = value;
maxLength = length;
}
}
}
return maxValue;
}
}
final DataType fieldType = fieldDefinition.getDataType();
final Class<?> fieldClass = fieldDefinition.getTypeClass();
final int fieldLength = fieldDefinition.getLength();
if (DataTypes.BOOLEAN.equals(fieldType)) {
return Byte.MIN_VALUE;
} else if (DataTypes.BYTE.equals(fieldType)) {
return Byte.MIN_VALUE;
} else if (DataTypes.SHORT.equals(fieldType)) {
return Short.MAX_VALUE;
} else if (DataTypes.INT.equals(fieldType)) {
return Integer.MAX_VALUE;
} else if (DataTypes.LONG.equals(fieldType)) {
return Long.MAX_VALUE;
} else if (DataTypes.SQL_DATE.equals(fieldType)) {
return Dates.getSqlDate("9999-12-31");
} else if (DataTypes.DATE.equals(fieldType)) {
return Dates.getDate("9999-12-29 23:59:59.999");
} else if (DataTypes.DATE_TIME.equals(fieldType)) {
return Dates.getTimestamp("9999-12-29 23:59:59.999");
} else if (DataTypes.TIMESTAMP.equals(fieldType)) {
return Dates.getTimestamp("9999-12-29 23:59:59.999");
} else if (Geometry.class.isAssignableFrom(fieldClass)) {
return fieldType.getName();
} else {
if (fieldLength < 1 || fieldLength > 30) {
return "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
} else {
final StringBuilder string = new StringBuilder();
for (int i = 0; i < fieldLength; i++) {
string.append("X");
}
return string.toString();
}
}
// if (Number.class.isAssignableFrom(fieldClass)) {
// final StringBuilder numberString = new StringBuilder();
// final Object maxValue = fieldDefinition.getMaxValue();
// final Object minValue = fieldDefinition.getMinValue();
// if (maxValue == null) {
// for (int i = 0; i <= maxLength; i++) {
// numberString.append("9");
// }
// return fieldType.toObject(numberString.toString());
// } else {
// if (minValue instanceof Number) {
// numberString.append(((Number)minValue).longValue());
// }
// }
// if (Float.class.isAssignableFrom(fieldClass) ||
// Double.class.isAssignableFrom(fieldClass)
// || BigDecimal.class.isAssignableFrom(fieldClass)) {
// numberString.append('.');
// final int fieldScale = fieldDefinition.getScale();
// for (int i = 0; i <= fieldScale; i++) {
// numberString.append("9");
// }
// final BigDecimal scaleValue = new BigDecimal(numberString.toString());
// }
// }
// return super.getPrototypeValue(columnIndex);
}
}
use of com.revolsys.record.code.CodeTable in project com.revolsys.open by revolsys.
the class FileGdbRecordStore method getRecordDefinition.
public RecordDefinitionImpl getRecordDefinition(final PathName schemaName, final String path, final String tableDefinition) {
synchronized (this.apiSync) {
synchronized (API_SYNC) {
try {
final XmlProcessor parser = new EsriGdbXmlParser();
final DETable deTable = parser.process(tableDefinition);
final String tableName = deTable.getName();
final PathName typePath = PathName.newPathName(schemaName.newChild(tableName));
final RecordStoreSchema schema = getSchema(schemaName);
final RecordDefinitionImpl recordDefinition = new RecordDefinitionImpl(schema, typePath);
recordDefinition.setPolygonRingDirection(ClockDirection.CLOCKWISE);
String lengthFieldName = null;
String areaFieldName = null;
if (deTable instanceof DEFeatureClass) {
final DEFeatureClass featureClass = (DEFeatureClass) deTable;
lengthFieldName = featureClass.getLengthFieldName();
final LengthFieldName lengthFieldNameProperty = new LengthFieldName(lengthFieldName);
lengthFieldNameProperty.setRecordDefinition(recordDefinition);
areaFieldName = featureClass.getAreaFieldName();
final LengthFieldName areaFieldNameProperty = new LengthFieldName(areaFieldName);
areaFieldNameProperty.setRecordDefinition(recordDefinition);
}
for (final Field field : deTable.getFields()) {
final String fieldName = field.getName();
AbstractFileGdbFieldDefinition fieldDefinition = null;
if (fieldName.equals(lengthFieldName)) {
fieldDefinition = new LengthFieldDefinition(field);
} else if (fieldName.equals(areaFieldName)) {
fieldDefinition = new AreaFieldDefinition(field);
} else {
final FieldType type = field.getType();
final Constructor<? extends AbstractFileGdbFieldDefinition> fieldConstructor = ESRI_FIELD_TYPE_FIELD_DEFINITION_MAP.get(type);
if (fieldConstructor != null) {
try {
fieldDefinition = JavaBeanUtil.invokeConstructor(fieldConstructor, field);
} catch (final Throwable e) {
Logs.error(this, tableDefinition);
throw new RuntimeException("Error creating field for " + typePath + "." + field.getName() + " : " + field.getType(), e);
}
} else {
Logs.error(this, "Unsupported field type " + fieldName + ":" + type);
}
}
if (fieldDefinition != null) {
final Domain domain = field.getDomain();
if (domain != null) {
CodeTable codeTable = getCodeTable(domain.getDomainName() + "_ID");
if (codeTable == null) {
codeTable = new FileGdbDomainCodeTable(this, domain);
addCodeTable(codeTable);
}
fieldDefinition.setCodeTable(codeTable);
}
fieldDefinition.setRecordStore(this);
recordDefinition.addField(fieldDefinition);
if (fieldDefinition instanceof GlobalIdFieldDefinition) {
recordDefinition.setIdFieldName(fieldName);
}
}
}
final String oidFieldName = deTable.getOIDFieldName();
recordDefinition.setProperty(EsriGeodatabaseXmlConstants.ESRI_OBJECT_ID_FIELD_NAME, oidFieldName);
if (deTable instanceof DEFeatureClass) {
final DEFeatureClass featureClass = (DEFeatureClass) deTable;
final String shapeFieldName = featureClass.getShapeFieldName();
recordDefinition.setGeometryFieldName(shapeFieldName);
}
for (final Index index : deTable.getIndexes()) {
if (index.getName().endsWith("_PK")) {
for (final Field field : index.getFields()) {
final String fieldName = field.getName();
recordDefinition.setIdFieldName(fieldName);
}
}
}
addRecordDefinitionProperties(recordDefinition);
if (recordDefinition.getIdFieldIndex() == -1) {
recordDefinition.setIdFieldName(deTable.getOIDFieldName());
}
this.catalogPathByPath.put(typePath, deTable.getCatalogPath());
return recordDefinition;
} catch (final RuntimeException e) {
Logs.debug(this, tableDefinition);
throw e;
}
}
}
}
use of com.revolsys.record.code.CodeTable 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;
}
use of com.revolsys.record.code.CodeTable in project com.revolsys.open by revolsys.
the class QueryValue method toQueryValue.
@SuppressWarnings("unchecked")
static <V extends QueryValue> V toQueryValue(final RecordDefinition recordDefinition, final ValueNode expression) {
if (expression instanceof BetweenOperatorNode) {
final BetweenOperatorNode betweenExpression = (BetweenOperatorNode) expression;
final ValueNode leftValueNode = betweenExpression.getLeftOperand();
final ValueNodeList rightOperandList = betweenExpression.getRightOperandList();
final ValueNode betweenExpressionStart = rightOperandList.get(0);
final ValueNode betweenExpressionEnd = rightOperandList.get(1);
if (!(leftValueNode instanceof ColumnReference)) {
throw new IllegalArgumentException("Between operator must use a column name not: " + leftValueNode);
}
if (!(betweenExpressionStart instanceof NumericConstantNode)) {
throw new IllegalArgumentException("Between min value must be a number not: " + betweenExpressionStart);
}
if (!(betweenExpressionEnd instanceof NumericConstantNode)) {
throw new IllegalArgumentException("Between max value must be a number not: " + betweenExpressionEnd);
}
final Column column = toQueryValue(recordDefinition, leftValueNode);
final Value min = toQueryValue(recordDefinition, betweenExpressionStart);
final Value max = toQueryValue(recordDefinition, betweenExpressionEnd);
if (recordDefinition != null) {
final FieldDefinition field = recordDefinition.getField(column.getName());
min.convert(field);
max.convert(field);
}
return (V) new Between(column, min, max);
} else if (expression instanceof BinaryLogicalOperatorNode) {
final BinaryLogicalOperatorNode binaryOperatorNode = (BinaryLogicalOperatorNode) expression;
final String operator = binaryOperatorNode.getOperator().toUpperCase();
final ValueNode leftValueNode = binaryOperatorNode.getLeftOperand();
final ValueNode rightValueNode = binaryOperatorNode.getRightOperand();
final Condition leftCondition = toQueryValue(recordDefinition, leftValueNode);
final Condition rightCondition = toQueryValue(recordDefinition, rightValueNode);
if ("AND".equals(operator)) {
return (V) new And(leftCondition, rightCondition);
} else if ("OR".equals(operator)) {
return (V) new Or(leftCondition, rightCondition);
} else {
throw new IllegalArgumentException("Binary logical operator " + operator + " not supported.");
}
} else if (expression instanceof BinaryOperatorNode) {
final BinaryOperatorNode binaryOperatorNode = (BinaryOperatorNode) expression;
final String operator = binaryOperatorNode.getOperator();
final ValueNode leftValueNode = binaryOperatorNode.getLeftOperand();
final ValueNode rightValueNode = binaryOperatorNode.getRightOperand();
if (SUPPORTED_BINARY_OPERATORS.contains(operator.toUpperCase())) {
final QueryValue leftCondition = toQueryValue(recordDefinition, leftValueNode);
QueryValue rightCondition = toQueryValue(recordDefinition, rightValueNode);
if (leftCondition instanceof Column) {
if (rightCondition instanceof Value) {
final Column column = (Column) leftCondition;
final String name = column.getName();
final Object value = ((Value) rightCondition).getValue();
if (value == null) {
throw new IllegalArgumentException("Values can't be null for " + operator + " use IS NULL or IS NOT NULL instead.");
} else if (recordDefinition != null) {
final FieldDefinition fieldDefinition = recordDefinition.getField(name);
final CodeTable codeTable = recordDefinition.getCodeTableByFieldName(name);
if (codeTable == null || fieldDefinition == recordDefinition.getIdField()) {
final Object convertedValue = fieldDefinition.toFieldValueException(value);
if (convertedValue == null) {
throw new IllegalArgumentException("Values can't be null for " + operator + " use IS NULL or IS NOT NULL instead.");
} else {
rightCondition = new Value(fieldDefinition, convertedValue);
}
} else {
Object id;
if (value instanceof String) {
final String string = (String) value;
final String[] values = string.split(":");
id = codeTable.getIdentifier((Object[]) values);
} else {
id = codeTable.getIdentifier(value);
}
if (id == null) {
throw new IllegalArgumentException(name + "='" + value + "' could not be found in the code table " + codeTable.getName());
} else {
rightCondition = new Value(fieldDefinition, id);
}
}
}
}
}
if (expression instanceof BinaryArithmeticOperatorNode) {
final QueryValue arithmaticCondition = Q.arithmatic(leftCondition, operator, rightCondition);
return (V) arithmaticCondition;
} else {
final Condition binaryCondition = Q.binary(leftCondition, operator, rightCondition);
return (V) binaryCondition;
}
} else {
throw new IllegalArgumentException("Unsupported binary operator " + operator);
}
} else if (expression instanceof ColumnReference) {
final ColumnReference column = (ColumnReference) expression;
String columnName = column.getColumnName();
columnName = columnName.replaceAll("\"", "");
if (recordDefinition == null) {
return (V) new Column(columnName);
} else {
final FieldDefinition fieldDefinition = recordDefinition.getField(columnName);
if (fieldDefinition == null) {
recordDefinition.getField(columnName);
throw new IllegalArgumentException("Invalid column name " + columnName);
} else {
return (V) new Column(fieldDefinition);
}
}
} else if (expression instanceof LikeEscapeOperatorNode) {
final LikeEscapeOperatorNode likeEscapeOperatorNode = (LikeEscapeOperatorNode) expression;
final ValueNode leftValueNode = likeEscapeOperatorNode.getReceiver();
final ValueNode rightValueNode = likeEscapeOperatorNode.getLeftOperand();
final QueryValue leftCondition = toQueryValue(recordDefinition, leftValueNode);
final QueryValue rightCondition = toQueryValue(recordDefinition, rightValueNode);
return (V) new ILike(leftCondition, rightCondition);
} else if (expression instanceof NotNode) {
final NotNode notNode = (NotNode) expression;
final ValueNode operand = notNode.getOperand();
final Condition condition = toQueryValue(recordDefinition, operand);
return (V) new Not(condition);
} else if (expression instanceof InListOperatorNode) {
final InListOperatorNode inListOperatorNode = (InListOperatorNode) expression;
final ValueNode leftOperand = inListOperatorNode.getLeftOperand();
final QueryValue leftCondition = toQueryValue(recordDefinition, leftOperand);
final List<QueryValue> conditions = new ArrayList<>();
final RowConstructorNode itemsList = inListOperatorNode.getRightOperandList();
for (final ValueNode itemValueNode : itemsList.getNodeList()) {
final QueryValue itemCondition = toQueryValue(recordDefinition, itemValueNode);
conditions.add(itemCondition);
}
return (V) new In(leftCondition, new CollectionValue(conditions));
} else if (expression instanceof IsNullNode) {
final IsNullNode isNullNode = (IsNullNode) expression;
final ValueNode operand = isNullNode.getOperand();
final QueryValue value = toQueryValue(recordDefinition, operand);
if (isNullNode.getNodeType() == NodeTypes.IS_NOT_NULL_NODE) {
return (V) new IsNotNull(value);
} else {
return (V) new IsNull(value);
}
// } else if (expression instanceof Parenthesis) {
// final Parenthesis parenthesis = (Parenthesis)expression;
// final ValueNode parenthesisValueNode = parenthesis.getExpression();
// final Condition condition = toCondition(parenthesisExpression);
// final ParenthesisCondition parenthesisCondition = new
// ParenthesisCondition(
// condition);
// if (parenthesis.isNot()) {
// return (V)Q.not(parenthesisCondition);
// } else {
// return (V)parenthesisCondition;
// }
} else if (expression instanceof RowConstructorNode) {
final RowConstructorNode rowConstructorNode = (RowConstructorNode) expression;
final ValueNodeList values = rowConstructorNode.getNodeList();
final ValueNode valueNode = values.get(0);
return (V) toQueryValue(recordDefinition, valueNode);
} else if (expression instanceof UserTypeConstantNode) {
final UserTypeConstantNode constant = (UserTypeConstantNode) expression;
final Object objectValue = constant.getObjectValue();
return (V) new Value(objectValue);
} else if (expression instanceof ConstantNode) {
final ConstantNode constant = (ConstantNode) expression;
final Object value = constant.getValue();
return (V) new Value(value);
} else if (expression instanceof SimpleStringOperatorNode) {
final SimpleStringOperatorNode operatorNode = (SimpleStringOperatorNode) expression;
final String functionName = operatorNode.getMethodName().toUpperCase();
final ValueNode operand = operatorNode.getOperand();
final QueryValue condition = toQueryValue(recordDefinition, operand);
return (V) new Function(functionName, condition);
} else if (expression instanceof CastNode) {
final CastNode castNode = (CastNode) expression;
final String typeName = castNode.getType().getSQLstring();
final ValueNode operand = castNode.getCastOperand();
final QueryValue condition = toQueryValue(recordDefinition, operand);
return (V) new Cast(condition, typeName);
} else if (expression instanceof JavaToSQLValueNode) {
final JavaToSQLValueNode node = (JavaToSQLValueNode) expression;
final JavaValueNode javaValueNode = node.getJavaValueNode();
if (javaValueNode instanceof StaticMethodCallNode) {
final StaticMethodCallNode methodNode = (StaticMethodCallNode) javaValueNode;
final List<QueryValue> parameters = new ArrayList<>();
final String methodName = methodNode.getMethodName();
for (final JavaValueNode parameter : methodNode.getMethodParameters()) {
if (parameter instanceof SQLToJavaValueNode) {
final SQLToJavaValueNode sqlNode = (SQLToJavaValueNode) parameter;
final QueryValue param = toQueryValue(recordDefinition, sqlNode.getSQLValueNode());
parameters.add(param);
}
}
if (methodName.equals("get_map_value")) {
return (V) new GetMapValue(parameters);
}
}
return null;
} else if (expression == null) {
return null;
} else {
throw new IllegalArgumentException("Unsupported expression" + expression.getClass() + " " + expression);
}
}
Aggregations