Search in sources :

Example 6 with Selection

use of org.javarosa.core.model.data.helper.Selection 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;
    }
}
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) ArrayList(java.util.ArrayList) List(java.util.List) ConstraintHint(org.javarosa.core.model.condition.pivot.ConstraintHint) Constraint(org.javarosa.core.model.condition.Constraint)

Example 7 with Selection

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

the class XFormAnswerDataSerializer method serializeAnswerData.

/**
 * @param data The AnswerDataObject to be serialized
 * @return A string containing the xforms compliant format
 * for a <select> tag, a string containing a list of answers
 * separated by space characters.
 */
public Object serializeAnswerData(SelectMultiData data) {
    List<Selection> selections = (List<Selection>) data.getValue();
    StringBuilder selectString = new StringBuilder();
    for (Selection selection : selections) {
        if (selectString.length() > 0)
            selectString.append(DELIMITER);
        selectString.append(selection.getValue());
    }
    // for storing multiple selections.
    return selectString.toString();
}
Also used : Selection(org.javarosa.core.model.data.helper.Selection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with Selection

use of org.javarosa.core.model.data.helper.Selection 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 9 with Selection

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

the class SelectMultiData method getDisplayText.

/**
 * @return THE XMLVALUE!!
 */
/*
     * (non-Javadoc)
     * @see org.javarosa.core.model.data.IAnswerData#getDisplayText()
     */
@Override
public String getDisplayText() {
    StringBuilder b = new StringBuilder();
    for (int i = 0; i < vs.size(); i++) {
        Selection s = (Selection) vs.get(i);
        b.append(s.getValue());
        if (i < vs.size() - 1)
            b.append(", ");
    }
    return b.toString();
}
Also used : Selection(org.javarosa.core.model.data.helper.Selection)

Example 10 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 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)

Aggregations

Selection (org.javarosa.core.model.data.helper.Selection)26 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)14 ArrayList (java.util.ArrayList)13 SelectChoice (org.javarosa.core.model.SelectChoice)12 List (java.util.List)8 SelectOneData (org.javarosa.core.model.data.SelectOneData)8 QuestionDef (org.javarosa.core.model.QuestionDef)4 Constraint (org.javarosa.core.model.condition.Constraint)4 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 SuppressLint (android.annotation.SuppressLint)3 NonNull (android.support.annotation.NonNull)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 TreeElement (org.javarosa.core.model.instance.TreeElement)2 TreeReference (org.javarosa.core.model.instance.TreeReference)2 Test (org.junit.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 BigDecimal (java.math.BigDecimal)1