use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class RecordTreeLoaderTest method subforms.
@Test
public void subforms() {
IncidentForm incidentForm = setup.getCatalog().getIncidentForm();
RecordRef rootRecordRef = incidentForm.getRecordRef(0);
Observable<Maybe<RecordTree>> recordTree = setup.getFormStore().getRecordTree(rootRecordRef);
Connection<Maybe<RecordTree>> recordTreeView = setup.connect(recordTree);
Iterable<FormInstance> subRecords = recordTreeView.assertLoaded().get().getSubRecords(rootRecordRef, ReferralSubForm.FORM_ID);
assertThat(Iterables.size(subRecords), equalTo(4));
}
use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class RelevanceHandler method applyRelevanceLogic.
private void applyRelevanceLogic(final FormField field) {
if (field.hasRelevanceConditionExpression()) {
try {
FormulaLexer lexer = new FormulaLexer(field.getRelevanceConditionExpression());
FormulaParser parser = new FormulaParser(lexer);
FormulaNode expr = parser.parse();
FieldContainer fieldContainer = simpleFormPanel.getWidgetCreator().get(field.getId());
if (fieldContainer != null) {
FormModel model = simpleFormPanel.getModel();
Optional<FormInstance> instance = model.getWorkingInstance(field.getId(), simpleFormPanel.getSelectedKey(field));
FormClass formClass = model.getClassByField(field.getId());
boolean relevant;
if (instance.isPresent()) {
relevant = expr.evaluateAsBoolean(new FormEvalContext(formClass, instance.get()));
} else {
relevant = expr.evaluateAsBoolean(new FormEvalContext(formClass, new FormInstance(ResourceId.generateSubmissionId(formClass), formClass.getId())));
}
fieldContainer.getFieldWidget().setReadOnly(!relevant);
if (!relevant) {
if (resettingValues) {
// we are in resetting state -> handle nested relevance
fieldContainer.getFieldWidget().clearValue();
fieldContainer.getFieldWidget().fireValueChanged();
}
fieldsWithAppliedRelevance.add(fieldContainer);
} else {
fieldsWithAppliedRelevance.remove(fieldContainer);
}
} else {
Log.error("Can't find container for fieldId: " + field.getId() + ", fieldName: " + field.getLabel() + ", expression: " + field.getRelevanceConditionExpression());
}
} catch (Exception e) {
Log.error("Error: Unable to apply relevance logic. FieldId: " + field.getId() + ", fieldName: " + field.getLabel() + ", expression: " + field.getRelevanceConditionExpression(), e);
}
}
}
use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class SimpleFormPanel method onFieldUpdated.
public void onFieldUpdated(FormField field, FieldValue newValue) {
Optional<FormInstance> 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);
// skip handler must be applied after workingInstance is updated
relevanceHandler.onValueChange();
}
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();
}
}
}
use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class FormModel method getWorkingInstance.
public Optional<FormInstance> getWorkingInstance(ResourceId formFieldId, String keyId) {
FormClass classByField = getClassByField(formFieldId);
if (classByField.equals(rootFormClass)) {
return Optional.of(getWorkingRootInstance());
}
if (classByField.isSubForm()) {
Optional<FormInstance> valueInstance = getSubformValueInstance(classByField, getWorkingRootInstance(), keyId);
if (valueInstance.isPresent()) {
return valueInstance;
} else {
FormInstance newInstance = new FormInstance(ResourceId.generatedPeriodSubmissionId(getWorkingRootInstance().getId(), keyId), classByField.getId());
newInstance.setParentRecordId(getWorkingRootInstance().getId());
SubformValueKey valueKey = new SubformValueKey(classByField, getWorkingRootInstance());
Set<FormInstance> allInstances = subFormInstances.get(valueKey);
if (allInstances == null) {
allInstances = Sets.newHashSet();
subFormInstances.put(valueKey, allInstances);
}
allInstances.add(newInstance);
return Optional.of(newInstance);
}
}
throw new RuntimeException("Failed to identify working instance for field: " + formFieldId + ", keyId: " + keyId);
}
use of org.activityinfo.model.form.FormInstance in project activityinfo by bedatadriven.
the class RepeatingSubFormPanel method newValueInstance.
private FormInstance newValueInstance() {
FormInstance newInstance = new FormInstance(ResourceId.generateSubmissionId(subForm.getId()), subForm.getId());
newInstance.setParentRecordId(formModel.getWorkingRootInstance().getId());
FormModel.SubformValueKey key = key();
Set<FormInstance> instances = formModel.getSubFormInstances().get(key);
if (instances == null) {
instances = Sets.newHashSet();
formModel.getSubFormInstances().put(key, instances);
}
instances.add(newInstance);
formModel.getChangedInstances().add(newInstance);
return newInstance;
}
Aggregations