Search in sources :

Example 1 with MultipleItemsData

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

Aggregations

BigDecimal (java.math.BigDecimal)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 List (java.util.List)1 DateData (org.javarosa.core.model.data.DateData)1 DateTimeData (org.javarosa.core.model.data.DateTimeData)1 IAnswerData (org.javarosa.core.model.data.IAnswerData)1 MultipleItemsData (org.javarosa.core.model.data.MultipleItemsData)1 Selection (org.javarosa.core.model.data.helper.Selection)1 ItemsetDao (org.odk.collect.android.fastexternalitemset.ItemsetDao)1 ItemsetDbAdapter (org.odk.collect.android.fastexternalitemset.ItemsetDbAdapter)1