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);
}
}
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;
}
}
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;
}
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();
}
}
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);
}
}
Aggregations