Search in sources :

Example 1 with DateData

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

the class DateWidgetTest method setData.

@Test
public void setData() {
    DateWidget widget = getWidget();
    LocalDateTime date = new LocalDateTime().withYear(2010).withMonthOfYear(5).withDayOfMonth(12);
    widget.setBinaryData(date);
    assertFalse(widget.isWaitingForData());
    assertFalse(widget.isNullAnswer);
    assertEquals(widget.getAnswer().getDisplayText(), new DateData(date.toDate()).getDisplayText());
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) DateData(org.javarosa.core.model.data.DateData) Test(org.junit.Test) GeneralDateTimeWidgetTest(org.odk.collect.android.widgets.base.GeneralDateTimeWidgetTest)

Example 2 with DateData

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

the class DateDataTests method testSetData.

public void testSetData() {
    DateData data = new DateData(notToday);
    data.setValue(today);
    assertTrue("DateData did not set value properly. Maintained old value.", !(data.getValue().equals(notToday)));
    assertEquals("DateData did not properly set value ", data.getValue(), today);
    data.setValue(notToday);
    assertTrue("DateData did not set value properly. Maintained old value.", !(data.getValue().equals(today)));
    assertEquals("DateData did not properly reset value ", data.getValue(), notToday);
    Date temp = new Date(notToday.getTime());
    notToday.setTime(notToday.getTime() - 1324);
    assertEquals("DateData's value was mutated incorrectly", data.getValue(), temp);
}
Also used : DateData(org.javarosa.core.model.data.DateData) Date(java.util.Date)

Example 3 with DateData

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

the class XFormAnswerDataSerializerTest method setUp.

public void setUp() throws Exception {
    super.setUp();
    stringData = new StringData(stringDataValue);
    stringElement.setValue(stringData);
    integerData = new IntegerData(integerDataValue);
    intElement.setValue(integerData);
    dateData = new DateData(dateDataValue);
    dateElement.setValue(dateData);
    timeData = new TimeData(timeDataValue);
    timeElement.setValue(timeData);
    serializer = new XFormAnswerDataSerializer();
}
Also used : XFormAnswerDataSerializer(org.javarosa.xform.util.XFormAnswerDataSerializer) DateData(org.javarosa.core.model.data.DateData) TimeData(org.javarosa.core.model.data.TimeData) IntegerData(org.javarosa.core.model.data.IntegerData) StringData(org.javarosa.core.model.data.StringData)

Example 4 with DateData

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

the class XFormAnswerDataParser method getAnswerData.

public static IAnswerData getAnswerData(String text, int dataType, QuestionDef q) {
    String trimmedText = text.trim();
    if (trimmedText.length() == 0)
        trimmedText = null;
    switch(dataType) {
        case Constants.DATATYPE_NULL:
        case Constants.DATATYPE_UNSUPPORTED:
        case Constants.DATATYPE_TEXT:
        case Constants.DATATYPE_BARCODE:
        case Constants.DATATYPE_BINARY:
            return new StringData(text);
        case Constants.DATATYPE_INTEGER:
            try {
                return (trimmedText == null ? null : new IntegerData(Integer.parseInt(trimmedText)));
            } catch (NumberFormatException nfe) {
                return null;
            }
        case Constants.DATATYPE_LONG:
            try {
                return (trimmedText == null ? null : new LongData(Long.parseLong(trimmedText)));
            } catch (NumberFormatException nfe) {
                return null;
            }
        case Constants.DATATYPE_DECIMAL:
            try {
                return (trimmedText == null ? null : new DecimalData(Double.parseDouble(trimmedText)));
            } catch (NumberFormatException nfe) {
                return null;
            }
        case Constants.DATATYPE_CHOICE:
            Selection selection = getSelection(text, q);
            return (selection == null ? null : new SelectOneData(selection));
        case Constants.DATATYPE_CHOICE_LIST:
            return new SelectMultiData(getSelections(text, q));
        case Constants.DATATYPE_DATE_TIME:
            Date dt = (trimmedText == null ? null : DateUtils.parseDateTime(trimmedText));
            return (dt == null ? null : new DateTimeData(dt));
        case Constants.DATATYPE_DATE:
            Date d = (trimmedText == null ? null : DateUtils.parseDate(trimmedText));
            return (d == null ? null : new DateData(d));
        case Constants.DATATYPE_TIME:
            Date t = (trimmedText == null ? null : DateUtils.parseTime(trimmedText));
            return (t == null ? null : new TimeData(t));
        case Constants.DATATYPE_BOOLEAN:
            if (trimmedText == null) {
                return null;
            } else {
                if (trimmedText.equals("1")) {
                    return new BooleanData(true);
                }
                if (trimmedText.equals("0")) {
                    return new BooleanData(false);
                }
                return trimmedText.equals("t") ? new BooleanData(true) : new BooleanData(false);
            }
        case Constants.DATATYPE_GEOPOINT:
            if (trimmedText == null) {
                return new GeoPointData();
            }
            try {
                UncastData uncast = new UncastData(trimmedText);
                // silly...
                GeoPointData gp = new GeoPointData();
                return gp.cast(uncast);
            } catch (Exception e) {
                return null;
            }
        case Constants.DATATYPE_GEOSHAPE:
            if (trimmedText == null) {
                return new GeoShapeData();
            }
            try {
                UncastData uncast = new UncastData(trimmedText);
                // silly...
                GeoShapeData gs = new GeoShapeData();
                return gs.cast(uncast);
            } catch (Exception e) {
                return null;
            }
        case Constants.DATATYPE_GEOTRACE:
            if (trimmedText == null) {
                return new GeoTraceData();
            }
            try {
                UncastData uncast = new UncastData(trimmedText);
                // silly...
                GeoTraceData gl = new GeoTraceData();
                return gl.cast(uncast);
            } catch (Exception e) {
                return null;
            }
        default:
            return new UncastData(trimmedText);
    }
}
Also used : SelectOneData(org.javarosa.core.model.data.SelectOneData) Selection(org.javarosa.core.model.data.helper.Selection) 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) DateData(org.javarosa.core.model.data.DateData) GeoShapeData(org.javarosa.core.model.data.GeoShapeData) 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 5 with DateData

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

the class FormEntryPromptUtils method getAnswerText.

public static String getAnswerText(FormEntryPrompt fep, Context context, FormController formController) {
    IAnswerData data = fep.getAnswerValue();
    final String appearance = fep.getQuestion().getAppearanceAttr();
    if (data instanceof SelectMultiData) {
        StringBuilder b = new StringBuilder();
        String sep = "";
        for (Selection value : (List<Selection>) data.getValue()) {
            b.append(sep);
            sep = ", ";
            b.append(fep.getSelectItemText(value));
        }
        return b.toString();
    }
    if (data instanceof DateTimeData) {
        return DateTimeUtils.getDateTimeLabel((Date) data.getValue(), DateTimeUtils.getDatePickerDetails(appearance), true, context);
    }
    if (data instanceof DateData) {
        return DateTimeUtils.getDateTimeLabel((Date) data.getValue(), DateTimeUtils.getDatePickerDetails(appearance), false, context);
    }
    if (data != null && appearance != null && appearance.contains("thousands-sep")) {
        try {
            final BigDecimal answerAsDecimal = new BigDecimal(fep.getAnswerText());
            DecimalFormat df = new DecimalFormat();
            df.setGroupingSize(3);
            df.setGroupingUsed(true);
            df.setMaximumFractionDigits(Integer.MAX_VALUE);
            // Use . as decimal marker for consistency with DecimalWidget
            DecimalFormatSymbols customFormat = new DecimalFormatSymbols();
            customFormat.setDecimalSeparator('.');
            if (df.getDecimalFormatSymbols().getGroupingSeparator() == '.') {
                customFormat.setGroupingSeparator(' ');
            }
            df.setDecimalFormatSymbols(customFormat);
            return df.format(answerAsDecimal);
        } catch (NumberFormatException e) {
            return fep.getAnswerText();
        }
    }
    if (data != null && data.getValue() != null && fep.getDataType() == DATATYPE_TEXT && fep.getQuestion().getAdditionalAttribute(null, "query") != null) {
        // ItemsetWidget
        return new ItemsetDao().getItemLabel(fep.getAnswerValue().getDisplayText(), formController.getMediaFolder().getAbsolutePath(), formController.getLanguage());
    }
    return fep.getAnswerText();
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) DecimalFormatSymbols(java.text.DecimalFormatSymbols) Selection(org.javarosa.core.model.data.helper.Selection) DecimalFormat(java.text.DecimalFormat) BigDecimal(java.math.BigDecimal) ItemsetDao(org.odk.collect.android.dao.ItemsetDao) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) DateData(org.javarosa.core.model.data.DateData) List(java.util.List) DateTimeData(org.javarosa.core.model.data.DateTimeData)

Aggregations

DateData (org.javarosa.core.model.data.DateData)11 Date (java.util.Date)6 DateTimeData (org.javarosa.core.model.data.DateTimeData)4 IntegerData (org.javarosa.core.model.data.IntegerData)4 StringData (org.javarosa.core.model.data.StringData)4 TimeData (org.javarosa.core.model.data.TimeData)4 DecimalData (org.javarosa.core.model.data.DecimalData)3 LongData (org.javarosa.core.model.data.LongData)3 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)3 List (java.util.List)2 BooleanData (org.javarosa.core.model.data.BooleanData)2 GeoPointData (org.javarosa.core.model.data.GeoPointData)2 GeoShapeData (org.javarosa.core.model.data.GeoShapeData)2 GeoTraceData (org.javarosa.core.model.data.GeoTraceData)2 IAnswerData (org.javarosa.core.model.data.IAnswerData)2 SelectOneData (org.javarosa.core.model.data.SelectOneData)2 UncastData (org.javarosa.core.model.data.UncastData)2 Selection (org.javarosa.core.model.data.helper.Selection)2 LocalDateTime (org.joda.time.LocalDateTime)2 Test (org.junit.Test)2