com.hris.payroll.thirteenthmonth.ThirteenthMonth.java Source code

Java tutorial

Introduction

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

import com.hris.common.CommonUtil;
import com.hris.common.EmploymentStatusComboBox;
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.vaadin.data.Item;
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.DateField;
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.logging.Level;
import java.util.logging.Logger;

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

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

    ComboBox employmentStatus = new EmploymentStatusComboBox();
    ComboBox year;
    DateField fromDate = new DateField();
    DateField toDate = new DateField();

    Grid grid;
    ProgressBar progress = new ProgressBar(new Float(0.0));
    Label status = new Label();
    Button exportToExcelButton = new Button("EXPORT TO EXCEL");

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

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

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

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

        h1.addComponent(employmentStatus);
        h1.addComponent(selectYear());
        h1.addComponent(generate13thMonth());
        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);

        grid = new ThirteenthMonthDataGridProperty();
        addComponent(h2);
        addComponent(grid);
        setExpandRatio(grid, 3);
    }

    private void populateDataGrid() {
        if (getBranchId() == 0) {
            Notification.show("Select a Branch!!!", Notification.Type.WARNING_MESSAGE);
            return;
        }
        exportToExcelButton.setEnabled(false);
        removeComponent(grid);
        grid = new ThirteenthMonthDataGridProperty(CommonUtil.convertStringToInteger(year.getValue().toString()));
        addComponent(grid);
        setExpandRatio(grid, 3);
        grid.getContainerDataSource().removeAllItems();

        getUI().getSession().getLockInstance();
        try {
            ImplementThirteenthMonthRunnable ir = new ImplementThirteenthMonthRunnable(grid, progress, status,
                    exportToExcelButton, getBranchId(),
                    CommonUtil.convertStringToInteger(year.getValue().toString()),
                    employmentStatus.getValue().toString(), current, dataSize);
            Thread t = new Thread(ir);
            t.start();
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            Logger.getLogger(ThirteenthMonth.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;
    }

    DateField payrollCoveredFrom() {
        fromDate.setWidth("200px");
        fromDate.setValue(new Date());
        fromDate.addStyleName(ValoTheme.COMBOBOX_SMALL);

        return fromDate;
    }

    private DateField payrollCoveredTo() {
        toDate.setWidth("200px");
        toDate.setValue(new Date());
        toDate.addStyleName(ValoTheme.COMBOBOX_SMALL);

        return toDate;
    }

    private Button generate13thMonth() {
        Button button = new Button("13th MONTH");
        button.setWidth("200px");
        button.setIcon(FontAwesome.SPINNER);
        button.addStyleName(ValoTheme.BUTTON_PRIMARY);
        button.addStyleName(ValoTheme.BUTTON_SMALL);
        button.addClickListener((Button.ClickEvent event) -> {
            status.setValue(" Loading...");
            current = 1.0;
            dataSize = es.findEmployeeByBranch(getBranchId(), employmentStatus.getValue().toString(),
                    CommonUtil.convertStringToInteger(year.getValue().toString())).size();

            populateDataGrid();
        });

        return button;
    }

    private Button exportToExcelButton() {
        exportToExcelButton.setWidth("200px");
        exportToExcelButton.setIcon(FontAwesome.EXCLAMATION_CIRCLE);
        exportToExcelButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
        exportToExcelButton.addStyleName(ValoTheme.BUTTON_SMALL);
        exportToExcelButton.addClickListener((Button.ClickEvent e) -> {
            ExportDataGridToExcel exportGrid = new ExportDataGridToExcel(grid);
            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, "ThirteenthMonth-" + corporateName + "-"
                    + tradeName + "-" + branchName + "-" + date.getTime() + ".xls");
            resource.setMIMEType("application/vnd.ms-office");

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

        return exportToExcelButton;
    }

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

        StreamResource resource = new StreamResource(source, path);
        resource.setMIMEType("application/vnd.ms-excel");

        return resource;
    }

    void calculateGrandTotal(Item item) {
        item.getItemProperty("grand total").setValue(CommonUtil
                .convertStringToDouble(item.getItemProperty("salary january").getValue().toString())
                + CommonUtil.convertStringToDouble(item.getItemProperty("salary february").getValue().toString())
                + CommonUtil.convertStringToDouble(item.getItemProperty("salary march").getValue().toString())
                + CommonUtil.convertStringToDouble(item.getItemProperty("salary april").getValue().toString())
                + CommonUtil.convertStringToDouble(item.getItemProperty("salary may").getValue().toString())
                + CommonUtil.convertStringToDouble(item.getItemProperty("salary june").getValue().toString())
                + CommonUtil.convertStringToDouble(item.getItemProperty("salary july").getValue().toString())
                + CommonUtil.convertStringToDouble(item.getItemProperty("salary august").getValue().toString())
                + CommonUtil.convertStringToDouble(item.getItemProperty("salary september").getValue().toString())
                + CommonUtil.convertStringToDouble(item.getItemProperty("salary october").getValue().toString())
                + CommonUtil.convertStringToDouble(item.getItemProperty("salary november").getValue().toString())
                + CommonUtil.convertStringToDouble(item.getItemProperty("salary december").getValue().toString()));

        item.getItemProperty("13th month").setValue(
                CommonUtil.convertStringToDouble(item.getItemProperty("grand total").getValue().toString()) / 12);
    }
}