use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.
the class MySqlUpdateTest method createFormWithSerialNumber.
@Test
public void createFormWithSerialNumber() {
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 serialNumField = new FormField(CuidAdapter.generateIndicatorId()).setType(new SerialNumberType()).setLabel("NUM").setRequired(true);
formClass.addElement(serialNumField);
FormField nameField = new FormField(CuidAdapter.generateIndicatorId()).setType(TextType.SIMPLE).setLabel("Name").setRequired(true);
formClass.addElement(nameField);
catalog.createOrUpdateFormSchema(formClass);
newRequest();
ResourceId siteId = CuidAdapter.generateSiteCuid();
// Create the record
FormInstance creation = new FormInstance(siteId, formClass.getId());
creation.set(nameField.getId(), TextValue.valueOf("Bob"));
creation.set(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
executeUpdate(creation);
newRequest();
// Verify that the record has been created
FormInstance created = FormInstance.toFormInstance(formClass, catalog.getForm(formClass.getId()).get().get(siteId).get());
assertThat(created.get(nameField.getId()), equalTo((FieldValue) TextValue.valueOf("Bob")));
assertThat(created.get(serialNumField.getId()), equalTo((FieldValue) new SerialNumber(1)));
newRequest();
// Now update the record's name
JsonValue fieldValues = createObject();
fieldValues.put(nameField.getName(), TextValue.valueOf("Sue").toJson());
// the UI may send null values
fieldValues.put(serialNumField.getName(), Json.createNull());
JsonValue update = createObject();
update.put("fieldValues", fieldValues);
updater().execute(formClass.getId(), siteId, update);
newRequest();
// Finally verify that the serial number is unchanged
FormInstance updated = FormInstance.toFormInstance(formClass, catalog.getForm(formClass.getId()).get().get(siteId).get());
assertThat(updated.get(nameField.getId()), equalTo((FieldValue) TextValue.valueOf("Sue")));
assertThat(updated.get(serialNumField.getId()), equalTo((FieldValue) new SerialNumber(1)));
}
use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.
the class MySqlUpdateTest method addNewAttributes.
@Test
public void addNewAttributes() {
KeyGenerator generator = new KeyGenerator();
int activityId = generator.generateInt();
EnumType enumType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "A"), new EnumItem(EnumItem.generateId(), "B"));
FormField selectField = new FormField(ResourceId.generateFieldId(EnumType.TYPE_CLASS)).setType(enumType).setLabel("Select");
FormClass formClass = new FormClass(CuidAdapter.activityFormClass(activityId));
formClass.setDatabaseId(1);
formClass.setLabel("New Form");
formClass.addElement(selectField);
catalog.createOrUpdateFormSchema(formClass);
System.out.println("Created activity " + activityId);
// Now change the enum items
EnumType updatedType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "C"), new EnumItem(EnumItem.generateId(), "D"));
selectField.setType(updatedType);
newRequest();
catalog.createOrUpdateFormSchema(formClass);
newRequest();
// Now try to save a new instance with the value
FieldValue valueC = new EnumValue(updatedType.getValues().get(0).getId());
FormInstance newRecord = new FormInstance(CuidAdapter.generateSiteCuid(), formClass.getId());
newRecord.set(selectField.getId(), new EnumValue(updatedType.getValues().get(0).getId()));
newRecord.set(CuidAdapter.partnerField(activityId), CuidAdapter.partnerRef(1, 1));
executeUpdate(newRecord);
// Ensure that the select field has been saved
FormRecord saved = catalog.getForm(formClass.getId()).get().get(newRecord.getId()).get();
FormInstance savedInstance = FormInstance.toFormInstance(formClass, saved);
assertThat(savedInstance.get(selectField.getId()), equalTo(valueC));
}
use of org.activityinfo.model.form.FormField 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.form.FormField in project activityinfo by bedatadriven.
the class ActivityLoader method patchDeserializedFormClass.
/**
* Apply any updates to the serialized FormClass that might be required to do changes in
* ActivityInfo.
*/
private static FormClass patchDeserializedFormClass(Activity activity, FormClass formClass) {
// Ensure that all forms have the database id
formClass.setDatabaseId(activity.getDatabaseId());
// Some partner fields have been stored to the JSON as pointing to the wrong database,
// either because there was a bug in the past or because the databaseId was manually updated
// in the activity table without a corresponding change to the formClass field.
ResourceId partnerFieldId = CuidAdapter.partnerField(activity.getId());
ReferenceType expectedPartnerType = ReferenceType.single(CuidAdapter.partnerFormId(activity.getDatabaseId()));
Optional<FormField> partnerField = formClass.getFieldIfPresent(partnerFieldId);
if (!partnerField.isPresent()) {
FormField newPartnerField = new FormField(partnerFieldId);
newPartnerField.setType(expectedPartnerType);
newPartnerField.setVisible(true);
newPartnerField.setRequired(true);
newPartnerField.setLabel("Partner");
formClass.addElement(newPartnerField);
} else {
partnerField.get().setType(expectedPartnerType);
}
// The (classic) pivot table components rely on a fixed fields with date1 and date2
ResourceId startDateId = CuidAdapter.field(formClass.getId(), CuidAdapter.START_DATE_FIELD);
ResourceId endDateId = CuidAdapter.field(formClass.getId(), CuidAdapter.END_DATE_FIELD);
for (FormField field : formClass.getFields()) {
if (field.getId().equals(startDateId) && Strings.isNullOrEmpty(field.getCode())) {
field.setCode("date1");
}
if (field.getId().equals(endDateId) && Strings.isNullOrEmpty(field.getCode())) {
field.setCode("date2");
}
}
return formClass;
}
use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.
the class ActivityLoader method addField.
private void addField(Activity activity, ResultSet rs, Map<Integer, List<EnumItem>> attributes) throws SQLException {
int id = rs.getInt("id");
FormField formField;
if (rs.getString("Type").equals("ENUM")) {
formField = new FormField(CuidAdapter.attributeGroupField(id));
} else {
formField = new FormField(CuidAdapter.indicatorField(id));
}
formField.setLabel(rs.getString("Name"));
formField.setRequired(getMandatory(rs));
formField.setVisible(getVisible(rs));
formField.setDescription(rs.getString("Description"));
formField.setCode(rs.getString("code"));
if (rs.getBoolean("ca") && rs.getString("expr") != null) {
formField.setType(new CalculatedFieldType(rs.getString("expr")));
} else {
switch(rs.getString("Type")) {
default:
case "QUANTITY":
formField.setType(new QuantityType().setUnits(rs.getString("units")).setAggregation(rs.getInt("Aggregation")));
break;
case "BARCODE":
formField.setType(BarcodeType.INSTANCE);
break;
case "FREE_TEXT":
formField.setType(TextType.SIMPLE);
break;
case "NARRATIVE":
formField.setType(NarrativeType.INSTANCE);
break;
case "ENUM":
formField.setType(createEnumType(rs, attributes));
break;
}
}
ActivityField field = new ActivityField(id, rs.getString("category"), formField, rs.getInt("sortOrder"));
field.sortOrder = rs.getInt("sortOrder");
activity.fields.add(field);
}
Aggregations