dataaccess.SourceDataReader.java Source code

Java tutorial

Introduction

Here is the source code for dataaccess.SourceDataReader.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 dataaccess;

import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author FUAN
 */
public class SourceDataReader {

    public static Map readRawData(File f) {
        Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();

        try {
            FileInputStream is = new FileInputStream(f);
            XSSFWorkbook wb = new XSSFWorkbook(is);
            XSSFSheet dataSheet = wb.getSheetAt(0);

            int rowID = 0;
            XSSFRow currRow = dataSheet.getRow(rowID);
            while (currRow != null) {
                if (rowID == 0) {
                    //
                    rowID = rowID + 1;
                    currRow = dataSheet.getRow(rowID);
                } else {
                    //??
                    String serialNumber = currRow.getCell(1).toString();
                    //???
                    String supplierName = currRow.getCell(2).toString();
                    //?
                    String price = currRow.getCell(4).toString();
                    //?
                    String time = currRow.getCell(6).toString();
                    ArrayList<String> info = new ArrayList<String>();
                    info.add(supplierName);
                    info.add(price);
                    info.add(time);
                    map.put(serialNumber, info);
                    rowID = rowID + 1;
                    currRow = dataSheet.getRow(rowID);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    //SRM?xlsx
    public static XSSFWorkbook readSRMData(File f) {
        try {
            FileInputStream fis = new FileInputStream(f);
            XSSFWorkbook wb = new XSSFWorkbook(fis);
            return wb;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //?
    //Key  PHIDValue price(TOTAL_VALUE) time(RECEICE_TIME)
    public static Map readPurchasingSupplierInformationSheet(XSSFWorkbook wb, int sheetNum) {
        Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();

        //?
        XSSFSheet dataSheet = wb.getSheetAt(sheetNum);

        int rowID = 0;
        XSSFRow currRow = dataSheet.getRow(rowID);
        while (currRow != null) {
            if (rowID == 0) {
                //
                rowID = rowID + 1;
                currRow = dataSheet.getRow(rowID);
            } else {

                //?ID
                String recordID;
                if (currRow.getCell(0) != null) {
                    recordID = currRow.getCell(0).toString();
                } else {
                    recordID = "null";
                }

                //????
                String companyName;
                if (currRow.getCell(1) != null) {
                    companyName = currRow.getCell(1).toString();
                } else {
                    companyName = null;
                }

                //??
                String companyID;
                if (currRow.getCell(2) != null) {
                    companyID = currRow.getCell(2).toString();
                } else {
                    companyID = "null";
                }
                //??                
                String supplierPackageNumber;
                if (currRow.getCell(3) != null) {
                    supplierPackageNumber = currRow.getCell(3).toString();
                } else {
                    supplierPackageNumber = "null";
                }
                //???
                String supplierPackageName;
                if (currRow.getCell(4) != null) {
                    supplierPackageName = currRow.getCell(4).toString();
                } else {
                    supplierPackageName = "null";
                }
                //?PurchaseHeadID
                String PHID;
                if (currRow.getCell(6) != null) {
                    PHID = currRow.getCell(6).toString();
                } else {
                    PHID = "null";
                }

                //?
                String receivedTime;
                if (currRow.getCell(7) != null) {
                    receivedTime = currRow.getCell(7).toString();
                } else {
                    receivedTime = "null";
                }

                ArrayList<String> info = new ArrayList<String>();
                info.add(companyName);
                info.add(companyID);
                info.add(supplierPackageNumber);
                info.add(supplierPackageName);
                info.add(PHID);
                info.add(receivedTime);
                map.put(recordID, info);
                rowID = rowID + 1;
                currRow = dataSheet.getRow(rowID);
            }
        }
        return map;
    }

    public static Map readBasicProcurementPlan(XSSFWorkbook wb, int sheetNum) {
        Map<String, Double> procurementPlan = new HashMap<String, Double>();
        XSSFSheet sheet = wb.getSheetAt(sheetNum);
        int rowID = 0;
        XSSFRow currRow = sheet.getRow(rowID);
        while (currRow != null) {
            if ((rowID == 0) || (rowID == 1)) {
                //?
                rowID = rowID + 1;
                currRow = sheet.getRow(rowID);
            } else {

                //?PHID
                String PHID = currRow.getCell(1).toString();
                //?
                Double price = Double.valueOf(currRow.getCell(7).toString());
                procurementPlan.put(PHID, price);
                rowID = rowID + 1;
                currRow = sheet.getRow(rowID);
            }

        }

        return procurementPlan;
    }

}