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