Search in sources :

Example 1 with FormDef

use of org.javarosa.core.model.FormDef 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 FormDef

use of org.javarosa.core.model.FormDef 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 FormDef

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

the class FormController method getSubmissionDataReference.

/**
 * Find the portion of the form that is to be submitted
 */
private IDataReference getSubmissionDataReference() {
    FormDef formDef = formEntryController.getModel().getForm();
    // Determine the information about the submission...
    SubmissionProfile p = formDef.getSubmissionProfile();
    if (p == null || p.getRef() == null) {
        return new XPathReference("/");
    } else {
        return p.getRef();
    }
}
Also used : FormDef(org.javarosa.core.model.FormDef) SubmissionProfile(org.javarosa.core.model.SubmissionProfile) XPathReference(org.javarosa.model.xform.XPathReference)

Example 4 with FormDef

use of org.javarosa.core.model.FormDef 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 5 with FormDef

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

the class InstanceGoogleSheetsUploader method getInstanceElement.

private TreeElement getInstanceElement(String formFilePath, File instanceFile) throws UploadException {
    FormDef formDef;
    try {
        formDef = XFormUtils.getFormFromInputStream(new FileInputStream(new File(formFilePath)));
    } catch (FileNotFoundException e) {
        throw new UploadException(e);
    }
    FormLoaderTask.importData(instanceFile, new FormEntryController(new FormEntryModel(formDef)));
    return formDef.getMainInstance().getRoot();
}
Also used : FormEntryController(org.javarosa.form.api.FormEntryController) FormEntryModel(org.javarosa.form.api.FormEntryModel) FormDef(org.javarosa.core.model.FormDef) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

FormDef (org.javarosa.core.model.FormDef)44 IOException (java.io.IOException)13 TreeElement (org.javarosa.core.model.instance.TreeElement)10 Test (org.junit.Test)10 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)7 FormInstance (org.javarosa.core.model.instance.FormInstance)6 File (java.io.File)5 SubmissionProfile (org.javarosa.core.model.SubmissionProfile)5 TreeReference (org.javarosa.core.model.instance.TreeReference)5 XPathExpression (org.javarosa.xpath.expr.XPathExpression)5 FileInputStream (java.io.FileInputStream)4 QuestionDef (org.javarosa.core.model.QuestionDef)4 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)4 FormEntryController (org.javarosa.form.api.FormEntryController)4 XPathNodeset (org.javarosa.xpath.XPathNodeset)4 FormController (org.odk.collect.android.javarosawrapper.FormController)4 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)4 DataInputStream (java.io.DataInputStream)3 FormIndex (org.javarosa.core.model.FormIndex)3