Search in sources :

Example 1 with OptionInput

use of org.apache.zeppelin.display.ui.OptionInput in project zeppelin by apache.

the class Input method getSimpleQuery.

public static String getSimpleQuery(Map<String, Object> params, String script, boolean noteForm) {
    String replaced = script;
    Pattern pattern = noteForm ? VAR_NOTE_PTN : VAR_PTN;
    Matcher match = pattern.matcher(replaced);
    while (match.find()) {
        int first = match.start();
        if (!noteForm && first > 0 && replaced.charAt(first - 1) == '$') {
            continue;
        }
        Input input = getInputForm(match);
        Object value;
        if (params.containsKey(input.name)) {
            value = params.get(input.name);
        } else {
            value = input.getDefaultValue();
        }
        String expanded;
        if (value instanceof Object[] || value instanceof Collection) {
            // multi-selection
            OptionInput optionInput = (OptionInput) input;
            String delimiter = input.argument;
            if (delimiter == null) {
                delimiter = DEFAULT_DELIMITER;
            }
            Collection<Object> checked = value instanceof Collection ? (Collection<Object>) value : Arrays.asList((Object[]) value);
            List<Object> validChecked = new LinkedList<>();
            for (Object o : checked) {
                // filter out obsolete checked values
                if (optionInput.getOptions() != null) {
                    for (ParamOption option : optionInput.getOptions()) {
                        if (option.getValue().equals(o)) {
                            validChecked.add(o);
                            break;
                        }
                    }
                }
            }
            if (validChecked.isEmpty()) {
                expanded = StringUtils.join(checked, delimiter);
            } else {
                params.put(input.name, validChecked);
                expanded = StringUtils.join(validChecked, delimiter);
            }
        } else {
            // single-selection
            expanded = StringUtils.defaultString((String) value, "");
        }
        replaced = match.replaceFirst(expanded);
        match = pattern.matcher(replaced);
    }
    return replaced;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) OptionInput(org.apache.zeppelin.display.ui.OptionInput) LinkedList(java.util.LinkedList) ParamOption(org.apache.zeppelin.display.ui.OptionInput.ParamOption) OptionInput(org.apache.zeppelin.display.ui.OptionInput) Collection(java.util.Collection)

Aggregations

Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 OptionInput (org.apache.zeppelin.display.ui.OptionInput)1 ParamOption (org.apache.zeppelin.display.ui.OptionInput.ParamOption)1