Search in sources :

Example 1 with WrappedException

use of org.javarosa.core.log.WrappedException in project javarosa by opendatakit.

the class FormDef method initEvalContext.

/**
 * @param ec The new Evaluation Context
 */
private void initEvalContext(EvaluationContext ec) {
    if (!ec.getFunctionHandlers().containsKey("jr:itext")) {
        final FormDef f = this;
        ec.addFunctionHandler(new IFunctionHandler() {

            @Override
            public String getName() {
                return "jr:itext";
            }

            @Override
            public Object eval(Object[] args, EvaluationContext ec) {
                String textID = (String) args[0];
                try {
                    // SUUUUPER HACKY
                    String form = ec.getOutputTextForm();
                    if (form != null) {
                        textID = textID + ";" + form;
                        String result = f.getLocalizer().getRawText(f.getLocalizer().getLocale(), textID);
                        return result == null ? "" : result;
                    } else {
                        String text = f.getLocalizer().getText(textID);
                        return text == null ? "[itext:" + textID + "]" : text;
                    }
                } catch (NoSuchElementException nsee) {
                    return "[nolocale]";
                }
            }

            @Override
            public List<Class[]> getPrototypes() {
                Class[] proto = { String.class };
                List<Class[]> v = new ArrayList<Class[]>(1);
                v.add(proto);
                return v;
            }

            @Override
            public boolean rawArgs() {
                return false;
            }

            @Override
            public boolean realTime() {
                return false;
            }
        });
    }
    /*
       * function to reverse a select value into the display label for that
       * choice in the question it came from
       * 
       * arg 1: select value arg 2: string xpath referring to origin question;
       * must be absolute path
       * 
       * this won't work at all if the original label needed to be
       * processed/calculated in some way (<output>s, etc.) (is this even
       * allowed?) likely won't work with multi-media labels _might_ work for
       * itemsets, but probably not very well or at all; could potentially work
       * better if we had some context info DOES work with localization
       * 
       * it's mainly intended for the simple case of reversing a question with
       * compile-time-static fields, for use inside an <output>
       */
    if (!ec.getFunctionHandlers().containsKey("jr:choice-name")) {
        final FormDef f = this;
        ec.addFunctionHandler(new IFunctionHandler() {

            @Override
            public String getName() {
                return "jr:choice-name";
            }

            @Override
            public Object eval(Object[] args, EvaluationContext ec) {
                try {
                    String value = (String) args[0];
                    String questionXpath = (String) args[1];
                    TreeReference ref = RestoreUtils.xfFact.ref(questionXpath);
                    QuestionDef q = findQuestionByRef(ref, f);
                    if (q == null || (q.getControlType() != Constants.CONTROL_SELECT_ONE && q.getControlType() != Constants.CONTROL_SELECT_MULTI)) {
                        return "";
                    }
                    // NOTE: this is highly suspect. We have no context against
                    // which to evaluate
                    // a dynamic selection list. This will generally cause that
                    // evaluation to break
                    // if any filtering is done, or, worst case, give unexpected
                    // results.
                    // 
                    // We should hook into the existing code (FormEntryPrompt) for
                    // pulling
                    // display text for select choices. however, it's hard,
                    // because we don't really have
                    // any context to work with, and all the situations where that
                    // context would be used
                    // don't make sense for trying to reverse a select value back
                    // to a label in an unrelated
                    // expression
                    List<SelectChoice> choices;
                    ItemsetBinding itemset = q.getDynamicChoices();
                    if (itemset != null) {
                        if (itemset.getChoices() == null) {
                            if (ref.isAmbiguous()) {
                                // SurveyCTO: We need a absolute "ref" to populate the dynamic choices,
                                // like we do when we populate those at FormEntryPrompt (line 251).
                                // The "ref" here is ambiguous, so we need to make it concrete first.
                                ref = ref.contextualize(ec.getContextRef());
                            }
                            f.populateDynamicChoices(itemset, ref);
                        }
                        choices = itemset.getChoices();
                    } else {
                        // static choices
                        choices = q.getChoices();
                    }
                    if (choices != null) {
                        for (SelectChoice ch : choices) {
                            if (ch.getValue().equals(value)) {
                                // this is really not ideal. we should hook into the
                                // existing code (FormEntryPrompt) for pulling
                                // display text for select choices. however, it's
                                // hard, because we don't really have
                                // any context to work with, and all the situations
                                // where that context would be used
                                // don't make sense for trying to reverse a select
                                // value back to a label in an unrelated
                                // expression
                                String textID = ch.getTextID();
                                String templateStr;
                                if (textID != null) {
                                    templateStr = f.getLocalizer().getText(textID);
                                } else {
                                    templateStr = ch.getLabelInnerText();
                                }
                                return fillTemplateString(templateStr, ref);
                            }
                        }
                    }
                    return "";
                } catch (Exception e) {
                    throw new WrappedException("error in evaluation of xpath function [choice-name]", e);
                }
            }

            @Override
            public List<Class[]> getPrototypes() {
                Class[] proto = { String.class, String.class };
                List<Class[]> v = new ArrayList<Class[]>(1);
                v.add(proto);
                return v;
            }

            @Override
            public boolean rawArgs() {
                return false;
            }

            @Override
            public boolean realTime() {
                return false;
            }
        });
    }
}
Also used : WrappedException(org.javarosa.core.log.WrappedException) IFunctionHandler(org.javarosa.core.model.condition.IFunctionHandler) XPathException(org.javarosa.xpath.XPathException) InvalidReferenceException(org.javarosa.core.model.instance.InvalidReferenceException) DeserializationException(org.javarosa.core.util.externalizable.DeserializationException) NoSuchElementException(java.util.NoSuchElementException) WrappedException(org.javarosa.core.log.WrappedException) IOException(java.io.IOException) XFormParseException(org.javarosa.xform.parse.XFormParseException) TreeReference(org.javarosa.core.model.instance.TreeReference) List(java.util.List) ArrayList(java.util.ArrayList) ExtWrapList(org.javarosa.core.util.externalizable.ExtWrapList) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 WrappedException (org.javarosa.core.log.WrappedException)1 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)1 IFunctionHandler (org.javarosa.core.model.condition.IFunctionHandler)1 InvalidReferenceException (org.javarosa.core.model.instance.InvalidReferenceException)1 TreeReference (org.javarosa.core.model.instance.TreeReference)1 DeserializationException (org.javarosa.core.util.externalizable.DeserializationException)1 ExtWrapList (org.javarosa.core.util.externalizable.ExtWrapList)1 XFormParseException (org.javarosa.xform.parse.XFormParseException)1 XPathException (org.javarosa.xpath.XPathException)1