org.ikasan.dashboard.ui.topology.component.WiretapTab.java Source code

Java tutorial

Introduction

Here is the source code for org.ikasan.dashboard.ui.topology.component.WiretapTab.java

Source

/*
 * $Id$  
 * $URL$
 * 
 * ====================================================================
 * Ikasan Enterprise Integration Platform
 * 
 * Distributed under the Modified BSD License.
 * Copyright notice: The copyright for this software and a full listing 
 * of individual contributors are as shown in the packaged copyright.txt 
 * file. 
 * 
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *
 *  - Neither the name of the ORGANIZATION nor the names of its contributors may
 *    be used to endorse or promote products derived from this software without 
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */
package org.ikasan.dashboard.ui.topology.component;

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

import org.apache.log4j.Logger;
import org.ikasan.dashboard.ui.framework.window.ProgressBarWindow;
import org.ikasan.dashboard.ui.mappingconfiguration.component.IkasanCellStyleGenerator;
import org.ikasan.dashboard.ui.mappingconfiguration.component.IkasanSmallCellStyleGenerator;
import org.ikasan.dashboard.ui.topology.window.WiretapPayloadViewWindow;
import org.ikasan.spec.search.PagedSearchResult;
import org.ikasan.spec.wiretap.WiretapEvent;
import org.ikasan.topology.model.BusinessStream;
import org.ikasan.topology.model.BusinessStreamFlow;
import org.ikasan.topology.model.Component;
import org.ikasan.topology.model.Flow;
import org.ikasan.topology.model.Module;
import org.ikasan.wiretap.dao.WiretapDao;
import org.ikasan.wiretap.model.WiretapFlowEvent;
import org.vaadin.teemu.VaadinIcons;

import com.vaadin.event.DataBoundTransferable;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.server.Resource;
import com.vaadin.server.Sizeable.Unit;
import com.vaadin.shared.ui.datefield.Resolution;
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.CssLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.ProgressBar;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.TableDragMode;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalSplitPanel;
import com.vaadin.ui.themes.ValoTheme;

/**
 * 
 * @author Ikasan Development Team
 *
 */
public class WiretapTab extends TopologyTab {
    private Logger logger = Logger.getLogger(WiretapTab.class);

    private Table wiretapTable;

    private WiretapDao wiretapDao;

    private Table modules = new Table("Modules");
    private Table flows = new Table("Flows");
    private Table components = new Table("Components");

    private PopupDateField fromDate;
    private PopupDateField toDate;

    private ComboBox businessStreamCombo;
    private TextField eventId;
    private TextField payloadContent;

    private float splitPosition;
    private Unit splitUnit;

    public WiretapTab(WiretapDao wiretapDao, ComboBox businessStreamCombo) {
        this.wiretapDao = wiretapDao;
        this.businessStreamCombo = businessStreamCombo;
    }

    public Layout createWiretapLayout() {
        this.wiretapTable = new Table();
        this.wiretapTable.setSizeFull();
        this.wiretapTable.addStyleName(ValoTheme.TABLE_SMALL);
        this.wiretapTable.addStyleName("ikasan");

        this.wiretapTable.addContainerProperty("Module Name", String.class, null);
        this.wiretapTable.addContainerProperty("Flow Name", String.class, null);
        this.wiretapTable.addContainerProperty("Component Name", String.class, null);
        this.wiretapTable.addContainerProperty("Event Id / Payload Id", String.class, null);
        this.wiretapTable.addContainerProperty("Timestamp", String.class, null);
        this.wiretapTable.setCellStyleGenerator(new IkasanSmallCellStyleGenerator());

        this.wiretapTable.addItemClickListener(new ItemClickEvent.ItemClickListener() {
            @Override
            public void itemClick(ItemClickEvent itemClickEvent) {
                WiretapEvent<String> wiretapEvent = (WiretapEvent<String>) itemClickEvent.getItemId();
                WiretapPayloadViewWindow wiretapPayloadViewWindow = new WiretapPayloadViewWindow(wiretapEvent);

                UI.getCurrent().addWindow(wiretapPayloadViewWindow);
            }
        });

        final Button searchButton = new Button("Search");
        searchButton.setStyleName(ValoTheme.BUTTON_SMALL);
        searchButton.addClickListener(new Button.ClickListener() {
            @SuppressWarnings("unchecked")
            public void buttonClick(ClickEvent event) {
                ProgressBarWindow pbWindow = new ProgressBarWindow();

                UI.getCurrent().addWindow(pbWindow);

                wiretapTable.removeAllItems();

                HashSet<String> modulesNames = null;

                if (modules.getItemIds().size() > 0) {
                    modulesNames = new HashSet<String>();
                    for (Object module : modules.getItemIds()) {
                        modulesNames.add(((Module) module).getName());
                    }
                }

                HashSet<String> flowNames = null;

                if (flows.getItemIds().size() > 0) {
                    flowNames = new HashSet<String>();
                    for (Object flow : flows.getItemIds()) {
                        flowNames.add(((Flow) flow).getName());
                    }
                }

                HashSet<String> componentNames = null;

                if (components.getItemIds().size() > 0) {
                    componentNames = new HashSet<String>();
                    for (Object component : components.getItemIds()) {

                        componentNames.add("before " + ((Component) component).getName());
                        componentNames.add("after " + ((Component) component).getName());
                    }
                }

                if (modulesNames == null && flowNames == null && componentNames == null
                        && !((BusinessStream) businessStreamCombo.getValue()).getName().equals("All")) {
                    BusinessStream businessStream = ((BusinessStream) businessStreamCombo.getValue());

                    modulesNames = new HashSet<String>();

                    for (BusinessStreamFlow flow : businessStream.getFlows()) {
                        modulesNames.add(flow.getFlow().getModule().getName());
                    }
                }

                String errorCategory = null;

                // TODO Need to take a proper look at the wiretap search interface. We do not need to worry about paging search
                // results with Vaadin.
                PagedSearchResult<WiretapEvent> events = wiretapDao.findWiretapEvents(0, 10000, "timestamp", false,
                        modulesNames, flowNames, componentNames, eventId.getValue(), null, fromDate.getValue(),
                        toDate.getValue(), payloadContent.getValue());

                for (WiretapEvent<String> wiretapEvent : events.getPagedResults()) {
                    Date date = new Date(wiretapEvent.getTimestamp());
                    SimpleDateFormat format = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
                    String timestamp = format.format(date);

                    wiretapTable
                            .addItem(
                                    new Object[] { wiretapEvent.getModuleName(), wiretapEvent.getFlowName(),
                                            wiretapEvent.getComponentName(),
                                            ((WiretapFlowEvent) wiretapEvent).getEventId(), timestamp },
                                    wiretapEvent);
                }

                pbWindow.close();
            }
        });

        Button clearButton = new Button("Clear");
        clearButton.setStyleName(ValoTheme.BUTTON_SMALL);
        clearButton.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                modules.removeAllItems();
                flows.removeAllItems();
                components.removeAllItems();
            }
        });

        GridLayout layout = new GridLayout(1, 6);
        layout.setMargin(false);
        layout.setHeight(270, Unit.PIXELS);

        GridLayout listSelectLayout = new GridLayout(3, 1);
        listSelectLayout.setSpacing(true);
        listSelectLayout.setSizeFull();

        modules.setIcon(VaadinIcons.ARCHIVE);
        modules.addContainerProperty("Module Name", String.class, null);
        modules.addContainerProperty("", Button.class, null);
        modules.setSizeFull();
        modules.setCellStyleGenerator(new IkasanSmallCellStyleGenerator());
        modules.setDragMode(TableDragMode.ROW);
        modules.setDropHandler(new DropHandler() {
            @Override
            public void drop(final DragAndDropEvent dropEvent) {
                // criteria verify that this is safe
                logger.info("Trying to drop: " + dropEvent);

                final DataBoundTransferable t = (DataBoundTransferable) dropEvent.getTransferable();

                if (t.getItemId() instanceof Module) {
                    final Module module = (Module) t.getItemId();
                    logger.info("sourceContainer.getText(): " + module.getName());

                    Button deleteButton = new Button();
                    deleteButton.setIcon(VaadinIcons.TRASH);
                    deleteButton.addStyleName(ValoTheme.BUTTON_BORDERLESS);
                    deleteButton.addStyleName(ValoTheme.BUTTON_ICON_ONLY);

                    // Add the delete functionality to each role that is added
                    deleteButton.addClickListener(new Button.ClickListener() {
                        public void buttonClick(ClickEvent event) {
                            modules.removeItem(module);
                        }
                    });

                    modules.addItem(new Object[] { module.getName(), deleteButton }, module);

                    for (final Flow flow : module.getFlows()) {
                        deleteButton = new Button();
                        deleteButton.setIcon(VaadinIcons.TRASH);
                        deleteButton.addStyleName(ValoTheme.BUTTON_BORDERLESS);
                        deleteButton.addStyleName(ValoTheme.BUTTON_ICON_ONLY);

                        // Add the delete functionality to each role that is added
                        deleteButton.addClickListener(new Button.ClickListener() {
                            public void buttonClick(ClickEvent event) {
                                flows.removeItem(flow);
                            }
                        });

                        flows.addItem(new Object[] { flow.getName(), deleteButton }, flow);

                        for (final Component component : flow.getComponents()) {
                            deleteButton = new Button();
                            deleteButton.setIcon(VaadinIcons.TRASH);
                            deleteButton.addStyleName(ValoTheme.BUTTON_BORDERLESS);
                            deleteButton.addStyleName(ValoTheme.BUTTON_ICON_ONLY);

                            // Add the delete functionality to each role that is added
                            deleteButton.addClickListener(new Button.ClickListener() {
                                public void buttonClick(ClickEvent event) {
                                    components.removeItem(component);
                                }
                            });

                            components.addItem(new Object[] { component.getName(), deleteButton }, component);
                        }
                    }
                }

            }

            @Override
            public AcceptCriterion getAcceptCriterion() {
                return AcceptAll.get();
            }
        });

        listSelectLayout.addComponent(modules, 0, 0);

        flows.setIcon(VaadinIcons.AUTOMATION);
        flows.addContainerProperty("Flow Name", String.class, null);
        flows.addContainerProperty("", Button.class, null);
        flows.setSizeFull();
        flows.setCellStyleGenerator(new IkasanSmallCellStyleGenerator());
        flows.setDropHandler(new DropHandler() {
            @Override
            public void drop(final DragAndDropEvent dropEvent) {
                // criteria verify that this is safe
                logger.info("Trying to drop: " + dropEvent);

                final DataBoundTransferable t = (DataBoundTransferable) dropEvent.getTransferable();

                if (t.getItemId() instanceof Flow) {
                    final Flow flow = (Flow) t.getItemId();
                    logger.info("sourceContainer.getText(): " + flow.getName());

                    Button deleteButton = new Button();
                    deleteButton.setIcon(VaadinIcons.TRASH);
                    deleteButton.addStyleName(ValoTheme.BUTTON_BORDERLESS);
                    deleteButton.addStyleName(ValoTheme.BUTTON_ICON_ONLY);

                    // Add the delete functionality to each role that is added
                    deleteButton.addClickListener(new Button.ClickListener() {
                        public void buttonClick(ClickEvent event) {
                            flows.removeItem(flow);
                        }
                    });

                    flows.addItem(new Object[] { flow.getName(), deleteButton }, flow);

                    for (final Component component : flow.getComponents()) {
                        deleteButton = new Button();
                        deleteButton.setIcon(VaadinIcons.TRASH);
                        deleteButton.addStyleName(ValoTheme.BUTTON_BORDERLESS);
                        deleteButton.addStyleName(ValoTheme.BUTTON_ICON_ONLY);

                        // Add the delete functionality to each role that is added
                        deleteButton.addClickListener(new Button.ClickListener() {
                            public void buttonClick(ClickEvent event) {
                                components.removeItem(component);
                            }
                        });

                        components.addItem(new Object[] { component.getName(), deleteButton }, component);
                    }
                }

            }

            @Override
            public AcceptCriterion getAcceptCriterion() {
                return AcceptAll.get();
            }
        });

        listSelectLayout.addComponent(flows, 1, 0);

        components.setIcon(VaadinIcons.COG);
        components.setSizeFull();
        components.addContainerProperty("Component Name", String.class, null);
        components.addContainerProperty("", Button.class, null);
        components.setCellStyleGenerator(new IkasanCellStyleGenerator());
        components.setSizeFull();
        components.setCellStyleGenerator(new IkasanSmallCellStyleGenerator());
        components.setDropHandler(new DropHandler() {
            @Override
            public void drop(final DragAndDropEvent dropEvent) {
                // criteria verify that this is safe
                logger.info("Trying to drop: " + dropEvent);

                final DataBoundTransferable t = (DataBoundTransferable) dropEvent.getTransferable();

                if (t.getItemId() instanceof Component) {
                    final Component component = (Component) t.getItemId();
                    logger.info("sourceContainer.getText(): " + component.getName());

                    Button deleteButton = new Button();
                    deleteButton.setIcon(VaadinIcons.TRASH);
                    deleteButton.addStyleName(ValoTheme.BUTTON_BORDERLESS);
                    deleteButton.addStyleName(ValoTheme.BUTTON_ICON_ONLY);

                    // Add the delete functionality to each role that is added
                    deleteButton.addClickListener(new Button.ClickListener() {
                        public void buttonClick(ClickEvent event) {
                            components.removeItem(component);
                        }
                    });

                    components.addItem(new Object[] { component.getName(), deleteButton }, component);

                }

            }

            @Override
            public AcceptCriterion getAcceptCriterion() {
                return AcceptAll.get();
            }
        });
        listSelectLayout.addComponent(this.components, 2, 0);

        GridLayout dateSelectLayout = new GridLayout(2, 2);
        dateSelectLayout.setColumnExpandRatio(0, 0.25f);
        dateSelectLayout.setColumnExpandRatio(1, 0.75f);
        dateSelectLayout.setSizeFull();
        this.fromDate = new PopupDateField("From date");
        this.fromDate.setResolution(Resolution.MINUTE);
        this.fromDate.setValue(this.getMidnightToday());
        dateSelectLayout.addComponent(this.fromDate, 0, 0);
        this.toDate = new PopupDateField("To date");
        this.toDate.setResolution(Resolution.MINUTE);
        this.toDate.setValue(this.getTwentyThreeFixtyNineToday());
        dateSelectLayout.addComponent(this.toDate, 0, 1);

        this.eventId = new TextField("Event Id");
        this.eventId.setWidth("80%");
        this.payloadContent = new TextField("Payload Content");
        this.payloadContent.setWidth("80%");

        this.eventId.setNullSettingAllowed(true);
        this.payloadContent.setNullSettingAllowed(true);

        dateSelectLayout.addComponent(this.eventId, 1, 0);
        dateSelectLayout.addComponent(this.payloadContent, 1, 1);

        final VerticalSplitPanel vSplitPanel = new VerticalSplitPanel();
        vSplitPanel.setHeight("95%");

        GridLayout searchLayout = new GridLayout(2, 1);
        searchLayout.setSpacing(true);
        searchLayout.addComponent(searchButton, 0, 0);
        searchLayout.addComponent(clearButton, 1, 0);

        final Button hideFilterButton = new Button();
        hideFilterButton.setIcon(VaadinIcons.MINUS);
        hideFilterButton.setCaption("Hide Filter");
        hideFilterButton.setStyleName(ValoTheme.BUTTON_LINK);
        hideFilterButton.addStyleName(ValoTheme.BUTTON_SMALL);

        final Button showFilterButton = new Button();
        showFilterButton.setIcon(VaadinIcons.PLUS);
        showFilterButton.setCaption("Show Filter");
        showFilterButton.addStyleName(ValoTheme.BUTTON_LINK);
        showFilterButton.addStyleName(ValoTheme.BUTTON_SMALL);
        showFilterButton.setVisible(false);

        final HorizontalLayout hListSelectLayout = new HorizontalLayout();
        hListSelectLayout.setHeight(150, Unit.PIXELS);
        hListSelectLayout.setWidth("100%");
        hListSelectLayout.addComponent(listSelectLayout);

        final HorizontalLayout hDateSelectLayout = new HorizontalLayout();
        hDateSelectLayout.setHeight(80, Unit.PIXELS);
        hDateSelectLayout.setWidth("100%");
        hDateSelectLayout.addComponent(dateSelectLayout);

        final HorizontalLayout hSearchLayout = new HorizontalLayout();
        hSearchLayout.setHeight(30, Unit.PIXELS);
        hSearchLayout.setWidth("100%");
        hSearchLayout.addComponent(searchLayout);
        hSearchLayout.setComponentAlignment(searchLayout, Alignment.MIDDLE_CENTER);

        hideFilterButton.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                hideFilterButton.setVisible(false);
                showFilterButton.setVisible(true);
                splitPosition = vSplitPanel.getSplitPosition();
                splitUnit = vSplitPanel.getSplitPositionUnit();
                vSplitPanel.setSplitPosition(0, Unit.PIXELS);
            }
        });

        showFilterButton.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                hideFilterButton.setVisible(true);
                showFilterButton.setVisible(false);
                vSplitPanel.setSplitPosition(splitPosition, splitUnit);
            }
        });

        GridLayout filterButtonLayout = new GridLayout(2, 1);
        filterButtonLayout.setHeight(25, Unit.PIXELS);
        filterButtonLayout.addComponent(hideFilterButton, 0, 0);
        filterButtonLayout.addComponent(showFilterButton, 1, 0);

        Label filterHintLabel = new Label();
        filterHintLabel.setCaptionAsHtml(true);
        filterHintLabel.setCaption(VaadinIcons.QUESTION_CIRCLE_O.getHtml()
                + " Drag items from the topology tree to the tables below in order to narrow your search.");
        filterHintLabel.addStyleName(ValoTheme.LABEL_TINY);
        filterHintLabel.addStyleName(ValoTheme.LABEL_LIGHT);

        layout.addComponent(filterHintLabel);
        layout.addComponent(hListSelectLayout);
        layout.addComponent(hDateSelectLayout);
        layout.addComponent(hSearchLayout);
        layout.setSizeFull();

        Panel filterPanel = new Panel();
        filterPanel.setHeight(340, Unit.PIXELS);
        filterPanel.setWidth("100%");
        filterPanel.setContent(layout);
        filterPanel.addStyleName(ValoTheme.PANEL_BORDERLESS);

        vSplitPanel.setFirstComponent(filterPanel);

        CssLayout hErrorTable = new CssLayout();
        hErrorTable.setSizeFull();
        hErrorTable.addComponent(this.wiretapTable);

        vSplitPanel.setSecondComponent(hErrorTable);
        vSplitPanel.setSplitPosition(350, Unit.PIXELS);

        GridLayout wrapper = new GridLayout(1, 2);
        wrapper.setRowExpandRatio(0, .01f);
        wrapper.setRowExpandRatio(1, .99f);
        wrapper.setSizeFull();
        wrapper.addComponent(filterButtonLayout);
        wrapper.setComponentAlignment(filterButtonLayout, Alignment.MIDDLE_RIGHT);
        wrapper.addComponent(vSplitPanel);

        return wrapper;
    }

}