use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.
the class ExprEvalTest method evaluate.
private void evaluate(String exprString, double expectedValue) {
FormulaLexer lexer = new FormulaLexer(exprString);
FormulaParser parser = new FormulaParser(lexer);
FormulaNode expr = parser.parse();
FieldValue value = expr.evaluate(EmptyEvalContext.INSTANCE);
if (Double.isNaN(expectedValue)) {
assertTrue(exprString + " is NaN", Double.isNaN(Casting.toQuantity(value).getValue()));
} else {
assertThat(exprString, Casting.toQuantity(value).getValue(), closeTo(expectedValue, 0));
}
}
use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.
the class ExprEvalTest method evaluate.
private void evaluate(String exprString, boolean expectedValue) {
FormulaLexer lexer = new FormulaLexer(exprString);
FormulaParser parser = new FormulaParser(lexer);
FormulaNode expr = parser.parse();
FieldValue result = expr.evaluate(EmptyEvalContext.INSTANCE);
assertThat(exprString, Casting.toBoolean(result), equalTo(expectedValue));
}
use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.
the class MySqlRecordHistoryBuilder method computeDeltas.
private List<RecordDelta> computeDeltas(FormClass formClass, FormField subFormField, List<RecordVersion> versions, Map<ResourceId, String> monthlyFieldLabels) {
List<RecordDelta> deltas = new ArrayList<>();
Map<ResourceId, Map<ResourceId, FieldValue>> currentStateMap = new HashMap<>();
for (RecordVersion version : versions) {
RecordDelta delta = new RecordDelta();
delta.version = version;
delta.subFormField = subFormField;
Map<ResourceId, FieldValue> currentState = currentStateMap.get(version.getRecordId());
if (currentState == null) {
// Initialize our state map for this record
currentStateMap.put(version.getRecordId(), new HashMap<>(delta.version.getValues()));
} else {
// version.
for (FormField field : formClass.getFields()) {
if (version.getValues().containsKey(field.getId())) {
FieldValue oldValue = currentState.get(field.getId());
FieldValue newValue = version.getValues().get(field.getId());
if (!Objects.equals(oldValue, newValue)) {
FieldDelta fieldDelta = new FieldDelta();
fieldDelta.field = field;
fieldDelta.oldValue = oldValue;
fieldDelta.newValue = newValue;
if (!Strings.isNullOrEmpty(version.getSubformKey())) {
// subforms
fieldDelta.subFormKey = version.getSubformKey();
fieldDelta.subFormKind = version.getSubformKind();
}
delta.changes.add(fieldDelta);
}
currentState.put(field.getId(), newValue);
}
}
// special handling for old legacy history
for (Map.Entry<ResourceId, FieldValue> entry : version.getValues().entrySet()) {
ResourceId fieldId = entry.getKey();
String fieldIdAsString = fieldId.asString();
if (fieldIdAsString.startsWith("I") && fieldIdAsString.contains("M")) {
// e.g. I309566527M2016-8
int idInt = CuidAdapter.getLegacyIdFromCuid(fieldIdAsString.substring(fieldIdAsString.indexOf("I"), fieldIdAsString.indexOf("M")));
ResourceId id = CuidAdapter.indicatorField(idInt);
if (monthlyFieldLabels.containsKey(id)) {
// check to ensure field still exists
FieldValue oldValue = currentState.get(fieldId);
FieldValue newValue = version.getValues().get(fieldId);
if (!Objects.equals(oldValue, newValue)) {
String month = fieldIdAsString.substring(fieldIdAsString.indexOf("M") + 1);
FieldDelta fieldDelta = new FieldDelta();
fieldDelta.field = new FormField(fieldId);
fieldDelta.field.setLabel(Strings.nullToEmpty(monthlyFieldLabels.get(id)) + " (" + month + ")");
fieldDelta.field.setType(new QuantityType());
fieldDelta.oldValue = oldValue;
fieldDelta.newValue = newValue;
delta.changes.add(fieldDelta);
}
currentState.put(fieldId, newValue);
}
}
}
}
deltas.add(delta);
}
return deltas;
}
use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.
the class FormRecordEntity method toFieldValueMap.
public Map<ResourceId, FieldValue> toFieldValueMap(FormClass formClass) {
Map<ResourceId, FieldValue> map = new HashMap<>();
for (FormField formField : formClass.getFields()) {
Object value = fieldValues.getProperty(formField.getName());
if (value != null) {
FieldConverter<?> converter = FieldConverters.forType(formField.getType());
map.put(formField.getId(), converter.toFieldValue(value));
}
}
return map;
}
use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.
the class Updater method computeSerialNumberPrefix.
private String computeSerialNumberPrefix(FormClass formClass, SerialNumberType type, FormInstance effectiveRecord) {
if (!type.hasPrefix()) {
return null;
}
try {
FormEvalContext evalContext = new FormEvalContext(formClass);
evalContext.setInstance(effectiveRecord);
FormulaNode formula = FormulaParser.parse(type.getPrefixFormula());
FieldValue prefixValue = formula.evaluate(evalContext);
if (prefixValue instanceof TextValue) {
return ((TextValue) prefixValue).asString();
} else {
throw new IllegalStateException("Prefix " + type.getPrefixFormula() + " resolves to type " + prefixValue.getTypeClass().getId());
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to compute prefix for serial number", e);
return null;
}
}
Aggregations