com.save.clients.AcknowledgementPromoForm.java Source code

Java tutorial

Introduction

Here is the source code for com.save.clients.AcknowledgementPromoForm.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.clients;

import com.save.client.promodeals.PDDataGridProperties;
import com.save.common.CommonComboBox;
import com.save.model.PromoDeals;
import com.save.service.ClientService;
import com.save.service.PromoDealService;
import com.save.serviceprovider.ClientServiceImpl;
import com.save.serviceprovider.PromoDealServiceImpl;
import com.save.utilities.CommonUtilities;
import com.vaadin.data.Property;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.DateField;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 *
 * @author jetdario
 */
public class AcknowledgementPromoForm extends Window implements Button.ClickListener {

    ClientService cs = new ClientServiceImpl();
    PromoDealService pds = new PromoDealServiceImpl();

    DateField entryDate;
    TextField promoItem;
    TextField promoAmount;
    TextField quantity;
    ComboBox productItems;
    DateField startDate;
    DateField endDate;
    TextArea remarks;
    ComboBox salesRep = CommonComboBox.employeeWithPosition("sales representative", "Sales Representative: ");
    ComboBox areaSales = CommonComboBox.employeeWithPosition("area sales manager", "Area Sales Manager: ");

    private int clientId = 0;
    private int promoId = 0;
    private String salesRepId;
    private String areaSalesId;

    public AcknowledgementPromoForm(int clientId) {
        this.clientId = clientId;

        setCaption("Acknowledment Form");
        setWidth("600px");
        setModal(true);
        center();

        setContent(acknowledgementFormContent());
        getContent().setHeightUndefined();
    }

    public AcknowledgementPromoForm(int clientId, int promoId) {
        this.clientId = clientId;
        this.promoId = promoId;

        setCaption("Acknowledment Form");
        setWidth("600px");
        setModal(true);
        center();

        setContent(acknowledgementFormContent());
        getContent().setHeightUndefined();
    }

    VerticalLayout acknowledgementFormContent() {
        VerticalLayout content = new VerticalLayout();
        content.setSizeFull();
        content.setMargin(true);
        content.setSpacing(true);

        GridLayout glayout = new GridLayout(4, 5);
        glayout.setWidth("100%");
        glayout.setSpacing(true);

        entryDate = new DateField("Date: ");
        entryDate.setWidth("100%");
        glayout.addComponent(entryDate, 0, 0);

        promoItem = new TextField("Promo Items: ");
        promoItem.setWidth("100%");
        glayout.addComponent(promoItem, 1, 0, 3, 0);

        promoAmount = new TextField("Amount: ");
        promoAmount.setWidth("100%");
        promoAmount.setStyleName("align-right");
        glayout.addComponent(promoAmount, 0, 1);

        quantity = new TextField("Quantity: ");
        quantity.setWidth("100%");
        quantity.setStyleName("align-right");
        glayout.addComponent(quantity, 1, 1);

        productItems = CommonComboBox.productItems();
        productItems.setWidth("100%");
        glayout.addComponent(productItems, 2, 1, 3, 1);

        startDate = new DateField("From: ");
        startDate.setWidth("100%");
        glayout.addComponent(startDate, 0, 2);

        endDate = new DateField("To: ");
        endDate.setWidth("100%");
        glayout.addComponent(endDate, 1, 2);

        salesRep.setWidth("100%");
        glayout.addComponent(salesRep, 2, 2, 3, 2);

        areaSales.setWidth("100%");
        glayout.addComponent(areaSales, 2, 3, 3, 3);

        remarks = new TextArea("Remarks: ");
        remarks.setWidth("100%");
        remarks.setRows(4);
        glayout.addComponent(remarks, 0, 3, 1, 4);

        Button submitBtn = new Button();
        submitBtn.setCaption("SAVE");
        submitBtn.setWidth("100%");
        submitBtn.addClickListener(this);
        glayout.addComponent(submitBtn, 2, 4, 3, 4);
        glayout.setComponentAlignment(submitBtn, Alignment.BOTTOM_CENTER);

        if (getPromoId() != 0) {
            PromoDeals pd = pds.getPromoDealById(getPromoId());
            submitBtn.setCaption("UPDATE");
            entryDate.setValue(pd.getEntryDate());
            startDate.setValue(pd.getStartDate());
            endDate.setValue(pd.getEndDate());
            promoItem.setValue(pd.getPromoItem());
            promoAmount.setValue(String.valueOf(pd.getPromoAmount()));
            quantity.setValue(String.valueOf(pd.getQuantity()));
            productItems.setValue(pd.getProductId());
            areaSales.setValue(pd.getAreaSalesId());
            salesRep.setValue(pd.getSalesRepId());
            remarks.setValue(pd.getRemarks());
        }

        content.addComponent(glayout);
        return content;
    }

    void clearForm() {
        promoItem.setValue("");
        promoAmount.setValue("0.00");
        quantity.setValue("0");
        remarks.setValue("");
    }

    void getNotification() {
        Notification.show("Fill-up all Fields!", Notification.Type.ERROR_MESSAGE);
    }

    @Override
    public void buttonClick(Button.ClickEvent event) {
        if (getClientId() == 0) {
            Notification.show("Select a Client!", Notification.Type.ERROR_MESSAGE);
            return;
        }
        if (entryDate.getValue() == null) {
            getNotification();
            return;
        }
        if (promoItem.getValue() == null || promoItem.getValue().trim().isEmpty()) {
            getNotification();
            return;
        }
        if (promoAmount.getValue() == null || promoAmount.getValue().trim().isEmpty()) {
            getNotification();
            return;
        } else {
            if (!CommonUtilities.checkInputIfDouble(promoAmount.getValue().trim())) {
                Notification.show("Enter a numerical format for Amount!", Notification.Type.ERROR_MESSAGE);
                return;
            }
        }
        if (quantity.getValue() == null || quantity.getValue().trim().isEmpty()) {
            getNotification();
            return;
        } else {
            if (!CommonUtilities.checkInputIfDouble(quantity.getValue().trim())) {
                Notification.show("Enter a numerical format for Quantity!", Notification.Type.ERROR_MESSAGE);
                return;
            }
        }
        if (productItems.getValue() == null) {
            getNotification();
            return;
        }
        if (startDate.getValue() == null) {
            getNotification();
            return;
        }
        if (endDate.getValue() == null) {
            getNotification();
            return;
        }
        //        if(areaSales.getValue() == null){ getNotification(); return; }
        if (remarks.getValue() == null || remarks.getValue().trim().isEmpty()) {
            getNotification();
            return;
        }

        PromoDeals pd = new PromoDeals();
        pd.setClientId(getClientId());
        pd.setPromoItem(promoItem.getValue().trim().toLowerCase());
        pd.setPromoAmount(CommonUtilities.convertStringToDouble(promoAmount.getValue().trim()));
        pd.setQuantity(CommonUtilities.convertStringToDouble(quantity.getValue().trim()));
        pd.setProductId((int) productItems.getValue());
        pd.setEntryDate(entryDate.getValue());
        pd.setStartDate(startDate.getValue());
        pd.setEndDate(endDate.getValue());
        pd.setRemarks(remarks.getValue().trim().toLowerCase());
        pd.setSalesRepId((int) ((salesRep.getValue() == null) ? 0 : salesRep.getValue()));
        pd.setAreaSalesId((int) ((areaSales.getValue() == null) ? 0 : areaSales.getValue()));

        PDDataGridProperties dataGrid = new PDDataGridProperties(getClientId());

        if (event.getButton().getCaption().equals("SAVE")) {
            boolean result = pds.create(pd);
            if (result) {
                this.close();
            }
        } else {
            pd.setPromoId(getPromoId());
            boolean result = pds.update(pd);
            if (result) {
                this.close();
            }
        }

    }

    int getClientId() {
        return clientId;
    }

    private int getPromoId() {
        return promoId;
    }

    private String getSalesRepId() {
        return salesRepId;
    }

    private String getAreaSalesId() {
        return areaSalesId;
    }

}