Search in sources :

Example 1 with SelectOption

use of web.SelectOption in project common by zenlunatics.

the class RowsSelect method readOptions.

// --------------------------------------------------------------------------
private void readOptions() {
    List<String> options = new ArrayList<String>();
    List<String> option_values = new ArrayList<String>();
    m_num_options = 0;
    try {
        ResultSet rs = m_request.db.select(m_query);
        int num_columns = rs.getMetaData().getColumnCount();
        StringBuilder option = new StringBuilder();
        int[] option_column_indexes = null;
        String[] option_columns = null;
        int value_column_num = -1;
        boolean view_value_column = false;
        if (m_option_column != null) {
            option_columns = m_option_column.split(",");
            option_column_indexes = new int[option_columns.length];
            for (int i = 0; i < option_column_indexes.length; i++) {
                option_column_indexes[i] = rs.findColumn(option_columns[i]);
                if (option_columns[i].equals(m_value_column))
                    view_value_column = true;
            }
        }
        if (m_value_column != null)
            value_column_num = rs.findColumn(m_value_column);
        while (rs.next()) {
            if (m_filter != null && !m_filter.accept(rs, m_request))
                continue;
            option.setLength(0);
            if (m_option_renderer != null)
                m_option_renderer.appendOption(option, rs);
            else if (option_column_indexes != null)
                for (int option_column_index : option_column_indexes) {
                    String col = rs.getString(option_column_index);
                    if (col != null) {
                        if (option.length() > 0)
                            option.append(' ');
                        option.append(col.trim());
                    }
                }
            else
                for (int i = 1; i <= num_columns; i++) {
                    if (i == value_column_num && !view_value_column)
                        continue;
                    String col = rs.getString(i);
                    if (col != null) {
                        if (option.length() > 0)
                            option.append(' ');
                        option.append(col.trim());
                    }
                }
            if (option.length() > 0) {
                if (m_exclude_option != null && m_exclude_option.equals(option.toString()))
                    continue;
                String option_value = null;
                if (m_value_column != null) {
                    option_value = rs.getString(value_column_num).trim();
                    if (option_value.equals(m_exclude_option_value))
                        continue;
                }
                ++m_num_options;
                options.add(option.toString());
                if (m_value_column != null && option_value != null)
                    option_values.add(URLStringBuilder.encode(option_value));
                else
                    option_values.add(URLStringBuilder.encode(option.toString()));
            }
        }
        rs.getStatement().close();
    } catch (SQLException e) {
        m_request.abort(e);
    }
    if (m_num_options > 0) {
        ArrayList<SelectOption> list = new ArrayList<SelectOption>(options.size());
        for (int i = 0; i < options.size(); i++) list.add(new Option(options.get(i), option_values.get(i)));
        m_options = list;
    }
}
Also used : URLStringBuilder(web.URLStringBuilder) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SelectOption(web.SelectOption) ResultSet(java.sql.ResultSet) SelectOption(web.SelectOption)

Example 2 with SelectOption

use of web.SelectOption in project common by zenlunatics.

the class People method writeRolesPane.

// --------------------------------------------------------------------
@SuppressWarnings("unchecked")
private void writeRolesPane(Request request) throws IOException {
    HTMLWriter writer = request.writer;
    writer.setAttribute("style", "margin-bottom:20px;");
    writer.aButtonOnClick("add", "dialog_prompt('Role',function(r){XHR_post(context+'/People/Roles/'+r+'/add',null,function(){$('EditSite').replace()})},{title:'Add Role'})");
    List<SelectOption> roles = (List<SelectOption>) request.site.getObjects("_roles_").getObjects();
    for (SelectOption role : roles) {
        String text = role.getText();
        writer.write("<div class=\"panel panel-default\"><div class=\"panel-heading\" style=\"display:flex;justify-content:space-between\">");
        writer.write(text);
        if (!"admin".equals(text) && !"coho".equals(text))
            writer.aButtonOnClick("delete", "dialog_confirm('Delete " + text + " role?' ,function(){XHR_post(context+'/People/Roles/" + text + "/delete',null,function(){$('EditSite').replace()})})");
        writer.write("</div><div class=\"panel-body\">");
        List<String> people = request.db.readValues(new Select("first || ' ' || last").from("people").join("people", "user_roles").where("role='" + text + "'"));
        writer.write(Util.join(", ", people));
        writer.write("</div></div>");
    }
}
Also used : HTMLWriter(web.HTMLWriter) SelectOption(web.SelectOption) Select(db.Select) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with SelectOption

use of web.SelectOption in project common by zenlunatics.

the class SelectRenderer method writeInput.

// --------------------------------------------------------------------------
@Override
public boolean writeInput(View view, Form form, ColumnBase<?> column, View.Mode mode, String default_value, Request request) throws IOException {
    String name = column.getName();
    Select select;
    if (m_options != null) {
        select = new Select(name, m_options).setType(m_type);
        if (mode == View.Mode.ADD_FORM)
            select.setSelectedOption(null, default_value);
        else if (mode != View.Mode.FILTER_FORM) {
            Iterator<? extends SelectOption> i = m_options.iterator();
            if (i.hasNext())
                if (i.next().getValue() == null)
                    select.setSelectedOption(view.data().getString(name), null);
                else
                    select.setSelectedOption(null, view.data().getString(name));
        }
    } else if (m_query != null) {
        select = new RowsSelect(name, m_query.toString(), m_option_column, m_value_col, request).setType(m_type);
        if (m_filter != null)
            ((RowsSelect) select).setFilter(m_filter);
        if (m_option_renderer != null)
            ((RowsSelect) select).setOptionRenderer(m_option_renderer);
        if (mode == View.Mode.ADD_FORM)
            select.setSelectedOption(null, default_value);
        else if (mode != View.Mode.FILTER_FORM)
            select.setSelectedOption(null, view.data().getString(name));
    } else {
        select = new Select(name, m_options_texts, m_option_values).setType(m_type);
        if (mode == View.Mode.ADD_FORM)
            select.setSelectedOption(default_value, null);
        else if (mode == View.Mode.EDIT_FORM)
            if (m_option_values == null)
                select.setSelectedOption(view.data().getString(name), null);
            else
                select.setSelectedOption(null, view.data().getString(name));
    }
    HTMLWriter writer = request.writer;
    if (m_is_required) {
        select.setIsRequired(true);
        writer.setAttribute("required", "yes");
        writer.setAttribute("title", column.getDisplayName(false));
    }
    if (m_first_option_text != null)
        select.addFirstOption(m_first_option_text, m_first_option_value);
    if (m_allow_no_selection || mode == View.Mode.FILTER_FORM)
        select.setAllowNoSelection(true);
    if (m_on_change != null)
        select.setOnChange(m_on_change);
    select.write(request);
    if (m_edit_view_def != null && mode != View.Mode.FILTER_FORM) {
        AccessPolicy access_policy = request.site.getViewDef(m_edit_view_def, request.db).getAccessPolicy();
        if (access_policy == null || access_policy.showEditButtons(view, request)) {
            writer.space();
            StringBuilder options = new StringBuilder("column:'");
            options.append(column.getName());
            options.append("',db_view_def:'");
            options.append(view.getViewDef().getName());
            options.append('\'');
            if (mode == View.Mode.EDIT_FORM) {
                options.append(",db_key_value:");
                options.append(view.data().getString("id"));
            }
            String title = "Edit " + request.site.getViewDef(m_query.getFirstTable(), request.db).getRecordNamePlural();
            writer.setAttribute("style", "vertical-align:baseline;");
            writer.setAttribute("title", title);
            writer.buttonIconOnClick("pencil", "new Dialog({title:'" + HTMLWriter.escapeJSString(title) + "',width:'auto',url:context+'/Views/" + m_edit_view_def + "/component'," + options.toString() + "})");
        }
    }
    return true;
}
Also used : HTMLWriter(web.HTMLWriter) SelectOption(web.SelectOption) Select(web.Select) Iterator(java.util.Iterator) AccessPolicy(db.access.AccessPolicy)

Aggregations

SelectOption (web.SelectOption)3 ArrayList (java.util.ArrayList)2 HTMLWriter (web.HTMLWriter)2 Select (db.Select)1 AccessPolicy (db.access.AccessPolicy)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Select (web.Select)1 URLStringBuilder (web.URLStringBuilder)1