use of org.activityinfo.store.hrd.entity.FormEntity in project activityinfo by bedatadriven.
the class AuditLogWriter method writeForm.
public void writeForm(FormStorageProvider catalog, ResourceId formId) throws IOException {
FormStorage formStorage = catalog.getForm(formId).get();
FormClass formClass = formStorage.getFormClass();
Key<FormEntity> parentKey = FormEntity.key(formId);
Query<FormRecordSnapshotEntity> query = Hrd.ofy().load().type(FormRecordSnapshotEntity.class).ancestor(parentKey);
for (FormRecordSnapshotEntity snapshot : query) {
User user;
try {
user = userCache.get((int) snapshot.getUserId());
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
csv.writeLine(formatTime(snapshot.getTime()), snapshot.getType().name(), user.getEmail(), user.getName(), databaseId(), db.getName(), formId.asString(), formClass.getLabel(), // Field ID
"", // Field Name
"", snapshot.getRecordId().asString(), partner());
}
}
use of org.activityinfo.store.hrd.entity.FormEntity in project activityinfo by bedatadriven.
the class CreateOrUpdateForm method create.
private void create() {
FormEntity rootEntity = new FormEntity();
rootEntity.setId(formClass.getId());
rootEntity.setVersion(1);
rootEntity.setSchemaVersion(1);
FormSchemaEntity formClassEntity = new FormSchemaEntity(formClass);
formClassEntity.setSchemaVersion(1);
ofy().save().entities(rootEntity, formClassEntity);
}
use of org.activityinfo.store.hrd.entity.FormEntity in project activityinfo by bedatadriven.
the class CreateOrUpdateForm method update.
private void update(FormSchemaEntity formClassEntity) {
FormEntity rootEntity = ofy().load().key(FormEntity.key(formClass)).safe();
// Increment the version counter
long newVersion = rootEntity.getVersion() + 1;
rootEntity.setVersion(newVersion);
rootEntity.setSchemaVersion(newVersion);
// Update the schema
formClassEntity.setSchema(formClass);
formClassEntity.setSchemaVersion(newVersion);
ofy().save().entities(rootEntity, formClassEntity);
}
use of org.activityinfo.store.hrd.entity.FormEntity 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);
}
}
use of org.activityinfo.store.hrd.entity.FormEntity in project activityinfo by bedatadriven.
the class QueryVersions method run.
@Override
public List<RecordVersion> run() {
Query<FormRecordSnapshotEntity> query;
if (recordId != null) {
Key<FormRecordEntity> recordKey = FormRecordEntity.key(formClass, recordId);
query = ofy().load().type(FormRecordSnapshotEntity.class).ancestor(recordKey);
} else {
Key<FormEntity> rootKey = FormEntity.key(formClass);
query = ofy().load().type(FormRecordSnapshotEntity.class).ancestor(rootKey).filter("parentRecordId", parentRecordId.asString());
}
List<RecordVersion> versions = new ArrayList<>();
for (FormRecordSnapshotEntity snapshot : query.iterable()) {
RecordVersion version = new RecordVersion();
version.setRecordId(snapshot.getRecordId());
version.setVersion(snapshot.getVersion());
version.setUserId(snapshot.getUserId());
version.setTime(snapshot.getTime().getTime());
version.setType(snapshot.getType());
if (formClass.isSubForm()) {
version.setSubformKind(formClass.getSubFormKind());
version.setSubformKey(subformKey(snapshot));
}
version.getValues().putAll(snapshot.getRecord().toFieldValueMap(formClass));
versions.add(version);
}
return versions;
}
Aggregations