Search in sources :

Example 21 with FieldValue

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

the class BoundLocationBuilder method executeMixedQuery.

/**
 * Generates a list of references for a location field that references a mix of bound location types
 * and non-bound location types.
 */
private void executeMixedQuery(QueryExecutor executor) throws SQLException {
    String sql = "SELECT " + // (1)
    "s.siteId, " + // (2)
    "s.locationId, " + // (3)
    "t.locationTypeId, " + // (4)
    "t.boundAdminLevelId, " + // (5)
    "k.adminLevelId, " + // (6)
    "k.adminEntityId " + "FROM site s " + "LEFT JOIN location g ON (s.locationId = g.locationId) " + "LEFT JOIN locationtype t ON (g.locationTypeId = t.locationTypeId) " + "LEFT JOIN locationadminlink k ON (s.locationId = k.locationId) " + "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;
    boolean foundEntry = false;
    try (ResultSet rs = executor.query(sql)) {
        while (rs.next()) {
            int siteId = rs.getInt(1);
            if (lastSiteId > 0 && lastSiteId != siteId) {
                if (!foundEntry) {
                    emit(null);
                }
                foundEntry = false;
            }
            int locationId = rs.getInt(2);
            int locationTypeId = rs.getInt(3);
            int boundAdminLevelId = rs.getInt(4);
            if (rs.wasNull()) {
                // if we haven't already
                if (siteId != lastSiteId) {
                    foundEntry = true;
                    emit(new ReferenceValue(new RecordRef(CuidAdapter.locationFormClass(locationTypeId), CuidAdapter.locationInstanceId(locationId))));
                }
            } else {
                int adminLevelId = rs.getInt(5);
                if (boundAdminLevelId == adminLevelId) {
                    int adminEntityId = rs.getInt(6);
                    foundEntry = true;
                    emit(new ReferenceValue(new RecordRef(CuidAdapter.adminLevelFormClass(adminLevelId), CuidAdapter.entity(adminEntityId))));
                }
            }
            lastSiteId = siteId;
        }
    }
    if (lastSiteId != -1 && !foundEntry) {
        emit(null);
    }
    for (CursorObserver<FieldValue> observer : observers) {
        observer.done();
    }
}
Also used : ReferenceValue(org.activityinfo.model.type.ReferenceValue) ResultSet(java.sql.ResultSet) RecordRef(org.activityinfo.model.type.RecordRef) FieldValue(org.activityinfo.model.type.FieldValue)

Example 22 with FieldValue

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

the class TableMappingBuilder method addGeoPoint.

public void addGeoPoint(FormField field) {
    add(new FieldMapping(field, Arrays.asList("x", "y"), new FieldValueConverter() {

        @Override
        public FieldValue toFieldValue(ResultSet rs, int index) throws SQLException {
            double lat = rs.getDouble(index + 1);
            if (rs.wasNull()) {
                return null;
            }
            double lon = rs.getDouble(index);
            if (rs.wasNull()) {
                return null;
            }
            return new GeoPoint(lat, lon);
        }

        @Override
        public Collection<Double> toParameters(FieldValue value) {
            GeoPoint pointValue = (GeoPoint) value;
            return Arrays.asList(pointValue.getLongitude(), pointValue.getLatitude());
        }
    }));
}
Also used : GeoPoint(org.activityinfo.model.type.geo.GeoPoint) ResultSet(java.sql.ResultSet) FieldValue(org.activityinfo.model.type.FieldValue)

Example 23 with FieldValue

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

the class SelectionViewModel method evalPermission.

private static boolean evalPermission(FormMetadata form, FormInstance record, Operation operation) {
    if (!form.getPermissions().isAllowed(operation)) {
        return false;
    }
    if (!form.getPermissions().isFiltered(operation)) {
        return true;
    }
    String filter = form.getPermissions().getFilter(operation);
    try {
        FormEvalContext context = new FormEvalContext(form.getSchema(), record);
        FormulaNode formula = FormulaParser.parse(filter);
        FieldValue result = formula.evaluate(context);
        return result == BooleanFieldValue.TRUE;
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to evaluate permission filter '" + filter + "'", e);
        return false;
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) FieldValue(org.activityinfo.model.type.FieldValue) BooleanFieldValue(org.activityinfo.model.type.primitive.BooleanFieldValue) FormEvalContext(org.activityinfo.model.form.FormEvalContext)

Example 24 with FieldValue

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

the class FormInstance method toJsonObject.

public JsonValue toJsonObject() {
    JsonValue fields = createObject();
    for (Map.Entry<ResourceId, FieldValue> entry : fieldMap.entrySet()) {
        if (entry.getValue() != null) {
            fields.put(entry.getKey().asString(), entry.getValue().toJson());
        }
    }
    JsonValue object = createObject();
    object.put("formId", getFormId().asString());
    object.put("recordId", getId().asString());
    object.put("fieldValues", fields);
    return object;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) JsonValue(org.activityinfo.json.JsonValue) FieldValue(org.activityinfo.model.type.FieldValue) BooleanFieldValue(org.activityinfo.model.type.primitive.BooleanFieldValue) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with FieldValue

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

the class HrdQueryColumnBuilder method execute.

@Override
public void execute() {
    Query<FormRecordEntity> query = ofy().load().type(FormRecordEntity.class).ancestor(FormEntity.key(formClass));
    for (FormRecordEntity entity : query.iterable()) {
        for (CursorObserver<ResourceId> idObserver : idObservers) {
            idObserver.onNext(entity.getRecordId());
        }
        for (FieldObserver fieldObserver : fieldObservers) {
            fieldObserver.onNext(entity.getFieldValues());
        }
        if (parentFieldObservers != null) {
            ResourceId parentRecordId = ResourceId.valueOf(entity.getParentRecordId());
            RecordRef parentRef = new RecordRef(formClass.getParentFormId().get(), parentRecordId);
            ReferenceValue parent = new ReferenceValue(parentRef);
            for (CursorObserver<FieldValue> parentFieldObserver : parentFieldObservers) {
                parentFieldObserver.onNext(parent);
            }
        }
    }
    for (CursorObserver<?> observer : observers) {
        observer.done();
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ReferenceValue(org.activityinfo.model.type.ReferenceValue) FormRecordEntity(org.activityinfo.store.hrd.entity.FormRecordEntity) RecordRef(org.activityinfo.model.type.RecordRef) 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