use of org.javarosa.core.model.data.IAnswerData in project javarosa by opendatakit.
the class FormDef method canCreateRepeat.
public boolean canCreateRepeat(TreeReference repeatRef, FormIndex repeatIndex) {
GroupDef repeat = (GroupDef) this.getChild(repeatIndex);
// Check to see if this repeat can have children added by the user
if (repeat.noAddRemove) {
// should have
if (repeat.getCountReference() != null) {
int currentMultiplicity = repeatIndex.getElementMultiplicity();
// Lu Gram: the count XPath needs to be contextualized for nested
// repeat groups...
TreeReference countRef = FormInstance.unpackReference(repeat.getCountReference());
TreeElement countNode = this.getMainInstance().resolveReference(countRef.contextualize(repeatRef));
if (countNode == null) {
throw new RuntimeException("Could not locate the repeat count value expected at " + repeat.getCountReference().getReference().toString());
}
// get the total multiplicity possible
IAnswerData count = countNode.getValue();
long fullcount = count == null ? 0 : (Integer) count.getValue();
if (fullcount <= currentMultiplicity) {
return false;
}
} else {
// Otherwise the user can never add repeat instances
return false;
}
}
return true;
}
use of org.javarosa.core.model.data.IAnswerData in project javarosa by opendatakit.
the class FormDef method attachControlsToInstanceData.
private void attachControlsToInstanceData(TreeElement node) {
for (int i = 0; i < node.getNumChildren(); i++) {
attachControlsToInstanceData(node.getChildAt(i));
}
IAnswerData val = node.getValue();
List<Selection> selections = null;
if (val instanceof SelectOneData) {
selections = new ArrayList<Selection>();
selections.add((Selection) val.getValue());
} else if (val instanceof SelectMultiData) {
selections = (List<Selection>) val.getValue();
}
if (selections != null) {
QuestionDef q = findQuestionByRef(node.getRef(), this);
if (q == null) {
throw new RuntimeException("FormDef.attachControlsToInstanceData: can't find question to link");
}
if (q.getDynamicChoices() != null) {
// droos: i think we should do something like initializing the
// itemset here, so that default answers
// can be linked to the selectchoices. however, there are
// complications. for example, the itemset might
// not be ready to be evaluated at form initialization; it may
// require certain questions to be answered
// first. e.g., if we evaluate an itemset and it has no choices, the
// xform engine will throw an error
// itemset TODO
}
for (Selection s : selections) {
s.attachChoice(q);
}
}
}
use of org.javarosa.core.model.data.IAnswerData in project collect by opendatakit.
the class FormController method saveAllScreenAnswers.
/**
* @return FailedConstraint of first failed constraint or null if all questions were saved.
*/
public FailedConstraint saveAllScreenAnswers(HashMap<FormIndex, IAnswerData> answers, boolean evaluateConstraints) throws JavaRosaException {
if (currentPromptIsQuestion()) {
for (FormIndex index : answers.keySet()) {
// Within a group, you can only save for question events
if (getEvent(index) == FormEntryController.EVENT_QUESTION) {
int saveStatus;
IAnswerData answer = answers.get(index);
if (evaluateConstraints) {
saveStatus = answerQuestion(index, answer);
if (saveStatus != FormEntryController.ANSWER_OK) {
return new FailedConstraint(index, saveStatus);
}
} else {
saveAnswer(index, answer);
}
} else {
Timber.w("Attempted to save an index referencing something other than a question: %s", index.getReference().toString());
}
}
}
return null;
}
use of org.javarosa.core.model.data.IAnswerData 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();
}
use of org.javarosa.core.model.data.IAnswerData in project collect by opendatakit.
the class DecimalWidget method getDoubleAnswerValue.
private Double getDoubleAnswerValue() {
IAnswerData dataHolder = getFormEntryPrompt().getAnswerValue();
Double d = null;
if (dataHolder != null) {
Object dataValue = dataHolder.getValue();
if (dataValue != null) {
if (dataValue instanceof Integer) {
d = (double) (Integer) dataValue;
} else {
d = (Double) dataValue;
}
}
}
return d;
}
Aggregations