jp.primecloud.auto.ui.WinUserAuthEditDetail.java Source code

Java tutorial

Introduction

Here is the source code for jp.primecloud.auto.ui.WinUserAuthEditDetail.java

Source

/*
 * Copyright 2014 by SCSK Corporation.
 * 
 * This file is part of PrimeCloud Controller(TM).
 * 
 * PrimeCloud Controller(TM) 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 2 of the License, or
 * (at your option) any later version.
 * 
 * PrimeCloud Controller(TM) 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 PrimeCloud Controller(TM). If not, see <http://www.gnu.org/licenses/>.
 */
package jp.primecloud.auto.ui;

import org.apache.commons.lang.BooleanUtils;

import jp.primecloud.auto.entity.crud.UserAuth;
import jp.primecloud.auto.exception.AutoApplicationException;
import jp.primecloud.auto.service.UserManagementService;
import jp.primecloud.auto.service.UserService;
import jp.primecloud.auto.service.dto.UserAuthDto;
import jp.primecloud.auto.ui.util.BeanContext;
import jp.primecloud.auto.ui.util.Icons;
import jp.primecloud.auto.ui.util.ViewMessages;
import jp.primecloud.auto.ui.util.ViewProperties;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * <p>
 * ?????
 * </p>
 *
 */
@SuppressWarnings("serial")
public class WinUserAuthEditDetail extends Window {

    /////  /////
    //?(??)
    private Long userNo;
    //?
    private Long farmNo;
    //(?)
    private UserAuthDto userAuthDto;

    ///// vaadin component /////
    // 
    private ComboBox myCloudSelect;
    //?? 
    private ComboBox serverMakeSelect;
    //? 
    private ComboBox serverDeleteSelect;
    //?? 
    private ComboBox serverOperateSelect;
    //? 
    private ComboBox serviceMakeSelect;
    // 
    private ComboBox serviceDeleteSelect;
    //? 
    private ComboBox serviceOperateSelect;
    //?? 
    private ComboBox lbMakeSelect;
    //? 
    private ComboBox lbDeleteSelect;
    //?? 
    private ComboBox lbOperateSelect;

    WinUserAuthEditDetail(Long userNo, Long farmNo) {

        this.userNo = userNo;
        this.farmNo = farmNo;

        //
        setIcon(Icons.EDIT.resource());
        setCaption(ViewProperties.getCaption("window.winUserDetailEdit"));

        //
        setModal(true);
        setWidth("300px");
        //
        setResizable(false);

        //
        VerticalLayout layout = (VerticalLayout) getContent();
        layout.setMargin(false, true, false, true);
        layout.setSpacing(false);

        layout.addComponent(new AuthForm());

        // ??
        HorizontalLayout okbar = new HorizontalLayout();
        okbar.setSpacing(true);
        okbar.setMargin(false, false, true, false);
        layout.addComponent(okbar);
        layout.setComponentAlignment(okbar, Alignment.BOTTOM_RIGHT);

        // OK
        Button okButton = new Button(ViewProperties.getCaption("button.ok"));
        okButton.setDescription(ViewProperties.getCaption("description.editUserAuthDetail.ok"));
        okButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                WinUserAuthEditDetail.this.okButtonClick(event);
            }
        });
        okbar.addComponent(okButton);

        // Cancel
        Button cancelButton = new Button(ViewProperties.getCaption("button.cancel"));
        cancelButton.setDescription(ViewProperties.getCaption("description.cancel"));
        cancelButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                WinUserAuthEditDetail.this.cancelButtonClick(event);
            }
        });
        okbar.addComponent(cancelButton);

        // ??
        initData();

        // ?
        showData();

        // ??
        initValidation();
    }

    private void setCustomErrorHandler(Button button) {
        //?????PCC??????????
        if (button.getErrorHandler() == null
                || button.getErrorHandler().getClass() != ComponentsErrorHandler.class) {
            button.setErrorHandler(new ComponentsErrorHandler(this.getParent()));
        }
    }

    private class AuthForm extends Form {

        private static final String PID_MYCLOUD_AUTH = "MyCloudAuth";
        private static final String PID_AUTH = "Auth";

        private AuthForm() {
            //()
            Label myCloudLbl = new Label(ViewProperties.getCaption("label.userAuth.myCloud"));

            //
            myCloudSelect = new ComboBox();
            myCloudSelect.setNullSelectionAllowed(false);
            myCloudSelect.setItemCaptionPropertyId(PID_MYCLOUD_AUTH);
            myCloudSelect.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
            myCloudSelect.setContainerDataSource(createMyCloudAuthContainer());
            myCloudSelect.setImmediate(true);
            myCloudSelect.addListener(createMyCloudChange());

            //()
            Label serverLbl = new Label(ViewProperties.getCaption("label.userAuth.server"));

            //??()
            Label serverMakeLbl = new Label(ViewProperties.getCaption("label.userAuth.authType.make"));

            //??
            serverMakeSelect = new ComboBox();
            serverMakeSelect.setNullSelectionAllowed(false);
            serverMakeSelect.setItemCaptionPropertyId(PID_AUTH);
            serverMakeSelect.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
            serverMakeSelect.setContainerDataSource(createAuthContainer());
            serverMakeSelect.setImmediate(true);

            //?()
            Label serverDeleteLbl = new Label(ViewProperties.getCaption("label.userAuth.authType.delete"));

            //?
            serverDeleteSelect = new ComboBox();
            serverDeleteSelect.setNullSelectionAllowed(false);
            serverDeleteSelect.setItemCaptionPropertyId(PID_AUTH);
            serverDeleteSelect.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
            serverDeleteSelect.setContainerDataSource(createAuthContainer());
            serverDeleteSelect.setImmediate(true);

            //??()
            Label serverOperateLbl = new Label(ViewProperties.getCaption("label.userAuth.authType.operate"));

            //??
            serverOperateSelect = new ComboBox();
            serverOperateSelect.setNullSelectionAllowed(false);
            serverOperateSelect.setItemCaptionPropertyId(PID_AUTH);
            serverOperateSelect.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
            serverOperateSelect.setContainerDataSource(createAuthContainer());
            serverOperateSelect.setImmediate(true);

            //()
            Label serviceLbl = new Label(ViewProperties.getCaption("label.userAuth.service"));

            //?()
            Label serviceMakeLbl = new Label(ViewProperties.getCaption("label.userAuth.authType.make"));

            //?
            serviceMakeSelect = new ComboBox();
            serviceMakeSelect.setNullSelectionAllowed(false);
            serviceMakeSelect.setItemCaptionPropertyId(PID_AUTH);
            serviceMakeSelect.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
            serviceMakeSelect.setContainerDataSource(createAuthContainer());
            serviceMakeSelect.setImmediate(true);

            //()
            Label serviceDeleteLbl = new Label(ViewProperties.getCaption("label.userAuth.authType.delete"));

            //
            serviceDeleteSelect = new ComboBox();
            serviceDeleteSelect.setNullSelectionAllowed(false);
            serviceDeleteSelect.setItemCaptionPropertyId(PID_AUTH);
            serviceDeleteSelect.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
            serviceDeleteSelect.setContainerDataSource(createAuthContainer());
            serviceDeleteSelect.setImmediate(true);

            //?()
            Label serviceOperateLbl = new Label(ViewProperties.getCaption("label.userAuth.authType.operate"));

            //?
            serviceOperateSelect = new ComboBox();
            serviceOperateSelect.setNullSelectionAllowed(false);
            serviceOperateSelect.setItemCaptionPropertyId(PID_AUTH);
            serviceOperateSelect.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
            serviceOperateSelect.setContainerDataSource(createAuthContainer());
            serviceOperateSelect.setImmediate(true);

            //?()
            Label lbLbl = new Label(ViewProperties.getCaption("label.userAuth.lb"));

            //?()
            Label lbMakeLbl = new Label(ViewProperties.getCaption("label.userAuth.authType.make"));

            //??
            lbMakeSelect = new ComboBox();
            lbMakeSelect.setNullSelectionAllowed(false);
            lbMakeSelect.setItemCaptionPropertyId(PID_AUTH);
            lbMakeSelect.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
            lbMakeSelect.setContainerDataSource(createAuthContainer());
            lbMakeSelect.setImmediate(true);

            //?()
            Label lbDeleteLbl = new Label(ViewProperties.getCaption("label.userAuth.authType.delete"));

            //?
            lbDeleteSelect = new ComboBox();
            lbDeleteSelect.setNullSelectionAllowed(false);
            lbDeleteSelect.setItemCaptionPropertyId(PID_AUTH);
            lbDeleteSelect.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
            lbDeleteSelect.setContainerDataSource(createAuthContainer());
            lbDeleteSelect.setImmediate(true);

            //??()
            Label lbOperateLbl = new Label(ViewProperties.getCaption("label.userAuth.authType.operate"));

            //??
            lbOperateSelect = new ComboBox();
            lbOperateSelect.setNullSelectionAllowed(false);
            lbOperateSelect.setItemCaptionPropertyId(PID_AUTH);
            lbOperateSelect.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
            lbOperateSelect.setContainerDataSource(createAuthContainer());
            lbOperateSelect.setImmediate(true);

            /////////////// ? //////////////////////
            //
            AbsoluteLayout myCloudLayout = createAbsoluteLayout();
            myCloudLayout.addComponent(myCloudLbl, "left:0%");
            myCloudLayout.addComponent(myCloudSelect, "left:40%");
            getLayout().addComponent(myCloudLayout);

            //?(??)
            AbsoluteLayout serverLblLayout = createAbsoluteLayout();
            serverLblLayout.addComponent(serverLbl, "left:0%");
            getLayout().addComponent(serverLblLayout);

            //??
            AbsoluteLayout serverMakeLayout = createAbsoluteLayout();
            serverMakeLayout.addComponent(serverMakeLbl, "left:0%");
            serverMakeLayout.addComponent(serverMakeSelect, "left:40%");
            getLayout().addComponent(serverMakeLayout);

            //?
            AbsoluteLayout serverDeleteLayout = createAbsoluteLayout();
            serverDeleteLayout.addComponent(serverDeleteLbl, "left:0%");
            serverDeleteLayout.addComponent(serverDeleteSelect, "left:40%");
            getLayout().addComponent(serverDeleteLayout);

            //??
            AbsoluteLayout serverOperateLayout = createAbsoluteLayout();
            serverOperateLayout.addComponent(serverOperateLbl, "left:0%");
            serverOperateLayout.addComponent(serverOperateSelect, "left:40%");
            getLayout().addComponent(serverOperateLayout);

            //?(??)
            AbsoluteLayout serviceLblLayout = createAbsoluteLayout();
            serviceLblLayout.addComponent(serviceLbl, "left:0%");
            getLayout().addComponent(serviceLblLayout);

            //?
            AbsoluteLayout serviceMakeLayout = createAbsoluteLayout();
            serviceMakeLayout.addComponent(serviceMakeLbl, "left:0%");
            serviceMakeLayout.addComponent(serviceMakeSelect, "left:40%");
            getLayout().addComponent(serviceMakeLayout);

            //
            AbsoluteLayout serviceDeleteLayout = createAbsoluteLayout();
            serviceDeleteLayout.addComponent(serviceDeleteLbl, "left:0%");
            serviceDeleteLayout.addComponent(serviceDeleteSelect, "left:40%");
            getLayout().addComponent(serviceDeleteLayout);

            //?
            AbsoluteLayout serviceOperateLayout = createAbsoluteLayout();
            serviceOperateLayout.addComponent(serviceOperateLbl, "left:0%");
            serviceOperateLayout.addComponent(serviceOperateSelect, "left:40%");
            getLayout().addComponent(serviceOperateLayout);

            //?(??)
            AbsoluteLayout lbLblLayout = createAbsoluteLayout();
            lbLblLayout.addComponent(lbLbl, "left:0%");
            getLayout().addComponent(lbLblLayout);

            //??
            AbsoluteLayout lbMakeLayout = createAbsoluteLayout();
            lbMakeLayout.addComponent(lbMakeLbl, "left:0%");
            lbMakeLayout.addComponent(lbMakeSelect, "left:40%");
            getLayout().addComponent(lbMakeLayout);

            //?
            AbsoluteLayout lbDeleteLayout = createAbsoluteLayout();
            lbDeleteLayout.addComponent(lbDeleteLbl, "left:0%");
            lbDeleteLayout.addComponent(lbDeleteSelect, "left:40%");
            getLayout().addComponent(lbDeleteLayout);

            //??
            AbsoluteLayout lbOperateLayout = createAbsoluteLayout();
            lbOperateLayout.addComponent(lbOperateLbl, "left:0%");
            lbOperateLayout.addComponent(lbOperateSelect, "left:40%");
            getLayout().addComponent(lbOperateLayout);
        }

        private ValueChangeListener createMyCloudChange() {
            ValueChangeListener listener = new Property.ValueChangeListener() {
                @Override
                public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
                    //??FALSE???????
                    Boolean isFarmUse = (Boolean) myCloudSelect.getValue();

                    //?
                    serverMakeSelect.setEnabled(BooleanUtils.isTrue(isFarmUse));
                    serverDeleteSelect.setEnabled(BooleanUtils.isTrue(isFarmUse));
                    serverOperateSelect.setEnabled(BooleanUtils.isTrue(isFarmUse));

                    //
                    serviceMakeSelect.setEnabled(BooleanUtils.isTrue(isFarmUse));
                    serviceDeleteSelect.setEnabled(BooleanUtils.isTrue(isFarmUse));
                    serviceOperateSelect.setEnabled(BooleanUtils.isTrue(isFarmUse));

                    //?
                    lbMakeSelect.setEnabled(BooleanUtils.isTrue(isFarmUse));
                    lbDeleteSelect.setEnabled(BooleanUtils.isTrue(isFarmUse));
                    lbOperateSelect.setEnabled(BooleanUtils.isTrue(isFarmUse));
                }
            };
            return listener;
        }

        private AbsoluteLayout createAbsoluteLayout() {
            AbsoluteLayout layout = new AbsoluteLayout();
            layout.setWidth("100%");
            layout.setHeight("27px");
            layout.setMargin(true, false, true, false);
            return layout;
        }

        private IndexedContainer createMyCloudAuthContainer() {
            IndexedContainer authContainer = new IndexedContainer();
            authContainer.addContainerProperty(PID_MYCLOUD_AUTH, String.class, null);

            //?
            Item itemEnable = authContainer.addItem(true);
            itemEnable.getItemProperty(PID_MYCLOUD_AUTH)
                    .setValue(ViewProperties.getCaption("label.userAuth.available"));

            //??
            Item itemDisable = authContainer.addItem(false);
            itemDisable.getItemProperty(PID_MYCLOUD_AUTH)
                    .setValue(ViewProperties.getCaption("label.userAuth.notAvailable"));

            return authContainer;
        }

        private IndexedContainer createAuthContainer() {
            IndexedContainer authContainer = new IndexedContainer();
            authContainer.addContainerProperty(PID_AUTH, String.class, null);

            //?
            Item itemEnable = authContainer.addItem(true);
            itemEnable.getItemProperty(PID_AUTH).setValue(ViewProperties.getCaption("label.userAuth.possible"));

            //??
            Item itemDisable = authContainer.addItem(false);
            itemDisable.getItemProperty(PID_AUTH).setValue(ViewProperties.getCaption("label.userAuth.impossible"));

            return authContainer;
        }
    }

    private void initValidation() {
        //TODO
    }

    private void initData() {
        //??
        UserService userService = BeanContext.getBean(UserService.class);
        this.userAuthDto = userService.getUserAuth(userNo, farmNo);

        //??
        UserManagementService userManagementService = BeanContext.getBean(UserManagementService.class);
        UserAuth userAuth = userManagementService.getUserAuth(userNo, farmNo);
        if (userAuth == null) {
            //?????
            this.userAuthDto = new UserAuthDto(false);
        } else {
            //???
            this.userAuthDto = new UserAuthDto(userAuth);
        }
    }

    private void showData() {
        //myCloud
        myCloudSelect.select(BooleanUtils.isTrue(userAuthDto.isFarmUse()));

        //?
        serverMakeSelect.select(BooleanUtils.isTrue(userAuthDto.isServerMake()));
        serverDeleteSelect.select(BooleanUtils.isTrue(userAuthDto.isServerDelete()));
        serverOperateSelect.select(BooleanUtils.isTrue(userAuthDto.isServerOperate()));

        //
        serviceMakeSelect.select(BooleanUtils.isTrue(userAuthDto.isServiceMake()));
        serviceDeleteSelect.select(BooleanUtils.isTrue(userAuthDto.isServiceDelete()));
        serviceOperateSelect.select(BooleanUtils.isTrue(userAuthDto.isServiceOperate()));

        //?
        lbMakeSelect.select(BooleanUtils.isTrue(userAuthDto.isLbMake()));
        lbDeleteSelect.select(BooleanUtils.isTrue(userAuthDto.isLbDelete()));
        lbOperateSelect.select(BooleanUtils.isTrue(userAuthDto.isLbOperate()));
    }

    private UserAuthDto getInputData() {
        UserAuthDto authDto = new UserAuthDto(false);
        authDto.setFarmUse((Boolean) myCloudSelect.getValue());
        authDto.setServerMake((Boolean) serverMakeSelect.getValue());
        authDto.setServerDelete((Boolean) serverDeleteSelect.getValue());
        authDto.setServerOperate((Boolean) serverOperateSelect.getValue());
        authDto.setServiceMake((Boolean) serviceMakeSelect.getValue());
        authDto.setServiceDelete((Boolean) serviceDeleteSelect.getValue());
        authDto.setServiceOperate((Boolean) serviceOperateSelect.getValue());
        authDto.setLbMake((Boolean) lbMakeSelect.getValue());
        authDto.setLbDelete((Boolean) lbDeleteSelect.getValue());
        authDto.setLbOperate((Boolean) lbOperateSelect.getValue());
        return authDto;
    }

    private void okButtonClick(ClickEvent event) {
        //?????PCC??????????
        //??ParentWindow????(NULL)?????
        setCustomErrorHandler(event.getButton());

        // ?
        UserAuthDto inputAuthDto = getInputData();

        // ?
        //TODO

        //?
        try {
            UserManagementService userManagementService = BeanContext.getBean(UserManagementService.class);
            userManagementService.updateUserAuth(farmNo, userNo, inputAuthDto);
        } catch (AutoApplicationException e) {
            //??
            String message = ViewMessages.getMessage(e.getCode(), e.getAdditions());
            DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), message);
            getParent().addWindow(dialog);
            return;
        }

        // ??
        close();
    }

    private void cancelButtonClick(ClickEvent event) {
        //?????PCC??????????
        setCustomErrorHandler(event.getButton());
        WinUserAuthEditDetail.this.close();
    }
}