use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.
the class MySqlUpdateTest method updateLocation.
@Test
public void updateLocation() throws SQLException {
ResourceId recordId = CuidAdapter.cuid(LOCATION_DOMAIN, 1);
ResourceId formId = CuidAdapter.locationFormClass(1);
RecordUpdate update = new RecordUpdate();
update.setFormId(formId);
update.setRecordId(recordId);
update.setFieldValue(CuidAdapter.field(formId, CuidAdapter.NAME_FIELD), TextValue.valueOf("New Name"));
Updater updater = updater();
updater.executeChange(update);
newRequest();
FormStorage formStorage = catalog.getForm(formId).get();
FormRecord record = formStorage.get(recordId).get();
FormInstance typedRecord = FormInstance.toFormInstance(formStorage.getFormClass(), record);
GeoPoint point = (GeoPoint) typedRecord.get(CuidAdapter.field(formId, CuidAdapter.GEOMETRY_FIELD));
assertThat(point, not(nullValue()));
}
use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.
the class MySqlUpdateTest method createFormWithCalculationAndRelevance.
@Test
public void createFormWithCalculationAndRelevance() {
userId = 1;
KeyGenerator generator = new KeyGenerator();
int activityId = generator.generateInt();
FormClass formClass = new FormClass(CuidAdapter.activityFormClass(activityId));
formClass.setDatabaseId(1);
formClass.setLabel("New Form");
FormField numberField = new FormField(CuidAdapter.generateIndicatorId()).setType(new QuantityType("widgets")).setLabel("NUM").setRequired(true);
formClass.addElement(numberField);
FormField calculatedField = new FormField(CuidAdapter.generateIndicatorId()).setType(new CalculatedFieldType("1")).setLabel("Calculation").setRelevanceConditionExpression("NUM>42").setRequired(true);
formClass.addElement(calculatedField);
catalog.createOrUpdateFormSchema(formClass);
newRequest();
// Create two records
RecordTransactionBuilder tx = new RecordTransactionBuilder();
RecordUpdate site1 = tx.update(formClass.getId(), CuidAdapter.generateSiteCuid());
site1.setFieldValue(numberField.getId(), new Quantity(10));
site1.setFieldValue(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
RecordUpdate site2 = tx.update(formClass.getId(), CuidAdapter.generateSiteCuid());
site2.setFieldValue(numberField.getId(), new Quantity(60));
site2.setFieldValue(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
updater().execute(tx.build());
newRequest();
// Query results
QueryModel queryModel = new QueryModel(formClass.getId());
queryModel.selectResourceId();
queryModel.selectExpr("Num").as("num");
queryModel.selectExpr("Calculation").as("calc");
query(queryModel);
ColumnView num = columnSet.getColumnView("num");
ColumnView calculation = columnSet.getColumnView("calc");
assertThat(calculation.isMissing(0), equalTo(num.getDouble(0) <= 42));
assertThat(calculation.isMissing(1), equalTo(num.getDouble(1) <= 42));
}
use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.
the class MySqlUpdateTest method deleteSite.
@Test
public void deleteSite() {
RecordUpdate changeObject = new RecordUpdate();
changeObject.setRecordId("s0000000001");
changeObject.setFormId(activityFormClass(1).asString());
changeObject.setDeleted(true);
Updater updater = updater();
updater.executeChange(changeObject);
newRequest();
query(activityFormClass(1), "_id");
assertThat(column("_id"), hasValues("s0000000002", "s0000000003"));
}
use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.
the class FormInputViewModel method buildUpdate.
public RecordUpdate buildUpdate(Optional<RecordRef> parentRef) {
RecordUpdate update = new RecordUpdate();
update.setRecordId(inputModel.getRecordRef().getRecordId());
update.setFormId(inputModel.getRecordRef().getFormId());
if (parentRef.isPresent()) {
update.setParentRecordId(parentRef.get().getRecordId().asString());
}
for (FormTree.Node node : formTree.getRootFields()) {
FieldInput newInput = inputModel.get(node.getFieldId());
if (newInput.getState() == FieldInput.State.VALID) {
update.setFieldValue(node.getFieldId(), newInput.getValue());
} else if (existingValues.containsKey(node.getFieldId()) && newInput.getState() == FieldInput.State.EMPTY) {
update.setFieldValue(node.getFieldId().asString(), Json.createNull());
} else if (existingValues.containsKey(node.getFieldId()) && !relevant.contains(node.getFieldId())) {
update.setFieldValue(node.getFieldId().asString(), Json.createNull());
}
}
return update;
}
use of org.activityinfo.model.resource.RecordUpdate in project activityinfo by bedatadriven.
the class SubFormViewModel method buildUpdates.
public List<RecordUpdate> buildUpdates(RecordRef parentRef) {
List<RecordUpdate> updates = new ArrayList<>();
for (FormInputViewModel subRecord : subRecords) {
if (!subRecord.isPlaceholder()) {
updates.add(subRecord.buildUpdate(Optional.of(parentRef)));
}
}
for (RecordRef deletedRecord : deletedRecords) {
RecordUpdate update = new RecordUpdate();
update.setFormId(deletedRecord.getFormId());
update.setRecordId(deletedRecord.getRecordId());
update.setDeleted(true);
updates.add(update);
}
return updates;
}
Aggregations