Search in sources :

Example 1 with JoinNode

use of org.activityinfo.store.query.shared.join.JoinNode in project activityinfo by bedatadriven.

the class SubFormSummaryTest method matchNodes.

@Test
public void matchNodes() {
    TestingStorageProvider catalog = new TestingStorageProvider();
    ClinicForm clinicForm = catalog.getClinicForm();
    FormTree formTree = catalog.getFormTree(clinicForm.getFormId());
    NodeMatcher nodeMatcher = new NodeMatcher(formTree);
    Collection<NodeMatch> nodeMatches = nodeMatcher.resolveSymbol(new SymbolNode("NUM_CONSULT"));
    assertThat(nodeMatches, hasSize(1));
    NodeMatch nodeMatch = Iterables.getOnlyElement(nodeMatches);
    JoinNode joinNode = Iterables.getOnlyElement(nodeMatch.getJoins());
}
Also used : SymbolNode(org.activityinfo.model.formula.SymbolNode) FormTree(org.activityinfo.model.formTree.FormTree) JoinNode(org.activityinfo.store.query.shared.join.JoinNode) NodeMatch(org.activityinfo.store.query.shared.NodeMatch) NodeMatcher(org.activityinfo.store.query.shared.NodeMatcher) Test(org.junit.Test)

Example 2 with JoinNode

use of org.activityinfo.store.query.shared.join.JoinNode in project activityinfo by bedatadriven.

the class NodeMatch method toDebugString.

public String toDebugString() {
    StringBuilder s = new StringBuilder();
    for (JoinNode join : joins) {
        s.append(join.getReferenceField());
        s.append('>');
    }
    switch(type) {
        case ID:
            s.append(formClass.getId());
            s.append("@id");
            break;
        case CLASS:
            s.append(formClass.getId());
            s.append("@class");
            break;
        case FIELD:
            s.append(fieldExpr.toString());
            break;
    }
    return s.toString();
}
Also used : JoinNode(org.activityinfo.store.query.shared.join.JoinNode)

Example 3 with JoinNode

use of org.activityinfo.store.query.shared.join.JoinNode in project activityinfo by bedatadriven.

the class NodeMatch method forId.

public static NodeMatch forId(FormTree.Node parent, FormClass formClass) {
    List<List<FormTree.Node>> partitions = partitionOnJoins(parent);
    List<FormTree.Node> leaf = partitions.get(partitions.size() - 1);
    // Embedded records are not independent resources, and so
    // do not have their own ID. So the leaf field MUST be a reference field
    Preconditions.checkArgument(leaf.get(0).isReference());
    NodeMatch match = new NodeMatch();
    match.joins = joinsTo(partitions, Optional.<StatFunction>absent());
    match.joins.add(new JoinNode(JoinType.REFERENCE, leaf.get(0).getDefiningFormClass().getId(), toExpr(leaf), formClass.getId()));
    match.formClass = formClass;
    match.type = Type.ID;
    return match;
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) StatFunction(org.activityinfo.model.formula.functions.StatFunction) 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) List(java.util.List) LinkedList(java.util.LinkedList)

Example 4 with JoinNode

use of org.activityinfo.store.query.shared.join.JoinNode 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

JoinNode (org.activityinfo.store.query.shared.join.JoinNode)4 SymbolNode (org.activityinfo.model.formula.SymbolNode)3 LinkedList (java.util.LinkedList)2 FormTree (org.activityinfo.model.formTree.FormTree)2 FormulaNode (org.activityinfo.model.formula.FormulaNode)2 StatFunction (org.activityinfo.model.formula.functions.StatFunction)2 List (java.util.List)1 FormField (org.activityinfo.model.form.FormField)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 ReferenceType (org.activityinfo.model.type.ReferenceType)1 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)1 NodeMatch (org.activityinfo.store.query.shared.NodeMatch)1 NodeMatcher (org.activityinfo.store.query.shared.NodeMatcher)1 Test (org.junit.Test)1