Search in sources :

Example 36 with Selection

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

the class SelectMultiMinimalWidgetTest method whenAnswerChanges_shouldAnswerLabelBeUpdated.

@Test
public void whenAnswerChanges_shouldAnswerLabelBeUpdated() {
    assertThat(getSpyWidget().binding.answer.getText().toString(), is("Select Answer"));
    SelectMultiData answer = getInitialAnswer();
    Selection selectedChoice = ((List<Selection>) answer.getValue()).get(0);
    when(formEntryPrompt.getSelectItemText(selectedChoice)).thenReturn(selectedChoice.getValue());
    getSpyWidget().setData(Collections.singletonList(selectedChoice));
    assertThat(getSpyWidget().binding.answer.getText().toString(), is(selectedChoice.getValue()));
    getSpyWidget().clearAnswer();
    assertThat(getSpyWidget().binding.answer.getText().toString(), is("Select Answer"));
}
Also used : SelectMultiData(org.javarosa.core.model.data.SelectMultiData) Selection(org.javarosa.core.model.data.helper.Selection) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test) GeneralSelectMultiWidgetTest(org.odk.collect.android.widgets.base.GeneralSelectMultiWidgetTest)

Example 37 with Selection

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

the class SelectOneMinimalWidgetTest method whenAnswerChanges_shouldValueChangeListenersBeCalled.

@Test
public void whenAnswerChanges_shouldValueChangeListenersBeCalled() {
    WidgetValueChangedListener valueChangedListener = mockValueChangedListener(getSpyWidget());
    SelectOneData answer = getInitialAnswer();
    Selection selectedChoice = (Selection) answer.getValue();
    getSpyWidget().setData(Collections.singletonList(selectedChoice));
    verify(valueChangedListener).widgetValueChanged(getSpyWidget());
}
Also used : SelectOneData(org.javarosa.core.model.data.SelectOneData) Selection(org.javarosa.core.model.data.helper.Selection) WidgetValueChangedListener(org.odk.collect.android.listeners.WidgetValueChangedListener) GeneralSelectOneWidgetTest(org.odk.collect.android.widgets.base.GeneralSelectOneWidgetTest) Test(org.junit.Test)

Example 38 with Selection

use of org.javarosa.core.model.data.helper.Selection 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 MultipleItemsData) {
        StringBuilder answerText = new StringBuilder();
        List<Selection> values = (List<Selection>) data.getValue();
        for (Selection value : values) {
            if (fep.getControlType() == Constants.CONTROL_RANK) {
                answerText.append(values.indexOf(value) + 1).append(". ");
            }
            answerText.append(fep.getSelectItemText(value));
            if ((values.size() - 1) > values.indexOf(value)) {
                answerText.append(", ");
            }
        }
        return answerText.toString();
    }
    if (data instanceof DateTimeData) {
        return DateTimeWidgetUtils.getDateTimeLabel((Date) data.getValue(), DateTimeWidgetUtils.getDatePickerDetails(appearance), true, context);
    }
    if (data instanceof DateData) {
        return DateTimeWidgetUtils.getDateTimeLabel((Date) data.getValue(), DateTimeWidgetUtils.getDatePickerDetails(appearance), false, context);
    }
    if (data != null && appearance != null && appearance.contains(Appearances.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
        String language = "";
        if (formController.getLanguages() != null && formController.getLanguages().length > 0) {
            language = formController.getLanguage();
        }
        return new ItemsetDao(new ItemsetDbAdapter()).getItemLabel(fep.getAnswerValue().getDisplayText(), formController.getMediaFolder().getAbsolutePath(), language);
    }
    return fep.getAnswerText();
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) ItemsetDbAdapter(org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter) DecimalFormatSymbols(java.text.DecimalFormatSymbols) Selection(org.javarosa.core.model.data.helper.Selection) DecimalFormat(java.text.DecimalFormat) MultipleItemsData(org.javarosa.core.model.data.MultipleItemsData) BigDecimal(java.math.BigDecimal) ItemsetDao(org.odk.collect.android.fastexternalitemset.ItemsetDao) DateData(org.javarosa.core.model.data.DateData) List(java.util.List) DateTimeData(org.javarosa.core.model.data.DateTimeData)

Example 39 with Selection

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

the class ExternalAnswerResolver method resolveAnswer.

@Override
public IAnswerData resolveAnswer(String textVal, TreeElement treeElement, FormDef formDef) {
    QuestionDef questionDef = XFormParser.ghettoGetQuestionDef(treeElement.getDataType(), formDef, treeElement.getRef());
    if (questionDef != null && (questionDef.getControlType() == Constants.CONTROL_SELECT_ONE || questionDef.getControlType() == Constants.CONTROL_SELECT_MULTI || questionDef.getControlType() == Constants.CONTROL_RANK)) {
        boolean containsSearchExpression = false;
        XPathFuncExpr xpathExpression = null;
        try {
            xpathExpression = ExternalDataUtil.getSearchXPathExpression(questionDef.getAppearanceAttr());
        } catch (Exception e) {
            Timber.e(e);
            // there is a search expression, but has syntax errors
            containsSearchExpression = true;
        }
        if (xpathExpression != null || containsSearchExpression) {
            // that means that we have dynamic selects
            // read the static choices from the options sheet
            List<SelectChoice> staticChoices = questionDef.getChoices();
            for (int index = 0; index < staticChoices.size(); index++) {
                SelectChoice selectChoice = staticChoices.get(index);
                String selectChoiceValue = selectChoice.getValue();
                if (ExternalDataUtil.isAnInteger(selectChoiceValue)) {
                    Selection selection = selectChoice.selection();
                    switch(questionDef.getControlType()) {
                        case Constants.CONTROL_SELECT_ONE:
                            {
                                if (selectChoiceValue.equals(textVal)) {
                                    // we just need to make sure, so we will override that.
                                    if (questionDef.getControlType() == Constants.CONTROL_SELECT_ONE) {
                                        // we don't need another, just return the static choice.
                                        return new SelectOneData(selection);
                                    }
                                }
                                break;
                            }
                        case Constants.CONTROL_SELECT_MULTI:
                        case Constants.CONTROL_RANK:
                            {
                                // we should search in a potential comma-separated string of
                                // values for a match
                                // copied from org.javarosa.xform.util.XFormAnswerDataParser
                                // .getSelections()
                                List<String> textValues = DateUtils.split(textVal, XFormAnswerDataSerializer.DELIMITER, true);
                                if (textValues.contains(textVal)) {
                                    // choice.
                                    if (selectChoiceValue.equals(textVal)) {
                                        // this means that the user selected ONLY the static
                                        // answer, so just return that
                                        List<Selection> customSelections = new ArrayList<>();
                                        customSelections.add(selection);
                                        return new SelectMultiData(customSelections);
                                    } else {
                                    // we will ignore it for now since we will return that
                                    // selection together with the dynamic ones.
                                    }
                                }
                                break;
                            }
                        default:
                            {
                                // There is a bug if we get here, so let's throw an Exception
                                throw createBugRuntimeException(treeElement, textVal);
                            }
                    }
                } else {
                    switch(questionDef.getControlType()) {
                        case Constants.CONTROL_SELECT_ONE:
                            {
                                // the default implementation will search for the "textVal"
                                // (saved answer) inside the static choices.
                                // Since we know that there isn't such, we just wrap the textVal
                                // in a virtual choice in order to
                                // create a SelectOneData object to be used as the IAnswer to the
                                // TreeElement.
                                // (the caller of this function is searching for such an answer
                                // to populate the in-memory model.)
                                SelectChoice customSelectChoice = new SelectChoice(textVal, textVal, false);
                                customSelectChoice.setIndex(index);
                                return new SelectOneData(customSelectChoice.selection());
                            }
                        case Constants.CONTROL_SELECT_MULTI:
                        case Constants.CONTROL_RANK:
                            {
                                // we should create multiple selections and add them to the pile
                                List<SelectChoice> customSelectChoices = createCustomSelectChoices(textVal);
                                List<Selection> customSelections = new ArrayList<>();
                                for (SelectChoice customSelectChoice : customSelectChoices) {
                                    customSelections.add(customSelectChoice.selection());
                                }
                                return new SelectMultiData(customSelections);
                            }
                        default:
                            {
                                // There is a bug if we get here, so let's throw an Exception
                                throw createBugRuntimeException(treeElement, textVal);
                            }
                    }
                }
            }
            // if we get there then that means that we have a bug
            throw createBugRuntimeException(treeElement, textVal);
        }
    }
    // default behavior matches original behavior (for static selects, etc.)
    return super.resolveAnswer(textVal, treeElement, formDef);
}
Also used : SelectOneData(org.javarosa.core.model.data.SelectOneData) SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) XPathFuncExpr(org.javarosa.xpath.expr.XPathFuncExpr) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) ArrayList(java.util.ArrayList) List(java.util.List) QuestionDef(org.javarosa.core.model.QuestionDef)

Example 40 with Selection

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

the class SelectMultiMinimalWidget method updateAnswerLabel.

private void updateAnswerLabel() {
    if (selectedItems.isEmpty()) {
        binding.answer.setText(R.string.select_answer);
    } else {
        StringBuilder builder = new StringBuilder();
        for (Selection selectedItem : selectedItems) {
            builder.append(getFormEntryPrompt().getSelectItemText(selectedItem));
            if (selectedItems.size() - 1 > selectedItems.indexOf(selectedItem)) {
                builder.append(", ");
            }
        }
        binding.answer.setText(HtmlUtils.textToHtml(builder.toString()));
    }
}
Also used : Selection(org.javarosa.core.model.data.helper.Selection)

Aggregations

Selection (org.javarosa.core.model.data.helper.Selection)44 SelectChoice (org.javarosa.core.model.SelectChoice)20 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)19 ArrayList (java.util.ArrayList)17 SelectOneData (org.javarosa.core.model.data.SelectOneData)14 List (java.util.List)12 Test (org.junit.Test)10 NonNull (androidx.annotation.NonNull)5 QuestionDef (org.javarosa.core.model.QuestionDef)5 SuppressLint (android.annotation.SuppressLint)4 Constraint (org.javarosa.core.model.condition.Constraint)4 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 GeneralSelectMultiWidgetTest (org.odk.collect.android.widgets.base.GeneralSelectMultiWidgetTest)4 Arrays.asList (java.util.Arrays.asList)3 GeneralSelectOneWidgetTest (org.odk.collect.android.widgets.base.GeneralSelectOneWidgetTest)3 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 TreeElement (org.javarosa.core.model.instance.TreeElement)2