use of org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity 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;
}
use of org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity in project activityinfo by bedatadriven.
the class ProjectMigrator method map.
@Override
public void map(final Integer databaseId) {
try (MySqlQueryExecutor executor = new MySqlQueryExecutor()) {
ProjectTable table = new ProjectTable(new DatabaseCacheImpl(executor));
final TableMapping mapping = table.getMapping(executor, CuidAdapter.projectFormClass(databaseId));
final ResourceId formId = mapping.getFormClass().getId();
Hrd.run(new Work<Void>() {
@Override
public Void run() {
RecordCursor recordBuilder = new RecordCursor(mapping, executor);
Iterator<FormInstance> it = recordBuilder.execute();
// Only create if there is one or more projects....
if (!it.hasNext()) {
return null;
}
final List<Object> toSave = new ArrayList<>();
FormEntity formEntity = new FormEntity();
formEntity.setId(formId);
formEntity.setSchemaVersion(1L);
formEntity.setVersion(mapping.getVersion());
toSave.add(formEntity);
FormSchemaEntity schemaEntity = new FormSchemaEntity(mapping.getFormClass());
schemaEntity.setSchemaVersion(1L);
schemaEntity.setSchema(mapping.getFormClass());
toSave.add(schemaEntity);
Date updateTime = new Date();
long userId = -1L;
while (it.hasNext()) {
FormInstance record = it.next();
FormRecordEntity recordEntity = new FormRecordEntity(formId, record.getId());
recordEntity.setFieldValues(mapping.getFormClass(), record.getFieldValueMap());
recordEntity.setSchemaVersion(1L);
recordEntity.setVersion(1L);
FormRecordSnapshotEntity snapshot = new FormRecordSnapshotEntity(userId, RecordChangeType.CREATED, recordEntity);
snapshot.setTime(updateTime);
snapshot.setMigrated(true);
toSave.add(record);
toSave.add(snapshot);
}
Hrd.ofy().save().entities(toSave).now();
return null;
}
});
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
use of org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity in project activityinfo by bedatadriven.
the class HrdFormStorage method getVersionRange.
@Override
public FormSyncSet getVersionRange(long localVersion, long toVersion, Predicate<ResourceId> visibilityPredicate) {
Query<FormRecordSnapshotEntity> query = ofy().load().type(FormRecordSnapshotEntity.class).ancestor(FormEntity.key(formClass));
if (localVersion > 0) {
query = query.filter("version >", localVersion);
}
SyncSetBuilder builder = new SyncSetBuilder(getFormClass(), localVersion, visibilityPredicate);
QueryResultIterator<FormRecordSnapshotEntity> it = query.iterator();
while (it.hasNext()) {
FormRecordSnapshotEntity snapshot = it.next();
builder.add(snapshot);
}
return builder.build();
}
Aggregations