Search in sources :

Example 56 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class QueryWhereConditionField method toQueryValue.

@SuppressWarnings("unchecked")
private <V extends QueryValue> V toQueryValue(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)) {
            setInvalidMessage("Between operator must use a column name not: " + leftValueNode);
            return null;
        }
        if (!(betweenExpressionStart instanceof NumericConstantNode)) {
            setInvalidMessage("Between min value must be a number not: " + betweenExpressionStart);
            return null;
        }
        if (!(betweenExpressionEnd instanceof NumericConstantNode)) {
            setInvalidMessage("Between max value must be a number not: " + betweenExpressionEnd);
            return null;
        }
        final Column column = toQueryValue(leftValueNode);
        final Value min = toQueryValue(betweenExpressionStart);
        final Value max = toQueryValue(betweenExpressionEnd);
        final FieldDefinition fieldDefinition = this.recordDefinition.getField(column.getName());
        min.convert(fieldDefinition);
        max.convert(fieldDefinition);
        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 QueryValue leftValue = toQueryValue(leftValueNode);
        if (leftValue instanceof Condition) {
            final Condition leftCondition = (Condition) leftValue;
            final QueryValue rightValue = toQueryValue(rightValueNode);
            if (rightValue instanceof Condition) {
                final Condition rightCondition = (Condition) rightValue;
                if ("AND".equals(operator)) {
                    return (V) new And(leftCondition, rightCondition);
                } else if ("OR".equals(operator)) {
                    return (V) new Or(leftCondition, rightCondition);
                } else {
                    setInvalidMessage("Binary logical operator " + operator + " not supported.");
                    return null;
                }
            } else {
                setInvalidMessage("Right side of " + operator + " must be a condition (e.g. column_name = 'value') not: " + rightValue);
                return null;
            }
        } else {
            setInvalidMessage("Left side of " + operator + " must be a condition (e.g. column_name = 'value') not: " + leftValue);
            return null;
        }
    } 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 (QueryValue.SUPPORTED_BINARY_OPERATORS.contains(operator.toUpperCase())) {
            final QueryValue leftCondition = toQueryValue(leftValueNode);
            QueryValue rightCondition = toQueryValue(rightValueNode);
            if (leftCondition instanceof Column) {
                if (rightCondition instanceof Value) {
                    final Object value = ((Value) rightCondition).getValue();
                    if (value == null) {
                        setInvalidMessage("Values can't be null for " + operator + " use IS NULL or IS NOT NULL instead.");
                    } else {
                        final Column column = (Column) leftCondition;
                        final String name = column.getName();
                        final FieldDefinition fieldDefinition = this.recordDefinition.getField(name);
                        final CodeTable codeTable = this.recordDefinition.getCodeTableByFieldName(name);
                        if (codeTable == null || fieldDefinition == this.recordDefinition.getIdField()) {
                            try {
                                final Object convertedValue = fieldDefinition.toFieldValueException(value);
                                if (convertedValue == null) {
                                    setInvalidMessage("Values can't be null for " + operator + " use IS NULL or IS NOT NULL instead.");
                                    return null;
                                } else {
                                    rightCondition = new Value(fieldDefinition, convertedValue);
                                }
                            } catch (final Throwable t) {
                                setInvalidMessage(name + "='" + value + "' is not a valid " + fieldDefinition.getDataType().getValidationName());
                            }
                        } 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) {
                                setInvalidMessage(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 {
            setInvalidMessage("Unsupported binary operator " + operator);
        }
    } else if (expression instanceof ColumnReference) {
        final ColumnReference column = (ColumnReference) expression;
        String columnName = column.getColumnName();
        columnName = columnName.replaceAll("\"", "");
        final FieldDefinition fieldDefinition = this.recordDefinition.getField(columnName);
        if (fieldDefinition == null) {
            setInvalidMessage("Invalid field 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(leftValueNode);
        final QueryValue rightCondition = toQueryValue(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(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(leftOperand);
        final List<QueryValue> conditions = new ArrayList<>();
        final RowConstructorNode itemsList = inListOperatorNode.getRightOperandList();
        for (final ValueNode itemValueNode : itemsList.getNodeList()) {
            final QueryValue itemCondition = toQueryValue(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(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(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(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(operand);
        return (V) new Cast(condition, typeName);
    } else if (expression == null) {
        return null;
    } else {
        setInvalidMessage("Unsupported expression" + expression.getClass() + " " + expression);
    }
    return null;
}
Also used : Cast(com.revolsys.record.query.Cast) NotNode(com.akiban.sql.parser.NotNode) Or(com.revolsys.record.query.Or) In(com.revolsys.record.query.In) FieldDefinition(com.revolsys.record.schema.FieldDefinition) ArrayList(java.util.ArrayList) BinaryArithmeticOperatorNode(com.akiban.sql.parser.BinaryArithmeticOperatorNode) UserTypeConstantNode(com.akiban.sql.parser.UserTypeConstantNode) Function(com.revolsys.record.query.functions.Function) CollectionValue(com.revolsys.record.query.CollectionValue) IsNotNull(com.revolsys.record.query.IsNotNull) NumericConstantNode(com.akiban.sql.parser.NumericConstantNode) ConstantNode(com.akiban.sql.parser.ConstantNode) UserTypeConstantNode(com.akiban.sql.parser.UserTypeConstantNode) Column(com.revolsys.record.query.Column) ValueNodeList(com.akiban.sql.parser.ValueNodeList) NumericConstantNode(com.akiban.sql.parser.NumericConstantNode) SimpleStringOperatorNode(com.akiban.sql.parser.SimpleStringOperatorNode) InListOperatorNode(com.akiban.sql.parser.InListOperatorNode) BinaryOperatorNode(com.akiban.sql.parser.BinaryOperatorNode) BetweenOperatorNode(com.akiban.sql.parser.BetweenOperatorNode) Condition(com.revolsys.record.query.Condition) CodeTable(com.revolsys.record.code.CodeTable) BinaryLogicalOperatorNode(com.akiban.sql.parser.BinaryLogicalOperatorNode) Between(com.revolsys.record.query.Between) ILike(com.revolsys.record.query.ILike) QueryValue(com.revolsys.record.query.QueryValue) CastNode(com.akiban.sql.parser.CastNode) Not(com.revolsys.record.query.Not) IsNullNode(com.akiban.sql.parser.IsNullNode) And(com.revolsys.record.query.And) ValueNode(com.akiban.sql.parser.ValueNode) QueryValue(com.revolsys.record.query.QueryValue) CollectionValue(com.revolsys.record.query.CollectionValue) Value(com.revolsys.record.query.Value) IsNull(com.revolsys.record.query.IsNull) RowConstructorNode(com.akiban.sql.parser.RowConstructorNode) ColumnReference(com.akiban.sql.parser.ColumnReference) LikeEscapeOperatorNode(com.akiban.sql.parser.LikeEscapeOperatorNode)

Example 57 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class LayerRecordForm method setFieldValue.

public void setFieldValue(final String fieldName, Object value, final boolean validate) {
    final Object oldValue = getFieldValue(fieldName);
    final RecordDefinition recordDefinition = getRecordDefinition();
    if (recordDefinition != null) {
        try {
            final FieldDefinition field = recordDefinition.getField(fieldName);
            if (field != null) {
                value = field.toFieldValue(value);
            }
        } catch (final Throwable e) {
        }
    }
    this.fieldValues.put(fieldName, value);
    final JComponent field = (JComponent) getField(fieldName);
    boolean changed = Property.isChanged(oldValue, value);
    if (!changed) {
        final Object recordValue = this.record.getValue(fieldName);
        if (Property.isChanged(oldValue, recordValue)) {
            this.record.setValueByPath(fieldName, value);
            changed = true;
        }
    }
    SwingUtil.setFieldValue(field, value);
    if (changed) {
        if (validate) {
            validateFields(fieldName);
        } else {
            final Set<String> fieldsToValidate = this.fieldsToValidate.get();
            if (fieldsToValidate != null) {
                fieldsToValidate.add(fieldName);
            }
        }
    }
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition) JComponent(javax.swing.JComponent) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 58 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class LayerRecordForm method setRecordDefinition.

public void setRecordDefinition(final RecordDefinition recordDefinition) {
    this.recordDefinition = recordDefinition;
    setRecordStore(recordDefinition.getRecordStore());
    final String idFieldName = recordDefinition.getIdFieldName();
    if (Property.hasValue(idFieldName)) {
        this.readOnlyFieldNames.add(idFieldName);
    }
    for (final FieldDefinition field : recordDefinition.getFields()) {
        if (field.isRequired()) {
            final String name = field.getName();
            addRequiredFieldNames(name);
        }
    }
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition)

Example 59 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class LayerRecordForm method addToolBar.

protected ToolBar addToolBar(final AbstractRecordLayer layer) {
    this.toolBar = new ToolBar();
    add(this.toolBar, BorderLayout.NORTH);
    final RecordDefinition recordDefinition = getRecordDefinition();
    final FieldDefinition geometryField = recordDefinition.getGeometryField();
    final boolean hasGeometry = geometryField != null;
    final EnableCheck editable = new ObjectPropertyEnableCheck(this, "editable");
    if (layer != null) {
        final MenuFactory menuFactory = MenuFactory.findMenu(layer);
        if (menuFactory != null) {
            this.toolBar.addButtonTitleIcon("menu", "Layer Menu", "menu", () -> menuFactory.showMenu(layer, this, 10, 10));
        }
    }
    final EnableCheck deletableEnableCheck = editable.and(new ObjectPropertyEnableCheck(this, "deletable"));
    this.toolBar.addButton("record", "Delete Record", "table_row_delete", deletableEnableCheck, this::deleteRecord);
    // Cut, Copy Paste
    this.toolBar.addButton("dnd", "Copy Record", "page_copy", (EnableCheck) null, this::dataTransferCopy);
    if (hasGeometry) {
        this.toolBar.addButton("dnd", "Copy Geometry", "geometry_copy", (EnableCheck) null, this::copyGeometry);
    }
    this.toolBar.addButton("dnd", "Paste Record", "paste_plain", editable, this::dataTransferPaste);
    if (hasGeometry) {
        this.toolBar.addButton("dnd", "Paste Geometry", "geometry_paste", editable, this::pasteGeometry);
    }
    final EnableCheck canUndo = new ObjectPropertyEnableCheck(this.undoManager, "canUndo");
    final EnableCheck canRedo = new ObjectPropertyEnableCheck(this.undoManager, "canRedo");
    final EnableCheck modifiedOrDeleted = new ObjectPropertyEnableCheck(this, "modifiedOrDeleted");
    this.toolBar.addButton("changes", "Revert Record", "arrow_revert", modifiedOrDeleted, this::revertChanges);
    this.toolBar.addButton("changes", "Revert Empty Fields", "field_empty_revert", new ObjectPropertyEnableCheck(this, "hasModifiedEmptyFields"), this::revertEmptyFields);
    this.toolBar.addButton("changes", "Undo", "arrow_undo", canUndo, this.undoManager::undo);
    this.toolBar.addButton("changes", "Redo", "arrow_redo", canRedo, this.undoManager::redo);
    if (hasGeometry) {
        this.toolBar.addButtonTitleIcon("zoom", "Zoom to Record", "magnifier_zoom_selected", () -> {
            final LayerRecord record = getRecord();
            layer.zoomToRecord(record);
        });
        this.toolBar.addButtonTitleIcon("zoom", "Pan to Record", "pan_selected", () -> {
            final LayerRecord record = getRecord();
            final MapPanel mapPanel = layer.getMapPanel();
            if (mapPanel != null) {
                mapPanel.panToRecord(record);
            }
        });
    }
    // Geometry manipulation
    if (hasGeometry) {
        final DataType geometryDataType = geometryField.getDataType();
        if (geometryDataType == DataTypes.LINE_STRING || geometryDataType == DataTypes.MULTI_LINE_STRING) {
            if (DirectionalFields.getProperty(recordDefinition).hasDirectionalFields()) {
                this.toolBar.addButton("geometry", FLIP_RECORD_NAME, FLIP_RECORD_ICON, editable, this::flipRecordOrientation);
                this.toolBar.addButton("geometry", FLIP_LINE_ORIENTATION_NAME, FLIP_LINE_ORIENTATION_ICON, editable, this::flipLineOrientation);
                this.toolBar.addButton("geometry", FLIP_FIELDS_NAME, FLIP_FIELDS_ICON, editable, this::flipFields);
            } else {
                this.toolBar.addButton("geometry", "Flip Line Orientation", "flip_line", editable, this::flipLineOrientation);
            }
        }
    }
    return this.toolBar;
}
Also used : ObjectPropertyEnableCheck(com.revolsys.swing.action.enablecheck.ObjectPropertyEnableCheck) MapPanel(com.revolsys.swing.map.MapPanel) MenuFactory(com.revolsys.swing.menu.MenuFactory) FieldDefinition(com.revolsys.record.schema.FieldDefinition) ToolBar(com.revolsys.swing.toolbar.ToolBar) DataType(com.revolsys.datatype.DataType) EnableCheck(com.revolsys.swing.action.enablecheck.EnableCheck) ObjectPropertyEnableCheck(com.revolsys.swing.action.enablecheck.ObjectPropertyEnableCheck) LayerRecord(com.revolsys.swing.map.layer.record.LayerRecord) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 60 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class LayerRecordTableModel method isCellEditable.

@Override
public boolean isCellEditable(final int rowIndex, final int columnIndex) {
    if (columnIndex == 2) {
        if (this.form.get().isEditable()) {
            final String fieldName = getColumnFieldName(rowIndex);
            final RecordDefinition recordDefinition = getRecordDefinition();
            final FieldDefinition idField = recordDefinition.getIdField();
            if (idField != null) {
                if (recordDefinition.isIdField(fieldName)) {
                    if (this.record != null && this.record.getState() == RecordState.NEW) {
                        if (!Number.class.isAssignableFrom(idField.getTypeClass())) {
                            return true;
                        }
                    }
                    return false;
                }
            }
            if (recordDefinition.getGeometryFieldNames().contains(fieldName)) {
                return false;
            } else {
                return this.form.get().isEditable(fieldName);
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Aggregations

FieldDefinition (com.revolsys.record.schema.FieldDefinition)133 RecordDefinition (com.revolsys.record.schema.RecordDefinition)38 DataType (com.revolsys.datatype.DataType)32 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)23 JdbcFieldDefinition (com.revolsys.jdbc.field.JdbcFieldDefinition)19 PathName (com.revolsys.io.PathName)15 Record (com.revolsys.record.Record)15 ArrayList (java.util.ArrayList)15 Geometry (com.revolsys.geometry.model.Geometry)13 CodeTable (com.revolsys.record.code.CodeTable)9 Query (com.revolsys.record.query.Query)8 LineString (com.revolsys.geometry.model.LineString)7 ArrayRecord (com.revolsys.record.ArrayRecord)7 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)7 SQLException (java.sql.SQLException)7 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)4 IOException (java.io.IOException)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 BadLocationException (javax.swing.text.BadLocationException)4