use of org.activityinfo.store.spi.RecordChangeType in project activityinfo by bedatadriven.
the class CreateOrUpdateRecord method vrun.
@Override
public void vrun() {
FormEntity rootEntity = ofy().load().key(FormEntity.key(formId)).safe();
FormClass formClass = ofy().load().key(FormSchemaEntity.key(formId)).safe().readFormClass();
long currentVersion = rootEntity.getVersion();
long newVersion = currentVersion + 1;
rootEntity.setVersion(newVersion);
FormRecordEntity existingEntity = ofy().load().key(FormRecordEntity.key(formClass, update.getRecordId())).now();
FormRecordEntity updated;
RecordChangeType changeType;
if (existingEntity != null) {
updated = existingEntity;
changeType = update.isDeleted() ? RecordChangeType.DELETED : RecordChangeType.UPDATED;
} else {
updated = new FormRecordEntity(formId, update.getRecordId());
changeType = RecordChangeType.CREATED;
if (update.isDeleted()) {
throw new InvalidUpdateException("Creation of entity with deleted flag is not allowed.");
}
if (formClass.getParentFormId().isPresent()) {
ResourceId parentId = update.getParentId();
if (parentId == null) {
throw new InvalidUpdateException("@parent is required for subform submissions");
}
updated.setParentRecordId(parentId);
}
}
updated.setVersion(newVersion);
updated.setSchemaVersion(rootEntity.getSchemaVersion());
updated.setFieldValues(formClass, update.getChangedFieldValues());
// Store a copy as a snapshot
FormRecordSnapshotEntity snapshotEntity = new FormRecordSnapshotEntity(update.getUserId(), changeType, updated);
if (update.isDeleted()) {
ofy().save().entities(rootEntity, snapshotEntity);
ofy().delete().entities(updated);
} else {
ofy().save().entities(rootEntity, updated, snapshotEntity);
}
}
Aggregations