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