Search in sources :

Example 16 with IAnswerData

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;
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) TreeReference(org.javarosa.core.model.instance.TreeReference) Constraint(org.javarosa.core.model.condition.Constraint) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 17 with IAnswerData

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);
        }
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) SelectOneData(org.javarosa.core.model.data.SelectOneData) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) Selection(org.javarosa.core.model.data.helper.Selection) List(java.util.List) ArrayList(java.util.ArrayList) ExtWrapList(org.javarosa.core.util.externalizable.ExtWrapList) Constraint(org.javarosa.core.model.condition.Constraint)

Example 18 with IAnswerData

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;
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) FormIndex(org.javarosa.core.model.FormIndex)

Example 19 with IAnswerData

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();
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) DecimalFormatSymbols(java.text.DecimalFormatSymbols) Selection(org.javarosa.core.model.data.helper.Selection) DecimalFormat(java.text.DecimalFormat) BigDecimal(java.math.BigDecimal) ItemsetDao(org.odk.collect.android.dao.ItemsetDao) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) DateData(org.javarosa.core.model.data.DateData) List(java.util.List) DateTimeData(org.javarosa.core.model.data.DateTimeData)

Example 20 with IAnswerData

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;
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData)

Aggregations

IAnswerData (org.javarosa.core.model.data.IAnswerData)31 Test (org.junit.Test)8 Constraint (org.javarosa.core.model.condition.Constraint)6 TreeReference (org.javarosa.core.model.instance.TreeReference)6 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)5 ArrayList (java.util.ArrayList)4 SelectOneData (org.javarosa.core.model.data.SelectOneData)4 Selection (org.javarosa.core.model.data.helper.Selection)4 TreeElement (org.javarosa.core.model.instance.TreeElement)4 List (java.util.List)3 FormDef (org.javarosa.core.model.FormDef)3 StringData (org.javarosa.core.model.data.StringData)3 BigDecimal (java.math.BigDecimal)2 FormIndex (org.javarosa.core.model.FormIndex)2 SelectChoice (org.javarosa.core.model.SelectChoice)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 ConstraintHint (org.javarosa.core.model.condition.pivot.ConstraintHint)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