com.save.reports.reimbursement.ReimbursementReportUI.java Source code

Java tutorial

Introduction

Here is the source code for com.save.reports.reimbursement.ReimbursementReportUI.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.reimbursement;

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 ReimbursementReportUI extends VerticalLayout implements Button.ClickListener {

    ReimbursementDataGridProperties mrDataGrid = new ReimbursementDataGridProperties();

    boolean isEmployee = false;
    boolean isAmount = false;
    boolean isDate = false;

    public ReimbursementReportUI() {
        setSizeFull();

        addComponent(mrDataGrid);
        setExpandRatio(mrDataGrid, 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 print = new CommonButton("EXPORT TO EXCEL");
        print.setWidth("200px");
        print.addClickListener(this);
        h.addComponent(print);
        h.setComponentAlignment(print, 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 employeeCheckBox = new CheckBox("Filter by Employee");
        f.addComponent(employeeCheckBox);

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

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

        ComboBox employeeComboBox = CommonComboBox.getAllEmpployees(null);
        employeeComboBox.setEnabled(false);
        f.addComponent(employeeComboBox);
        employeeCheckBox.addValueChangeListener((Property.ValueChangeEvent e) -> {
            employeeComboBox.setEnabled((boolean) e.getProperty().getValue());
            isEmployee = (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());
            isAmount = (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());
            isDate = (boolean) e.getProperty().getValue();
        });

        f.addComponent(h);

        Button generate = new CommonButton("Generate Report");
        generate.addClickListener((Button.ClickEvent e) -> {
            if (!isEmployee && !isAmount && !isDate) {
                mrDataGrid.setContainerDataSource(new ReimbursementDataContainer());
                mrDataGrid.setFrozenColumnCount(2);
            }

            if (isEmployee && !isAmount && !isDate) {
                if (employeeComboBox.getValue() == null) {
                    Notification.show("Select a Client!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                mrDataGrid.setContainerDataSource(
                        new ReimbursementDataContainer(employeeComboBox.getValue().toString()));
                mrDataGrid.setFrozenColumnCount(2);
            }

            if (!isEmployee && isAmount && !isDate) {
                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;
                    }
                }

                mrDataGrid.setContainerDataSource(new ReimbursementDataContainer(
                        CommonUtilities.convertStringToDouble(amountField.getValue().trim())));
                mrDataGrid.setFrozenColumnCount(2);
            }

            if (!isEmployee && !isAmount && isDate) {
                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;
                }

                mrDataGrid.setContainerDataSource(new ReimbursementDataContainer(from.getValue(), to.getValue()));
                mrDataGrid.setFrozenColumnCount(2);
            }

            if (isEmployee && isAmount && !isDate) {
                if (employeeComboBox.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;
                    }
                }

                mrDataGrid.setContainerDataSource(
                        new ReimbursementDataContainer(employeeComboBox.getValue().toString(),
                                CommonUtilities.convertStringToDouble(amountField.getValue().trim())));
                mrDataGrid.setFrozenColumnCount(2);
            }

            if (isEmployee && !isAmount && isDate) {
                if (employeeComboBox.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;
                }

                mrDataGrid.setContainerDataSource(new ReimbursementDataContainer(
                        employeeComboBox.getValue().toString(), from.getValue(), to.getValue()));
                mrDataGrid.setFrozenColumnCount(2);
            }

            if (!isEmployee && isAmount && isDate) {
                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;
                }

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

            if (isEmployee && isAmount && isDate) {
                if (employeeComboBox.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;
                }

                mrDataGrid.setContainerDataSource(
                        new ReimbursementDataContainer(employeeComboBox.getValue().toString(),
                                CommonUtilities.convertStringToDouble(amountField.getValue().trim()),
                                from.getValue(), to.getValue()));
                mrDataGrid.setFrozenColumnCount(2);
            }

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

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

        return sub;
    }

    @Override
    public void buttonClick(Button.ClickEvent event) {
        //TODO
        switch (event.getButton().getCaption()) {
        case "FILTER": {
            reset();
            Window sub = filterReport();
            if (sub.getParent() == null) {
                UI.getCurrent().addWindow(sub);
            }
            break;
        }

        default: {
            if (!isEmployee() && !isAmount() && !isDate()) {
                Window sub = exportLargeData();
                if (sub.getParent() == null) {
                    UI.getCurrent().addWindow(sub);
                }

                return;
            }

            processExportDataToExcel();

            break;
        }
        }
    }

    boolean isEmployee() {
        return isEmployee;
    }

    boolean isAmount() {
        return isAmount;
    }

    boolean isDate() {
        return isDate;
    }

    void reset() {
        isEmployee = false;
        isAmount = false;
        isDate = false;
    }

    void processExportDataToExcel() {
        ExportDataGridToExcel export = new ExportDataGridToExcel(mrDataGrid);
        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, "ReimbursementReport" + "-" + 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;
    }
}