com.fnc.view.ui.AlphaListMainUI.java Source code

Java tutorial

Introduction

Here is the source code for com.fnc.view.ui.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.fnc.view.ui;

import com.fnc.common.CommonUtilities;
import com.fnc.common.CorporateComboBox;
import com.fnc.common.CorporateComboBoxPropertyListener;
import com.fnc.model.Employee;
import com.fnc.service.AlphaListService;
import com.fnc.service.CorporationService;
import com.fnc.service.EmployeeService;
import com.fnc.serviceprovider.AlphaListServiceImpl;
import com.fnc.serviceprovider.CorporationServiceImpl;
import com.fnc.serviceprovider.EmployeeServiceImpl;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.server.FontAwesome;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.ProgressBar;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @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;
    Grid grid = new AlphaListDataGridPropertyForSalary();
    ProgressBar progress = new ProgressBar(new Float(0.0));
    Label status = new Label();

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

    public AlphaListMainUI() {
        setSizeFull();
        setMargin(true);

        addComponent(buildForms());

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

        progress.setWidth("150px");
        h.addComponent(progress);
        h.setComponentAlignment(progress, Alignment.MIDDLE_LEFT);

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

        addComponent(h);
        addComponent(grid);
    }

    FormLayout buildForms() {
        FormLayout form = new FormLayout();
        form.setWidth("50%");

        corporation.setCaption("Corporation: ");
        corporation.addStyleName(ValoTheme.COMBOBOX_SMALL);
        trade.setCaption("Trade: ");
        trade.addStyleName(ValoTheme.COMBOBOX_SMALL);
        branch.setCaption("Branch: ");
        branch.addStyleName(ValoTheme.COMBOBOX_SMALL);

        corporation.addValueChangeListener(new CorporateComboBoxPropertyListener(trade, trade.getCaption()));
        form.addComponent(corporation);

        trade.addValueChangeListener(new CorporateComboBoxPropertyListener(branch, branch.getCaption()));
        form.addComponent(trade);

        branch.addValueChangeListener(branchPropertyChangeListener);
        form.addComponent(branch);

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

        h.addComponent(selectYear());
        h.addComponent(alphaListButton());

        form.addComponent(h);

        return form;
    }

    void populateDataGrid() {
        grid.getContainerDataSource().removeAllItems();
        new Thread() {
            @Override
            public void run() {
                for (Employee e : es.getEmployeeByBranch(getBranchId())) {
                    // Do some "heavy work"                
                    getUI().getSession().getLockInstance();
                    try {
                        Thread.sleep(50);
                        if (e.getEndDate() == null || CommonUtilities.convertStringToInteger(
                                year.getValue().toString()) <= CommonUtilities.getYearFromDate(e.getEndDate())) {
                            grid.addRow(e.getTinNo(),
                                    (e.getLastname() + ", " + e.getFirstname() + " " + e.getMiddlename()),
                                    e.getEmployeeId(), e.getTotalDependent(),
                                    CommonUtilities.convertDateFormat(e.getEntryDate().toString()),
                                    (e.getEndDate() == null ? ""
                                            : CommonUtilities.convertDateFormat(e.getEndDate().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 1,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 2,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 3,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 4,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 5,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 6,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 7,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 8,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 9,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 10,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 11,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getGrossPayPerMonth(e.getEmployeeId(), 12,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    null,
                                    als.getSssPerMonth(e.getEmployeeId(), 1,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getSssPerMonth(e.getEmployeeId(), 2,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getSssPerMonth(e.getEmployeeId(), 3,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getSssPerMonth(e.getEmployeeId(), 4,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getSssPerMonth(e.getEmployeeId(), 5,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getSssPerMonth(e.getEmployeeId(), 6,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getSssPerMonth(e.getEmployeeId(), 7,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getSssPerMonth(e.getEmployeeId(), 8,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getSssPerMonth(e.getEmployeeId(), 9,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getSssPerMonth(e.getEmployeeId(), 10,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getSssPerMonth(e.getEmployeeId(), 11,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getSssPerMonth(e.getEmployeeId(), 12,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    null,
                                    als.getPhicPerMonth(e.getEmployeeId(), 1,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getPhicPerMonth(e.getEmployeeId(), 2,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getPhicPerMonth(e.getEmployeeId(), 3,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getPhicPerMonth(e.getEmployeeId(), 4,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getPhicPerMonth(e.getEmployeeId(), 5,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getPhicPerMonth(e.getEmployeeId(), 6,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getPhicPerMonth(e.getEmployeeId(), 7,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getPhicPerMonth(e.getEmployeeId(), 8,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getPhicPerMonth(e.getEmployeeId(), 9,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getPhicPerMonth(e.getEmployeeId(), 10,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getPhicPerMonth(e.getEmployeeId(), 11,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getPhicPerMonth(e.getEmployeeId(), 12,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    null,
                                    als.getHdmfPerMonth(e.getEmployeeId(), 1,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getHdmfPerMonth(e.getEmployeeId(), 2,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getHdmfPerMonth(e.getEmployeeId(), 3,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getHdmfPerMonth(e.getEmployeeId(), 4,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getHdmfPerMonth(e.getEmployeeId(), 5,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getHdmfPerMonth(e.getEmployeeId(), 6,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getHdmfPerMonth(e.getEmployeeId(), 7,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getHdmfPerMonth(e.getEmployeeId(), 8,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getHdmfPerMonth(e.getEmployeeId(), 9,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getHdmfPerMonth(e.getEmployeeId(), 10,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getHdmfPerMonth(e.getEmployeeId(), 11,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    als.getHdmfPerMonth(e.getEmployeeId(), 12,
                                            CommonUtilities.convertStringToInteger(year.getValue().toString())),
                                    null);
                        }

                        getUI().access(new Runnable() {

                            @Override
                            public void run() {
                                if (current < dataSize) {
                                    current += 1;
                                    progress.setValue(new Float(current / dataSize));
                                    status.setValue("" + ((int) ((current / dataSize) * 100)) + "%");

                                    Item item = grid.getContainerDataSource()
                                            .getItem(grid.getContainerDataSource().lastItemId());
                                    calculateGrandTotal(item);
                                } else {
                                    status.setValue("Finished... 100%");
                                }
                            }
                        });
                    } catch (InterruptedException ex) {
                    }
                }
            }
        }.start();
        // Enable polling and set frequency to 0.5 seconds
        UI.getCurrent().setPollInterval(500);
    }

    Grid populateGrid() {
        grid.getContainerDataSource().removeAllItems();
        grid.getListeners(ItemClickEvent.class).stream().forEach((listener) -> {
            grid.removeListener(ItemClickEvent.class, listener);
        });

        grid.addItemClickListener((ItemClickEvent event) -> {
            Item item = grid.getContainerDataSource().getItem(event.getItemId());

            System.out.println("employeeId: " + item.getItemProperty("employeeId").getValue());
        });

        return grid;
    }

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

    int getBranchId() {
        return branchId;
    }

    ComboBox selectYear() {
        year = new ComboBox();
        year.setWidth("100%");
        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;
    }

    Button alphaListButton() {
        Button button = new Button("ALPHA LIST");
        button.setWidth("100%");
        button.setIcon(FontAwesome.SPINNER);
        button.addStyleName(ValoTheme.BUTTON_PRIMARY);
        button.addStyleName(ValoTheme.BUTTON_SMALL);
        button.addClickListener((Button.ClickEvent event) -> {
            status.setValue(" Loading...");
            current = 0.0;
            dataSize = es.getEmployeeByBranch(getBranchId()).size();
            populateDataGrid();
        });

        return button;
    }

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

        item.getItemProperty("sss grand total").setValue(CommonUtilities
                .convertStringToDouble(item.getItemProperty("sss january").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("sss february").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("sss march").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("sss april").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("sss may").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("sss june").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("sss july").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("sss august").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("sss september").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("sss october").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("sss november").getValue().toString())
                + CommonUtilities
                        .convertStringToDouble(item.getItemProperty("sss december").getValue().toString()));

        item.getItemProperty("phic grand total").setValue(CommonUtilities
                .convertStringToDouble(item.getItemProperty("phic january").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("phic february").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("phic march").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("phic april").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("phic may").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("phic june").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("phic july").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("phic august").getValue().toString())
                + CommonUtilities
                        .convertStringToDouble(item.getItemProperty("phic september").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("phic october").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("phic november").getValue().toString())
                + CommonUtilities
                        .convertStringToDouble(item.getItemProperty("phic december").getValue().toString()));

        item.getItemProperty("hdmf grand total").setValue(CommonUtilities
                .convertStringToDouble(item.getItemProperty("hdmf january").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("hdmf february").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("hdmf march").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("hdmf april").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("hdmf may").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("hdmf june").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("hdmf july").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("hdmf august").getValue().toString())
                + CommonUtilities
                        .convertStringToDouble(item.getItemProperty("hdmf september").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("hdmf october").getValue().toString())
                + CommonUtilities.convertStringToDouble(item.getItemProperty("hdmf november").getValue().toString())
                + CommonUtilities
                        .convertStringToDouble(item.getItemProperty("hdmf december").getValue().toString()));
    }
}