<commit_before>package org.navalplanner.web.resources.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.business.resources.entities.Worker;
import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.InputEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.SimpleListModel;
import org.zkoss.zul.SimpleTreeModel;
import org.zkoss.zul.SimpleTreeNode;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Tree;
import org.zkoss.zul.TreeModel;
import org.zkoss.zul.Treeitem;

/**
 * Controller for searching for {@link Worker}
 *
 * @author <NAME> <<EMAIL>>
 *
 */
public class WorkerSearchController extends GenericForwardComposer {

    @Autowired
    private IWorkerSearchModel workerSearchModel;

    private WorkerListRenderer workerListRenderer = new WorkerListRenderer();

    private Textbox txtName;

    private Tree criterionsTree;

    private Listbox listBoxWorkers;

    public WorkerSearchController() {

    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        comp.setVariable("controller", this, true);

        // Add event listener onSelect to criterionsTree widget
        criterionsTree.addEventListener("onSelect", new EventListener() {

            // Whenever an element of the tree is selected, a search query
            // is executed, refreshing the results into the workers listbox
            @Override
            public void onEvent(Event event) throws Exception {
                searchWorkers(txtName.getValue(), getSelectedCriterions());
            }
        });
    }

    /**
     * Get input text, and search for workers
     *
     * @param event
     */
    public void searchWorkers(InputEvent event) {
        searchWorkers(event.getValue(), getSelectedCriterions());
    }

    /**
     * Does the actual search for workers, and refresh results
     *
     * @param name
     * @param criterions
     */
    private void searchWorkers(String name, List<Criterion> criterions) {
        if (name == null || name.length() == 0) {
            refreshListBoxWorkers(new Worker[0]);
            return;
        }

        List<Worker> listWorkers = workerSearchModel.findWorkers(name,
                getSelectedCriterions());
        refreshListBoxWorkers(listWorkers
                .toArray(new Worker[listWorkers
                .size()]));
    }

    /**<commit_msg> ItEr23S10CUAsignacionGrupoRecursosAPlanificacionItEr22S10: [FixBug] Initializes some values for worker_search.zul components programatically<commit_after>package org.navalplanner.web.resources.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.navalplanner.business.resources.entities.Criterion;
import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.business.resources.entities.Worker;
import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.InputEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.SimpleListModel;
import org.zkoss.zul.SimpleTreeModel;
import org.zkoss.zul.SimpleTreeNode;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Tree;
import org.zkoss.zul.TreeModel;
import org.zkoss.zul.Treeitem;

/**
 * Controller for searching for {@link Worker}
 *
 * @author <NAME> <<EMAIL>>
 *
 */
public class WorkerSearchController extends GenericForwardComposer {

    @Autowired
    private IWorkerSearchModel workerSearchModel;

    private WorkerListRenderer workerListRenderer = new WorkerListRenderer();

    private Textbox txtName;

    private Tree criterionsTree;

    private Listbox listBoxWorkers;

    public WorkerSearchController() {

    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        comp.setVariable("controller", this, true);
        initController();
    }

    /**
     * Initializes ZUL components
     */
    private void initController() {
        // Add event listener onSelect to criterionsTree widget
        Tree tree = (Tree) self.getFellowIfAny("criterionsTree");
        if (tree != null) {
            tree.addEventListener("onSelect", new EventListener() {

                // Whenever an element of the tree is selected, a search query
                // is executed, refreshing the results into the workers listbox
                @Override
                public void onEvent(Event event) throws Exception {
                    searchWorkers(txtName.getValue(), getSelectedCriterions());
                }
            });
        }

        // Feed criterionsTree with criterions
        if (criterionsTree.getModel() == null) {
            criterionsTree.setModel(getCriterions());
        }

        // Set renderer for listboxWorkers
        if (listBoxWorkers.getItemRenderer() == null) {
            listBoxWorkers.setItemRenderer(getListitemRenderer());
        }
    }

    /**
     * Get input text, and search for workers
     *
     * @param event
     */
    public void searchWorkers(InputEvent event) {
        searchWorkers(event.getValue(), getSelectedCriterions());
    }

    /**
     * Does the actual search for workers, and refresh results
     *
     * @param name
     * @param criterions
     */
    private void searchWorkers(String name, List<Criterion> criterions) {
        if (name == null || name.length() == 0) {
            refreshListBoxWorkers(new Worker[0]);
            return;
        }

        List<Worker> listWorkers = workerSearchModel.findWorkers(name,
                getSelectedCriterions());
        refreshListBoxWorkers(listWorkers
                .toArray(new Worker[listWorkers
                .size()]));
    }

    /**
{'fraction_non_alphanumeric': 0.09817073170731708, 'fraction_numerical': 0.001829268292682927, 'mean_word_length': 2.9933049300060866, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4829023', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1872, 'n_words': 411}