@Override public TypedFormRecord next() { TypedFormRecord record = new TypedFormRecord(id.values.get(row), formId); for (FieldCollector field : fields) { record.set(field.fieldId, field.values.get(row)); } row++; return record; }
@Override public TypedFormRecord get() { ResourceId recordId = ids.recordId(schema.getId(), nextRecordIndex++); TypedFormRecord record = new TypedFormRecord(recordId, schema.getId()); for (Map.Entry<ResourceId, FieldValue> entry : ids.builtinValues().entrySet()) { record.set(entry.getKey(), entry.getValue()); } if(parentDistribution != null) { record.setParentRecordId(parentDistribution.get()); } for (Map.Entry<ResourceId, Supplier<FieldValue>> entry : generators.entrySet()) { record.set(entry.getKey(), entry.getValue().get()); } return record; } }
@VisibleForTesting TypedFormRecord computeEffectiveRecord(FormClass formClass, Optional<FormRecord> existingRecord, TypedRecordUpdate update) { TypedFormRecord record = new TypedFormRecord(update.getRecordId(), formClass.getId()); if(existingRecord.isPresent()) { TypedFormRecord existingTypedRecord = TypedFormRecord.toTypedFormRecord(formClass, existingRecord.get()); record.setAll(existingTypedRecord.getFieldValueMap()); } for (Map.Entry<ResourceId, FieldValue> entry : update.getChangedFieldValues().entrySet()) { record.set(entry.getKey(), entry.getValue()); } return record; }
public TypedFormRecord set(String fieldId, String name) { return set(ResourceId.valueOf(fieldId), name); }
public static TypedFormRecord toTypedFormRecord(FormClass formClass, FormRecord record) { TypedFormRecord instance = new TypedFormRecord(ResourceId.valueOf(record.getRecordId()), formClass.getId()); if (record.getParentRecordId() != null) { instance.setParentRecordId(ResourceId.valueOf(record.getParentRecordId())); } JsonValue fieldsObject = record.getFields(); for (FormField field : formClass.getFields()) { if(fieldsObject.hasKey(field.getName())) { JsonValue fieldValue = fieldsObject.get(field.getName()); if (!fieldValue.isJsonNull()) { instance.set(field.getId(), field.getType().parseJsonValue(fieldValue)); } } } return instance; }
public static void setLabel(TypedFormRecord instance, String label) { instance.set(getFormInstanceLabelCuid(instance), label); }
public TypedFormRecord set(@Nonnull ResourceId fieldId, double value) { return set(fieldId, new Quantity(value)); }
@Override public void add(TypedRecordUpdate update) { TypedFormRecord newRecord = new TypedFormRecord(update.getRecordId(), update.getFormId()); for (Map.Entry<ResourceId, FieldValue> entry : update.getChangedFieldValues().entrySet()) { newRecord.set(entry.getKey(), entry.getValue()); } ensureWeHaveOwnCopy(); records.add(newRecord); index.put(newRecord.getId(), newRecord); version++; }
private List<TypedFormRecord> testList() { final ResourceId key1 = ResourceId.valueOf("key1"); final ResourceId key2 = ResourceId.valueOf("key2"); TypedFormRecord instance1 = newInstance(1); instance1.set(key1, 3); instance1.set(key2, "d"); TypedFormRecord instance2 = newInstance(2); instance2.set(key1, 2); instance2.set(key2, "b"); TypedFormRecord instance3 = newInstance(3); instance3.set(key1, 1); instance3.set(key2, "c"); return Lists.newArrayList(instance1, instance2, instance3); }
@Override public boolean updateInstance(SourceRow row, TypedFormRecord instance) { final List<ValidationResult> validationResults = Lists.newArrayList(); validateInstance(row, validationResults); for (ValidationResult result : validationResults) { if (result.isPersistable() && result.getRef() != null) { instance.set(fieldId, new ReferenceValue(result.getRef())); break; } } return true; }
@Override public boolean updateInstance(SourceRow row, TypedFormRecord instance) { final List<ValidationResult> validationResults = Lists.newArrayList(); validateInstance(row, validationResults); final Map<ResourceId, RecordRef> toSave = Maps.newHashMap(); for (ValidationResult result : validationResults) { if (result.isPersistable() && result.getRef() != null) { ResourceId range = result.getRef().getFormId(); RecordRef value = toSave.get(range); if (value == null) { toSave.put(range, result.getRef()); } } } if (!toSave.isEmpty()) { instance.set(rootField.getFieldId(), new ReferenceValue(toSave.values())); return true; } return false; }
private boolean persistSingleValuedEnum(SourceRow row, TypedFormRecord instance) { if (validateSingleValuedEnum(row).isPersistable()) { final Set<ResourceId> result = Sets.newHashSet(); String value = sources.get(0).getValue(row); for (EnumItem enumItem : enumType.getValues()) { if (enumItem.getLabel().equalsIgnoreCase(value)) { result.add(enumItem.getId()); break; } } if (!result.isEmpty()) { instance.set(targets.get(0).getFormField().getId(), new EnumValue(result)); return true; } } return false; }
@Override public boolean updateInstance(SourceRow row, TypedFormRecord instance) { final ValidationResult validateResult = validate(row); if (validateResult.isPersistable()) { instance.set(target.getFormField().getId(), parser.convert(source.getValue(row))); return true; } return false; }
@Override public boolean updateInstance(SourceRow row, TypedFormRecord instance) { final boolean isLatOk = validateCoordinate(row, 0).isPersistable(); final boolean isLonOk = validateCoordinate(row, 1).isPersistable(); if (isLatOk && isLonOk) { double latitude = parseCoordinate(row, 0); double longitude = parseCoordinate(row, 1); instance.set(fieldId, new GeoPoint(latitude, longitude)); return true; } return false; } }
public void onFieldUpdated(FormField field, FieldValue newValue) { Optional<TypedFormRecord> workingInstance = model.getWorkingInstance(field.getId(), getSelectedKey(field)); if (workingInstance.isPresent()) { if (!Objects.equals(workingInstance.get().get(field.getId()), newValue)) { workingInstance.get().set(field.getId(), newValue); relevanceHandler.onValueChange(); // skip handler must be applied after workingInstance is updated } validateField(widgetCreator.get(field.getId())); model.getChangedInstances().add(workingInstance.get()); } else { FormClass formClass = model.getClassByField(field.getId()); if (formClass.isSubForm()) { widgetCreator.get(field.getId()).setInvalid(I18N.CONSTANTS.subFormTabNotSelected()); widgetCreator.get(field.getId()).getFieldWidget().clearValue(); } } }
@Test public void text() { TypedFormRecord instance = new TypedFormRecord(ResourceId.generateSubmissionId(formClass), formClass.getId()); instance.set(TEXT_FIELD_ID, "1"); eval(String.format("{%s}==\"1\"", TEXT_FIELD_ID.asString()), true, instance); eval(String.format("{%s}!=\"1\"", TEXT_FIELD_ID.asString()), false, instance); }
@Test public void text() { TypedFormRecord instance = new TypedFormRecord(ResourceId.generateSubmissionId(formClass), formClass.getId()); instance.set(TEXT_FIELD_ID, "1"); eval(String.format("{%s}==\"1\"", TEXT_FIELD_ID.asString()), true, instance); eval(String.format("{%s}!=\"1\"", TEXT_FIELD_ID.asString()), false, instance); }
@Test public void quantity() { TypedFormRecord instance = new TypedFormRecord(ResourceId.generateSubmissionId(formClass), formClass.getId()); instance.set(QUANTITY_FIELD_ID, 3); eval(String.format("{%s}==3", QUANTITY_FIELD_ID.asString()), true, instance); eval(String.format("{%s}==3.0", QUANTITY_FIELD_ID.asString()), true, instance); eval(String.format("{%s}!=3", QUANTITY_FIELD_ID.asString()), false, instance); }
@Test public void quantity() { TypedFormRecord instance = new TypedFormRecord(ResourceId.generateSubmissionId(formClass), formClass.getId()); instance.set(QUANTITY_FIELD_ID, 3); eval(String.format("{%s}==3", QUANTITY_FIELD_ID.asString()), true, instance); eval(String.format("{%s}==3.0", QUANTITY_FIELD_ID.asString()), true, instance); eval(String.format("{%s}!=3", QUANTITY_FIELD_ID.asString()), false, instance); eval(String.format("{%s}>2", QUANTITY_FIELD_ID.asString()), true, instance); eval(String.format("{%s}>3", QUANTITY_FIELD_ID.asString()), false, instance); eval(String.format("{%s}>=3", QUANTITY_FIELD_ID.asString()), true, instance); eval(String.format("{%s}<3", QUANTITY_FIELD_ID.asString()), false, instance); eval(String.format("{%s}<=3", QUANTITY_FIELD_ID.asString()), true, instance); }
@Test public void updateLocation() { // <location locationId="1" name="Penekusu Kivu" locationTypeId="1" // X="1.532" Y="27.323" timeEdited="1"/> // <locationAdminLink locationId="1" adminEntityId="2"/> // <locationAdminLink locationId="1" adminEntityId="12"/> TypedFormRecord instance = assertResolves(locator.getFormInstance(HEALTH_CENTER_CLASS, locationInstanceId(1))); instance.set(field(HEALTH_CENTER_CLASS, NAME_FIELD), "New Penekusu"); assertResolves(locator.persist(instance)); GetLocations query = new GetLocations(1); LocationResult result = execute(query); LocationDTO location = result.getData().get(0); assertThat(location.getName(), equalTo("New Penekusu")); assertThat(location.getLocationTypeId(), equalTo(1)); assertThat(location.getLatitude(), equalTo(27.323)); assertThat(location.getLongitude(), equalTo(1.532)); assertThat(location.getAdminEntity(1).getId(), equalTo(2)); // 12 admin level is not returned because we eliminate redundant information org.activityinfo.store.mysql.side.AdminColumnBuilder.emit(int[]) }