com.esofthead.mycollab.module.crm.view.contact.ContactSearchPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.esofthead.mycollab.module.crm.view.contact.ContactSearchPanel.java

Source

/**
 * This file is part of mycollab-web.
 *
 * mycollab-web is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * mycollab-web is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with mycollab-web.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.esofthead.mycollab.module.crm.view.contact;

import com.esofthead.mycollab.common.i18n.GenericI18Enum;
import com.esofthead.mycollab.core.arguments.*;
import com.esofthead.mycollab.core.db.query.Param;
import com.esofthead.mycollab.eventmanager.EventBusFactory;
import com.esofthead.mycollab.module.crm.CrmTypeConstants;
import com.esofthead.mycollab.module.crm.domain.criteria.ContactSearchCriteria;
import com.esofthead.mycollab.module.crm.events.ContactEvent;
import com.esofthead.mycollab.module.crm.i18n.ContactI18nEnum;
import com.esofthead.mycollab.module.crm.ui.components.CrmViewHeader;
import com.esofthead.mycollab.module.crm.view.account.AccountSelectionField;
import com.esofthead.mycollab.module.user.ui.components.ActiveUserListSelect;
import com.esofthead.mycollab.security.RolePermissionCollections;
import com.esofthead.mycollab.vaadin.AppContext;
import com.esofthead.mycollab.vaadin.ui.DefaultGenericSearchPanel;
import com.esofthead.mycollab.vaadin.ui.DynamicQueryParamLayout;
import com.esofthead.mycollab.vaadin.ui.UIConstants;
import com.vaadin.server.FontAwesome;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import org.apache.commons.lang3.StringUtils;
import org.vaadin.maddon.layouts.MHorizontalLayout;

/**
 * 
 * @author MyCollab Ltd.
 * @since 1.0
 * 
 */
@SuppressWarnings("serial")
public class ContactSearchPanel extends DefaultGenericSearchPanel<ContactSearchCriteria> {

    private static Param[] paramFields = new Param[] { ContactSearchCriteria.p_name,
            ContactSearchCriteria.p_account, ContactSearchCriteria.p_leadsource,
            ContactSearchCriteria.p_billingCountry, ContactSearchCriteria.p_shippingCountry,
            ContactSearchCriteria.p_anyPhone, ContactSearchCriteria.p_anyEmail, ContactSearchCriteria.p_anyCity,
            ContactSearchCriteria.p_assignee, ContactSearchCriteria.p_createdtime,
            ContactSearchCriteria.p_lastupdatedtime };

    private HorizontalLayout createSearchTopPanel() {
        final MHorizontalLayout layout = new MHorizontalLayout().withStyleName(UIConstants.HEADER_VIEW)
                .withWidth("100%").withSpacing(true).withMargin(new MarginInfo(true, false, true, false));

        final Label searchtitle = new CrmViewHeader(CrmTypeConstants.CONTACT,
                AppContext.getMessage(ContactI18nEnum.VIEW_LIST_TITLE));
        searchtitle.setStyleName(UIConstants.HEADER_TEXT);
        layout.with(searchtitle).withAlign(searchtitle, Alignment.MIDDLE_LEFT).expand(searchtitle);

        final Button createBtn = new Button(AppContext.getMessage(ContactI18nEnum.BUTTON_NEW_CONTACT),
                new Button.ClickListener() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public void buttonClick(final ClickEvent event) {
                        EventBusFactory.getInstance().post(new ContactEvent.GotoAdd(this, null));
                    }
                });
        createBtn.setIcon(FontAwesome.PLUS);
        createBtn.setStyleName(UIConstants.THEME_GREEN_LINK);
        createBtn.setEnabled(AppContext.canWrite(RolePermissionCollections.CRM_CONTACT));
        layout.with(createBtn).withAlign(createBtn, Alignment.MIDDLE_RIGHT);

        return layout;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected BasicSearchLayout<ContactSearchCriteria> createBasicSearchLayout() {
        return new ContactBasicSearchLayout();
    }

    @Override
    protected SearchLayout<ContactSearchCriteria> createAdvancedSearchLayout() {
        return new ContactAdvancedSearchLayout();
    }

    @SuppressWarnings("rawtypes")
    private class ContactBasicSearchLayout extends BasicSearchLayout {

        private static final long serialVersionUID = 1L;
        private TextField nameField;
        private CheckBox myItemCheckbox;

        @SuppressWarnings("unchecked")
        public ContactBasicSearchLayout() {
            super(ContactSearchPanel.this);
        }

        @Override
        public ComponentContainer constructHeader() {
            return ContactSearchPanel.this.createSearchTopPanel();
        }

        @Override
        public ComponentContainer constructBody() {
            final MHorizontalLayout basicSearchBody = new MHorizontalLayout().withSpacing(true).withMargin(true);
            this.nameField = this.createSeachSupportTextField(new TextField(), "NameFieldOfBasicSearch");
            this.nameField.setWidth(UIConstants.DEFAULT_CONTROL_WIDTH);
            basicSearchBody.with(nameField).withAlign(nameField, Alignment.MIDDLE_CENTER);

            this.myItemCheckbox = new CheckBox(AppContext.getMessage(GenericI18Enum.SEARCH_MYITEMS_CHECKBOX));
            this.myItemCheckbox.setWidth("75px");
            basicSearchBody.with(myItemCheckbox).withAlign(myItemCheckbox, Alignment.MIDDLE_CENTER);

            final Button searchBtn = new Button(AppContext.getMessage(GenericI18Enum.BUTTON_SEARCH));
            searchBtn.setStyleName(UIConstants.THEME_GREEN_LINK);
            searchBtn.setIcon(FontAwesome.SEARCH);

            searchBtn.addClickListener(new Button.ClickListener() {
                @Override
                public void buttonClick(final ClickEvent event) {
                    ContactBasicSearchLayout.this.callSearchAction();
                }
            });
            basicSearchBody.with(searchBtn).withAlign(searchBtn, Alignment.MIDDLE_LEFT);

            final Button cancelBtn = new Button(AppContext.getMessage(GenericI18Enum.BUTTON_CLEAR));
            cancelBtn.setStyleName(UIConstants.THEME_GRAY_LINK);
            cancelBtn.addStyleName("cancel-button");
            cancelBtn.addClickListener(new Button.ClickListener() {
                @Override
                public void buttonClick(final ClickEvent event) {
                    ContactBasicSearchLayout.this.nameField.setValue("");
                }
            });
            basicSearchBody.with(cancelBtn).withAlign(cancelBtn, Alignment.MIDDLE_CENTER);

            final Button advancedSearchBtn = new Button(
                    AppContext.getMessage(GenericI18Enum.BUTTON_ADVANCED_SEARCH), new Button.ClickListener() {
                        private static final long serialVersionUID = 1L;

                        @Override
                        public void buttonClick(final ClickEvent event) {
                            ContactSearchPanel.this.moveToAdvancedSearchLayout();
                        }
                    });
            advancedSearchBtn.setStyleName("link");
            basicSearchBody.with(advancedSearchBtn).withAlign(advancedSearchBtn, Alignment.MIDDLE_CENTER);
            return basicSearchBody;
        }

        @Override
        protected SearchCriteria fillUpSearchCriteria() {
            final ContactSearchCriteria searchCriteria = new ContactSearchCriteria();
            searchCriteria.setSaccountid(new NumberSearchField(SearchField.AND, AppContext.getAccountId()));
            if (StringUtils.isNotBlank(this.nameField.getValue().trim())) {
                searchCriteria
                        .setContactName(new StringSearchField(SearchField.AND, this.nameField.getValue().trim()));
            }

            if (this.myItemCheckbox.getValue()) {
                searchCriteria.setAssignUsers(
                        new SetSearchField<String>(SearchField.AND, new String[] { AppContext.getUsername() }));
            } else {
                searchCriteria.setAssignUsers(null);
            }
            return searchCriteria;
        }
    }

    private class ContactAdvancedSearchLayout extends DynamicQueryParamLayout<ContactSearchCriteria> {

        public ContactAdvancedSearchLayout() {
            super(ContactSearchPanel.this, CrmTypeConstants.CONTACT);
        }

        @Override
        public ComponentContainer constructHeader() {
            return ContactSearchPanel.this.createSearchTopPanel();
        }

        @Override
        public Param[] getParamFields() {
            return paramFields;
        }

        @Override
        protected Class<ContactSearchCriteria> getType() {
            return ContactSearchCriteria.class;
        }

        @Override
        protected Component buildSelectionComp(String fieldId) {
            if ("contact-assignuser".equals(fieldId)) {
                return new ActiveUserListSelect();
            } else if ("contact-account".equals(fieldId)) {
                return new AccountSelectionField();
            }
            return null;
        }
    }
}