Search in sources :

Example 1 with SelectOneData

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

the class SpinnerWidget method getAnswer.

@Override
public IAnswerData getAnswer() {
    clearFocus();
    int i = spinner.getSelectedItemPosition();
    if (i == -1 || i == items.size()) {
        return null;
    } else {
        SelectChoice sc = items.get(i);
        return new SelectOneData(new Selection(sc));
    }
}
Also used : SelectOneData(org.javarosa.core.model.data.SelectOneData) SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) SuppressLint(android.annotation.SuppressLint)

Example 2 with SelectOneData

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

the class TreeElement method populateTemplate.

// this method is for copying in the answers to an itemset. the template node of the destination
// is used for overall structure (including data types), and the itemset source node is used for
// raw data. note that data may be coerced across types, which may result in type conversion error
// very similar in structure to populate()
public void populateTemplate(TreeElement incoming, FormDef f) {
    if (this.isLeaf()) {
        IAnswerData value = incoming.getValue();
        if (value == null) {
            this.setValue(null);
        } else {
            Class classType = CompactInstanceWrapper.classForDataType(this.dataType);
            if (classType == null) {
                throw new RuntimeException("data type [" + value.getClass().getName() + "] not supported inside itemset");
            } else if (classType.isAssignableFrom(value.getClass()) && !(value instanceof SelectOneData || value instanceof SelectMultiData)) {
                this.setValue(value);
            } else {
                String textVal = RestoreUtils.xfFact.serializeData(value);
                IAnswerData typedVal = RestoreUtils.xfFact.parseData(textVal, this.dataType, this.getRef(), f);
                this.setValue(typedVal);
            }
        }
    } else {
        for (int i = 0; i < this.getNumChildren(); i++) {
            TreeElement child = this.getChildAt(i);
            List<TreeElement> newChildren = incoming.getChildrenWithName(child.getName());
            if (child.getMaskVar(MASK_REPEATABLE)) {
                for (int k = 0; k < newChildren.size(); k++) {
                    TreeElement template = f.getMainInstance().getTemplate(child.getRef());
                    TreeElement newChild = template.deepCopy(false);
                    newChild.setMult(k);
                    this.children.add(i + k + 1, newChild);
                    newChild.populateTemplate(newChildren.get(k), f);
                }
                i += newChildren.size();
            } else {
                child.populateTemplate(newChildren.get(0), f);
            }
        }
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) SelectOneData(org.javarosa.core.model.data.SelectOneData) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) Constraint(org.javarosa.core.model.condition.Constraint)

Example 3 with SelectOneData

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

the class SelectOneDataTests method testSetData.

public void testSetData() {
    SelectOneData data = new SelectOneData(one);
    data.setValue(two);
    assertTrue("SelectOneData did not set value properly. Maintained old value.", !(data.getValue().equals(one)));
    assertEquals("SelectOneData did not properly set value ", data.getValue(), two);
    data.setValue(one);
    assertTrue("SelectOneData did not set value properly. Maintained old value.", !(data.getValue().equals(two)));
    assertEquals("SelectOneData did not properly reset value ", data.getValue(), one);
}
Also used : SelectOneData(org.javarosa.core.model.data.SelectOneData)

Example 4 with SelectOneData

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

the class SelectOneDataTests method testNullData.

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

Example 5 with SelectOneData

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

Aggregations

SelectOneData (org.javarosa.core.model.data.SelectOneData)14 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)8 Selection (org.javarosa.core.model.data.helper.Selection)8 ArrayList (java.util.ArrayList)5 Constraint (org.javarosa.core.model.condition.Constraint)5 SelectChoice (org.javarosa.core.model.SelectChoice)4 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 List (java.util.List)3 Date (java.util.Date)2 QuestionDef (org.javarosa.core.model.QuestionDef)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 ConstraintHint (org.javarosa.core.model.condition.pivot.ConstraintHint)2 BooleanData (org.javarosa.core.model.data.BooleanData)2 DateData (org.javarosa.core.model.data.DateData)2 DateTimeData (org.javarosa.core.model.data.DateTimeData)2 DecimalData (org.javarosa.core.model.data.DecimalData)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 IntegerData (org.javarosa.core.model.data.IntegerData)2