Search in sources :

Example 1 with TreeElement

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

the class ItemsetWidget method getSelectionArgs.

private String[] getSelectionArgs(List<String> arguments, String nodesetStr, FormController formController) {
    // +1 is for the list_name
    String[] selectionArgs = new String[arguments.size() + 1];
    // parse out the list name, between the ''
    String listName = nodesetStr.substring(nodesetStr.indexOf('\'') + 1, nodesetStr.lastIndexOf('\''));
    // first argument is always listname
    selectionArgs[0] = listName;
    if (formController == null) {
        Timber.w("Can't instantiate ItemsetWidget with a null FormController.");
        return null;
    }
    // loop through the arguments, evaluate any expressions and build the query string for the DB
    for (int i = 0; i < arguments.size(); i++) {
        XPathExpression xpr;
        try {
            xpr = parseTool.parseXPath(arguments.get(i));
        } catch (XPathSyntaxException e) {
            Timber.e(e);
            TextView error = new TextView(getContext());
            error.setText(String.format(getContext().getString(R.string.parser_exception), arguments.get(i)));
            addAnswerView(error);
            break;
        }
        if (xpr != null) {
            FormDef form = formController.getFormDef();
            TreeElement treeElement = form.getMainInstance().resolveReference(formEntryPrompt.getIndex().getReference());
            EvaluationContext ec = new EvaluationContext(form.getEvaluationContext(), treeElement.getRef());
            Object value = xpr.eval(form.getMainInstance(), ec);
            if (value == null) {
                return null;
            } else {
                if (value instanceof XPathNodeset) {
                    value = ((XPathNodeset) value).getValAt(0);
                }
                selectionArgs[i + 1] = value.toString();
            }
        }
    }
    return selectionArgs;
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) FormDef(org.javarosa.core.model.FormDef) TextView(android.widget.TextView) XPathNodeset(org.javarosa.xpath.XPathNodeset) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) SuppressLint(android.annotation.SuppressLint) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 2 with TreeElement

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

the class FormController method getQuestionPromptRequiredText.

public String getQuestionPromptRequiredText(FormIndex index) {
    // look for the text under the requiredMsg bind attribute
    String constraintText = getBindAttribute(index, XFormParser.NAMESPACE_JAVAROSA, "requiredMsg");
    if (constraintText != null) {
        XPathExpression xpathRequiredMsg;
        try {
            xpathRequiredMsg = XPathParseTool.parseXPath("string(" + constraintText + ")");
        } catch (Exception e) {
            // This is a string literal, so no need to evaluate anything.
            return constraintText;
        }
        if (xpathRequiredMsg != null) {
            try {
                FormDef form = formEntryController.getModel().getForm();
                TreeElement treeElement = form.getMainInstance().resolveReference(index.getReference());
                EvaluationContext ec = new EvaluationContext(form.getEvaluationContext(), treeElement.getRef());
                Object value = xpathRequiredMsg.eval(form.getMainInstance(), ec);
                if (!value.equals("")) {
                    return (String) value;
                }
                return null;
            } catch (Exception e) {
                Timber.e(e, "Error evaluating a valid-looking required xpath ");
                return constraintText;
            }
        } else {
            return constraintText;
        }
    }
    return null;
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) FormDef(org.javarosa.core.model.FormDef) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) IOException(java.io.IOException) JavaRosaException(org.odk.collect.android.exception.JavaRosaException) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 3 with TreeElement

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

the class FormController method getSubmissionMetadata.

/**
 * Get the OpenRosa required metadata of the portion of the form beng submitted
 */
public InstanceMetadata getSubmissionMetadata() {
    FormDef formDef = formEntryController.getModel().getForm();
    TreeElement rootElement = formDef.getInstance().getRoot();
    TreeElement trueSubmissionElement;
    // Determine the information about the submission...
    SubmissionProfile p = formDef.getSubmissionProfile();
    if (p == null || p.getRef() == null) {
        trueSubmissionElement = rootElement;
    } else {
        IDataReference ref = p.getRef();
        trueSubmissionElement = formDef.getInstance().resolveReference(ref);
        // resolveReference returns null if the reference is to the root element...
        if (trueSubmissionElement == null) {
            trueSubmissionElement = rootElement;
        }
    }
    // and find the depth-first meta block in this...
    TreeElement e = findDepthFirst(trueSubmissionElement, "meta");
    String instanceId = null;
    String instanceName = null;
    boolean audit = false;
    if (e != null) {
        List<TreeElement> v;
        // instance id...
        v = e.getChildrenWithName(INSTANCE_ID);
        if (v.size() == 1) {
            IAnswerData sa = v.get(0).getValue();
            if (sa != null) {
                instanceId = sa.getDisplayText();
            }
        }
        // instance name...
        v = e.getChildrenWithName(INSTANCE_NAME);
        if (v.size() == 1) {
            IAnswerData sa = v.get(0).getValue();
            if (sa != null) {
                instanceName = sa.getDisplayText();
            }
        }
        // timing element...
        v = e.getChildrenWithName(AUDIT);
        if (v.size() == 1) {
            audit = true;
            IAnswerData answerData = new StringData();
            answerData.setValue(AUDIT_FILE_NAME);
            v.get(0).setValue(answerData);
        }
    }
    return new InstanceMetadata(instanceId, instanceName, audit);
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) FormDef(org.javarosa.core.model.FormDef) IDataReference(org.javarosa.core.model.IDataReference) SubmissionProfile(org.javarosa.core.model.SubmissionProfile) StringData(org.javarosa.core.model.data.StringData) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 4 with TreeElement

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

the class InstanceGoogleSheetsUploader method uploadOneInstance.

private void uploadOneInstance(File instanceFile, String formFilePath, String spreadsheetUrl) throws UploadException {
    TreeElement instanceElement = getInstanceElement(formFilePath, instanceFile);
    setUpSpreadsheet(spreadsheetUrl);
    if (hasRepeatableGroups(instanceElement)) {
        createSheetsIfNeeded(instanceElement);
    }
    String key = getInstanceID(getChildElements(instanceElement));
    if (key == null) {
        key = PropertyUtils.genUUID();
    }
    insertRows(instanceElement, null, key, instanceFile, spreadsheet.getSheets().get(0).getProperties().getTitle());
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 5 with TreeElement

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

the class InstanceGoogleSheetsUploader method getColumnTitles.

private List<Object> getColumnTitles(TreeElement element) {
    List<Object> columnTitles = new ArrayList<>();
    for (TreeElement child : getChildElements(element)) {
        columnTitles.add(getElementTitle(child));
    }
    if (element.isRepeatable()) {
        columnTitles.add(PARENT_KEY);
        columnTitles.add(KEY);
    } else if (hasRepeatableGroups(element)) {
        columnTitles.add(KEY);
    }
    return columnTitles;
}
Also used : ArrayList(java.util.ArrayList) 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