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

Java tutorial

Introduction

Here is the source code for jp.primecloud.auto.ui.WinServerAdd.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 java.util.List;

import org.apache.commons.lang.BooleanUtils;

import jp.primecloud.auto.common.constant.PCCConstant;
import jp.primecloud.auto.entity.crud.ComponentType;
import jp.primecloud.auto.exception.AutoApplicationException;
import jp.primecloud.auto.service.InstanceService;
import jp.primecloud.auto.service.dto.ImageDto;
import jp.primecloud.auto.service.dto.PlatformDto;
import jp.primecloud.auto.ui.util.BeanContext;
import jp.primecloud.auto.ui.util.CommonUtils;
import jp.primecloud.auto.ui.util.ContextUtils;
import jp.primecloud.auto.ui.util.Icons;
import jp.primecloud.auto.ui.util.VaadinUtils;
import jp.primecloud.auto.ui.util.ViewContext;
import jp.primecloud.auto.ui.util.ViewMessages;
import jp.primecloud.auto.ui.util.ViewProperties;
import com.vaadin.Application;
import com.vaadin.data.Property;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * <p>
 * ?????????
 * </p>
 *
 */
@SuppressWarnings("serial")
public class WinServerAdd extends Window {
    final String COLUMN_HEIGHT = "28px";

    Application apl;

    TextField serverNameField;

    TextField commentField;

    Table cloudTable;

    Table imageTable;

    Table serviceTable;

    List<PlatformDto> platforms;

    ImageDto selectImage;

    List<Long> componentNos;

    boolean attachService = false;

    WinServerAdd(Application ap) {
        apl = ap;

        //
        setIcon(Icons.ADD.resource());
        setCaption(ViewProperties.getCaption("window.winServerAdd"));
        setModal(true);
        setWidth("620px");
        //setHeight("600px");

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

        // 
        layout.addComponent(new BasicForm());

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

        // Add
        Button addButton = new Button(ViewProperties.getCaption("button.add"));
        addButton.setDescription(ViewProperties.getCaption("description.addServer"));

        addButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                addButtonClick(event);
            }
        });

        // [Enter]?addButton
        addButton.setClickShortcut(KeyCode.ENTER);
        addButton.focus();
        okbar.addComponent(addButton);

        // 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) {
                close();
            }
        });
        okbar.addComponent(cancelButton);

        // ??
        initValidation();

        // ???
        initData();

        // ?
        showClouds();
    }

    private class BasicForm extends Form {
        BasicForm() {
            // ???
            serverNameField = new TextField(ViewProperties.getCaption("field.serverName"));
            getLayout().addComponent(serverNameField);

            // 
            commentField = new TextField(ViewProperties.getCaption("field.comment"));
            commentField.setWidth("95%");
            getLayout().addComponent(commentField);

            // ?
            cloudTable = new SelectCloudTable();
            getLayout().addComponent(cloudTable);

            // ?
            imageTable = new SelectImageTable();
            getLayout().addComponent(imageTable);

            // ????
            Panel panel = new Panel();
            panel.setSizeFull();
            panel.setWidth("470px");
            CssLayout lay = new CssLayout();
            panel.setContent(lay);
            lay.setSizeFull();
            lay.addStyleName("win-server-add-panel");
            serviceTable = new AvailableServiceTable();
            panel.addComponent(serviceTable);
            getLayout().addComponent(panel);

            //?
            Button btnService = new Button(ViewProperties.getCaption("button.serverAttachService"));
            btnService.setDescription(ViewProperties.getCaption("description.serverAttachService"));
            btnService.setIcon(Icons.SERVICETAB.resource());
            btnService.addListener(new Button.ClickListener() {
                @Override
                public void buttonClick(ClickEvent event) {
                    WinServerAttachService winServerAttachService = new WinServerAttachService(getApplication(),
                            null, selectImage, componentNos);
                    winServerAttachService.addListener(new Window.CloseListener() {
                        @Override
                        public void windowClose(Window.CloseEvent e) {
                            List<Long> componentNos = (List<Long>) ContextUtils.getAttribute("componentNos");
                            if (componentNos != null) {
                                ContextUtils.removeAttribute("componentNos");
                                WinServerAdd.this.componentNos = componentNos;
                                attachService = true;
                            }
                        }
                    });
                    getWindow().getApplication().getMainWindow().addWindow(winServerAttachService);
                }
            });

            HorizontalLayout hlay = new HorizontalLayout();
            hlay.setSpacing(true);
            Label txt = new Label(ViewProperties.getCaption("label.serverAttachService"));
            hlay.addComponent(btnService);
            hlay.addComponent(txt);
            hlay.setComponentAlignment(txt, Alignment.MIDDLE_LEFT);

            getLayout().addComponent(hlay);
        }
    }

    private class SelectCloudTable extends Table {
        SelectCloudTable() {
            //
            setCaption(ViewProperties.getCaption("table.selectCloud"));
            //            setWidth("100%");
            setWidth("470px");
            setPageLength(4);
            setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN);
            setSortDisabled(true);
            setColumnReorderingAllowed(false);
            setColumnCollapsingAllowed(false);
            setSelectable(true);
            setMultiSelect(false);
            setNullSelectionAllowed(false);
            setImmediate(true);
            addStyleName("win-server-add-cloud");

            //
            addContainerProperty("No", Integer.class, null);
            addContainerProperty("Cloud", Label.class, new Label());
            setColumnExpandRatio("Cloud", 100);

            //????StyleName
            setCellStyleGenerator(new Table.CellStyleGenerator() {
                public String getStyle(Object itemId, Object propertyId) {
                    if (propertyId == null) {
                        return "";
                    } else {
                        return propertyId.toString().toLowerCase();
                    }
                }
            });

            // ????????
            addListener(new Property.ValueChangeListener() {
                @Override
                public void valueChange(Property.ValueChangeEvent event) {
                    Long platformNo = (Long) getValue();

                    // ?
                    showImages(platformNo);
                }
            });
        }
    }

    private class SelectImageTable extends Table {
        SelectImageTable() {
            //
            setCaption(ViewProperties.getCaption("table.selectImage"));
            //            setWidth("100%");
            setWidth("470px");
            setPageLength(3);
            setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN);
            setSortDisabled(true);
            setColumnReorderingAllowed(false);
            setColumnCollapsingAllowed(false);
            setSelectable(true);
            setMultiSelect(false);
            setNullSelectionAllowed(false);
            setImmediate(true);

            addStyleName("win-server-add-os");

            //
            addContainerProperty("No", Integer.class, null);
            addContainerProperty("Image", Label.class, new Label());
            addContainerProperty("Detail", Label.class, new Label());
            setColumnExpandRatio("Image", 100);

            //????StyleName
            setCellStyleGenerator(new Table.CellStyleGenerator() {
                public String getStyle(Object itemId, Object propertyId) {
                    if (propertyId == null) {
                        return "";
                    } else {
                        return propertyId.toString().toLowerCase();
                    }
                }
            });

            // ????????
            addListener(new Property.ValueChangeListener() {
                @Override
                public void valueChange(Property.ValueChangeEvent event) {
                    Long platformNo = (Long) cloudTable.getValue();
                    Long imageNo = (Long) imageTable.getValue();

                    // ?
                    showServices(platformNo, imageNo);

                    // ???
                    componentNos = null;
                    attachService = false;
                }
            });
        }
    }

    private class AvailableServiceTable extends Table {
        AvailableServiceTable() {
            //
            setCaption(ViewProperties.getCaption("table.availableService"));
            setWidth("100%");
            setHeight("100px");
            setPageLength(3);
            setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN);
            setSortDisabled(true);
            setColumnReorderingAllowed(false);
            setColumnCollapsingAllowed(false);
            setSelectable(false);
            setMultiSelect(false);
            setImmediate(true);
            addStyleName("win-server-add-service");

            //
            addContainerProperty("Service", Label.class, new Label());
            addContainerProperty("Description", String.class, null);
            setColumnExpandRatio("Service", 100);

            //????StyleName
            setCellStyleGenerator(new Table.CellStyleGenerator() {
                public String getStyle(Object itemId, Object propertyId) {

                    if (propertyId == null) {
                        return "";
                    }
                    String ret = propertyId.toString().toLowerCase();

                    Long componentTypeNo = (Long) itemId;
                    List<ComponentType> componentTypes = selectImage.getComponentTypes();
                    for (ComponentType componentType : componentTypes) {
                        if (componentType.getComponentTypeNo().equals(componentTypeNo)
                                && BooleanUtils.isNotTrue(componentType.getSelectable())) {
                            //???????Disable??
                            ret += " v-disabled";
                            break;
                        }
                    }
                    return ret;
                }
            });
        }
    }

    private void initValidation() {
        String message = ViewMessages.getMessage("IUI-000022");
        serverNameField.setRequired(true);
        serverNameField.setRequiredError(message);
        serverNameField.addValidator(new StringLengthValidator(message, -1, 15, false));
        serverNameField.addValidator(new RegexpValidator("^[a-z]|[a-z][0-9a-z-]*[0-9a-z]$", true, message));

        message = ViewMessages.getMessage("IUI-000003");
        commentField.addValidator(new StringLengthValidator(message, -1, 100, true));
    }

    private void initData() {
        // ?
        Long userNo = ViewContext.getUserNo();

        // ?
        InstanceService instanceService = BeanContext.getBean(InstanceService.class);
        platforms = instanceService.getPlatforms(userNo);
    }

    private void showClouds() {
        cloudTable.removeAllItems();

        // ?
        for (int i = 0; i < platforms.size(); i++) {
            PlatformDto platformDto = platforms.get(i);

            if (BooleanUtils.isNotTrue(platformDto.getPlatform().getSelectable())) {
                //??????
                continue;
            }

            //????
            Icons icon = CommonUtils.getPlatformIcon(platformDto);

            String description = platformDto.getPlatform().getPlatformNameDisp();

            Label slbl = new Label(
                    "<img src=\"" + VaadinUtils.getIconPath(apl, icon) + "\"><div>" + description + "</div>",
                    Label.CONTENT_XHTML);
            slbl.setHeight(COLUMN_HEIGHT);

            cloudTable.addItem(new Object[] { (i + 1), slbl }, platformDto.getPlatform().getPlatformNo());
        }

        Long platformNo = null;
        if (cloudTable.getItemIds().size() > 0) {
            platformNo = (Long) cloudTable.getItemIds().toArray()[0];
        }

        // ???
        cloudTable.select(platformNo);
    }

    private void showImages(Long platformNo) {
        imageTable.removeAllItems();
        serviceTable.removeAllItems();
        if (platformNo == null) {
            return;
        }

        // ????????
        List<ImageDto> images = null;
        for (PlatformDto platform : platforms) {
            if (platformNo.equals(platform.getPlatform().getPlatformNo())) {
                images = platform.getImages();
                break;
            }
        }

        // ?????
        if (images == null) {
            return;
        }

        // ??
        int n = 0;
        for (ImageDto image : images) {
            // ?????????
            if (BooleanUtils.isNotTrue(image.getImage().getSelectable())) {
                continue;
            }

            // ???
            String name = image.getImage().getImageNameDisp();
            Icons nameIcon = CommonUtils.getImageIcon(image);

            Label nlbl = new Label(
                    "<img src=\"" + VaadinUtils.getIconPath(apl, nameIcon) + "\"><div>" + name + "</div>",
                    Label.CONTENT_XHTML);
            nlbl.setHeight(COLUMN_HEIGHT);

            // OS??
            String os = image.getImage().getOsDisp();
            Icons osIcon = CommonUtils.getOsIcon(image);

            Label slbl = new Label(
                    "<img src=\"" + VaadinUtils.getIconPath(apl, osIcon) + "\"><div>" + os + "</div>",
                    Label.CONTENT_XHTML);
            slbl.setHeight(COLUMN_HEIGHT);

            n++;
            imageTable.addItem(new Object[] { n, nlbl, slbl }, image.getImage().getImageNo());
        }

        Long imageNo = null;
        if (imageTable.getItemIds().size() > 0) {
            imageNo = (Long) imageTable.getItemIds().toArray()[0];
        }

        // ????
        imageTable.select(imageNo);
    }

    private void showServices(Long platformNo, Long imageNo) {
        serviceTable.removeAllItems();
        if (platformNo == null || imageNo == null) {
            return;
        }

        // ???
        List<ComponentType> componentTypes = null;
        for (PlatformDto platform : platforms) {
            if (platformNo.equals(platform.getPlatform().getPlatformNo())) {
                List<ImageDto> images = platform.getImages();
                for (ImageDto image : images) {
                    if (imageNo.equals(image.getImage().getImageNo())) {
                        this.selectImage = image;

                        componentTypes = image.getComponentTypes();
                        //?
                        String caption = image.getImage().getImageNameDisp();
                        caption = "" + caption + "?????";
                        serviceTable.setCaption(caption);

                        break;
                    }
                }
                break;
            }
        }

        // ????
        if (componentTypes == null) {
            return;
        }

        // ?
        for (int i = 0; i < componentTypes.size(); i++) {
            ComponentType componentType = componentTypes.get(i);

            // ??
            String name = componentType.getComponentTypeNameDisp();
            Icons nameIcon = Icons.fromName(componentType.getComponentTypeName());

            Label slbl = new Label(
                    "<img src=\"" + VaadinUtils.getIconPath(apl, nameIcon) + "\"><div>" + name + "</div>",
                    Label.CONTENT_XHTML);
            slbl.setHeight("26px");

            // 
            String description = componentType.getLayerDisp();

            serviceTable.addItem(new Object[] { slbl, description }, componentType.getComponentTypeNo());
        }
    }

    private void addButtonClick(ClickEvent event) {
        // ?
        String serverName = (String) serverNameField.getValue();
        String comment = (String) commentField.getValue();
        Long platformNo = (Long) cloudTable.getValue();
        Long imageNo = (Long) imageTable.getValue();

        // TODO: ?
        try {
            serverNameField.validate();
            commentField.validate();
        } catch (InvalidValueException e) {
            String errMes = e.getMessage();
            if (null == errMes) {
                //?????????? ?
                InvalidValueException[] exceptions = e.getCauses();
                errMes = exceptions[0].getMessage();
            }

            DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), errMes);
            getApplication().getMainWindow().addWindow(dialog);
            return;
        }
        if (platformNo == null) {
            DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"),
                    ViewMessages.getMessage("IUI-000023"));
            getApplication().getMainWindow().addWindow(dialog);
            return;
        }
        if (imageNo == null) {
            DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"),
                    ViewMessages.getMessage("IUI-000024"));
            getApplication().getMainWindow().addWindow(dialog);
            return;
        }
        if (serverName.startsWith("lb-")) {
            DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"),
                    ViewMessages.getMessage("IUI-000083", serverName));
            getApplication().getMainWindow().addWindow(dialog);
            return;
        }

        // ????
        PlatformDto platformDto = null;
        ImageDto imageDto = null;
        for (PlatformDto platform : platforms) {
            if (platform.getPlatform().getPlatformNo().equals(platformNo)) {
                platformDto = platform;
                for (ImageDto tmpImage : platform.getImages()) {
                    if (tmpImage.getImage().getImageNo().equals(imageNo)) {
                        imageDto = tmpImage;
                        break;
                    }
                }
                break;
            }
        }

        // ?
        Long farmNo = ViewContext.getFarmNo();
        InstanceService instanceService = BeanContext.getBean(InstanceService.class);
        Long instanceNo = null;
        // TODO CLOUD BRANCHING
        if (PCCConstant.PLATFORM_TYPE_AWS.equals(platformDto.getPlatform().getPlatformType())) {
            // AWS??
            try {
                String[] instanceTypes = imageDto.getImageAws().getInstanceTypes().split(",");
                instanceNo = instanceService.createIaasInstance(farmNo, serverName, platformNo, comment, imageNo,
                        instanceTypes[0].trim());
            } catch (AutoApplicationException e) {
                String message = ViewMessages.getMessage(e.getCode(), e.getAdditions());
                DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), message);
                getApplication().getMainWindow().addWindow(dialog);
                return;
            }
        } else if (PCCConstant.PLATFORM_TYPE_VMWARE.equals(platformDto.getPlatform().getPlatformType())) {
            // VMware??
            try {
                String[] instanceTypes = imageDto.getImageVmware().getInstanceTypes().split(",");
                instanceNo = instanceService.createVmwareInstance(farmNo, serverName, platformNo, comment, imageNo,
                        instanceTypes[0].trim());
            } catch (AutoApplicationException e) {
                String message = ViewMessages.getMessage(e.getCode(), e.getAdditions());
                DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), message);
                getApplication().getMainWindow().addWindow(dialog);
                return;
            }
        } else if (PCCConstant.PLATFORM_TYPE_NIFTY.equals(platformDto.getPlatform().getPlatformType())) {
            // Nifty??
            try {
                String[] instanceTypes = imageDto.getImageNifty().getInstanceTypes().split(",");
                instanceNo = instanceService.createNiftyInstance(farmNo, serverName, platformNo, comment, imageNo,
                        instanceTypes[0].trim());
            } catch (AutoApplicationException e) {
                String message = ViewMessages.getMessage(e.getCode(), e.getAdditions());
                DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), message);
                getApplication().getMainWindow().addWindow(dialog);
                return;
            }
        } else if (PCCConstant.PLATFORM_TYPE_CLOUDSTACK.equals(platformDto.getPlatform().getPlatformType())) {
            // CloudStack??
            try {
                String[] instanceTypes = imageDto.getImageCloudstack().getInstanceTypes().split(",");
                instanceNo = instanceService.createIaasInstance(farmNo, serverName, platformNo, comment, imageNo,
                        instanceTypes[0].trim());
            } catch (AutoApplicationException e) {
                String message = ViewMessages.getMessage(e.getCode(), e.getAdditions());
                DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), message);
                getApplication().getMainWindow().addWindow(dialog);
                return;
            }
        } else if (PCCConstant.PLATFORM_TYPE_VCLOUD.equals(platformDto.getPlatform().getPlatformType())) {
            // VCloud??
            try {
                String[] instanceTypes = imageDto.getImageVcloud().getInstanceTypes().split(",");
                instanceNo = instanceService.createIaasInstance(farmNo, serverName, platformNo, comment, imageNo,
                        instanceTypes[0].trim());
            } catch (AutoApplicationException e) {
                String message = ViewMessages.getMessage(e.getCode(), e.getAdditions());
                DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), message);
                getApplication().getMainWindow().addWindow(dialog);
                return;
            }
        } else if (PCCConstant.PLATFORM_TYPE_AZURE.equals(platformDto.getPlatform().getPlatformType())) {
            // Azure??
            try {
                String[] instanceTypes = imageDto.getImageAzure().getInstanceTypes().split(",");
                instanceNo = instanceService.createIaasInstance(farmNo, serverName, platformNo, comment, imageNo,
                        instanceTypes[0].trim());
            } catch (AutoApplicationException e) {
                String message = ViewMessages.getMessage(e.getCode(), e.getAdditions());
                DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), message);
                getApplication().getMainWindow().addWindow(dialog);
                return;
            }
        } else if (PCCConstant.PLATFORM_TYPE_OPENSTACK.equals(platformDto.getPlatform().getPlatformType())) {
            // OpenStack??
            try {
                String[] instanceTypes = imageDto.getImageOpenstack().getInstanceTypes().split(",");
                instanceNo = instanceService.createIaasInstance(farmNo, serverName, platformNo, comment, imageNo,
                        instanceTypes[0].trim());
            } catch (AutoApplicationException e) {
                String message = ViewMessages.getMessage(e.getCode(), e.getAdditions());
                DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), message);
                getApplication().getMainWindow().addWindow(dialog);
                return;
            }
        }

        //
        AutoApplication aapl = (AutoApplication) apl;
        aapl.doOpLog("SERVER", "Make Server", instanceNo, null, null, null);

        // ???
        if (componentNos != null && attachService) {
            instanceService.associateComponents(instanceNo, componentNos);
        }

        // ??
        close();
    }

}