Search in sources :

Example 96 with FormField

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)));
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) SerialNumberType(org.activityinfo.model.type.SerialNumberType) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Example 97 with FormField

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));
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) FormClass(org.activityinfo.model.form.FormClass) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) FieldValue(org.activityinfo.model.type.FieldValue) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) FormInstance(org.activityinfo.model.form.FormInstance) FormRecord(org.activityinfo.model.form.FormRecord) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Example 98 with FormField

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));
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) RecordUpdate(org.activityinfo.model.resource.RecordUpdate) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) ColumnView(org.activityinfo.model.query.ColumnView) Quantity(org.activityinfo.model.type.number.Quantity) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) QueryModel(org.activityinfo.model.query.QueryModel) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Example 99 with FormField

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;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType)

Example 100 with FormField

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);
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) QuantityType(org.activityinfo.model.type.number.QuantityType) FormField(org.activityinfo.model.form.FormField)

Aggregations

FormField (org.activityinfo.model.form.FormField)119 FormClass (org.activityinfo.model.form.FormClass)48 ResourceId (org.activityinfo.model.resource.ResourceId)32 Test (org.junit.Test)30 QuantityType (org.activityinfo.model.type.number.QuantityType)18 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)17 ReferenceType (org.activityinfo.model.type.ReferenceType)16 EnumType (org.activityinfo.model.type.enumerated.EnumType)14 FieldValue (org.activityinfo.model.type.FieldValue)12 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)11 JsonValue (org.activityinfo.json.JsonValue)9 FormTree (org.activityinfo.model.formTree.FormTree)9 FormInstance (org.activityinfo.model.form.FormInstance)8 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)8 ColumnSet (org.activityinfo.model.query.ColumnSet)6 QueryModel (org.activityinfo.model.query.QueryModel)6 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)5 Quantity (org.activityinfo.model.type.number.Quantity)5 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)5 ArrayList (java.util.ArrayList)4