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();
}
}
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());
}
}));
}
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;
}
}
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;
}
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();
}
}
Aggregations