Search in sources :

Example 1 with RecordChangeType

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);
    }
}
Also used : FormRecordSnapshotEntity(org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity) RecordChangeType(org.activityinfo.store.spi.RecordChangeType) ResourceId(org.activityinfo.model.resource.ResourceId) FormEntity(org.activityinfo.store.hrd.entity.FormEntity) FormClass(org.activityinfo.model.form.FormClass) FormRecordEntity(org.activityinfo.store.hrd.entity.FormRecordEntity) InvalidUpdateException(org.activityinfo.store.query.server.InvalidUpdateException)

Aggregations

FormClass (org.activityinfo.model.form.FormClass)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 FormEntity (org.activityinfo.store.hrd.entity.FormEntity)1 FormRecordEntity (org.activityinfo.store.hrd.entity.FormRecordEntity)1 FormRecordSnapshotEntity (org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity)1 InvalidUpdateException (org.activityinfo.store.query.server.InvalidUpdateException)1 RecordChangeType (org.activityinfo.store.spi.RecordChangeType)1