Search in sources :

Example 21 with FormDef

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

the class CompactInstanceWrapper method loadTemplateInstance.

/**
 * load a template instance fresh from the original FormDef, retrieved from RMS
 * @param formID
 * @return
 */
public static FormInstance loadTemplateInstance(int formID) {
    IStorageUtility forms = StorageManager.getStorage(FormDef.STORAGE_KEY);
    FormDef f = (FormDef) forms.read(formID);
    return (f != null ? f.getMainInstance() : null);
}
Also used : IStorageUtility(org.javarosa.core.services.storage.IStorageUtility) FormDef(org.javarosa.core.model.FormDef)

Example 22 with FormDef

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

the class GeoShapeAreaTest method testGeoShapeSupportForEnclosedArea.

public void testGeoShapeSupportForEnclosedArea() throws Exception {
    // Read the form definition
    String FORM_NAME = (new File(PathConst.getTestResourcePath(), "area.xml")).getAbsolutePath();
    InputStream is = null;
    FormDef formDef = null;
    is = new FileInputStream(new File(FORM_NAME));
    formDef = XFormUtils.getFormFromInputStream(is);
    // trigger all calculations
    formDef.initialize(true, new InstanceInitializationFactory());
    // get the calculated area
    IAnswerData areaResult = formDef.getMainInstance().getRoot().getChildAt(1).getValue();
    assertTrue((int) Math.rint((Double) areaResult.getValue()) == 151452);
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FormDef(org.javarosa.core.model.FormDef) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 23 with FormDef

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

the class XFormParserTest method serializeAndRestoreMetaNamespaceFormInstance.

@Test
public void serializeAndRestoreMetaNamespaceFormInstance() throws IOException {
    // Given
    ParseResult parseResult = parse(r("meta-namespace-form.xml"));
    assertEquals(parseResult.formDef.getTitle(), "Namespace for Metadata");
    assertNoParseErrors(parseResult);
    FormDef formDef = parseResult.formDef;
    TreeElement audit = findDepthFirst(formDef.getInstance().getRoot(), AUDIT_NODE);
    TreeElement audit2 = findDepthFirst(formDef.getInstance().getRoot(), AUDIT_2_NODE);
    TreeElement audit3 = findDepthFirst(formDef.getInstance().getRoot(), AUDIT_3_NODE);
    assertNotNull(audit);
    assertEquals(ORX_2_NAMESPACE_PREFIX, audit.getNamespacePrefix());
    assertEquals(ORX_2_NAMESPACE_URI, audit.getNamespace());
    assertNotNull(audit2);
    assertEquals(ORX_2_NAMESPACE_PREFIX, audit2.getNamespacePrefix());
    assertEquals(ORX_2_NAMESPACE_URI, audit2.getNamespace());
    assertNotNull(audit3);
    assertEquals(null, audit3.getNamespacePrefix());
    assertEquals(null, audit3.getNamespace());
    audit.setAnswer(new StringData(AUDIT_ANSWER));
    audit2.setAnswer(new StringData(AUDIT_2_ANSWER));
    audit3.setAnswer(new StringData(AUDIT_3_ANSWER));
    // When
    // serialize the form instance
    XFormSerializingVisitor serializer = new XFormSerializingVisitor();
    ByteArrayPayload xml = (ByteArrayPayload) serializer.createSerializedPayload(formDef.getInstance());
    copy(xml.getPayloadStream(), FORM_INSTANCE_XML_FILE_NAME, REPLACE_EXISTING);
    // restore (deserialize) the form instance
    byte[] formInstanceBytes = readAllBytes(FORM_INSTANCE_XML_FILE_NAME);
    FormInstance formInstance = XFormParser.restoreDataModel(formInstanceBytes, null);
    // Then
    audit = findDepthFirst(formInstance.getRoot(), AUDIT_NODE);
    audit2 = findDepthFirst(formInstance.getRoot(), AUDIT_2_NODE);
    audit3 = findDepthFirst(formInstance.getRoot(), AUDIT_3_NODE);
    assertNotNull(audit);
    assertEquals(ORX_2_NAMESPACE_PREFIX, audit.getNamespacePrefix());
    assertEquals(ORX_2_NAMESPACE_URI, audit.getNamespace());
    assertEquals(AUDIT_ANSWER, audit.getValue().getValue());
    assertNotNull(audit2);
    assertEquals(ORX_2_NAMESPACE_PREFIX, audit2.getNamespacePrefix());
    assertEquals(ORX_2_NAMESPACE_URI, audit2.getNamespace());
    assertEquals(AUDIT_2_ANSWER, audit2.getValue().getValue());
    assertNotNull(audit3);
    assertEquals(null, audit3.getNamespacePrefix());
    assertEquals(null, audit3.getNamespace());
    assertEquals(AUDIT_3_ANSWER, audit3.getValue().getValue());
}
Also used : ParseResult(org.javarosa.xform.parse.FormParserHelper.ParseResult) ByteArrayPayload(org.javarosa.core.services.transport.payload.ByteArrayPayload) FormDef(org.javarosa.core.model.FormDef) XFormSerializingVisitor(org.javarosa.model.xform.XFormSerializingVisitor) StringData(org.javarosa.core.model.data.StringData) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Test(org.junit.Test)

Example 24 with FormDef

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

the class XFormParserTest method parsesSecondaryInstanceForm.

@Test
public void parsesSecondaryInstanceForm() throws IOException, XPathSyntaxException {
    FormDef formDef = parse(SECONDARY_INSTANCE_XML).formDef;
    assertEquals("Form with secondary instance", formDef.getTitle());
}
Also used : FormDef(org.javarosa.core.model.FormDef) Test(org.junit.Test)

Example 25 with FormDef

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

the class XFormParserTest method parsesExternalSecondaryInstanceForm.

@Test
public void parsesExternalSecondaryInstanceForm() throws IOException, XPathSyntaxException {
    FormDef formDef = parse(EXTERNAL_SECONDARY_INSTANCE_XML).formDef;
    assertEquals("Form with external secondary instance", formDef.getTitle());
    TreeReference treeReference = ((XPathPathExpr) parseXPath("instance('towns')/data_set")).getReference();
    EvaluationContext evaluationContext = formDef.getEvaluationContext();
    List<TreeReference> treeReferences = evaluationContext.expandReference(treeReference);
    assertEquals(1, treeReferences.size());
    DataInstance townInstance = formDef.getNonMainInstance("towns");
    AbstractTreeElement tiRoot = townInstance.getRoot();
    assertEquals("towndata", tiRoot.getName());
    assertEquals(1, tiRoot.getNumChildren());
    AbstractTreeElement dataSetChild = tiRoot.getChild("data_set", 0);
    assertEquals("us_east", dataSetChild.getValue().getDisplayText());
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) DataInstance(org.javarosa.core.model.instance.DataInstance) FormDef(org.javarosa.core.model.FormDef) TreeReference(org.javarosa.core.model.instance.TreeReference) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) Test(org.junit.Test)

Aggregations

FormDef (org.javarosa.core.model.FormDef)31 Test (org.junit.Test)10 IOException (java.io.IOException)9 TreeElement (org.javarosa.core.model.instance.TreeElement)7 FormInstance (org.javarosa.core.model.instance.FormInstance)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)4 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)4 DataInputStream (java.io.DataInputStream)3 SubmissionProfile (org.javarosa.core.model.SubmissionProfile)3 IAnswerData (org.javarosa.core.model.data.IAnswerData)3 TreeReference (org.javarosa.core.model.instance.TreeReference)3 FormEntryController (org.javarosa.form.api.FormEntryController)3 ParseResult (org.javarosa.xform.parse.FormParserHelper.ParseResult)3 XPathExpression (org.javarosa.xpath.expr.XPathExpression)3 InputStream (java.io.InputStream)2 GroupDef (org.javarosa.core.model.GroupDef)2 IDataReference (org.javarosa.core.model.IDataReference)2 QuestionDef (org.javarosa.core.model.QuestionDef)2