Search in sources :

Example 41 with FieldValue

use of org.activityinfo.model.type.FieldValue 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 42 with FieldValue

use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.

the class MySqlCatalogTest method singleSiteWithBoundLocation.

@Test
public void singleSiteWithBoundLocation() {
    FormStorage siteStorage = catalog.getForm(CuidAdapter.activityFormClass(4)).get();
    FormRecord siteRecord = siteStorage.get(CuidAdapter.cuid(CuidAdapter.SITE_DOMAIN, 6)).get();
    FormInstance site = FormInstance.toFormInstance(siteStorage.getFormClass(), siteRecord);
    FieldValue location = site.get(CuidAdapter.locationField(4));
}
Also used : VersionedFormStorage(org.activityinfo.store.spi.VersionedFormStorage) FormStorage(org.activityinfo.store.spi.FormStorage) FieldValue(org.activityinfo.model.type.FieldValue) Test(org.junit.Test)

Example 43 with FieldValue

use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.

the class TableMappingBuilder method addGeoAreaField.

public void addGeoAreaField(FormField field) {
    add(new FieldMapping(field, Arrays.asList("x1", "y1", "x2", "y2"), new FieldValueConverter() {

        @Override
        public FieldValue toFieldValue(ResultSet rs, int index) throws SQLException {
            double x1 = rs.getDouble(index);
            if (rs.wasNull()) {
                return null;
            }
            double y1 = rs.getDouble(index + 1);
            if (rs.wasNull()) {
                return null;
            }
            double x2 = rs.getDouble(index + 2);
            if (rs.wasNull()) {
                return null;
            }
            double y2 = rs.getDouble(index + 3);
            if (rs.wasNull()) {
                return null;
            }
            return new GeoArea(Extents.create(x1, y1, x2, y2), "FIXME");
        }

        @Override
        public Collection<? extends Object> toParameters(FieldValue value) {
            GeoArea area = (GeoArea) value;
            Extents bbox = area.getEnvelope();
            return Arrays.asList(bbox.getX1(), bbox.getY1(), bbox.getX2(), bbox.getY2());
        }
    }));
}
Also used : GeoArea(org.activityinfo.model.type.geo.GeoArea) ResultSet(java.sql.ResultSet) FieldValue(org.activityinfo.model.type.FieldValue) Extents(org.activityinfo.model.type.geo.Extents)

Example 44 with FieldValue

use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.

the class BoundLocationBuilder method executeSimpleQuery.

/**
 * Maps each location reference to the corresponding admin entity reference. We only look at the
 * one admin level that is set as the current location type of the activity.
 *
 * If there are references to old location types, we just use the locationadminlink table to map
 * them to a reference in the new bound admin level.
 */
private void executeSimpleQuery(QueryExecutor executor) throws SQLException {
    ResourceId referenceFormId = CuidAdapter.adminLevelFormClass(activity.getAdminLevelId());
    String sql = "SELECT s.siteId, k.adminEntityId " + "FROM site s " + "LEFT JOIN locationadminlink k ON (s.locationId = k.locationId AND k.adminLevelId = " + activity.getAdminLevelId() + ") " + "WHERE s.deleted = 0 AND s.activityId = " + activity.getId();
    if (siteId != null) {
        sql += " AND s.siteId=" + siteId;
    }
    sql += " ORDER BY s.siteId";
    System.out.println(sql);
    int lastSiteId = -1;
    try (ResultSet rs = executor.query(sql)) {
        while (rs.next()) {
            int siteId = rs.getInt(1);
            if (siteId != lastSiteId) {
                int adminEntityId = rs.getInt(2);
                if (rs.wasNull()) {
                    emit(null);
                } else {
                    emit(new ReferenceValue(new RecordRef(referenceFormId, CuidAdapter.entity(adminEntityId))));
                }
            }
            lastSiteId = siteId;
        }
    }
    for (CursorObserver<FieldValue> observer : observers) {
        observer.done();
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceValue(org.activityinfo.model.type.ReferenceValue) ResultSet(java.sql.ResultSet) RecordRef(org.activityinfo.model.type.RecordRef) FieldValue(org.activityinfo.model.type.FieldValue)

Example 45 with FieldValue

use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.

the class FormInputViewModelBuilder method computeDirty.

private boolean computeDirty(boolean placeholder, Map<ResourceId, FieldValue> existingValues, FormInstance currentValues) {
    if (placeholder) {
        // the user themselves hasn't entered any information.
        return false;
    }
    for (FormTree.Node node : formTree.getRootFields()) {
        FieldValue originalValue = existingValues.get(node.getFieldId());
        FieldValue currentValue = currentValues.get(node.getFieldId());
        if (!Objects.equals(originalValue, currentValue)) {
            return true;
        }
    }
    return false;
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) FieldValue(org.activityinfo.model.type.FieldValue)

Aggregations

FieldValue (org.activityinfo.model.type.FieldValue)49 ResourceId (org.activityinfo.model.resource.ResourceId)16 FormField (org.activityinfo.model.form.FormField)13 Test (org.junit.Test)12 FormInstance (org.activityinfo.model.form.FormInstance)10 Quantity (org.activityinfo.model.type.number.Quantity)9 JsonValue (org.activityinfo.json.JsonValue)8 FormClass (org.activityinfo.model.form.FormClass)8 ReferenceValue (org.activityinfo.model.type.ReferenceValue)7 RecordRef (org.activityinfo.model.type.RecordRef)6 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 SerialNumberType (org.activityinfo.model.type.SerialNumberType)5 BooleanFieldValue (org.activityinfo.model.type.primitive.BooleanFieldValue)5 SerialNumber (org.activityinfo.model.type.SerialNumber)4 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)4 LocalDate (org.activityinfo.model.type.time.LocalDate)4 JsonMappingException (org.activityinfo.json.JsonMappingException)3 FormulaNode (org.activityinfo.model.formula.FormulaNode)3