use of org.activityinfo.model.form.FormInstance 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.model.form.FormInstance in project activityinfo by bedatadriven.
the class SerialNumberTest method simpleTest.
@Test
public void simpleTest() {
TestDatabase database = harness.createDatabase();
ActivityInfoClient client = harness.client();
FormClass formClass = new FormClass(harness.newFormId());
formClass.setLabel("Test Form");
formClass.setDatabaseId(database.getId());
FormField partnerField = CuidAdapter.partnerField(formClass);
formClass.addElement(partnerField);
FormField fileNumberField = formClass.addField(harness.newFieldId()).setType(new SerialNumberType()).setLabel("Record Number");
FormField nameField = formClass.addField(harness.newFieldId()).setType(TextType.SIMPLE).setLabel("Name");
client.createForm(formClass);
FormInstance record1 = new FormInstance(harness.newRecordId(), formClass.getId());
record1.set(nameField.getId(), "Sue");
record1.set(partnerField.getId(), database.getDefaultPartner());
FormInstance record2 = new FormInstance(harness.newRecordId(), formClass.getId());
record2.set(nameField.getId(), "Bob");
record2.set(partnerField.getId(), database.getDefaultPartner());
client.createRecord(record1);
client.createRecord(record2);
record1 = client.getTypedRecord(formClass, record1.getId());
record2 = client.getTypedRecord(formClass, record2.getId());
assertThat(record1.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber(1)));
assertThat(record2.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber(2)));
}
use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class TestingFormQueryBuilder method execute.
@Override
public void execute() {
for (FormInstance record : records) {
for (CursorObserver<ResourceId> idObserver : idObservers) {
idObserver.onNext(record.getId());
}
for (CursorObserver<FieldValue> parentObserver : parentObservers) {
parentObserver.onNext(new ReferenceValue(new RecordRef(formClass.getParentFormId().get(), record.getParentRecordId())));
}
for (Map.Entry<ResourceId, CursorObserver<FieldValue>> field : fieldObservers.entries()) {
ResourceId fieldId = field.getKey();
FieldValue fieldValue = record.get(fieldId);
CursorObserver<FieldValue> observer = field.getValue();
observer.onNext(fieldValue);
}
}
for (CursorObserver<ResourceId> observer : idObservers) {
observer.done();
}
for (CursorObserver<FieldValue> parentObserver : parentObservers) {
parentObserver.done();
}
for (CursorObserver<FieldValue> observer : fieldObservers.values()) {
observer.done();
}
}
use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class TestingStorageProvider method addNew.
public RecordUpdate addNew(ResourceId formId) {
TestingFormStorage form = formMap.get(formId);
if (form == null) {
throw new RuntimeException("No such form: " + formId);
}
FormInstance newRecord = form.getGenerator().get();
RecordUpdate update = new RecordUpdate();
update.setFormId(formId);
update.setRecordId(newRecord.getRef().getRecordId());
for (Map.Entry<ResourceId, FieldValue> entry : newRecord.getFieldValueMap().entrySet()) {
update.setFieldValue(entry.getKey(), entry.getValue());
}
return update;
}
use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class PersistImportCommand method apply.
@Nullable
@Override
public Promise<Void> apply(Void input) {
final ImportModel model = commandExecutor.getImportModel();
final ResourceId formClassId = model.getFormTree().getRootFields().iterator().next().getDefiningFormClass().getId();
final List<FormInstance> toPersist = Lists.newArrayList();
final ValidatedRowTable validatedRowTable = model.getValidatedRowTable();
for (SourceRow row : model.getSource().getRows()) {
ValidatedRow validatedRow = validatedRowTable.getRow(row);
if (validatedRow.isValid()) {
// persist instance only if it's valid
// new instance per row
FormInstance newInstance = new FormInstance(CuidAdapter.newLegacyFormInstanceId(formClassId), formClassId);
for (FieldImporter importer : commandExecutor.getImporters()) {
importer.updateInstance(row, newInstance);
}
toPersist.add(newInstance);
}
}
SerialQueue queue = new SerialQueue(commandExecutor.getResourceLocator(), toPersist, monitor);
return queue.execute();
}
Aggregations