com.jiangyifen.ec2.ui.csr.toolbar.CsrPhone2PhoneSettingWindow.java Source code

Java tutorial

Introduction

Here is the source code for com.jiangyifen.ec2.ui.csr.toolbar.CsrPhone2PhoneSettingWindow.java

Source

package com.jiangyifen.ec2.ui.csr.toolbar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jiangyifen.ec2.bean.DayOfWeek;
import com.jiangyifen.ec2.entity.Domain;
import com.jiangyifen.ec2.entity.Phone2PhoneSetting;
import com.jiangyifen.ec2.entity.User;
import com.jiangyifen.ec2.service.eaoservice.Phone2PhoneSettingService;
import com.jiangyifen.ec2.utils.SpringContextHolder;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * ???
 * 
 * @author jrh
 *
 */
@SuppressWarnings("serial")
public class CsrPhone2PhoneSettingWindow extends Window implements ClickListener, ValueChangeListener {
    // 
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    // ?
    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");

    private Notification notification; // ???
    private Label noticeLabel; // ???????

    private OptionGroup startSettingOption; // ???
    private ComboBox dayOfWeekTypeSelector; // (??)
    private OptionGroup daysOfWeekOption; // (??...)
    private HorizontalLayout dayOfWeekLayout; // dayOfWeekOption
    private ComboBox startRedirectHour; // ??
    private ComboBox startRedirectMinute; // ?
    private ComboBox stopRedirectHour; // ???
    private ComboBox stopRedirectMinute; // ??
    private OptionGroup redirectTypeOption; // (noanswer?unonline?busy)
    private ComboBox noanswerTimeoutSelector; // noanswer ?
    private HorizontalLayout noanswerLayout; //  noanswerTimeoutSelector

    // ?
    private Button edit; // ?
    private Button save; // ??
    private Button cancel; // ?
    private HorizontalLayout operatorLayout; // 

    /**
     * ?
     */
    private Domain domain; // ?
    private User loginUser; // ?
    private boolean isGlobalP2PSetting; // ???????
    private Phone2PhoneSetting customP2PSetting; // ?
    private Phone2PhoneSettingService phone2PhoneSettingService; // ??

    public CsrPhone2PhoneSettingWindow() {
        this.setWidth("413px");
        this.setHeight("220px");
        this.setResizable(false);

        VerticalLayout mainLayout = new VerticalLayout();
        mainLayout.setWidth("100%");
        mainLayout.setSpacing(true);
        mainLayout.setMargin(true);
        this.setContent(mainLayout);

        domain = SpringContextHolder.getDomain();
        loginUser = SpringContextHolder.getLoginUser();
        phone2PhoneSettingService = SpringContextHolder.getBean("phone2PhoneSettingService");

        notification = new Notification("");
        notification.setDelayMsec(1000);
        notification.setHtmlContentAllowed(true);

        // ???????
        String notice = "<font color='red'><B>??????</B></font>";
        noticeLabel = new Label(notice, Label.CONTENT_XHTML);
        noticeLabel.setVisible(false);
        mainLayout.addComponent(noticeLabel);

        // ??? 
        this.customP2PSetting = initializeCustomP2PSetting();

        // ?
        createlStartSetting(mainLayout);

        // 
        createlDaysOfWeekType(mainLayout);

        // 
        createlDayOfWeek(mainLayout);

        // 
        createRunRedirectTime(mainLayout);

        // 
        createlRedirectType(mainLayout);

        // ?? 
        createNoanwserTimeout(mainLayout);

        // ?
        createOperatorButtons(mainLayout);
    }

    @Override
    public void attach() {
        // ?
        this.center();
        this.isGlobalP2PSetting = false;

        //  1????2?? <= ?3??????4???????
        Phone2PhoneSetting globalSetting = phone2PhoneSettingService.getGlobalSettingByDomain(domain.getId());
        if (globalSetting == null) {
            // ??
            changeComponentsStatus(true);
            noticeLabel.setVisible(true);
            edit.setVisible(false);
            return;
        }

        // ????  TODO ???
        if (!globalSetting.getIsLicensed2Csr()) { // 1?????
            isGlobalP2PSetting = true;
        } else if (!globalSetting.getIsSpecifiedPhones()) { // 2?????????
            for (User csr : globalSetting.getSpecifiedCsrs()) { // ????
                if (csr.getId() == loginUser.getId()) {
                    isGlobalP2PSetting = true;
                    break;
                }
            }
        } else if (globalSetting.getIsStartedRedirect()) { // 3????????
            boolean beforeNowGlobal = phone2PhoneSettingService.confirmSettingIsRunning(globalSetting);
            isGlobalP2PSetting = beforeNowGlobal ? true : false; // 
        }

        if (isGlobalP2PSetting) { // ?
            this.setCaption("?");
            refreshSettingInfos(globalSetting);
        } else { // ?
            this.setCaption("?");
            refreshSettingInfos(this.customP2PSetting);
        }
    }

    /**
     * ??? 
     * @return
     */
    private Phone2PhoneSetting initializeCustomP2PSetting() {
        Phone2PhoneSetting p2pSetting = phone2PhoneSettingService.getByUser(loginUser.getId());
        // ??
        if (p2pSetting == null) {
            p2pSetting = new Phone2PhoneSetting();
            p2pSetting.setIsStartedRedirect(false);
            p2pSetting.setDayOfWeekType("weekday");

            Set<DayOfWeek> daysOfWeek = new HashSet<DayOfWeek>();
            daysOfWeek.add(DayOfWeek.mon);
            daysOfWeek.add(DayOfWeek.fri);
            p2pSetting.setDaysOfWeek(daysOfWeek);

            p2pSetting.setStartTime("18:00");
            p2pSetting.setStopTime("22:59");

            Set<String> types = new HashSet<String>();
            types.add("unonline");
            p2pSetting.setRedirectTypes(types);

            p2pSetting.setIsSpecifiedPhones(true);
            p2pSetting.setCreator(loginUser);
            p2pSetting.setDomain(domain);
        }
        return p2pSetting;
    }

    /**
     * ?
     * @param mainLayout
     */
    private void createlStartSetting(VerticalLayout mainLayout) {
        HorizontalLayout layout = new HorizontalLayout();
        layout.setSpacing(true);
        mainLayout.addComponent(layout);

        Label caption = new Label("??");
        caption.setWidth("-1px");
        caption.setDescription(
                "<B>????</B>");
        layout.addComponent(caption);

        startSettingOption = new OptionGroup();
        startSettingOption.addItem(true);
        startSettingOption.addItem(false);
        startSettingOption.setItemCaption(true, "?");
        startSettingOption.setItemCaption(false, "");
        startSettingOption.setImmediate(true);
        startSettingOption.setValue(false);
        startSettingOption.setReadOnly(true);
        startSettingOption.setDescription(
                "<B>????</B>");
        startSettingOption.setNullSelectionAllowed(false);
        startSettingOption.addStyleName("twocol200");
        startSettingOption.addStyleName("myopacity");
        layout.addComponent(startSettingOption);
    }

    /**
     *  (????)
     * @param mainLayout
     */
    private void createlDaysOfWeekType(VerticalLayout mainLayout) {
        HorizontalLayout layout = new HorizontalLayout();
        layout.setSpacing(true);
        mainLayout.addComponent(layout);

        Label caption = new Label("");
        caption.setWidth("-1px");
        caption.setDescription("<B>?</B>");
        layout.addComponent(caption);

        dayOfWeekTypeSelector = new ComboBox();
        dayOfWeekTypeSelector.addItem("weekday");
        dayOfWeekTypeSelector.setItemCaption("weekday", "");
        dayOfWeekTypeSelector.addItem("weekend");
        dayOfWeekTypeSelector.setItemCaption("weekend", "");
        dayOfWeekTypeSelector.addItem("custom");
        dayOfWeekTypeSelector.setItemCaption("custom", "");
        dayOfWeekTypeSelector.setWidth("200px");
        dayOfWeekTypeSelector.setImmediate(true);
        dayOfWeekTypeSelector.setReadOnly(true);
        dayOfWeekTypeSelector.setDescription("<B>?</B>");
        dayOfWeekTypeSelector.setNullSelectionAllowed(false);
        dayOfWeekTypeSelector.addListener(this);
        layout.addComponent(dayOfWeekTypeSelector);
    }

    /**
     * (??...) ???
     * @param mainLayout
     */
    private void createlDayOfWeek(VerticalLayout mainLayout) {
        dayOfWeekLayout = new HorizontalLayout();
        dayOfWeekLayout.setSpacing(true);
        dayOfWeekLayout.setVisible(false);
        mainLayout.addComponent(dayOfWeekLayout);

        Label caption = new Label("");
        caption.setWidth("-1px");
        caption.setDescription("<B>?</B>");
        dayOfWeekLayout.addComponent(caption);

        BeanItemContainer<DayOfWeek> container = new BeanItemContainer<DayOfWeek>(DayOfWeek.class);
        container.addBean(DayOfWeek.sun);
        container.addBean(DayOfWeek.mon);
        container.addBean(DayOfWeek.tue);
        container.addBean(DayOfWeek.wen);
        container.addBean(DayOfWeek.thu);
        container.addBean(DayOfWeek.fri);
        container.addBean(DayOfWeek.sat);

        daysOfWeekOption = new OptionGroup();
        daysOfWeekOption.setContainerDataSource(container);
        daysOfWeekOption.setItemCaptionPropertyId("name");
        daysOfWeekOption.setMultiSelect(true);
        daysOfWeekOption.setNullSelectionAllowed(false);
        daysOfWeekOption.setImmediate(true);
        daysOfWeekOption.setReadOnly(true);
        daysOfWeekOption.setDescription("<B>?</B>");
        daysOfWeekOption.addStyleName("threecol300");
        daysOfWeekOption.addStyleName("myopacity");
        dayOfWeekLayout.addComponent(daysOfWeekOption);
    }

    /**
     * ? (18:00 - 23:59 )
     * @param mainLayout
     */
    private void createRunRedirectTime(VerticalLayout mainLayout) {
        HorizontalLayout layout = new HorizontalLayout();
        layout.setSpacing(true);
        mainLayout.addComponent(layout);

        Label caption = new Label("");
        caption.setWidth("-1px");
        caption.setDescription("<B></B>");
        layout.addComponent(caption);

        startRedirectHour = new ComboBox();
        startRedirectHour.setImmediate(true);
        startRedirectHour.setWidth("50px");
        startRedirectHour.setNullSelectionAllowed(false);
        layout.addComponent(startRedirectHour);

        Label space1 = new Label(" ");
        space1.setWidth("-1px");
        layout.addComponent(space1);

        startRedirectMinute = new ComboBox();
        startRedirectMinute.setImmediate(true);
        startRedirectMinute.setWidth("50px");
        startRedirectMinute.setNullSelectionAllowed(false);
        layout.addComponent(startRedirectMinute);

        Label to = new Label(" - ");
        to.setWidth("-1px");
        layout.addComponent(to);

        stopRedirectHour = new ComboBox();
        stopRedirectHour.setImmediate(true);
        stopRedirectHour.setWidth("50px");
        stopRedirectHour.setNullSelectionAllowed(false);
        layout.addComponent(stopRedirectHour);

        Label space2 = new Label(" ");
        space2.setWidth("-1px");
        layout.addComponent(space2);

        stopRedirectMinute = new ComboBox();
        stopRedirectMinute.setImmediate(true);
        stopRedirectMinute.setWidth("50px");
        stopRedirectMinute.setNullSelectionAllowed(false);
        layout.addComponent(stopRedirectMinute);

        for (int hour = 0; hour < 24; hour++) {
            startRedirectHour.addItem(hour);
            stopRedirectHour.addItem(hour);
        }

        for (int minute = 0; minute < 60; minute++) {
            startRedirectMinute.addItem(minute);
            stopRedirectMinute.addItem(minute);
        }

        startRedirectHour.setValue(18);
        stopRedirectHour.setValue(23);
        startRedirectMinute.setValue(0);
        stopRedirectMinute.setValue(59);

        startRedirectHour.setReadOnly(true);
        startRedirectMinute.setReadOnly(true);
        stopRedirectHour.setReadOnly(true);
        stopRedirectMinute.setReadOnly(true);
    }

    /**
     *  (noanswer?unonline?busy?force)
     * @param mainLayout
     */
    private void createlRedirectType(VerticalLayout mainLayout) {
        HorizontalLayout layout = new HorizontalLayout();
        layout.setSpacing(true);
        mainLayout.addComponent(layout);

        Label caption = new Label("");
        caption.setWidth("-1px");
        caption.setDescription("<B>????</B>");
        layout.addComponent(caption);

        redirectTypeOption = new OptionGroup();
        redirectTypeOption.addItem("noanswer");
        redirectTypeOption.setItemCaption("noanswer", "?");
        redirectTypeOption.addItem("busy");
        redirectTypeOption.setItemCaption("busy", "");
        redirectTypeOption.addItem("unonline");
        redirectTypeOption.setItemCaption("unonline", "");
        redirectTypeOption.setNullSelectionAllowed(false);
        redirectTypeOption.addListener(this);
        redirectTypeOption.setImmediate(true);
        redirectTypeOption.setMultiSelect(true);
        redirectTypeOption.addStyleName("threecol300");
        redirectTypeOption.addStyleName("myopacity");
        redirectTypeOption.setReadOnly(true);
        redirectTypeOption
                .setDescription("<B>????</B>");
        layout.addComponent(redirectTypeOption);
    }

    /**
     * ?? 
     * @param mainLayout
     */
    private void createNoanwserTimeout(VerticalLayout mainLayout) {
        noanswerLayout = new HorizontalLayout();
        noanswerLayout.setSpacing(true);
        noanswerLayout.setVisible(false);
        mainLayout.addComponent(noanswerLayout);

        Label freCaption = new Label("?");
        freCaption.setWidth("-1px");
        freCaption.setDescription(
                "<B>'?'??</B>");
        noanswerLayout.addComponent(freCaption);

        noanswerTimeoutSelector = new ComboBox();
        noanswerTimeoutSelector.setImmediate(true);
        noanswerTimeoutSelector.setWidth("50px");
        noanswerTimeoutSelector.setNullSelectionAllowed(false);
        noanswerTimeoutSelector.setDescription(
                "<B>'?'??</B>");
        noanswerLayout.addComponent(noanswerTimeoutSelector);

        for (int seconds = 3; seconds < 61; seconds++) {
            noanswerTimeoutSelector.addItem(seconds);
        }
        noanswerTimeoutSelector.setValue(10);
        noanswerTimeoutSelector.setReadOnly(true);

        Label postCaption = new Label(" ");
        postCaption.setWidth("-1px");
        postCaption.setDescription(
                "<B>'?'??</B>");
        noanswerLayout.addComponent(postCaption);
    }

    /**
     * ?
     * @return
     */
    private void createOperatorButtons(VerticalLayout mainLayout) {
        operatorLayout = new HorizontalLayout();
        operatorLayout.setSpacing(true);
        mainLayout.addComponent(operatorLayout);

        // 
        edit = new Button(" ", this);
        edit.setStyleName("default");
        edit.setVisible(true);
        edit.setImmediate(true);
        operatorLayout.addComponent(edit);

        // ?
        save = new Button("? ", this);
        save.setStyleName("default");
        save.setVisible(false);
        save.setImmediate(true);
        operatorLayout.addComponent(save);

        // ?
        cancel = new Button("? ", this);
        operatorLayout.addComponent(cancel);
        cancel.setVisible(false);
        cancel.setImmediate(true);
    }

    /**
     * ??
     */
    private void refreshSettingInfos(Phone2PhoneSetting p2pSetting) {
        // ?
        changeComponentsStatus(false);

        // ?
        startSettingOption.setValue(p2pSetting.getIsStartedRedirect());
        // 
        dayOfWeekTypeSelector.setValue(p2pSetting.getDayOfWeekType());

        // ?
        Set<DayOfWeek> dayOfWeeks = p2pSetting.getDaysOfWeek();
        daysOfWeekOption.setValue(dayOfWeeks);

        // 
        String[] startTimeStrs = p2pSetting.getStartTime().split(":");
        startRedirectHour.setValue(Integer.parseInt(startTimeStrs[0]));
        startRedirectMinute.setValue(Integer.parseInt(startTimeStrs[1]));

        String[] stopTimeStrs = p2pSetting.getStopTime().split(":");
        stopRedirectHour.setValue(Integer.parseInt(stopTimeStrs[0]));
        stopRedirectMinute.setValue(Integer.parseInt(stopTimeStrs[1]));

        // 
        Set<String> redirectTypes = p2pSetting.getRedirectTypes();
        redirectTypeOption.setValue(redirectTypes);
        // 
        noanswerLayout.setVisible(redirectTypes.contains("noanswer"));
        noanswerTimeoutSelector.setValue(p2pSetting.getNoanswerTimeout());

        // ??
        changeComponentsStatus(true);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void valueChange(ValueChangeEvent event) {
        Property source = event.getProperty();
        if (source == dayOfWeekTypeSelector) {
            String type = (String) dayOfWeekTypeSelector.getValue();
            boolean isCustomType = "custom".equals(type);
            dayOfWeekLayout.setVisible(isCustomType);
            if (this.getHeight() < 275 && isCustomType) {
                float height = this.getHeight() + 55;
                this.setHeight(height + "px");
            } else if (this.getHeight() >= 275 && !isCustomType) {
                float height = this.getHeight() - 55;
                this.setHeight(height + "px");
            }
            this.center();
        } else if (source == redirectTypeOption) {
            Set<String> redirectTypes = (Set<String>) redirectTypeOption.getValue();
            boolean iscontain = redirectTypes.contains("noanswer");
            if ((this.getHeight() == 220 || this.getHeight() == 275) && iscontain) {
                float height = this.getHeight() + 15;
                this.setHeight(height + "px");
            } else if ((this.getHeight() == 235 || this.getHeight() == 290) && !iscontain) {
                float height = this.getHeight() - 15;
                this.setHeight(height + "px");
            }
            if (noanswerLayout != null) {
                noanswerLayout.setVisible(iscontain);
            }
            this.center();
        }
    }

    @Override
    public void buttonClick(ClickEvent event) {
        Button source = event.getButton();
        if (source == edit) {
            changeComponentsStatus(false);
        } else if (source == save) {
            try {
                String[] runTimeScope = executeConfirmRunTime();
                if (runTimeScope != null) {
                    executeSave(runTimeScope);
                }
                notification.setCaption("<B>??!</B>");
                this.getApplication().getMainWindow().showNotification(notification);
                // ?
                refreshSettingInfos(this.customP2PSetting);
            } catch (Exception e) {
                logger.error("Manager ??? --> " + e.getMessage(), e);
                notification.setCaption("<font color='red'>?????</font>");
                this.getApplication().getMainWindow().showNotification(notification);
            }
        } else if (source == cancel) {
            refreshSettingInfos(this.customP2PSetting);
        }
    }

    /**
     * ????
     * @param readOnly
     */
    private void changeComponentsStatus(boolean readOnly) {
        // ??
        startSettingOption.setReadOnly(readOnly);
        dayOfWeekTypeSelector.setReadOnly(readOnly);
        daysOfWeekOption.setReadOnly(readOnly);
        startRedirectHour.setReadOnly(readOnly);
        startRedirectMinute.setReadOnly(readOnly);
        stopRedirectHour.setReadOnly(readOnly);
        stopRedirectMinute.setReadOnly(readOnly);
        redirectTypeOption.setReadOnly(readOnly);
        noanswerTimeoutSelector.setReadOnly(readOnly);

        // ??
        edit.setVisible(readOnly);
        save.setVisible(!readOnly);
        cancel.setVisible(!readOnly);

        noticeLabel.setVisible(isGlobalP2PSetting);
        operatorLayout.setVisible(!isGlobalP2PSetting);
    }

    /**
     * ?????
     * @return boolean 
     * @throws ParseException
     */
    private String[] executeConfirmRunTime() {
        String[] runTimeScope = new String[2];
        try {
            // ??? < ?
            StringBuffer startTimeStr = new StringBuffer();
            startTimeStr.append(((Integer) startRedirectHour.getValue()).toString());
            startTimeStr.append(":");
            startTimeStr.append(((Integer) startRedirectMinute.getValue()).toString());
            Date startDate = simpleDateFormat.parse(startTimeStr.toString());

            StringBuffer stopTimeStr = new StringBuffer();
            stopTimeStr.append(((Integer) stopRedirectHour.getValue()).toString());
            stopTimeStr.append(":");
            stopTimeStr.append(((Integer) stopRedirectMinute.getValue()).toString());
            Date stopDate = simpleDateFormat.parse(stopTimeStr.toString());
            if (startDate.after(stopDate)) {
                notification.setCaption(
                        "<font color='red'>???18:00-22:00!</font>");
                save.getApplication().getMainWindow().showNotification(notification);
                return null;
            }

            runTimeScope[0] = startTimeStr.toString();
            runTimeScope[1] = stopTimeStr.toString();
        } catch (Exception e) {
            logger.error("Manager ? ?? ---> " + e.getMessage(), e);
            notification.setCaption("<font color='red'>??!</font>");
            this.getApplication().getMainWindow().showNotification(notification);
            return null;
        }
        return runTimeScope;
    }

    /**
     * ????
     */
    @SuppressWarnings("unchecked")
    private void executeSave(String[] runTimeScope) {
        // ??
        if (customP2PSetting == null) {
            customP2PSetting = new Phone2PhoneSetting();
        }
        // ??
        Boolean isStartSetting = (Boolean) startSettingOption.getValue();
        String dayOfWeekType = (String) dayOfWeekTypeSelector.getValue();
        Set<DayOfWeek> daysOfWeek = (Set<DayOfWeek>) daysOfWeekOption.getValue();
        Set<String> redirectTypes = (Set<String>) redirectTypeOption.getValue();
        Integer noanswerTimeout = (Integer) noanswerTimeoutSelector.getValue();

        // ?
        customP2PSetting.setIsStartedRedirect(isStartSetting);
        customP2PSetting.setDayOfWeekType(dayOfWeekType);
        if ("custom".equals(dayOfWeekType)) {
            customP2PSetting.setDaysOfWeek(daysOfWeek);
        }
        customP2PSetting.setStartTime(runTimeScope[0]);
        customP2PSetting.setStopTime(runTimeScope[1]);
        customP2PSetting.setRedirectTypes(redirectTypes);
        customP2PSetting.setNoanswerTimeout(noanswerTimeout);

        // ??
        customP2PSetting = phone2PhoneSettingService.update(customP2PSetting);

        // 
        if (customP2PSetting.getIsStartedRedirect()) {
            phone2PhoneSettingService.createP2PScheduler(customP2PSetting);
        } else {
            phone2PhoneSettingService.stopP2PScheduler(customP2PSetting);
        }
    }

}