Search in sources :

Example 1 with ExternalDataHandlerSearch

use of org.odk.collect.android.externaldata.handler.ExternalDataHandlerSearch in project collect by opendatakit.

the class ExternalDataUtil method populateExternalChoices.

public static ArrayList<SelectChoice> populateExternalChoices(FormEntryPrompt formEntryPrompt, XPathFuncExpr xpathfuncexpr) throws FileNotFoundException {
    try {
        List<SelectChoice> selectChoices = formEntryPrompt.getSelectChoices();
        ArrayList<SelectChoice> returnedChoices = new ArrayList<>();
        for (SelectChoice selectChoice : selectChoices) {
            String value = selectChoice.getValue();
            if (isAnInteger(value)) {
                // treat this as a static choice
                returnedChoices.add(selectChoice);
            } else {
                String displayColumns = formEntryPrompt.getSelectChoiceText(selectChoice);
                String imageColumn = formEntryPrompt.getSpecialFormSelectChoiceText(selectChoice, FormEntryCaption.TEXT_FORM_IMAGE);
                if (imageColumn != null && imageColumn.startsWith(JR_IMAGES_PREFIX)) {
                    imageColumn = imageColumn.substring(JR_IMAGES_PREFIX.length());
                }
                // if (displayColumns == null || displayColumns.trim().length() == 0) {
                // throw new InvalidSyntaxException("The label column in the choices sheet
                // appears to be empty (or has been calculated as empty).");
                // }
                ExternalDataManager externalDataManager = Collect.getInstance().getExternalDataManager();
                FormInstance formInstance = Collect.getInstance().getFormController().getFormDef().getInstance();
                EvaluationContext baseEvaluationContext = new EvaluationContext(formInstance);
                EvaluationContext evaluationContext = new EvaluationContext(baseEvaluationContext, formEntryPrompt.getIndex().getReference());
                // we can only add only the appropriate by querying the xPathFuncExpr.id.name
                evaluationContext.addFunctionHandler(new ExternalDataHandlerSearch(externalDataManager, displayColumns, value, imageColumn));
                Object eval = xpathfuncexpr.eval(formInstance, evaluationContext);
                if (eval.getClass().isAssignableFrom(ArrayList.class)) {
                    @SuppressWarnings("unchecked") List<SelectChoice> dynamicChoices = (ArrayList<SelectChoice>) eval;
                    for (SelectChoice dynamicChoice : dynamicChoices) {
                        returnedChoices.add(dynamicChoice);
                    }
                } else {
                    throw new ExternalDataException(getLocalizedString(Collect.getInstance(), R.string.ext_search_return_error, eval.getClass().getName()));
                }
            }
        }
        return returnedChoices;
    } catch (Exception e) {
        String fileName = String.valueOf(xpathfuncexpr.args[0].eval(null, null));
        if (!fileName.endsWith(".csv")) {
            fileName = fileName + ".csv";
        }
        FormController formController = Collect.getInstance().getFormController();
        String filePath = fileName;
        if (formController != null) {
            filePath = Collect.getInstance().getFormController().getMediaFolder() + File.separator + fileName;
        }
        if (!new File(filePath).exists()) {
            throw new FileNotFoundException(filePath);
        }
        throw new ExternalDataException(e.getMessage(), e);
    }
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) SelectChoice(org.javarosa.core.model.SelectChoice) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) ExternalDataException(org.odk.collect.android.exception.ExternalDataException) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) ExternalDataHandlerSearch(org.odk.collect.android.externaldata.handler.ExternalDataHandlerSearch) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) FileNotFoundException(java.io.FileNotFoundException) ExternalDataException(org.odk.collect.android.exception.ExternalDataException) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) FormInstance(org.javarosa.core.model.instance.FormInstance) File(java.io.File)

Aggregations

File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 SelectChoice (org.javarosa.core.model.SelectChoice)1 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)1 FormInstance (org.javarosa.core.model.instance.FormInstance)1 XPathSyntaxException (org.javarosa.xpath.parser.XPathSyntaxException)1 ExternalDataException (org.odk.collect.android.exception.ExternalDataException)1 ExternalDataHandlerSearch (org.odk.collect.android.externaldata.handler.ExternalDataHandlerSearch)1 FormController (org.odk.collect.android.javarosawrapper.FormController)1 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)1