Search in sources :

Example 11 with StringData

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

the class StringWidget method getAnswer.

@Override
public IAnswerData getAnswer() {
    clearFocus();
    String s = getAnswerText();
    return !s.equals("") ? new StringData(s) : null;
}
Also used : StringData(org.javarosa.core.model.data.StringData)

Example 12 with StringData

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

the class ItemsetWidgetTest method getAnswerShouldReflectWhichSelectionWasMade.

@Test
public void getAnswerShouldReflectWhichSelectionWasMade() {
    ItemsetWidget widget = getWidget();
    assertNull(widget.getAnswer());
    int randomIndex = (Math.abs(random.nextInt()) % widget.getChoiceCount());
    widget.setChoiceSelected(randomIndex, true);
    String selectedChoice = choices.get(Integer.toString(randomIndex));
    StringData answer = (StringData) widget.getAnswer();
    assertEquals(answer.getDisplayText(), selectedChoice);
}
Also used : RandomString(net.bytebuddy.utility.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StringData(org.javarosa.core.model.data.StringData) Test(org.junit.Test) QuestionWidgetTest(org.odk.collect.android.widgets.base.QuestionWidgetTest)

Example 13 with StringData

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

the class QuestionWidgetTest method getAnswerShouldReturnExistingAnswerIfPromptHasExistingAnswer.

@Test
public void getAnswerShouldReturnExistingAnswerIfPromptHasExistingAnswer() {
    A answer = getInitialAnswer();
    if (answer instanceof StringData && !(this instanceof ItemsetWidgetTest)) {
        when(formEntryPrompt.getAnswerText()).thenReturn((String) answer.getValue());
    } else {
        when(formEntryPrompt.getAnswerValue()).thenReturn(answer);
    }
    W widget = getWidget();
    IAnswerData newAnswer = widget.getAnswer();
    assertNotNull(newAnswer);
    assertEquals(newAnswer.getDisplayText(), answer.getDisplayText());
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) ItemsetWidgetTest(org.odk.collect.android.widgets.ItemsetWidgetTest) StringData(org.javarosa.core.model.data.StringData) Test(org.junit.Test) ItemsetWidgetTest(org.odk.collect.android.widgets.ItemsetWidgetTest)

Example 14 with StringData

use of org.javarosa.core.model.data.StringData in project javarosa by opendatakit.

the class Recalculate method wrapData.

// droos 1/29/10: we need to come up with a consistent rule for whether the resulting data is determined
// by the type of the instance node, or the type of the expression result. right now it's a mix and a mess
// note a caveat with going solely by instance node type is that untyped nodes default to string!
// for now, these are the rules:
// if node type == bool, convert to boolean (for numbers, zero = f, non-zero = t; empty string = f, all other datatypes -> error)
// if numeric data, convert to int if node type is int OR data is an integer; else convert to double
// if string data or date data, keep as is
// if NaN or empty string, null
/**
 * convert the data object returned by the xpath expression into an IAnswerData suitable for
 * storage in the FormInstance
 */
public static IAnswerData wrapData(Object val, int dataType) {
    if ((val instanceof String && ((String) val).length() == 0) || (val instanceof Double && ((Double) val).isNaN())) {
        return null;
    }
    if (Constants.DATATYPE_BOOLEAN == dataType || val instanceof Boolean) {
        // ctsims: We should really be using the boolean datatype for real, it's
        // necessary for backend calculations and XSD compliance
        boolean b;
        if (val instanceof Boolean) {
            b = (Boolean) val;
        } else if (val instanceof Double) {
            Double d = (Double) val;
            b = Math.abs(d) > 1.0e-12 && !Double.isNaN(d);
        } else if (val instanceof String) {
            String s = (String) val;
            b = s.length() > 0;
        } else {
            throw new RuntimeException("unrecognized data representation while trying to convert to BOOLEAN");
        }
        return new BooleanData(b);
    } else if (val instanceof Double) {
        double d = (Double) val;
        long l = (long) d;
        boolean isIntegral = Math.abs(d - l) < 1.0e-9;
        if (Constants.DATATYPE_INTEGER == dataType || (isIntegral && (Integer.MAX_VALUE >= l) && (Integer.MIN_VALUE <= l))) {
            return new IntegerData((int) d);
        } else if (Constants.DATATYPE_LONG == dataType || isIntegral) {
            return new LongData((long) d);
        } else {
            return new DecimalData(d);
        }
    } else if (dataType == Constants.DATATYPE_GEOPOINT) {
        return new GeoPointData().cast(new UncastData(String.valueOf(val)));
    } else if (dataType == Constants.DATATYPE_GEOSHAPE) {
        return new GeoShapeData().cast(new UncastData(String.valueOf(val)));
    } else if (dataType == Constants.DATATYPE_GEOTRACE) {
        return new GeoTraceData().cast(new UncastData(String.valueOf(val)));
    } else if (dataType == Constants.DATATYPE_CHOICE) {
        return new SelectOneData().cast(new UncastData(String.valueOf(val)));
    } else if (dataType == Constants.DATATYPE_CHOICE_LIST) {
        return new SelectMultiData().cast(new UncastData(String.valueOf(val)));
    } else if (val instanceof String) {
        return new StringData((String) val);
    } else if (val instanceof Date) {
        if (dataType == Constants.DATATYPE_TIME)
            return new TimeData((Date) val);
        if (dataType == Constants.DATATYPE_DATE)
            return new DateData((Date) val);
        return new DateTimeData((Date) val);
    } else {
        throw new RuntimeException("unrecognized data type in 'calculate' expression: " + val.getClass().getName());
    }
}
Also used : SelectOneData(org.javarosa.core.model.data.SelectOneData) IntegerData(org.javarosa.core.model.data.IntegerData) GeoPointData(org.javarosa.core.model.data.GeoPointData) LongData(org.javarosa.core.model.data.LongData) Date(java.util.Date) DecimalData(org.javarosa.core.model.data.DecimalData) BooleanData(org.javarosa.core.model.data.BooleanData) GeoTraceData(org.javarosa.core.model.data.GeoTraceData) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) GeoShapeData(org.javarosa.core.model.data.GeoShapeData) DateData(org.javarosa.core.model.data.DateData) DateTimeData(org.javarosa.core.model.data.DateTimeData) TimeData(org.javarosa.core.model.data.TimeData) UncastData(org.javarosa.core.model.data.UncastData) DateTimeData(org.javarosa.core.model.data.DateTimeData) StringData(org.javarosa.core.model.data.StringData)

Example 15 with StringData

use of org.javarosa.core.model.data.StringData 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)

Aggregations

StringData (org.javarosa.core.model.data.StringData)19 IntegerData (org.javarosa.core.model.data.IntegerData)7 TreeElement (org.javarosa.core.model.instance.TreeElement)6 DateData (org.javarosa.core.model.data.DateData)4 TimeData (org.javarosa.core.model.data.TimeData)4 Date (java.util.Date)3 IDataReference (org.javarosa.core.model.IDataReference)3 DateTimeData (org.javarosa.core.model.data.DateTimeData)3 DecimalData (org.javarosa.core.model.data.DecimalData)3 IAnswerData (org.javarosa.core.model.data.IAnswerData)3 LongData (org.javarosa.core.model.data.LongData)3 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)3 FormInstance (org.javarosa.core.model.instance.FormInstance)3 Test (org.junit.Test)3 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 IOException (java.io.IOException)2 RandomString (net.bytebuddy.utility.RandomString)2 FormDef (org.javarosa.core.model.FormDef)2 BooleanData (org.javarosa.core.model.data.BooleanData)2