com.save.reports.promodeals.PromoDealReportUI.java Source code

Java tutorial

Introduction

Here is the source code for com.save.reports.promodeals.PromoDealReportUI.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.save.reports.promodeals;

import com.save.common.CommonButton;
import com.save.common.CommonComboBox;
import com.save.common.CommonTextField;
import com.save.global.ErrorLoggedNotification;
import com.save.reports.ExportDataGridToExcel;
import com.save.utilities.CommonUtilities;
import com.vaadin.data.Property;
import com.vaadin.server.Page;
import com.vaadin.server.StreamResource;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.DateField;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;

/**
 *
 * @author jetdario
 */
public class PromoDealReportUI extends VerticalLayout implements Button.ClickListener {

    PromoDealDataGridProperties promoDealGrid = new PromoDealDataGridProperties();

    boolean isAreaSelect = false;
    boolean isClientSelect = false;
    boolean isAmountSelect = false;
    boolean isDateSelect = false;

    public PromoDealReportUI() {
        setSizeFull();

        addComponent(promoDealGrid);
        setExpandRatio(promoDealGrid, 1);

        HorizontalLayout h = new HorizontalLayout();
        h.setWidth("100%");
        h.setSpacing(true);
        h.setMargin(new MarginInfo(false, true, true, false));

        Button filter = new CommonButton("FILTER");
        filter.setWidth("200px");
        filter.addClickListener(this);
        h.addComponent(filter);
        h.setComponentAlignment(filter, Alignment.MIDDLE_RIGHT);
        h.setExpandRatio(filter, 1);

        Button exportToExcel = new CommonButton("EXPORT TO EXCEL");
        exportToExcel.setWidth("200px");
        exportToExcel.addClickListener(this);
        h.addComponent(exportToExcel);
        h.setComponentAlignment(exportToExcel, Alignment.MIDDLE_RIGHT);

        addComponent(h);
    }

    private Window filterReport() {
        Window sub = new Window("FILTER REPORT");
        sub.setWidth("400px");
        sub.setModal(true);
        sub.center();

        FormLayout f = new FormLayout();
        f.setWidth("100%");
        f.setMargin(true);
        f.setSpacing(true);

        CheckBox areaCheckBox = new CheckBox("Filter by Area");
        f.addComponent(areaCheckBox);

        CheckBox clientCheckBox = new CheckBox("Filter by Client");
        f.addComponent(clientCheckBox);

        CheckBox amountCheckBox = new CheckBox("Filter by Amount");
        f.addComponent(amountCheckBox);

        CheckBox dateCheckBox = new CheckBox("Filter by Date");
        f.addComponent(dateCheckBox);

        ComboBox areaComboBox = CommonComboBox.areas();
        ComboBox clientComboBox = CommonComboBox.getAllClients();

        areaComboBox.setEnabled(false);
        f.addComponent(areaComboBox);
        areaCheckBox.addValueChangeListener((Property.ValueChangeEvent event) -> {
            areaComboBox.setEnabled((boolean) event.getProperty().getValue());
            clientComboBox.setEnabled(!(boolean) event.getProperty().getValue());
            clientCheckBox.setValue(!(boolean) event.getProperty().getValue());
            isAreaSelect = (boolean) event.getProperty().getValue();
            isClientSelect = !(boolean) event.getProperty().getValue();
        });

        clientComboBox.setEnabled(false);
        f.addComponent(clientComboBox);
        clientCheckBox.addValueChangeListener((Property.ValueChangeEvent e) -> {
            clientComboBox.setEnabled((boolean) e.getProperty().getValue());
            areaComboBox.setEnabled(!(boolean) e.getProperty().getValue());
            areaCheckBox.setValue(!(boolean) e.getProperty().getValue());
            isClientSelect = (boolean) e.getProperty().getValue();
            isAreaSelect = !(boolean) e.getProperty().getValue();
        });

        TextField amountField = new CommonTextField("Amount: ");
        amountField.addStyleName("align-right");
        amountField.setEnabled(false);
        f.addComponent(amountField);
        amountCheckBox.addValueChangeListener((Property.ValueChangeEvent e) -> {
            amountField.setEnabled((boolean) e.getProperty().getValue());
            isAmountSelect = (boolean) e.getProperty().getValue();
        });

        HorizontalLayout h = new HorizontalLayout();
        h.setCaption("Date: ");
        h.setWidth("100%");
        h.setSpacing(true);

        DateField from = new DateField();
        from.setWidth("100%");
        from.setEnabled(false);
        h.addComponent(from);

        DateField to = new DateField();
        to.setWidth("100%");
        to.setEnabled(false);
        h.addComponent(to);

        dateCheckBox.addValueChangeListener((Property.ValueChangeEvent e) -> {
            from.setEnabled((boolean) e.getProperty().getValue());
            to.setEnabled((boolean) e.getProperty().getValue());
            isDateSelect = (boolean) e.getProperty().getValue();
        });

        f.addComponent(h);

        Button generate = new CommonButton("Generate Report");
        generate.addClickListener((Button.ClickEvent e) -> {
            if (!isClientSelect && !isAmountSelect && !isDateSelect) {
                promoDealGrid.setContainerDataSource(new PromoDealDataContainer());
                promoDealGrid.setFrozenColumnCount(2);
            }

            if (isAreaSelect && !isAmountSelect && !isDateSelect) {
                if (areaComboBox.getValue() == null) {
                    Notification.show("Select a Client!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                promoDealGrid.setContainerDataSource(
                        new PromoDealDataContainer(areaComboBox.getItemCaption(areaComboBox.getValue())));
                promoDealGrid.setFrozenColumnCount(2);
            }

            if (isClientSelect && !isAmountSelect && !isDateSelect) {
                if (clientComboBox.getValue() == null) {
                    Notification.show("Select a Client!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                promoDealGrid.setContainerDataSource(new PromoDealDataContainer((int) clientComboBox.getValue()));
                promoDealGrid.setFrozenColumnCount(2);
            }

            if (!isClientSelect && isAmountSelect && !isDateSelect) {
                if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) {
                    Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE);
                    return;
                } else {
                    if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) {
                        Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE);
                        return;
                    }
                }

                promoDealGrid.setContainerDataSource(new PromoDealDataContainer(
                        CommonUtilities.convertStringToDouble(amountField.getValue().trim())));
                promoDealGrid.setFrozenColumnCount(2);
            }

            if (!isClientSelect && !isAmountSelect && isDateSelect) {
                if (from.getValue() == null) {
                    Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (to.getValue() == null) {
                    Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                promoDealGrid.setContainerDataSource(new PromoDealDataContainer(from.getValue(), to.getValue()));
                promoDealGrid.setFrozenColumnCount(2);
            }

            if (isAreaSelect && isAmountSelect && !isDateSelect) {
                if (areaComboBox.getValue() == null) {
                    Notification.show("Select an Area!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) {
                    Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE);
                    return;
                } else {
                    if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) {
                        Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE);
                        return;
                    }
                }

                promoDealGrid.setContainerDataSource(
                        new PromoDealDataContainer(areaComboBox.getItemCaption(areaComboBox.getValue()),
                                CommonUtilities.convertStringToDouble(amountField.getValue().trim())));
                promoDealGrid.setFrozenColumnCount(2);
            }

            if (isClientSelect && isAmountSelect && !isDateSelect) {
                if (clientComboBox.getValue() == null) {
                    Notification.show("Select a Client!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) {
                    Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE);
                    return;
                } else {
                    if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) {
                        Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE);
                        return;
                    }
                }

                promoDealGrid.setContainerDataSource(new PromoDealDataContainer((int) clientComboBox.getValue(),
                        CommonUtilities.convertStringToDouble(amountField.getValue().trim())));
                promoDealGrid.setFrozenColumnCount(2);
            }

            if (isAreaSelect && !isAmountSelect && isDateSelect) {
                if (areaComboBox.getValue() == null) {
                    Notification.show("Select an Area!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (from.getValue() == null) {
                    Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (to.getValue() == null) {
                    Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                promoDealGrid.setContainerDataSource(new PromoDealDataContainer(
                        areaComboBox.getItemCaption(areaComboBox.getValue()), from.getValue(), to.getValue()));
                promoDealGrid.setFrozenColumnCount(2);
            }

            if (isClientSelect && !isAmountSelect && isDateSelect) {
                if (clientComboBox.getValue() == null) {
                    Notification.show("Select a Client!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (from.getValue() == null) {
                    Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (to.getValue() == null) {
                    Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                promoDealGrid.setContainerDataSource(new PromoDealDataContainer((int) clientComboBox.getValue(),
                        from.getValue(), to.getValue()));
                promoDealGrid.setFrozenColumnCount(2);
            }

            if (!isClientSelect && isAmountSelect && isDateSelect) {
                if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) {
                    Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE);
                    return;
                } else {
                    if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) {
                        Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE);
                        return;
                    }
                }

                if (from.getValue() == null) {
                    Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (to.getValue() == null) {
                    Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                promoDealGrid.setContainerDataSource(new PromoDealDataContainer(
                        CommonUtilities.convertStringToDouble(amountField.getValue().trim()), from.getValue(),
                        to.getValue()));
                promoDealGrid.setFrozenColumnCount(2);
            }

            if (isAreaSelect && isAmountSelect && isDateSelect) {
                if (areaComboBox.getValue() == null) {
                    Notification.show("Select an Area!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) {
                    Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE);
                    return;
                } else {
                    if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) {
                        Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE);
                        return;
                    }
                }

                if (from.getValue() == null) {
                    Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (to.getValue() == null) {
                    Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                promoDealGrid.setContainerDataSource(
                        new PromoDealDataContainer(areaComboBox.getItemCaption(areaComboBox.getValue()),
                                CommonUtilities.convertStringToDouble(amountField.getValue().trim()),
                                from.getValue(), to.getValue()));
                promoDealGrid.setFrozenColumnCount(2);
            }

            if (isClientSelect && isAmountSelect && isDateSelect) {
                if (clientComboBox.getValue() == null) {
                    Notification.show("Select a Client!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (amountField.getValue() == null || amountField.getValue().trim().isEmpty()) {
                    Notification.show("Enter Amount!", Notification.Type.ERROR_MESSAGE);
                    return;
                } else {
                    if (!CommonUtilities.checkInputIfDouble(amountField.getValue().trim())) {
                        Notification.show("Enter numeric format for Amount!", Notification.Type.ERROR_MESSAGE);
                        return;
                    }
                }

                if (from.getValue() == null) {
                    Notification.show("Select first Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                if (to.getValue() == null) {
                    Notification.show("Select 2nd Date!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                promoDealGrid.setContainerDataSource(new PromoDealDataContainer((int) clientComboBox.getValue(),
                        CommonUtilities.convertStringToDouble(amountField.getValue().trim()), from.getValue(),
                        to.getValue()));
                promoDealGrid.setFrozenColumnCount(2);
            }

            sub.close();
        });
        f.addComponent(generate);

        sub.setContent(f);
        sub.getContent().setHeightUndefined();

        return sub;
    }

    @Override
    public void buttonClick(Button.ClickEvent event) {
        switch (event.getButton().getCaption()) {
        case "FILTER": {
            reset();
            Window sub = filterReport();
            if (sub.getParent() == null) {
                UI.getCurrent().addWindow(sub);
            }
            break;
        }
        default: {
            if (!isClientSelect() && !isAmountSelect() && !isDateSelect()) {
                Window sub = exportLargeData();
                if (sub.getParent() == null) {
                    UI.getCurrent().addWindow(sub);
                }

                return;
            }

            processExportDataToExcel();

            break;
        }
        }
    }

    public boolean isAreaSelect() {
        return isAreaSelect;
    }

    public boolean isClientSelect() {
        return isClientSelect;
    }

    public boolean isAmountSelect() {
        return isAmountSelect;
    }

    public boolean isDateSelect() {
        return isDateSelect;
    }

    void reset() {
        isAreaSelect = false;
        isClientSelect = false;
        isAmountSelect = false;
        isDateSelect = false;
    }

    void processExportDataToExcel() {
        ExportDataGridToExcel export = new ExportDataGridToExcel(promoDealGrid);
        export.workSheet();

        StreamResource.StreamSource source = () -> {
            try {
                File f = new File(export.getFilePath());
                FileInputStream fis = new FileInputStream(f);
                return fis;
            } catch (Exception e) {
                ErrorLoggedNotification.showErrorLoggedOnWindow(e.toString(), this.getClass().getName());
                return null;
            }
        };

        Date date = new Date();

        StreamResource resource = new StreamResource(source, "PromoDealsReport" + "-" + date.getTime() + ".xls");
        resource.setMIMEType("application/vnd.ms-office");

        Page.getCurrent().open(resource, null, false);
    }

    Window exportLargeData() {
        Window sub = new Window("EXPORT LARGE DATA");
        sub.setWidth("300px");
        sub.setModal(true);
        sub.center();

        VerticalLayout v = new VerticalLayout();
        v.setSizeFull();
        v.setMargin(true);
        v.setSpacing(true);

        Label status = new Label("Exporting large amount of data will take longer and will eat a lot of memory.",
                ContentMode.HTML);
        status.setContentMode(ContentMode.HTML);
        v.addComponent(status);

        Button b = new CommonButton("PROCEED TO EXPORT?");
        b.addClickListener((Button.ClickEvent e) -> {
            sub.close();
            processExportDataToExcel();
        });
        v.addComponent(b);

        sub.setContent(v);
        sub.getContent().setHeightUndefined();
        return sub;
    }
}