com.firstbanknigeria.ofsaaenhancers.UI.AdjustmentUI.java Source code

Java tutorial

Introduction

Here is the source code for com.firstbanknigeria.ofsaaenhancers.UI.AdjustmentUI.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.firstbanknigeria.ofsaaenhancers.UI;

import com.firstbanknigeria.ofsaaenhancers.common.ObjectStore;
import com.firstbanknigeria.ofsaaenhancers.dbo.OfsaAdjustmentsTable;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.Upload;
import com.vaadin.ui.VerticalLayout;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.QueryResponse;
import org.apache.cayenne.query.ProcedureQuery;
import org.apache.cayenne.query.SQLTemplate;

/**
 *
 * @author mex
 */
public class AdjustmentUI extends Panel {

    private ObjectContext objectContext;
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");

    public AdjustmentUI() {
        super("Load Adjustments");
        this.setWidth("1000px");
        this.setHeight("250px");

        objectContext = ObjectStore.getObjectContext(VaadinSession.getCurrent().getSession().getId());

        final VerticalLayout layout = new VerticalLayout();
        layout.setDefaultComponentAlignment(Alignment.MIDDLE_CENTER);
        layout.setSpacing(true);

        AdjustmentFileProcessor adjFileProcessor = new AdjustmentFileProcessor();
        adjFileProcessor.setAdjustmentUI(this);

        Upload uploadComponent = new Upload("", adjFileProcessor);
        uploadComponent.addSucceededListener(adjFileProcessor);
        uploadComponent.setButtonCaption("Upload Adjustments");
        uploadComponent.setImmediate(true);

        Button runAdjustmenstButton = new Button("Run Adjustments");
        runAdjustmenstButton.addClickListener(new Button.ClickListener() {

            public void buttonClick(Button.ClickEvent e) {
                ProcedureQuery query = new ProcedureQuery("RUN_ADJUSTMENTS");
                QueryResponse response = objectContext.performGenericQuery(query);
            }
        });

        layout.addComponents(uploadComponent, runAdjustmenstButton);
        layout.setMargin(true);
        layout.setSpacing(true);

        setContent(layout);
    }

    public void setExcelContents(List<List<String>> excelContents) {
        SQLTemplate clearTable = new SQLTemplate(OfsaAdjustmentsTable.class,
                "truncate table OFSA_ADJUSTMENTS_TABLE");
        QueryResponse performGenericQuery;
        performGenericQuery = objectContext.performGenericQuery(clearTable);
        int i = 0;
        for (List<String> row : excelContents) {
            try {
                if (i == 0) {
                    i += 1;
                    continue;
                }
                i += 1;
                OfsaAdjustmentsTable ofsaRec = new OfsaAdjustmentsTable();

                ofsaRec.setAsOfDate(sdf.parse(row.get(0)));
                ofsaRec.setAccountNumber(row.get(1));
                ofsaRec.setElementCoaId(row.get(2));
                ofsaRec.setCurBookBal(checkNull(row.get(3)));
                ofsaRec.setAvgBookBal(checkNull(row.get(4)));
                ofsaRec.setInterestIncExp(checkNull(row.get(5)));
                ofsaRec.setLobId(row.get(6));
                ofsaRec.setRlmId(row.get(7));
                ofsaRec.setIsoCurrencyCd(row.get(8));
                try {
                    ofsaRec.setProductId(row.get(9) != null ? new Integer(row.get(9)) : null);
                    ofsaRec.setTransferRate(checkNull(row.get(10)));
                    ofsaRec.setFloatChrg(checkNull(row.get(11)));
                } catch (IndexOutOfBoundsException ex) {
                    //                    Logger.getLogger(AdjustmentUI.class.getName()).log(Level.SEVERE, null, ex);
                }
                //System.out.println(i);
                objectContext.registerNewObject(ofsaRec);

            } catch (ParseException ex) {
                Logger.getLogger(AdjustmentUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        //displayArea.setValue(displayContent.toString());
        objectContext.commitChanges();
        System.out.println("DONE");
    }

    private BigDecimal checkNull(String numberString) {
        if (numberString == null) {
            return null;
        } else {
            try {
                BigDecimal bigDecimal = new BigDecimal(numberString.replace(",", ""));
                return bigDecimal;
            } catch (NumberFormatException ex) {
                System.out.println("NumberFOrmat Ex:  " + numberString);
                return null;
            }

        }
    }

}