ru.codeinside.adm.ui.LogTab.java Source code

Java tutorial

Introduction

Here is the source code for ru.codeinside.adm.ui.LogTab.java

Source

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * Copyright (c) 2013, MPL CodeInside http://codeinside.ru
 */

package ru.codeinside.adm.ui;

import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.provider.CachingLocalEntityProvider;
import com.vaadin.data.Property;
import com.vaadin.terminal.DownloadStream;
import com.vaadin.terminal.StreamResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;
import com.vaadin.ui.themes.Reindeer;
import org.tepi.filtertable.FilterTable;
import ru.codeinside.adm.AdminServiceProvider;
import ru.codeinside.adm.database.SoapPacket;
import ru.codeinside.jpa.ActivitiEntityManager;
import ru.codeinside.jpa.LogEntityManager;

import javax.persistence.EntityManager;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;

public class LogTab extends VerticalLayout implements TabSheet.SelectedTabChangeListener {

    FilterTable sl;
    FilterTable ul;
    SmevLog smevLog;
    TabSheet tabSheet;

    public LogTab() {
        setSizeFull();
        tabSheet = new TabSheet();
        tabSheet.addStyleName(Reindeer.TABSHEET_MINIMAL);
        tabSheet.addStyleName(Reindeer.TABSHEET_BORDERLESS);
        tabSheet.addListener(this);
        tabSheet.setSizeFull();
        addComponent(tabSheet);
    }

    final class SmevLog extends VerticalLayout {

        VerticalSplitPanel splitPanel;

        public SmevLog() {
            splitPanel = new VerticalSplitPanel();
            sl = new FilterTable();
            splitPanel.setFirstComponent(sl);
            addComponent(splitPanel);
            setSizeFull();
            sl.setSizeFull();
            sl.setFilterBarVisible(true);
            sl.setSelectable(true);
            sl.setImmediate(true);
            sl.setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
            sl.setColumnCollapsingAllowed(true);
            sl.setColumnReorderingAllowed(true);
            sl.setFilterDecorator(new FilterDecorator_());
            sl.setFilterGenerator(new FilterGenerator_(Arrays.asList("bidId"), Arrays.asList("client")));
            final HorizontalLayout hl = new HorizontalLayout();
            hl.setMargin(true);
            hl.setSpacing(true);
            hl.setSizeFull();
            splitPanel.setSecondComponent(hl);
            Panel sendPanel = new Panel(" ?");
            sendPanel.setSizeFull();
            final FormLayout sendForm = new FormLayout();
            sendPanel.setContent(sendForm);
            hl.addComponent(sendPanel);
            Panel receivePanel = new Panel("? ?");
            receivePanel.setSizeFull();
            final FormLayout receiveForm = new FormLayout();
            receivePanel.setContent(receiveForm);
            hl.addComponent(receivePanel);
            final Panel error = new Panel("Error");
            error.setSizeFull();

            sl.setContainerDataSource(
                    jpaContainer(ru.codeinside.adm.database.SmevLog.class, ActivitiEntityManager.INSTANCE));
            sl.setVisibleColumns(new String[] { "bidId", "infoSystem", "component", "client", "logDate" });
            sl.setColumnHeaders(new String[] { " ?", "? ??",
                    "", "", "" });
            sl.setSortContainerPropertyId("logDate");
            sl.setSortAscending(false);
            sl.addGeneratedColumn("client", new YesNoColumnGenerator());
            sl.addGeneratedColumn("logDate", new DateColumnGenerator("dd.MM.yyyy HH:mm:ss.SSS"));
            sl.addListener(new Property.ValueChangeListener() {
                @Override
                public void valueChange(Property.ValueChangeEvent event) {
                    sendForm.removeAllComponents();
                    receiveForm.removeAllComponents();
                    error.removeAllComponents();
                    hl.removeComponent(error);
                    if (event.getProperty().getValue() == null) {
                        return;
                    }
                    final ru.codeinside.adm.database.SmevLog oepLog = ActivitiEntityManager.INSTANCE
                            .find(ru.codeinside.adm.database.SmevLog.class, event.getProperty().getValue());
                    if (oepLog != null) {
                        final String componentName = oepLog.getComponent() == null ? ""
                                : oepLog.getComponent() + "_";

                        final SoapPacket sendPacket = oepLog.getSendPacket();
                        if (sendPacket != null) {
                            sendForm.addComponent(new RoTextField("Sender", sendPacket.getSender()));
                            sendForm.addComponent(new RoTextField("Recipient", sendPacket.getRecipient()));
                            sendForm.addComponent(new RoTextField("Originator", sendPacket.getOriginator()));
                            sendForm.addComponent(new RoTextField("Service", sendPacket.getService()));
                            sendForm.addComponent(new RoTextField("Type code", sendPacket.getTypeCode()));
                            sendForm.addComponent(new RoTextField("Status", sendPacket.getStatus()));
                            sendForm.addComponent(new RoTextField("Date",
                                    sendPacket.getDate() == null ? null : sendPacket.getDate().toString()));
                            sendForm.addComponent(new RoTextField("Request ID ref", sendPacket.getRequestIdRef()));
                            sendForm.addComponent(
                                    new RoTextField("Origin request ID ref", sendPacket.getOriginRequestIdRef()));
                            sendForm.addComponent(new RoTextField("Service code", sendPacket.getServiceCode()));
                            sendForm.addComponent(new RoTextField("Case number", sendPacket.getCaseNumber()));
                            sendForm.addComponent(new RoTextField("Exchange type", sendPacket.getExchangeType()));
                        }

                        if (oepLog.getSendHttp() != null) {
                            Button sendHttp = new Button(" http-log");
                            sendHttp.addStyleName(BaseTheme.BUTTON_LINK);
                            sendHttp.addListener(new Button.ClickListener() {
                                @Override
                                public void buttonClick(Button.ClickEvent event) {
                                    StreamResource.StreamSource streamSource = new StreamResource.StreamSource() {

                                        private static final long serialVersionUID = 456334952891567271L;

                                        public InputStream getStream() {
                                            return new ByteArrayInputStream(oepLog.getSendHttp().getData());
                                        }
                                    };

                                    String ddMMyy_hhmmss = new SimpleDateFormat("ddMMyy_hhmmss")
                                            .format(oepLog.getLogDate());
                                    StreamResource resource = new StreamResource(streamSource,
                                            componentName + "send_" + ddMMyy_hhmmss + ".log",
                                            event.getButton().getApplication()) {
                                        private static final long serialVersionUID = -3869546661105532851L;

                                        public DownloadStream getStream() {
                                            final StreamSource ss = getStreamSource();
                                            if (ss == null) {
                                                return null;
                                            }
                                            DownloadStream ds = new DownloadStream(ss.getStream(), "text/plain",
                                                    getFilename());
                                            ds.setParameter("Content-Disposition",
                                                    "attachment; filename=\"" + getFilename() + "\"");
                                            return ds;
                                        }
                                    };
                                    Window window = event.getButton().getWindow();
                                    window.open(resource, "_top", false);
                                }
                            });
                            sendForm.addComponent(sendHttp);
                        }

                        final SoapPacket receivePacket = oepLog.getReceivePacket();
                        if (receivePacket != null) {
                            receiveForm.addComponent(new RoTextField("Sender", receivePacket.getSender()));
                            receiveForm.addComponent(new RoTextField("Recipient", receivePacket.getRecipient()));
                            receiveForm.addComponent(new RoTextField("Originator", receivePacket.getOriginator()));
                            receiveForm.addComponent(new RoTextField("Service", receivePacket.getService()));
                            receiveForm.addComponent(new RoTextField("Type code", receivePacket.getTypeCode()));
                            receiveForm.addComponent(new RoTextField("Status", receivePacket.getStatus()));
                            receiveForm.addComponent(new RoTextField("Date",
                                    receivePacket.getDate() == null ? null : receivePacket.getDate().toString()));
                            receiveForm.addComponent(
                                    new RoTextField("Request ID ref", receivePacket.getRequestIdRef()));
                            receiveForm.addComponent(new RoTextField("Origin request ID ref",
                                    receivePacket.getOriginRequestIdRef()));
                            receiveForm
                                    .addComponent(new RoTextField("Service code", receivePacket.getServiceCode()));
                            receiveForm.addComponent(new RoTextField("Case number", receivePacket.getCaseNumber()));
                            receiveForm.addComponent(
                                    new RoTextField("Exchange type", receivePacket.getExchangeType()));
                        }

                        if (oepLog.getReceiveHttp() != null) {
                            Button receiveHttp = new Button(" http-log");
                            receiveHttp.addStyleName(BaseTheme.BUTTON_LINK);
                            receiveHttp.addListener(new Button.ClickListener() {
                                @Override
                                public void buttonClick(Button.ClickEvent event) {
                                    StreamResource.StreamSource streamSource = new StreamResource.StreamSource() {

                                        private static final long serialVersionUID = 456334952891567271L;

                                        public InputStream getStream() {
                                            return new ByteArrayInputStream(oepLog.getReceiveHttp().getData());
                                        }
                                    };
                                    String ddMMyy_hhmmss = new SimpleDateFormat("ddMMyy_hhmmss")
                                            .format(oepLog.getLogDate());
                                    StreamResource resource = new StreamResource(streamSource,
                                            componentName + "receive_" + ddMMyy_hhmmss + ".log",
                                            event.getButton().getApplication()) {
                                        private static final long serialVersionUID = -3869546661105537851L;

                                        public DownloadStream getStream() {
                                            final StreamSource ss = getStreamSource();
                                            if (ss == null) {
                                                return null;
                                            }
                                            DownloadStream ds = new DownloadStream(ss.getStream(), "text/plain",
                                                    getFilename());
                                            ds.setParameter("Content-Disposition",
                                                    "attachment; filename=\"" + getFilename() + "\"");
                                            return ds;
                                        }
                                    };
                                    Window window = event.getButton().getWindow();
                                    window.open(resource, "_top", false);
                                }
                            });
                            receiveForm.addComponent(receiveHttp);
                        }

                        if (oepLog.getError() != null && !oepLog.getError().equals("")) {
                            FormLayout newContent = new FormLayout();
                            newContent.addComponent(new Label(oepLog.getError()));
                            error.setContent(newContent);
                            hl.addComponent(error);
                        }
                    }
                }
            });
        }

        private class RoTextField extends Label {
            RoTextField(String caption, String value) {
                super(value);
                setCaption(caption);
            }
        }
    }

    final class UserLog extends FilterTable {

        public UserLog() {
            ul = this;
            setSizeFull();
            setFilterBarVisible(true);
            setSelectable(true);
            setImmediate(true);
            setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
            setColumnCollapsingAllowed(true);
            setColumnReorderingAllowed(true);
            setFilterDecorator(new FilterDecorator_());

            //TODO:  ??    ??  ?,  
            JPAContainer<ru.codeinside.log.Log> container = jpaContainer(ru.codeinside.log.Log.class,
                    LogEntityManager.INSTANCE);
            container.addNestedContainerProperty("actor.name");
            container.addNestedContainerProperty("actor.ip");
            container.addNestedContainerProperty("actor.browser");
            container.addNestedContainerProperty("actor.os");
            setContainerDataSource(container);
            setVisibleColumns(new Object[] { "actor.name", "actor.browser", "actor.ip", "actor.os", "entityName",
                    "entityId", "action", "info", "date" });
            setColumnHeaders(new String[] { "", "", "IP", "", "", "ID",
                    "?", "", "" });
            setSortContainerPropertyId("date");
            setSortAscending(false);
            addGeneratedColumn("date", new DateColumnGenerator("dd.MM.yyyy HH:mm:ss.SSS"));
        }
    }

    /**
     * ?? ???.
     */
    @Override
    public void selectedTabChange(TabSheet.SelectedTabChangeEvent event) {
        Component tab = event.getTabSheet().getSelectedTab();
        if (this == tab) {
            if (smevLog == null) {
                tabSheet.addTab(smevLog = new SmevLog(), "");
                tabSheet.addTab(new UserLog(), "?? ?");
            }
            return;
        }
        if (this == tab || smevLog == tab) {
            ((JPAContainer) sl.getContainerDataSource()).getEntityProvider().refresh();
            sl.refreshRowCache();
        }
        if (this == tab || ul == tab) {
            ((JPAContainer) ul.getContainerDataSource()).getEntityProvider().refresh();
            ul.refreshRowCache();
        }
    }

    //TODO:  ??    ??  ?,  
    <T> JPAContainer<T> jpaContainer(Class<T> clazz, EntityManager entityManager) {
        JPAContainer<T> container = new JPAContainer<T>(clazz);
        container.setReadOnly(true);
        container.setEntityProvider(new CachingLocalEntityProvider<T>(clazz, entityManager));
        return container;
    }

}