Search in sources :

Example 51 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project collect by opendatakit.

the class InstanceGoogleSheetsUploader method insertRows.

private void insertRows(TreeElement element, String parentKey, String key, File instanceFile, String sheetTitle) throws UploadException {
    insertRow(element, parentKey, key, instanceFile, sheetTitle);
    int repeatIndex = 0;
    for (int i = 0; i < element.getNumChildren(); i++) {
        TreeElement child = element.getChildAt(i);
        if (child.isRepeatable() && child.getMultiplicity() != TreeReference.INDEX_TEMPLATE) {
            insertRows(child, key, getKeyBasedOnParentKey(key, child.getName(), repeatIndex++), instanceFile, getElementTitle(child));
        }
        if (child.getMultiplicity() == TreeReference.INDEX_TEMPLATE) {
            repeatIndex = 0;
        }
    }
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 52 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project collect by opendatakit.

the class InstanceGoogleSheetsUploader method getChildElements.

private List<TreeElement> getChildElements(TreeElement element) {
    List<TreeElement> elements = new ArrayList<>();
    TreeElement prior = null;
    for (int i = 0; i < element.getNumChildren(); ++i) {
        TreeElement current = element.getChildAt(i);
        // avoid duplicated elements https://github.com/opendatakit/javarosa/issues/248
        if ((prior != null) && (prior.getName().equals(current.getName()))) {
            prior = current;
        } else {
            switch(current.getDataType()) {
                case org.javarosa.core.model.Constants.DATATYPE_TEXT:
                case org.javarosa.core.model.Constants.DATATYPE_INTEGER:
                case org.javarosa.core.model.Constants.DATATYPE_DECIMAL:
                case org.javarosa.core.model.Constants.DATATYPE_DATE:
                case org.javarosa.core.model.Constants.DATATYPE_TIME:
                case org.javarosa.core.model.Constants.DATATYPE_DATE_TIME:
                case org.javarosa.core.model.Constants.DATATYPE_CHOICE:
                case org.javarosa.core.model.Constants.DATATYPE_CHOICE_LIST:
                case org.javarosa.core.model.Constants.DATATYPE_BOOLEAN:
                case org.javarosa.core.model.Constants.DATATYPE_GEOPOINT:
                case org.javarosa.core.model.Constants.DATATYPE_BARCODE:
                case org.javarosa.core.model.Constants.DATATYPE_BINARY:
                case org.javarosa.core.model.Constants.DATATYPE_LONG:
                case org.javarosa.core.model.Constants.DATATYPE_GEOSHAPE:
                case org.javarosa.core.model.Constants.DATATYPE_GEOTRACE:
                case org.javarosa.core.model.Constants.DATATYPE_UNSUPPORTED:
                    elements.add(current);
                    break;
                case org.javarosa.core.model.Constants.DATATYPE_NULL:
                    if (current.isRepeatable()) {
                        // repeat group
                        elements.add(current);
                    } else if (current.getNumChildren() == 0) {
                        // assume fields that don't have children are string fields
                        elements.add(current);
                    } else {
                        // one or more children - this is a group
                        elements.addAll(getChildElements(current));
                    }
                    break;
            }
            prior = current;
        }
    }
    return elements;
}
Also used : ArrayList(java.util.ArrayList) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 53 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project briefcase by opendatakit.

the class ExportToCsv method populateRepeatGroupsIntoFileMap.

private void populateRepeatGroupsIntoFileMap(TreeElement primarySet, TreeElement treeElement) throws IOException {
    // OK -- group with at least one element -- assume no value...
    // TreeElement list has the begin and end tags for the nested groups.
    // Swallow the end tag by looking to see if the prior and current
    // field names are the same.
    TreeElement prior = null;
    for (int i = 0; i < treeElement.getNumChildren(); ++i) {
        TreeElement current = (TreeElement) treeElement.getChildAt(i);
        if ((prior != null) && (prior.getName().equals(current.getName()))) {
            // it is the end-group tag... seems to happen with two adjacent repeat
            // groups
            log.info("repeating tag at " + i + " skipping " + current.getName());
            prior = current;
        } else {
            switch(current.getDataType()) {
                default:
                    break;
                case org.javarosa.core.model.Constants.DATATYPE_NULL:
                    /* for nodes that have no data, or data type otherwise unknown */
                    if (current.isRepeatable()) {
                        processRepeatingGroupDefinition(current, primarySet, false);
                    } else if (current.getNumChildren() == 0 && current != briefcaseLfd.getSubmissionElement()) {
                    // ignore - string type
                    } else {
                        /* one or more children -- this is a non-repeating group */
                        populateRepeatGroupsIntoFileMap(primarySet, current);
                    }
                    break;
            }
            prior = current;
        }
    }
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 54 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project briefcase by opendatakit.

the class ExportToCsv method emitRepeatingGroupCsv.

private void emitRepeatingGroupCsv(EncryptionInformation ei, List<Element> groupElementList, TreeElement group, String uniqueParentPath, String uniqueGroupPath, File instanceDir) throws IOException {
    OutputStreamWriter osw = fileMap.get(group);
    int trueOrdinal = 1;
    for (Element groupElement : groupElementList) {
        String uniqueGroupInstancePath = uniqueGroupPath + "[" + trueOrdinal + "]";
        boolean first = true;
        first = emitSubmissionCsv(osw, ei, groupElement, group, group, first, uniqueGroupInstancePath, instanceDir);
        emitString(osw, first, uniqueParentPath);
        emitString(osw, false, uniqueGroupInstancePath);
        emitString(osw, false, uniqueGroupPath);
        osw.append("\n");
        ++trueOrdinal;
    }
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) OutputStreamWriter(java.io.OutputStreamWriter)

Example 55 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class XFormParser method restoreDataModel.

public static FormInstance restoreDataModel(Document doc, Class restorableType) {
    Restorable r = (restorableType != null ? (Restorable) PrototypeFactory.getInstance(restorableType) : null);
    Element e = doc.getRootElement();
    TreeElement te = buildInstanceStructure(e, null, buildNamespacesMap(e), null);
    FormInstance dm = new FormInstance(te);
    loadNamespaces(e, dm);
    if (r != null) {
        RestoreUtils.templateData(r, dm, null);
    }
    loadInstanceData(e, te, null);
    return dm;
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) IFormElement(org.javarosa.core.model.IFormElement) FormInstance(org.javarosa.core.model.instance.FormInstance) Restorable(org.javarosa.core.model.util.restorable.Restorable) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Aggregations

TreeElement (org.javarosa.core.model.instance.TreeElement)86 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)33 TreeReference (org.javarosa.core.model.instance.TreeReference)28 FormInstance (org.javarosa.core.model.instance.FormInstance)16 ArrayList (java.util.ArrayList)15 Constraint (org.javarosa.core.model.condition.Constraint)11 Test (org.junit.Test)10 Element (org.kxml2.kdom.Element)9 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)8 FormDef (org.javarosa.core.model.FormDef)7 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)7 IOException (java.io.IOException)6 IFormElement (org.javarosa.core.model.IFormElement)6 StringData (org.javarosa.core.model.data.StringData)6 HashMap (java.util.HashMap)5 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 File (java.io.File)3 GroupDef (org.javarosa.core.model.GroupDef)3 IDataReference (org.javarosa.core.model.IDataReference)3 QuestionDef (org.javarosa.core.model.QuestionDef)3