use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class MySqlRecordHistoryBuilder method queryLabels.
private List<String> queryLabels(ReferenceType type, ReferenceValue value) {
Map<ResourceId, String> labelMap = new HashMap<>();
for (ResourceId formId : type.getRange()) {
Optional<FormStorage> form = catalog.getForm(formId);
if (form.isPresent()) {
Optional<ResourceId> labelFieldId = findLabelField(form.get().getFormClass());
for (RecordRef ref : value.getReferences()) {
Optional<FormRecord> record = form.get().get(ref.getRecordId());
if (record.isPresent()) {
JsonValue labelValue = Json.createNull();
if (labelFieldId.isPresent()) {
labelValue = record.get().getFields().get(labelFieldId.get().asString());
}
if (labelValue.isJsonNull()) {
labelValue = record.get().getFields().get(CuidAdapter.field(formId, CuidAdapter.NAME_FIELD).asString());
}
if (labelValue.isJsonPrimitive()) {
labelMap.put(ref.getRecordId(), labelValue.asString());
}
}
}
}
}
List<String> list = new ArrayList<>();
for (RecordRef ref : value.getReferences()) {
String label = labelMap.get(ref.getRecordId());
if (label == null) {
list.add(ref.toQualifiedString());
} else {
list.add(label);
}
}
return list;
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class ForeignKeyTest method filtering.
@Test
public void filtering() {
// Build the unfiltered foreign key map
// That maps row indexes to foreign keys
ResourceId formId = ResourceId.valueOf("a00001");
ForeignKeyBuilder builder = new ForeignKeyBuilder(formId, new PendingSlot<ForeignKey>());
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0272548382"))));
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0272548382"))));
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0362622291"))));
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0362622291"))));
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0890848243"))));
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0890848243"))));
ForeignKey fkMap = builder.build();
// Now define a filter that includes only the 4th and 5th rows
BitSet bitSet = new BitSet();
bitSet.set(4);
bitSet.set(5);
TableFilter filter = new TableFilter(bitSet);
// Apply the filter to the ForeignKey map and verify the results
ForeignKey filteredKey = filter.apply(fkMap);
assertThat(filteredKey.getKey(0), equalTo("s0890848243"));
assertThat(filteredKey.getKey(1), equalTo("s0890848243"));
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class ForeignKeyTest method emptyValues.
@Test
public void emptyValues() {
// Build the unfiltered foreign key map
// That maps row indexes to foreign keys
ResourceId formId = ResourceId.valueOf("a00001");
ForeignKeyBuilder builder = new ForeignKeyBuilder(formId, new PendingSlot<ForeignKey>());
builder.onNext(null);
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0272548382"))));
builder.onNext(null);
builder.onNext(null);
ForeignKey fkMap = builder.build();
assertThat(fkMap.numRows(), equalTo(4));
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class SubFormPatch method fromRecordId.
public static CursorObserver<ResourceId> fromRecordId(FormClass formClass, final CursorObserver<FieldValue> observer) {
final ResourceId formId = formClass.getId();
final PeriodType periodType = formClass.getSubFormKind().getPeriodType();
return new CursorObserver<ResourceId>() {
@Override
public void onNext(ResourceId value) {
observer.onNext(periodType.fromSubFormKey(new RecordRef(formId, value)));
}
@Override
public void done() {
observer.done();
}
};
}
use of org.activityinfo.model.type.RecordRef 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();
}
}
Aggregations