Search in sources :

Example 1 with FormEntryController

use of org.javarosa.form.api.FormEntryController in project collect by opendatakit.

the class FormLoaderTask method doInBackground.

/**
 * Initialize {@link FormEntryController} with {@link FormDef} from binary or
 * from XML. If given an instance, it will be used to fill the {@link FormDef}.
 */
@Override
protected FECWrapper doInBackground(String... path) {
    errorMsg = null;
    final String formPath = path[0];
    final File formXml = new File(formPath);
    final FormDef formDef = createFormDefFromCacheOrXml(formXml);
    if (errorMsg != null || formDef == null) {
        return null;
    }
    // set paths to /sdcard/odk/forms/formfilename-media/
    final String formFileName = formXml.getName().substring(0, formXml.getName().lastIndexOf("."));
    final File formMediaDir = new File(formXml.getParent(), formFileName + "-media");
    externalDataManager = new ExternalDataManagerImpl(formMediaDir);
    // add external data function handlers
    ExternalDataHandler externalDataHandlerPull = new ExternalDataHandlerPull(externalDataManager);
    formDef.getEvaluationContext().addFunctionHandler(externalDataHandlerPull);
    try {
        loadExternalData(formMediaDir);
    } catch (Exception e) {
        Timber.e(e, "Exception thrown while loading external data");
        errorMsg = e.getMessage();
        return null;
    }
    if (isCancelled()) {
        // that means that the user has cancelled, so no need to go further
        return null;
    }
    // create FormEntryController from formdef
    final FormEntryModel fem = new FormEntryModel(formDef);
    final FormEntryController fec = new FormEntryController(fem);
    boolean usedSavepoint = false;
    try {
        Timber.i("Initializing form.");
        final long start = System.currentTimeMillis();
        usedSavepoint = initializeForm(formDef, fec);
        Timber.i("Form initialized in %.3f seconds.", (System.currentTimeMillis() - start) / 1000F);
    } catch (RuntimeException e) {
        Timber.e(e);
        if (e.getCause() instanceof XPathTypeMismatchException) {
            // this is a case of
            // https://bitbucket.org/m
            // .sundt/javarosa/commits/e5d344783e7968877402bcee11828fa55fac69de
            // the data are imported, the survey will be unusable
            // but we should give the option to the user to edit the form
            // otherwise the survey will be TOTALLY inaccessible.
            Timber.w("We have a syntactically correct instance, but the data threw an " + "exception inside JR. We should allow editing.");
        } else {
            errorMsg = e.getMessage();
            return null;
        }
    }
    // Remove previous forms
    ReferenceManager.instance().clearSession();
    processItemSets(formMediaDir);
    // This should get moved to the Application Class
    if (ReferenceManager.instance().getFactories().length == 0) {
        // this is /sdcard/odk
        ReferenceManager.instance().addReferenceFactory(new FileReferenceFactory(Collect.ODK_ROOT));
    }
    // Set jr://... to point to /sdcard/odk/forms/filename-media/
    ReferenceManager.instance().addSessionRootTranslator(new RootTranslator("jr://images/", "jr://file/forms/" + formFileName + "-media/"));
    ReferenceManager.instance().addSessionRootTranslator(new RootTranslator("jr://image/", "jr://file/forms/" + formFileName + "-media/"));
    ReferenceManager.instance().addSessionRootTranslator(new RootTranslator("jr://audio/", "jr://file/forms/" + formFileName + "-media/"));
    ReferenceManager.instance().addSessionRootTranslator(new RootTranslator("jr://video/", "jr://file/forms/" + formFileName + "-media/"));
    final FormController fc = new FormController(formMediaDir, fec, instancePath == null ? null : new File(instancePath));
    if (xpath != null) {
        // we are resuming after having terminated -- set index to this
        // position...
        FormIndex idx = fc.getIndexFromXPath(xpath);
        fc.jumpToIndex(idx);
    }
    if (waitingXPath != null) {
        FormIndex idx = fc.getIndexFromXPath(waitingXPath);
        fc.setIndexWaitingForData(idx);
    }
    data = new FECWrapper(fc, usedSavepoint);
    return data;
}
Also used : FormController(org.odk.collect.android.logic.FormController) FormEntryController(org.javarosa.form.api.FormEntryController) FormEntryModel(org.javarosa.form.api.FormEntryModel) ExternalDataHandlerPull(org.odk.collect.android.external.handler.ExternalDataHandlerPull) ExternalDataHandler(org.odk.collect.android.external.ExternalDataHandler) RootTranslator(org.javarosa.core.reference.RootTranslator) IOException(java.io.IOException) XPathTypeMismatchException(org.javarosa.xpath.XPathTypeMismatchException) ExternalDataManagerImpl(org.odk.collect.android.external.ExternalDataManagerImpl) FormDef(org.javarosa.core.model.FormDef) FileReferenceFactory(org.odk.collect.android.logic.FileReferenceFactory) FormIndex(org.javarosa.core.model.FormIndex) XPathTypeMismatchException(org.javarosa.xpath.XPathTypeMismatchException) File(java.io.File)

Example 2 with FormEntryController

use of org.javarosa.form.api.FormEntryController 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)

Example 3 with FormEntryController

use of org.javarosa.form.api.FormEntryController in project javarosa by opendatakit.

the class TreeElementTests method testPopulate_withNodesAttributes.

@Test
public void testPopulate_withNodesAttributes() {
    // Given
    FormParseInit formParseInit = new FormParseInit();
    formParseInit.setFormToParse(Paths.get(PathConst.getTestResourcePath().getAbsolutePath(), "populate-nodes-attributes.xml").toString());
    FormEntryController formEntryController = formParseInit.getFormEntryController();
    byte[] formInstanceAsBytes = null;
    try {
        formInstanceAsBytes = Files.readAllBytes(Paths.get(PathConst.getTestResourcePath().getAbsolutePath(), "populate-nodes-attributes-instance.xml"));
    } catch (IOException e) {
        fail("There was a problem with reading the test data.\n" + e.getMessage());
    }
    TreeElement savedRoot = XFormParser.restoreDataModel(formInstanceAsBytes, null).getRoot();
    FormDef formDef = formEntryController.getModel().getForm();
    TreeElement dataRootNode = formDef.getInstance().getRoot().deepCopy(true);
    // When
    dataRootNode.populate(savedRoot, formDef);
    // Then
    assertEquals(2, dataRootNode.getNumChildren());
    TreeElement freeText1Question = dataRootNode.getChildAt(0);
    TreeElement regularGroup = dataRootNode.getChildAt(1);
    assertEquals(1, regularGroup.getNumChildren());
    TreeElement freeText2Question = regularGroup.getChildAt(0);
    assertEquals("free_text_1", freeText1Question.getName());
    assertEquals(1, freeText1Question.getAttributeCount());
    TreeElement customAttr1 = freeText1Question.getAttribute(null, "custom_attr_1");
    assertNotNull(customAttr1);
    assertEquals("custom_attr_1", customAttr1.getName());
    assertEquals("", customAttr1.getNamespace());
    assertEquals("xyz1", customAttr1.getAttributeValue());
    assertEquals("regular_group", regularGroup.getName());
    assertEquals(1, regularGroup.getAttributeCount());
    customAttr1 = regularGroup.getAttribute(null, "custom_attr_1");
    assertNotNull(customAttr1);
    assertEquals("custom_attr_1", customAttr1.getName());
    assertEquals("custom_name_space", customAttr1.getNamespace());
    assertEquals("xyz2", customAttr1.getAttributeValue());
    assertEquals("free_text_2", freeText2Question.getName());
    assertEquals(1, freeText1Question.getAttributeCount());
    TreeElement customAttr2 = freeText2Question.getAttribute(null, "custom_attr_2");
    assertNotNull(customAttr2);
    assertEquals("custom_attr_2", customAttr2.getName());
    assertEquals("", customAttr2.getNamespace());
    assertEquals("xyz3", customAttr2.getAttributeValue());
}
Also used : FormEntryController(org.javarosa.form.api.FormEntryController) FormDef(org.javarosa.core.model.FormDef) IOException(java.io.IOException) FormParseInit(org.javarosa.core.test.FormParseInit) TreeElement(org.javarosa.core.model.instance.TreeElement) Test(org.junit.Test)

Example 4 with FormEntryController

use of org.javarosa.form.api.FormEntryController in project javarosa by opendatakit.

the class FormParseInit method init.

public void init() {
    String xf_name = FORM_NAME;
    FileInputStream is;
    try {
        is = new FileInputStream(xf_name);
    } catch (FileNotFoundException e) {
        System.err.println("Error: the file '" + xf_name + "' could not be found!");
        throw new RuntimeException("Error: the file '" + xf_name + "' could not be found!");
    }
    // Parse the form
    xform = XFormUtils.getFormFromInputStream(is);
    femodel = new FormEntryModel(xform);
    fec = new FormEntryController(femodel);
    if (xform == null) {
        System.out.println("\n\n==================================\nERROR: XForm has failed validation!!");
    } else {
    }
}
Also used : FormEntryController(org.javarosa.form.api.FormEntryController) FormEntryModel(org.javarosa.form.api.FormEntryModel) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Example 5 with FormEntryController

use of org.javarosa.form.api.FormEntryController in project javarosa by opendatakit.

the class TextFormTests method testNonLocalizedText.

public void testNonLocalizedText() {
    FormEntryController fec = fpi.getFormEntryController();
    fec.jumpToIndex(FormIndex.createBeginningOfFormIndex());
    boolean testFlag = false;
    Localizer l = fpi.getFormDef().getLocalizer();
    l.setDefaultLocale(l.getAvailableLocales()[0]);
    l.setLocale(l.getAvailableLocales()[0]);
    do {
        if (fpi.getCurrentQuestion() == null)
            continue;
        QuestionDef q = fpi.getCurrentQuestion();
        fep = fpi.getFormEntryModel().getQuestionPrompt();
        String t = fep.getQuestionText();
        if (t == null)
            continue;
        if (t.equals("Non-Localized label inner text!"))
            testFlag = true;
    } while (fec.stepToNextEvent() != FormEntryController.EVENT_END_OF_FORM);
    if (!testFlag)
        fail("Failed to fallback to labelInnerText in testNonLocalizedText()");
}
Also used : FormEntryController(org.javarosa.form.api.FormEntryController) Localizer(org.javarosa.core.services.locale.Localizer) QuestionDef(org.javarosa.core.model.QuestionDef)

Aggregations

FormEntryController (org.javarosa.form.api.FormEntryController)9 FormEntryModel (org.javarosa.form.api.FormEntryModel)4 FormDef (org.javarosa.core.model.FormDef)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 FormIndex (org.javarosa.core.model.FormIndex)2 QuestionDef (org.javarosa.core.model.QuestionDef)2 Localizer (org.javarosa.core.services.locale.Localizer)2 FormParseInit (org.javarosa.core.test.FormParseInit)2 Test (org.junit.Test)2 IntegerData (org.javarosa.core.model.data.IntegerData)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 RootTranslator (org.javarosa.core.reference.RootTranslator)1 XPathTypeMismatchException (org.javarosa.xpath.XPathTypeMismatchException)1 ExternalDataHandler (org.odk.collect.android.external.ExternalDataHandler)1 ExternalDataManagerImpl (org.odk.collect.android.external.ExternalDataManagerImpl)1 ExternalDataHandlerPull (org.odk.collect.android.external.handler.ExternalDataHandlerPull)1 FileReferenceFactory (org.odk.collect.android.logic.FileReferenceFactory)1