Search in sources :

Example 6 with RecordState

use of com.revolsys.record.RecordState in project com.revolsys.open by revolsys.

the class JdbcRecordWriter method write.

@Override
public synchronized void write(final Record record) {
    try {
        final JdbcRecordDefinition recordDefinition = getRecordDefinition(record);
        final RecordStore recordStore = recordDefinition.getRecordStore();
        final RecordState state = record.getState();
        if (recordStore != this.recordStore) {
            if (state != RecordState.DELETED) {
                insert(recordDefinition, record);
            }
        } else {
            switch(state) {
                case NEW:
                    insert(recordDefinition, record);
                    break;
                case MODIFIED:
                    update(recordDefinition, record);
                    break;
                case PERSISTED:
                    // No action required
                    break;
                case DELETED:
                    delete(recordDefinition, record);
                    break;
                default:
                    throw new IllegalStateException("State not known");
            }
        }
    } catch (final RuntimeException e) {
        throw e;
    } catch (final Error e) {
        throw e;
    } catch (final BatchUpdateException e) {
        for (SQLException e1 = e.getNextException(); e1 != null; e1 = e1.getNextException()) {
            LOG.error("Unable to write", e1);
        }
        throw new RuntimeException("Unable to write", e);
    } catch (final Exception e) {
        throw new RuntimeException("Unable to write", e);
    }
}
Also used : SQLException(java.sql.SQLException) RecordStore(com.revolsys.record.schema.RecordStore) RecordState(com.revolsys.record.RecordState) DataAccessException(org.springframework.dao.DataAccessException) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) BatchUpdateException(java.sql.BatchUpdateException)

Example 7 with RecordState

use of com.revolsys.record.RecordState in project com.revolsys.open by revolsys.

the class ArrayLayerRecord method setValue.

@Override
protected boolean setValue(final FieldDefinition fieldDefinition, final Object value) {
    synchronized (getSync()) {
        boolean updated = false;
        final int fieldIndex = fieldDefinition.getIndex();
        final String fieldName = fieldDefinition.getName();
        final Object newValue = fieldDefinition.toFieldValue(value);
        final Object oldValue = getValue(fieldIndex);
        RecordState newState = null;
        if (!DataType.equal(oldValue, newValue)) {
            final AbstractRecordLayer layer = getLayer();
            final RecordState state = getState();
            switch(state) {
                case INITIALIZING:
                    // Allow modification on initialization
                    break;
                case NEW:
                    if (!layer.isCanAddRecords()) {
                        throw new IllegalStateException("Adding new records is not supported for layer " + layer);
                    }
                    break;
                case DELETED:
                    throw new IllegalStateException("Cannot edit a deleted record for layer " + layer);
                case PERSISTED:
                case MODIFIED:
                    if (layer.isCanEditRecords()) {
                        final Object originalValue = getOriginalValue(fieldName);
                        Map<String, Object> originalValues = this.originalValues;
                        synchronized (originalValues) {
                            if (fieldDefinition.equals(originalValue, newValue)) {
                                if (originalValues != EMPTY_ORIGINAL_VALUES) {
                                    originalValues.remove(fieldName);
                                    if (originalValues.isEmpty()) {
                                        originalValues = EMPTY_ORIGINAL_VALUES;
                                        newState = RecordState.PERSISTED;
                                    }
                                }
                            } else {
                                if (originalValues == EMPTY_ORIGINAL_VALUES) {
                                    originalValues = new HashMap<>();
                                }
                                originalValues.put(fieldName, originalValue);
                                if (RecordState.INITIALIZING != state) {
                                    newState = RecordState.MODIFIED;
                                }
                            }
                            this.originalValues = originalValues;
                        }
                    } else {
                        throw new IllegalStateException("Editing records is not supported for layer " + layer);
                    }
                    break;
            }
            updated |= super.setValue(fieldDefinition, newValue);
            if (newState != null) {
                setState(newState);
            }
            if (state != RecordState.INITIALIZING) {
                firePropertyChange(fieldName, oldValue, newValue);
                layer.updateRecordState(this);
            }
        }
        return updated;
    }
}
Also used : RecordState(com.revolsys.record.RecordState)

Example 8 with RecordState

use of com.revolsys.record.RecordState in project com.revolsys.open by revolsys.

the class ArrayLayerRecord method cancelChanges.

/**
 * Internal method to revert the records values to the original
 */
@Override
public final boolean cancelChanges() {
    boolean cancelled = false;
    synchronized (getSync()) {
        final Map<String, Object> originalValues = this.originalValues;
        RecordState state = getState();
        final AbstractRecordLayer layer = getLayer();
        try (BaseCloseable disabled = layer.eventsDisabled()) {
            if (originalValues != EMPTY_ORIGINAL_VALUES) {
                setState(RecordState.INITIALIZING);
                super.setValues(originalValues);
            }
            if (state == RecordState.MODIFIED || state == RecordState.DELETED) {
                state = RecordState.PERSISTED;
                cancelled = true;
            }
        } finally {
            setState(state);
        }
        this.originalValues = EMPTY_ORIGINAL_VALUES;
    }
    if (cancelled) {
        firePropertyChange(EVENT_RECORD_CHANGED, false, true);
    }
    return cancelled;
}
Also used : BaseCloseable(com.revolsys.io.BaseCloseable) RecordState(com.revolsys.record.RecordState)

Example 9 with RecordState

use of com.revolsys.record.RecordState in project com.revolsys.open by revolsys.

the class NewProxyLayerRecord method getRecordProxied.

@Override
protected LayerRecord getRecordProxied() {
    final AbstractRecordLayer layer = getLayer();
    synchronized (layer.getSync()) {
        if (this.record != null) {
            final RecordState state = this.record.getState();
            if (state == RecordState.PERSISTED) {
                this.identifier = this.record.getIdentifier();
                if (this.identifier != null) {
                    addProxiedRecordIdentifier(this.identifier);
                    this.record = removeProxiedRecord(this.record);
                }
            } else {
                return this.record;
            }
        }
        return super.getRecordProxied();
    }
}
Also used : RecordState(com.revolsys.record.RecordState)

Example 10 with RecordState

use of com.revolsys.record.RecordState in project com.revolsys.open by revolsys.

the class UnmodifiableRecordStoreLayerRecord method setState.

@Override
public RecordState setState(final RecordState state) {
    boolean setState = false;
    final RecordState currentState = getState();
    switch(state) {
        case DELETED:
            break;
        case INITIALIZING:
            if (currentState == RecordState.NEW || currentState == RecordState.INITIALIZING) {
                setState = true;
            }
            break;
        case MODIFIED:
            break;
        case NEW:
            if (currentState == RecordState.INITIALIZING) {
                setState = true;
            }
            break;
        case PERSISTED:
            if (currentState == RecordState.NEW || currentState == RecordState.INITIALIZING) {
                setState = true;
            }
            break;
        default:
            break;
    }
    if (setState) {
        return super.setState(state);
    } else {
        throw new UnsupportedOperationException("Cannot set record state=" + state + " (" + currentState + "):\t" + this);
    }
}
Also used : RecordState(com.revolsys.record.RecordState)

Aggregations

RecordState (com.revolsys.record.RecordState)10 RecordDefinition (com.revolsys.record.schema.RecordDefinition)2 Geometry (com.revolsys.geometry.model.Geometry)1 BaseCloseable (com.revolsys.io.BaseCloseable)1 Record (com.revolsys.record.Record)1 RecordStore (com.revolsys.record.schema.RecordStore)1 BaseDialog (com.revolsys.swing.component.BaseDialog)1 Field (com.revolsys.swing.field.Field)1 NumberTextField (com.revolsys.swing.field.NumberTextField)1 ObjectLabelField (com.revolsys.swing.field.ObjectLabelField)1 AbstractRecordLayer (com.revolsys.swing.map.layer.record.AbstractRecordLayer)1 LayerRecord (com.revolsys.swing.map.layer.record.LayerRecord)1 Window (java.awt.Window)1 BatchUpdateException (java.sql.BatchUpdateException)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 DataAccessException (org.springframework.dao.DataAccessException)1