Search in sources :

Example 1 with InvalidUpdateException

use of org.activityinfo.store.query.server.InvalidUpdateException in project activityinfo by bedatadriven.

the class RootResource method update.

@POST
@Path("/update")
@Consumes(MediaType.APPLICATION_JSON)
public Response update(String json) {
    final JsonValue jsonElement = Json.parse(json);
    Updater updater = backend.newUpdater();
    try {
        updater.execute(jsonElement);
    } catch (InvalidUpdateException e) {
        throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build());
    }
    return Response.ok().build();
}
Also used : Updater(org.activityinfo.store.query.server.Updater) JsonValue(org.activityinfo.json.JsonValue) InvalidUpdateException(org.activityinfo.store.query.server.InvalidUpdateException)

Example 2 with InvalidUpdateException

use of org.activityinfo.store.query.server.InvalidUpdateException 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

InvalidUpdateException (org.activityinfo.store.query.server.InvalidUpdateException)2 JsonValue (org.activityinfo.json.JsonValue)1 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 Updater (org.activityinfo.store.query.server.Updater)1 RecordChangeType (org.activityinfo.store.spi.RecordChangeType)1