use of org.javarosa.core.model.data.SelectMultiData in project collect by opendatakit.
the class GeneralSelectMultiWidgetTest method getNextAnswer.
@NonNull
@Override
public SelectMultiData getNextAnswer() {
List<SelectChoice> selectChoices = getSelectChoices();
int selectedIndex = Math.abs(random.nextInt()) % selectChoices.size();
SelectChoice selectChoice = selectChoices.get(selectedIndex);
Selection selection = new Selection(selectChoice);
return new SelectMultiData(ImmutableList.of(selection));
}
use of org.javarosa.core.model.data.SelectMultiData 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);
}
}
}
}
use of org.javarosa.core.model.data.SelectMultiData in project javarosa by opendatakit.
the class SelectMultiDataTests method testNullData.
public void testNullData() {
boolean exceptionThrown = false;
SelectMultiData data = new SelectMultiData();
data.setValue(firstTwo);
try {
data.setValue(null);
} catch (NullPointerException e) {
exceptionThrown = true;
}
assertTrue("SelectMultiData failed to throw an exception when setting null data", exceptionThrown);
assertTrue("SelectMultiData overwrote existing value on incorrect input", data.getValue().equals(firstTwo));
}
use of org.javarosa.core.model.data.SelectMultiData 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);
}
}
use of org.javarosa.core.model.data.SelectMultiData in project javarosa by opendatakit.
the class FormEntryPrompt method getAnswerText.
public String getAnswerText() {
IAnswerData data = this.getAnswerValue();
if (data == null)
return null;
else {
String text;
// and multi-selects.
if (data instanceof SelectOneData) {
text = this.getSelectItemText((Selection) data.getValue());
} else if (data instanceof SelectMultiData) {
StringBuilder b = new StringBuilder();
List<Selection> values = (List<Selection>) data.getValue();
for (Selection value : values) {
b.append(this.getSelectItemText(value)).append(" ");
}
text = b.toString();
} else {
text = data.getDisplayText();
}
if (getControlType() == Constants.CONTROL_SECRET) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < text.length(); ++i) {
b.append("*");
}
text = b.toString();
}
return text;
}
}
Aggregations