Search in sources :

Example 1 with LongData

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

the class OutputInComputedConstraintTextTest method testComputedQuestionText.

@Test
public void testComputedQuestionText() {
    // Answer first question to check label and constraint texts on next questions
    ctrl.answerQuestion(getFormIndex("/constraintMessageError/village:label"), new LongData(1), true);
    assertEquals("Please only conduct this survey with children aged 6 TO 24 MONTHS.", getFormEntryPrompt("/constraintMessageError/ageSetNote:label").getQuestionText());
}
Also used : LongData(org.javarosa.core.model.data.LongData) Test(org.junit.Test)

Example 2 with LongData

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

the class OutputInComputedConstraintTextTest method testComputedConstraintText.

@Test
public void testComputedConstraintText() {
    // Answer first question to check label and constraint texts on next questions
    ctrl.answerQuestion(getFormIndex("/constraintMessageError/village:label"), new LongData(1), true);
    assertEquals("Please only conduct this survey with children aged 6 TO 24 MONTHS.", getFormEntryPrompt("/constraintMessageError/age:label").getConstraintText());
}
Also used : LongData(org.javarosa.core.model.data.LongData) Test(org.junit.Test)

Example 3 with LongData

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

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

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

the class RestoreUtils method addData.

public static void addData(FormInstance dm, String xpath, Object data, int dataType) {
    if (data == null) {
        dataType = -1;
    }
    IAnswerData val;
    switch(dataType) {
        case -1:
            val = null;
            break;
        case Constants.DATATYPE_TEXT:
            val = new StringData((String) data);
            break;
        case Constants.DATATYPE_INTEGER:
            val = new IntegerData((Integer) data);
            break;
        case Constants.DATATYPE_LONG:
            val = new LongData((Long) data);
            break;
        case Constants.DATATYPE_DECIMAL:
            val = new DecimalData((Double) data);
            break;
        case Constants.DATATYPE_BOOLEAN:
            val = new StringData(((Boolean) data).booleanValue() ? "t" : "f");
            break;
        case Constants.DATATYPE_DATE:
            val = new DateData((Date) data);
            break;
        case Constants.DATATYPE_DATE_TIME:
            val = new DateTimeData((Date) data);
            break;
        case Constants.DATATYPE_TIME:
            val = new TimeData((Date) data);
            break;
        case Constants.DATATYPE_CHOICE_LIST:
            val = (SelectMultiData) data;
            break;
        default:
            throw new IllegalArgumentException("Don't know how to handle data type [" + dataType + "]");
    }
    TreeReference ref = absRef(xpath, dm);
    if (dm.addNode(ref, val, dataType) == null) {
        throw new RuntimeException("error setting value during object backup [" + xpath + "]");
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) IntegerData(org.javarosa.core.model.data.IntegerData) LongData(org.javarosa.core.model.data.LongData) Date(java.util.Date) DecimalData(org.javarosa.core.model.data.DecimalData) DateData(org.javarosa.core.model.data.DateData) TreeReference(org.javarosa.core.model.instance.TreeReference) DateTimeData(org.javarosa.core.model.data.DateTimeData) TimeData(org.javarosa.core.model.data.TimeData) DateTimeData(org.javarosa.core.model.data.DateTimeData) StringData(org.javarosa.core.model.data.StringData)

Aggregations

LongData (org.javarosa.core.model.data.LongData)5 Date (java.util.Date)3 DateData (org.javarosa.core.model.data.DateData)3 DateTimeData (org.javarosa.core.model.data.DateTimeData)3 DecimalData (org.javarosa.core.model.data.DecimalData)3 IntegerData (org.javarosa.core.model.data.IntegerData)3 StringData (org.javarosa.core.model.data.StringData)3 TimeData (org.javarosa.core.model.data.TimeData)3 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 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)2 SelectOneData (org.javarosa.core.model.data.SelectOneData)2 UncastData (org.javarosa.core.model.data.UncastData)2 Test (org.junit.Test)2 IAnswerData (org.javarosa.core.model.data.IAnswerData)1 Selection (org.javarosa.core.model.data.helper.Selection)1 TreeReference (org.javarosa.core.model.instance.TreeReference)1