Search in sources :

Example 21 with SubFormReferenceType

use of org.activityinfo.model.type.subform.SubFormReferenceType in project activityinfo by bedatadriven.

the class FixSubForm method maybeFixForm.

private boolean maybeFixForm(PrintWriter logger, FormClass parentForm, FormField formField) {
    SubFormReferenceType type = (SubFormReferenceType) formField.getType();
    ResourceId subFormId = type.getClassId();
    logger.println("Found subform " + formField.getLabel() + "(" + subFormId + ")");
    ResourceId parentFormId = parentForm.getId();
    FormSchemaEntity schemaEntity = Hrd.ofy().load().key(FormSchemaEntity.key(subFormId)).now();
    if (schemaEntity == null) {
        logger.println("Subform " + subFormId + " does not exist!!");
        return false;
    }
    FormClass schema = schemaEntity.readFormClass();
    if (schema.getParentFormId().get().equals(parentFormId)) {
        logger.println("Parent is OK");
        return false;
    }
    FormEntity root = Hrd.ofy().load().key(FormEntity.key(subFormId)).now();
    logger.println("At version " + root.getVersion());
    // Generate new subform id..
    ResourceId newSubFormId = ResourceId.generateId();
    schema.setId(newSubFormId);
    schema.setParentFormId(parentFormId);
    logger.println("Creating copy of subform => " + newSubFormId);
    if (!DRY_RUN) {
        new CreateOrUpdateForm(schema).run();
    }
    // Now find all records and update their schema
    copyRecords(logger, parentFormId, subFormId, newSubFormId);
    copySnapshots(logger, parentFormId, subFormId, newSubFormId);
    // Update the root entity to match version numbers
    FormEntity newRoot = new FormEntity();
    newRoot.setId(newSubFormId);
    newRoot.setVersion(root.getVersion());
    newRoot.setSchemaVersion(root.getSchemaVersion());
    if (!DRY_RUN) {
        Hrd.ofy().save().entity(newRoot);
    }
    // Finally update the MySQL parent
    formField.setType(new SubFormReferenceType(newSubFormId));
    return true;
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) FormEntity(org.activityinfo.store.hrd.entity.FormEntity) CreateOrUpdateForm(org.activityinfo.store.hrd.op.CreateOrUpdateForm) FormSchemaEntity(org.activityinfo.store.hrd.entity.FormSchemaEntity)

Example 22 with SubFormReferenceType

use of org.activityinfo.model.type.subform.SubFormReferenceType in project activityinfo by bedatadriven.

the class FormDesignerPanel method fillPanel.

private void fillPanel(final FormClass formClass, final FormDesigner formDesigner) {
    formClass.traverse(formClass, new TraverseFunction() {

        @Override
        public void apply(FormElement element, FormElementContainer container) {
            if (isSubFormKey(formClass, element)) {
            // NOOP
            } else if (element instanceof FormField) {
                FormField formField = (FormField) element;
                WidgetContainer widgetContainer = containerMap.get(formField.getId());
                if (widgetContainer != null) {
                    // widget container may be null if domain is not supported, should be removed later
                    Widget widget = widgetContainer.asWidget();
                    formDesigner.getDragController().makeDraggable(widget, widgetContainer.getDragHandle());
                    FlowPanel parentDropPanel = (FlowPanel) formDesigner.getDropControllerRegistry().getDropController(widgetContainer.getParentId()).getDropTarget();
                    parentDropPanel.add(widget);
                }
                if (formField.getType() instanceof SubFormReferenceType) {
                    ResourceId subFormId = ((SubFormReferenceType) formField.getType()).getClassId();
                    FormClass subForm = (FormClass) formDesigner.getModel().getElementContainer(subFormId);
                    if (subForm == null) {
                        throw new IllegalStateException("Subform " + subFormId + " does not exist.");
                    }
                    fillPanel(subForm, formDesigner);
                }
            } else if (element instanceof FormSection) {
                FormSection section = (FormSection) element;
                WidgetContainer widgetContainer = containerMap.get(section.getId());
                Widget widget = widgetContainer.asWidget();
                formDesigner.getDragController().makeDraggable(widget, widgetContainer.getDragHandle());
                dropPanel.add(widget);
            } else if (element instanceof FormLabel) {
                FormLabel label = (FormLabel) element;
                WidgetContainer widgetContainer = containerMap.get(label.getId());
                Widget widget = widgetContainer.asWidget();
                formDesigner.getDragController().makeDraggable(widget, widgetContainer.getDragHandle());
                dropPanel.add(widget);
            } else {
                throw new UnsupportedOperationException("Unknown form element.");
            }
        }
    });
}
Also used : FormFieldWidget(org.activityinfo.ui.client.component.form.field.FormFieldWidget) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ResourceId(org.activityinfo.model.resource.ResourceId) WidgetContainer(org.activityinfo.ui.client.component.formdesigner.container.WidgetContainer) FieldsHolderWidgetContainer(org.activityinfo.ui.client.component.formdesigner.container.FieldsHolderWidgetContainer) FieldWidgetContainer(org.activityinfo.ui.client.component.formdesigner.container.FieldWidgetContainer) LabelWidgetContainer(org.activityinfo.ui.client.component.formdesigner.container.LabelWidgetContainer)

Example 23 with SubFormReferenceType

use of org.activityinfo.model.type.subform.SubFormReferenceType in project activityinfo by bedatadriven.

the class FormDesignerPanel method buildWidgetContainers.

private void buildWidgetContainers(final FormDesigner formDesigner, final FormElementContainer container, final FormClass owner, final int depth, final List<Promise<Void>> promises) {
    for (FormElement element : container.getElements()) {
        if (element instanceof FormSection) {
            FormSection formSection = (FormSection) element;
            containerMap.put(formSection.getId(), FieldsHolderWidgetContainer.section(formDesigner, formSection, container.getId()));
            buildWidgetContainers(formDesigner, formSection, owner, depth + 1, promises);
        } else if (element instanceof FormLabel) {
            FormLabel label = (FormLabel) element;
            containerMap.put(label.getId(), new LabelWidgetContainer(formDesigner, label, container.getId()));
        } else if (element instanceof FormField && !isSubFormKey(owner, (FormField) element)) {
            final FormField formField = (FormField) element;
            if (formField.getType() instanceof SubFormReferenceType) {
                // subform
                SubFormReferenceType subform = (SubFormReferenceType) formField.getType();
                Promise<Void> promise = formDesigner.getResourceLocator().getFormClass(subform.getClassId()).then(new Function<FormClass, Void>() {

                    @Override
                    public Void apply(FormClass subform) {
                        formDesigner.getModel().registerSubform(formField.getId(), subform);
                        containerMap.put(formField.getId(), FieldsHolderWidgetContainer.subform(formDesigner, formField, subform, container.getId()));
                        buildWidgetContainers(formDesigner, subform, subform, depth + 1, promises);
                        return null;
                    }
                });
                promises.add(promise);
            } else {
                if (formField.getId().asString().startsWith("_")) {
                    // skip if form field is built-in
                    continue;
                }
                Promise<Void> promise = formDesigner.getFormFieldWidgetFactory().createWidget(owner, formField, new FieldUpdater() {

                    @Override
                    public void onInvalid(String errorMessage) {
                    }

                    @Override
                    public void update(Object value) {
                        formDesigner.getSavedGuard().setSaved(false);
                    }
                }).then(new Function<FormFieldWidget, Void>() {

                    @Nullable
                    @Override
                    public Void apply(@Nullable FormFieldWidget input) {
                        containerMap.put(formField.getId(), new FieldWidgetContainer(formDesigner, input, formField, container.getId()));
                        if (container instanceof FormClass && ((FormClass) container).isSubForm()) {
                            // Possibly returned after initial fill - refill for current subform
                            fillPanel((FormClass) container, formDesigner);
                        }
                        return null;
                    }
                });
                promises.add(promise);
            }
        }
    }
}
Also used : FieldUpdater(org.activityinfo.ui.client.component.form.field.FieldUpdater) LabelWidgetContainer(org.activityinfo.ui.client.component.formdesigner.container.LabelWidgetContainer) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormFieldWidget(org.activityinfo.ui.client.component.form.field.FormFieldWidget) FieldWidgetContainer(org.activityinfo.ui.client.component.formdesigner.container.FieldWidgetContainer) Nullable(javax.annotation.Nullable)

Example 24 with SubFormReferenceType

use of org.activityinfo.model.type.subform.SubFormReferenceType in project activityinfo by bedatadriven.

the class SubFormTemplate method create.

@Override
public FormField create() {
    FormField field = new FormField(ResourceId.generateFieldId(SubFormReferenceType.TYPE_CLASS));
    field.setLabel(label);
    field.setType(new SubFormReferenceType());
    return field;
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormField(org.activityinfo.model.form.FormField)

Example 25 with SubFormReferenceType

use of org.activityinfo.model.type.subform.SubFormReferenceType in project activityinfo by bedatadriven.

the class NodeMatch method joinsTo.

private static List<JoinNode> joinsTo(List<List<FormTree.Node>> partitions, Optional<StatFunction> aggregation) {
    /*
         *  Given a parent: "Site.Location.Territoire.District"
         *  This is represented as a tree of nodes:
         *      District -> Territoire -> Location -> Site
         *      
         *  We want to turn into a list of joins: 
         *      (site field -> form site), 
         *      (location field -> form school),
         *      (field territoire -> form Territoire)
         *      (field district -> form District)
         */
    LinkedList<JoinNode> joins = new LinkedList<>();
    for (int i = 0; i < partitions.size() - 1; i++) {
        // Reference field that functions as a foreign key
        List<FormTree.Node> left = partitions.get(i);
        FormField leftField = left.get(0).getField();
        ResourceId leftFormId = left.get(0).getDefiningFormClass().getId();
        FormulaNode leftFieldExpr = toExpr(left);
        // "RIGHT" side
        // Joining fom left to right using resource ids (primary key)
        List<FormTree.Node> right = partitions.get(i + 1);
        ResourceId rightFormId = right.get(0).getDefiningFormClass().getId();
        if (leftField.getType() instanceof ReferenceType) {
            // Join based on the (left) foreign key ==> (right) primary key
            joins.add(new JoinNode(JoinType.REFERENCE, leftFormId, leftFieldExpr, rightFormId, Optional.<StatFunction>absent()));
        } else if (leftField.getType() instanceof SubFormReferenceType) {
            joins.add(new JoinNode(JoinType.SUBFORM, leftFormId, new SymbolNode(ColumnModel.ID_SYMBOL), rightFormId, aggregation));
        } else {
            throw new IllegalStateException("Invalid field for joining: " + leftField.getType());
        }
    }
    return joins;
}
Also used : JoinNode(org.activityinfo.store.query.shared.join.JoinNode) SymbolNode(org.activityinfo.model.formula.SymbolNode) FormulaNode(org.activityinfo.model.formula.FormulaNode) JoinNode(org.activityinfo.store.query.shared.join.JoinNode) LinkedList(java.util.LinkedList) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ReferenceType(org.activityinfo.model.type.ReferenceType) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) SymbolNode(org.activityinfo.model.formula.SymbolNode) FormulaNode(org.activityinfo.model.formula.FormulaNode) ResourceId(org.activityinfo.model.resource.ResourceId) StatFunction(org.activityinfo.model.formula.functions.StatFunction) FormField(org.activityinfo.model.form.FormField)

Aggregations

SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)28 FormClass (org.activityinfo.model.form.FormClass)17 FormField (org.activityinfo.model.form.FormField)15 ResourceId (org.activityinfo.model.resource.ResourceId)9 QuantityType (org.activityinfo.model.type.number.QuantityType)5 Test (org.junit.Test)5 EnumType (org.activityinfo.model.type.enumerated.EnumType)4 FormFieldWidget (org.activityinfo.ui.client.component.form.field.FormFieldWidget)4 FormTree (org.activityinfo.model.formTree.FormTree)3 ReferenceType (org.activityinfo.model.type.ReferenceType)3 FormStorage (org.activityinfo.store.spi.FormStorage)3 FieldUpdater (org.activityinfo.ui.client.component.form.field.FieldUpdater)3 FieldWidgetContainer (org.activityinfo.ui.client.component.formdesigner.container.FieldWidgetContainer)3 LabelWidgetContainer (org.activityinfo.ui.client.component.formdesigner.container.LabelWidgetContainer)3 ArrayList (java.util.ArrayList)2 Nullable (javax.annotation.Nullable)2 FormClassProvider (org.activityinfo.model.formTree.FormClassProvider)2 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)2 ColumnSet (org.activityinfo.model.query.ColumnSet)2 QueryModel (org.activityinfo.model.query.QueryModel)2