Search in sources :

Example 41 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;
    AuditConfig auditConfig = null;
    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) {
            TreeElement auditElement = v.get(0);
            String locationPriority = auditElement.getBindAttributeValue(XML_OPENDATAKIT_NAMESPACE, "location-priority");
            String locationMinInterval = auditElement.getBindAttributeValue(XML_OPENDATAKIT_NAMESPACE, "location-min-interval");
            String locationMaxAge = auditElement.getBindAttributeValue(XML_OPENDATAKIT_NAMESPACE, "location-max-age");
            boolean isTrackingChangesEnabled = Boolean.parseBoolean(auditElement.getBindAttributeValue(XML_OPENDATAKIT_NAMESPACE, "track-changes"));
            boolean isIdentifyUserEnabled = Boolean.parseBoolean(auditElement.getBindAttributeValue(XML_OPENDATAKIT_NAMESPACE, "identify-user"));
            String trackChangesReason = auditElement.getBindAttributeValue(XML_OPENDATAKIT_NAMESPACE, "track-changes-reasons");
            auditConfig = new AuditConfig.Builder().setMode(locationPriority).setLocationMinInterval(locationMinInterval).setLocationMaxAge(locationMaxAge).setIsTrackingChangesEnabled(isTrackingChangesEnabled).setIsIdentifyUserEnabled(isIdentifyUserEnabled).setIsTrackChangesReasonEnabled(trackChangesReason != null && trackChangesReason.equals("on-form-edit")).createAuditConfig();
            IAnswerData answerData = new StringData();
            answerData.setValue(AUDIT_FILE_NAME);
            auditElement.setValue(answerData);
        }
    }
    return new InstanceMetadata(instanceId, instanceName, auditConfig);
}
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) AuditConfig(org.odk.collect.android.formentry.audit.AuditConfig) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 42 with FormDef

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

the class FileUtils method getMetadataFromFormDefinition.

/**
 * Given a form definition file, return a map containing form metadata. The form ID is required
 * by the specification and will always be included. Title and version are optionally included.
 * If the form definition contains a submission block, any or all of submission URI, base 64 RSA
 * public key, auto-delete and auto-send may be included.
 */
public static HashMap<String, String> getMetadataFromFormDefinition(File formDefinitionXml) {
    FormDef formDef = XFormUtils.getFormFromFormXml(formDefinitionXml.getAbsolutePath(), "jr://file/" + LAST_SAVED_FILENAME);
    final HashMap<String, String> fields = new HashMap<>();
    fields.put(TITLE, formDef.getTitle());
    fields.put(FORMID, formDef.getMainInstance().getRoot().getAttributeValue(null, "id"));
    String version = formDef.getMainInstance().getRoot().getAttributeValue(null, "version");
    if (version != null && StringUtils.isBlank(version)) {
        version = null;
    }
    fields.put(VERSION, version);
    if (formDef.getSubmissionProfile() != null) {
        fields.put(SUBMISSIONURI, formDef.getSubmissionProfile().getAction());
        final String key = formDef.getSubmissionProfile().getAttribute("base64RsaPublicKey");
        if (key != null && key.trim().length() > 0) {
            fields.put(BASE64_RSA_PUBLIC_KEY, key.trim());
        }
        fields.put(AUTO_DELETE, formDef.getSubmissionProfile().getAttribute("auto-delete"));
        fields.put(AUTO_SEND, formDef.getSubmissionProfile().getAttribute("auto-send"));
    }
    fields.put(GEOMETRY_XPATH, getOverallFirstGeoPoint(formDef));
    return fields;
}
Also used : HashMap(java.util.HashMap) FormDef(org.javarosa.core.model.FormDef) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)

Example 43 with FormDef

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

the class FormEntryViewModel method canAddRepeat.

public boolean canAddRepeat() {
    if (formController != null && formController.indexContainsRepeatableGroup()) {
        FormDef formDef = formController.getFormDef();
        FormIndex repeatGroupIndex = getRepeatGroupIndex(formController.getFormIndex(), formDef);
        return !((GroupDef) formDef.getChild(repeatGroupIndex)).noAddRemove;
    } else {
        return false;
    }
}
Also used : FormDef(org.javarosa.core.model.FormDef) FormIndex(org.javarosa.core.model.FormIndex)

Example 44 with FormDef

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

the class FormLoaderTask method createFormDefFromCacheOrXml.

private FormDef createFormDefFromCacheOrXml(String formPath, File formXml) {
    publishProgress(getLocalizedString(Collect.getInstance(), R.string.survey_loading_reading_form_message));
    final FormDef formDefFromCache = FormDefCache.readCache(formXml);
    if (formDefFromCache != null) {
        return formDefFromCache;
    }
    // no binary, read from xml
    Timber.i("Attempting to load from: %s", formXml.getAbsolutePath());
    final long start = System.currentTimeMillis();
    String lastSavedSrc = FileUtils.getOrCreateLastSavedSrc(formXml);
    FormDef formDefFromXml = XFormUtils.getFormFromFormXml(formPath, lastSavedSrc);
    if (formDefFromXml == null) {
        Timber.w("Error reading XForm file");
        errorMsg = "Error reading XForm file";
    } else {
        Timber.i("Loaded in %.3f seconds.", (System.currentTimeMillis() - start) / 1000F);
        formDef = formDefFromXml;
        try {
            FormDefCache.writeCache(formDef, formXml.getPath());
        } catch (IOException e) {
            Timber.e(e);
        }
        return formDefFromXml;
    }
    return null;
}
Also used : FormDef(org.javarosa.core.model.FormDef) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) IOException(java.io.IOException)

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