Search in sources :

Example 6 with DateData

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

the class DateTimeWidgetTest method setData.

@Test
public void setData() {
    DateTimeWidget widget = getWidget();
    LocalDateTime date = new LocalDateTime().withYear(2010).withMonthOfYear(5).withDayOfMonth(12);
    widget.setBinaryData(date);
    assertFalse(widget.isWaitingForData());
    assertFalse(widget.getDateWidget().isNullAnswer);
    assertEquals(widget.getDateWidget().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 7 with DateData

use of org.javarosa.core.model.data.DateData 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 8 with DateData

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

the class DateDataTests method testGetData.

public void testGetData() {
    DateData data = new DateData(today);
    assertEquals("DateData's getValue returned an incorrect date", data.getValue(), today);
    Date temp = new Date(today.getTime());
    today.setTime(1234);
    assertEquals("DateData's getValue was mutated incorrectly", data.getValue(), temp);
    Date rep = (Date) data.getValue();
    rep.setTime(rep.getTime() - 1000);
    assertEquals("DateData's getValue was mutated incorrectly", data.getValue(), temp);
}
Also used : DateData(org.javarosa.core.model.data.DateData) Date(java.util.Date)

Example 9 with DateData

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

the class DateDataTests method testNullData.

public void testNullData() {
    boolean exceptionThrown = false;
    DateData data = new DateData();
    data.setValue(today);
    try {
        data.setValue(null);
    } catch (NullPointerException e) {
        exceptionThrown = true;
    }
    assertTrue("DateData failed to throw an exception when setting null data", exceptionThrown);
    assertTrue("DateData overwrote existing value on incorrect input", data.getValue().equals(today));
}
Also used : DateData(org.javarosa.core.model.data.DateData)

Example 10 with DateData

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

the class QuestionPreloader method preloadDate.

/**
 * Preloads a DateData object for the preload type 'date'
 *
 * @param preloadParams The parameters determining the date
 * @return A preload date value if the parameters can be parsed,
 * null otherwise
 */
private IAnswerData preloadDate(String preloadParams) {
    Date d = null;
    if (preloadParams.equals("today")) {
        d = new Date();
    } else if (preloadParams.substring(0, 11).equals("prevperiod-")) {
        List<String> v = DateUtils.split(preloadParams.substring(11), "-", false);
        String[] params = new String[v.size()];
        for (int i = 0; i < params.length; i++) params[i] = (String) v.get(i);
        try {
            String type = params[0];
            String start = params[1];
            boolean beginning;
            if (params[2].equals("head"))
                beginning = true;
            else if (params[2].equals("tail"))
                beginning = false;
            else
                throw new RuntimeException();
            boolean includeToday;
            if (params.length >= 4) {
                if (params[3].equals("x"))
                    includeToday = true;
                else if (params[3].equals(""))
                    includeToday = false;
                else
                    throw new RuntimeException();
            } else {
                includeToday = false;
            }
            int nAgo;
            if (params.length >= 5) {
                nAgo = Integer.parseInt(params[4]);
            } else {
                nAgo = 1;
            }
            d = DateUtils.getPastPeriodDate(new Date(), type, start, beginning, includeToday, nAgo);
        } catch (Exception e) {
            throw new IllegalArgumentException("invalid preload params for preload mode 'date'");
        }
    }
    DateData data = new DateData(d);
    return data;
}
Also used : DateData(org.javarosa.core.model.data.DateData) List(java.util.List) Date(java.util.Date)

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