com.hris.payroll.alphalist.AlphaListMainUI.java Source code

Java tutorial

Introduction

Here is the source code for com.hris.payroll.alphalist.AlphaListMainUI.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.hris.payroll.alphalist;

import com.hris.common.CommonUtil;
import com.hris.common.CorporateComboBox;
import com.hris.common.EmploymentStatusComboBox;
import com.hris.payroll.thirteenthmonth.ExportDataGridToExcel;
import com.hris.service.AlphaListService;
import com.hris.service.CorporationService;
import com.hris.service.EmployeeService;
import com.hris.payroll.serviceprovider.AlphaListServiceImpl;
import com.hris.serviceprovider.CorporationServiceImpl;
import com.hris.employee.serviceprovider.EmployeeServiceImpl;
import com.hris.model.EmployeePersonalInformation;
import com.vaadin.data.Property;
import com.vaadin.server.FontAwesome;
import com.vaadin.server.Page;
import com.vaadin.server.StreamResource;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.ProgressBar;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;
import java.io.File;
import java.io.FileInputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jetdario
 */
public class AlphaListMainUI extends VerticalLayout {

    EmployeeService es = new EmployeeServiceImpl();
    CorporationService cs = new CorporationServiceImpl();
    AlphaListService als = new AlphaListServiceImpl();

    ComboBox corporation = new CorporateComboBox();
    ComboBox trade = new ComboBox();
    ComboBox branch = new ComboBox();
    ComboBox year;
    ComboBox employmentStatus = new EmploymentStatusComboBox();

    Grid grid = new AlphaListDataGridPropertyForSalary();
    ProgressBar progress = new ProgressBar(new Float(0.0));
    Label status = new Label();
    Label processLabel = new Label();
    Button exportToExcelButton = new Button("EXPORT TO EXCEL");
    Button alphaListBtn = new Button("ALPHA LIST");

    private int branchId;
    volatile double current;
    private double dataSize;

    public AlphaListMainUI(int branchId) {
        this.branchId = branchId;

        setSizeFull();
        setMargin(new MarginInfo(true, true, false, true));
        setSpacing(true);

        HorizontalLayout h1 = new HorizontalLayout();
        h1.setCaption("Year: ");
        h1.setWidthUndefined();
        h1.setSpacing(true);

        h1.addComponent(employmentStatus);
        h1.addComponent(selectYear());
        h1.addComponent(alphaListButton());
        h1.addComponent(exportToExcelButton());

        addComponent(h1);

        HorizontalLayout h2 = new HorizontalLayout();
        h2.setWidthUndefined();
        h2.setSpacing(true);

        progress.setWidth("410px");
        h2.addComponent(progress);
        h2.setComponentAlignment(progress, Alignment.MIDDLE_LEFT);

        status.setValue("0%");
        h2.addComponent(status);

        processLabel.setValue("");
        h2.addComponent(processLabel);

        addComponent(h2);
        addComponent(grid);
        setExpandRatio(grid, 3);
    }

    void populateDataGrid() {
        if (getBranchId() == 0) {
            Notification.show("Select a Branch!!!", Notification.Type.WARNING_MESSAGE);
            return;
        }
        exportToExcelButton.setEnabled(false);
        grid.getContainerDataSource().removeAllItems();

        getUI().getSession().getLockInstance();
        try {
            ImplementAlphaListRunnable ir = new ImplementAlphaListRunnable(grid, progress, status,
                    exportToExcelButton, getBranchId(),
                    CommonUtil.convertStringToInteger(year.getValue().toString()), current, dataSize, processLabel,
                    employmentStatus.getValue().toString(), alphaListButton());
            Thread t = new Thread(ir);
            t.start();
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(AlphaListMainUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        UI.getCurrent().setPollInterval(500);
    }

    ComboBox.ValueChangeListener branchPropertyChangeListener = (Property.ValueChangeEvent event) -> {
        if (event.getProperty().getValue() == null) {
        } else {
            branchId = (int) event.getProperty().getValue();
        }
    };

    int getBranchId() {
        return branchId;
    }

    private ComboBox selectYear() {
        year = new ComboBox();
        year.setWidth("200px");
        year.setNullSelectionAllowed(false);
        year.addStyleName(ValoTheme.COMBOBOX_SMALL);

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int y = cal.get(Calendar.YEAR);

        for (int i = y; i > 2011; i--) {
            year.addItem(String.valueOf(i));
        }
        year.setValue(String.valueOf(y));

        return year;
    }

    private Button alphaListButton() {
        alphaListBtn.setWidth("200px");
        alphaListBtn.setIcon(FontAwesome.SPINNER);
        alphaListBtn.addStyleName(ValoTheme.BUTTON_PRIMARY);
        alphaListBtn.addStyleName(ValoTheme.BUTTON_SMALL);
        alphaListBtn.setDisableOnClick(true);
        alphaListBtn.addClickListener((Button.ClickEvent event) -> {
            status.setValue(" Loading... ");
            current = 1.0;
            dataSize = es.findEmployeeByBranchForAlphaList(getBranchId(), employmentStatus.getValue().toString(),
                    CommonUtil.convertStringToInteger(year.getValue().toString())).size();

            List<EmployeePersonalInformation> employeeLists = es.findEmployeeByBranchForAlphaList(getBranchId(),
                    employmentStatus.getValue().toString(),
                    CommonUtil.convertStringToInteger(year.getValue().toString()));
            if (employeeLists.isEmpty()) {
                Notification.show("NO DATA FOUND!!!", Notification.Type.WARNING_MESSAGE);
                alphaListBtn.setEnabled(true);
                return;
            }

            populateDataGrid();
        });

        return alphaListBtn;
    }

    private Button exportToExcelButton() {
        ExportDataGridToExcel exportGrid = new ExportDataGridToExcel(grid);

        exportToExcelButton.setEnabled(false);
        exportToExcelButton.setWidth("200px");
        exportToExcelButton.setIcon(FontAwesome.EXCLAMATION_CIRCLE);
        exportToExcelButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
        exportToExcelButton.addStyleName(ValoTheme.BUTTON_SMALL);
        exportToExcelButton.addClickListener((Button.ClickEvent e) -> {
            exportGrid.workSheet();

            StreamResource.StreamSource source = () -> {
                try {
                    File f = new File(exportGrid.getFilePath());
                    FileInputStream fis = new FileInputStream(f);
                    return fis;
                } catch (Exception e1) {
                    e1.getMessage();
                    return null;
                }
            };

            Date date = new Date();
            String branchName = cs.getBranchById(getBranchId());
            int tradeId = cs.getTradeIdByBranchId(getBranchId());
            String tradeName = cs.getTradeById(tradeId);
            int corporateId = cs.getCorporateIdByTradeId(tradeId);
            String corporateName = cs.getCorporateById(corporateId);

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

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

        return exportToExcelButton;
    }
}