use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class NewDimensionDialog method onForestUpdated.
private void onForestUpdated(Observable<FormForest> forest) {
store.clear();
if (forest.isLoaded()) {
// The Form Name can be a dimension
store.add(new FormNode());
// Add root fields...
for (FormTree.Node node : forest.get().getRootNodes()) {
if (isEligible(node.getType())) {
store.add(new RootFieldNode(node.getDefiningFormClass().getId(), node.getField()));
}
}
// Add reference forms with eligible fields
for (FormClass formClass : forest.get().getReferencedForms()) {
List<DimensionNode> children = new ArrayList<>();
for (FormField field : formClass.getFields()) {
if (isEligible(field.getType())) {
children.add(new ReferencedNode(formClass, field));
}
}
if (!children.isEmpty()) {
ReferenceFormNode refNode = new ReferenceFormNode(formClass, children.get(0));
store.add(refNode);
store.add(refNode, children);
}
}
}
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class FormulaValidatorTest method invalidFormSchema.
@Test
public void invalidFormSchema() {
FormClass formClass = new FormClass(ResourceId.valueOf("XYZ"));
formClass.addField(ResourceId.valueOf("F1")).setCode("A").setLabel("Field A1").setType(TextType.SIMPLE);
formClass.addField(ResourceId.valueOf("F2")).setCode("A").setLabel("Field A2").setType(TextType.SIMPLE);
TestBatchFormClassProvider formProvider = new TestBatchFormClassProvider();
formProvider.add(formClass);
FormTreeBuilder formTreeBuilder = new FormTreeBuilder(formProvider);
FormTree formTree = formTreeBuilder.queryTree(formClass.getId());
FormulaValidator validator = new FormulaValidator(formTree);
assertFalse(validator.validate(FormulaParser.parse("A")));
assertThat(validator.getErrors(), hasSize(1));
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class FormInputViewModelTest method hiddenSubForm.
/**
* Test the ViewModel for when the user does not have access to a referenced sub form.
*/
@Test
public void hiddenSubForm() {
FormClass parentForm = new FormClass(ResourceId.valueOf("PARENT_FORM"));
parentForm.addField(ResourceId.valueOf("F1")).setLabel("What is your name?").setType(TextType.SIMPLE);
parentForm.addField(ResourceId.valueOf("F2")).setLabel("What are your secrets?").setType(new SubFormReferenceType(ResourceId.valueOf("SECRET_FORM")));
FormTreeBuilder treeBuilder = new FormTreeBuilder(new FormMetadataProvider() {
@Override
public FormMetadata getFormMetadata(ResourceId formId) {
if (formId.equals(parentForm.getId())) {
return FormMetadata.of(1, parentForm, FormPermissions.owner());
} else {
return FormMetadata.forbidden(formId);
}
}
});
FormTree formTree = treeBuilder.queryTree(parentForm.getId());
FormStore formStore = EasyMock.createMock(FormStore.class);
EasyMock.replay(formStore);
FormInputViewModelBuilder viewModelBuilder = new FormInputViewModelBuilder(formStore, formTree, new TestingActivePeriodMemory());
FormInputViewModel viewModel = viewModelBuilder.build(new FormInputModel(new RecordRef(parentForm.getId(), ResourceId.valueOf("R1"))));
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class SubFormPane method addOrUpdateTab.
private void addOrUpdateTab(FormTree formTree, FormTree.Node node) {
TabItemConfig tabItemConfig = tabs.get(node.getFieldId());
if (tabItemConfig == null) {
SubFormReferenceType subFormType = (SubFormReferenceType) node.getType();
FormClass subForm = formTree.getFormClass(subFormType.getClassId());
TabItemConfig config = new TabItemConfig(subForm.getLabel());
tabs.put(node.getFieldId(), config);
add(new SubFormGrid(viewModel, subForm.getId()), config);
}
}
use of org.activityinfo.model.form.FormClass 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);
}
}
}
Aggregations