Tools.PreProcessing.java Source code

Java tutorial

Introduction

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

import Files.XLSX2BatchHandler;
import PDF.PDFNumberingPartial;
import PDF.SwingSimpleController;
import PDF.SwingViewSimpleBuilder;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import Utilities.GlobalVar;
import Utilities.MyDate;

/**
 *
 * @author bob
 */
public class PreProcessing {
    private JRadioButton voidButton;
    private JRadioButton selectButton;
    private JRadioButton skipButton;
    private ButtonGroup statusButtonGroup;
    private JLabel seqText;
    private Boolean[][] statusArray;
    private JButton submitPDFButton;
    private List<Boolean> SELECTED;

    public PreProcessing(String XLSX_FILE_NAME, String PDF_FILE_NAME) {
        initComponent(PDF_FILE_NAME, XLSX_FILE_NAME);

        SwingSimpleController controller = new SwingSimpleController(statusButtonGroup, seqText, statusArray);
        controller.openDocument(PDF_FILE_NAME); // show the component 
        int pageNum = controller.getDocument().getNumberOfPages();

        System.out.println(controller.getDocument().getNumberOfPages());

        SwingViewSimpleBuilder factory = new SwingViewSimpleBuilder(controller, statusButtonGroup, seqText,
                submitPDFButton, statusArray);

        JPanel viewerComponentPanel = factory.buildViewerPanel();
        // add interactive mouse link annotation support via callback 
        //        controller.getDocumentViewController().setAnnotationCallback(
        //                new org.icepdf.ri.common.MyAnnotationCallback(controller.getDocumentViewController())); 

        JFrame applicationFrame = new JFrame();
        applicationFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        applicationFrame.getContentPane().add(viewerComponentPanel); // Now that the GUI is all in place, we can try opening a PDF 
        //        controller.openDocument(filePath); // show the component 
        //        System.out.println(controller.getDocument().getNumberOfPages());
        applicationFrame.pack();
        applicationFrame.setVisible(true);

    }

    private void initComponent(final String pdfFileName, final String xlsxFileName) {
        voidButton = new JRadioButton();
        selectButton = new JRadioButton();
        skipButton = new JRadioButton();

        voidButton.setText("Delete");
        voidButton.setEnabled(false);
        skipButton.setText("Skip");

        selectButton.setText("Select");
        //selectButton.setEnabled(false);

        statusButtonGroup = new ButtonGroup();
        statusButtonGroup.add(voidButton);
        statusButtonGroup.add(skipButton);
        statusButtonGroup.add(selectButton);

        seqText = new JLabel();
        seqText.setText("----");
        seqText.setSize(new Dimension(500, 20));
        //Icon ic = new ImageIcon("heart.gif");
        // seqText.setIcon(new ImageIcon("heart.gif"));
        seqText.setIcon(new ImageIcon(getClass().getResource("heart.gif")));
        statusArray = new Boolean[GlobalVar.NUM_BUTTON][GlobalVar.MAX_NUM_PAGES];

        for (int i = 0; i < GlobalVar.NUM_BUTTON; i++) {
            for (int j = 0; j < GlobalVar.MAX_NUM_PAGES; j++) {
                statusArray[i][j] = false;
            }
        }
        if (SELECTED == null) {
            for (int j = 0; j < GlobalVar.MAX_NUM_PAGES; j++) {
                statusArray[GlobalVar.SELECT_BUTTON_INDEX][j] = true;
            }
        } else {
            for (int j = 0; j < GlobalVar.MAX_NUM_PAGES; j++) {
                if (j < SELECTED.size()) {
                    //System.out.println(SELECTED.get(j));
                    statusArray[GlobalVar.SELECT_BUTTON_INDEX][j] = SELECTED.get(j);
                } else {
                    statusArray[GlobalVar.SELECT_BUTTON_INDEX][j] = true;
                }
            }
        }

        submitPDFButton = new JButton();
        submitPDFButton.setText("Generate PDF");
        //Icon ic4 = new ImageIcon("signup.gif");
        // this.setIconImage(new ImageIcon(getClass().getResource(GlobalVar.ICON_NAME)).getImage());
        //submitPDFButton.setIcon(new ImageIcon("signup.gif"));   
        submitPDFButton.setIcon(new ImageIcon(getClass().getResource("signup.gif")));
        submitPDFButton.setSize(500, 200);
        //submitPDFButton.setBorder(BorderFactory.createLineBorder(Color.black, 5, true));
        submitPDFButton.setBorder(BorderFactory.createRaisedBevelBorder());
        submitPDFButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {

                try {
                    // System.out.println("PDF is pressed");
                    //String cycle = JOptionPane.showInputDialog(null, "Please enter a cycle number");
                    // System.out.println("The cycle number is " + cycle);

                    generatePDFFile(pdfFileName, xlsxFileName, statusArray);
                    JOptionPane.showMessageDialog(null, "pdf file is successfully pre-processed.");
                } catch (IOException ex) {
                    Logger.getLogger(PDFNumberingPartial.class.getName()).log(Level.SEVERE, null, ex);
                } catch (COSVisitorException ex) {
                    Logger.getLogger(PDFNumberingPartial.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });
    }

    // given the original pdf file, date stamp every page, sequence selected pages and
    // output two pdf files, one for audit, the other for reject 
    private void generatePDFFile(String pdfFileName, String xlsxFileName, Boolean[][] statusArray)
            throws IOException, COSVisitorException {

        List<String> textList = readXlsxFile(xlsxFileName);
        // System.out.println("text list: " + textList);
        //Iterator<String> it = textList.iterator();
        PDDocument pdf = PDDocument.load(pdfFileName);
        List pages = pdf.getDocumentCatalog().getAllPages();
        Iterator<PDPage> iter = pages.iterator();

        int pageNum = 0; // 0 based
        int index = 0;

        while (iter.hasNext()) {
            PDPage page = iter.next();
            // PDPage pageBlank = new PDPage();            
            PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);
            if (statusArray[GlobalVar.SELECT_BUTTON_INDEX][pageNum]) {
                if (index < textList.size()) {
                    String text = textList.get(index); // zero based
                    //System.out.println(text);
                    pageWrite(stream, text, index);
                } else {
                    JOptionPane.showMessageDialog(null,
                            "Preprocessing is inaccurate. XLSX list is shorter than the pdf file.");
                    break;
                }
                index++;
            }
            stream.close();
            pageNum++;
        }
        if (index > textList.size()) {
            JOptionPane.showMessageDialog(null,
                    "Preproc might be inaccurate. XLSX list is longer than the pdf file.");
        }
        // out put two pdf files: one is template for printer print hardcopies, the other is digital copy
        String suffix = "_" + "_pre_processed.pdf";
        pdfFileName = pdfFileName.replace(".pdf", suffix);
        pdf.save(pdfFileName);
        pdf.close();

    }

    // write a sequence number on a specific page
    private void pageWrite(PDPageContentStream stream, String text, int index) throws IOException {
        stream.beginText();
        stream.setFont(PDType1Font.HELVETICA, GlobalVar.PRE_PROC_FONT_SIZE);
        stream.moveTextPositionByAmount(GlobalVar.PRE_PROC_TEXT_X_POSITION, GlobalVar.PRE_PROC_TEXT_Y_POSITION);
        stream.drawString(text);

        stream.setFont(PDType1Font.HELVETICA, GlobalVar.SEQ_NUM_FONT_SIZE);
        stream.moveTextPositionByAmount(GlobalVar.SEQ_NUM_TEXT_X_POSITION, GlobalVar.SEQ_NUM_TEXT_Y_POSITION);
        stream.setTextRotation(3.14 / 2, GlobalVar.SEQ_NUM_TEXT_X_POSITION, GlobalVar.SEQ_NUM_TEXT_Y_POSITION); // rotate text 90 degree at x = 600, y = 400
        stream.drawString("//" + GlobalVar.globalCountGenerator5Digit(index + 1));
        //sequenceNum++;
        stream.endText();
    }

    // read the ssn and ctrl number on the xlsx, and return the list.
    private List<String> readXlsxFile(String xlsxFileName) throws IOException {
        File myFile = new File(xlsxFileName);
        List<String> list = new ArrayList<>(); //store ctrlNum + GlobalVar.PRE_PROC_KEY_SYMBOL + fullSSN that printed on the pdf file
        try {
            FileInputStream fis = new FileInputStream(myFile);
            XSSFWorkbook myWorkBook = new XSSFWorkbook(fis);

            //Return first sheet from the XLSX workbook
            XSSFSheet mySheet = myWorkBook.getSheetAt(0);
            List<String> errorList = new ArrayList<>();
            Map<String, Map<String, List<String>>> leaves = new HashMap<String, Map<String, List<String>>>();
            //Get iterator to all the rows in current sheet
            Iterator<Row> rowIterator = mySheet.iterator();
            int lineCount = 1;
            // Traversing over each row of XLSX file
            if (rowIterator.hasNext()) {

                Row headerRow = rowIterator.next(); //skip the header row
                Iterator<Cell> it = headerRow.cellIterator();
                int numCell = 0;
                List<String> keyList = new ArrayList<String>(); //keep track info of each column

                while (it.hasNext()) {
                    keyList.add(it.next().getStringCellValue()); // add the title in xlsx to keyList                  
                    numCell++;
                }

                if (numCell == GlobalVar.LEAVE_TITLES_V2.length) {
                    System.out.println("XLSX2BatchHandler.java: V2, UCFR is not loaded.");
                    int globalCount = 1;
                    while (rowIterator.hasNext()) {
                        //int cellCount = 0;             
                        lineCount++;

                        Row row = rowIterator.next();
                        Cell cell = row.getCell(GlobalVar.CTRL_NUM_CELL_INDEX_V2);
                        String ctrlNumString = cell.getStringCellValue();
                        if (!ctrlNumString.equalsIgnoreCase("")) { //skip empty lines
                            Iterator<Cell> cellIterator = row.cellIterator();
                            Map<String, String> rowContainer = new HashMap<>(); //store info of each row                                       
                            rowContainerBuilder(rowContainer, keyList, cellIterator); // update rowContainer

                            //    public static final String[] SIGNED_LEAVE_TITLES = {"Ctrl Number", 
                            //            "SSN", "Last Name", "Sign-in Date", "Sign-out Date", "Leave Area",
                            //        "Leave Type", "Num of Days", "First Five","Projected Sign-in Date", "Projected Sign-out Date"};
                            //      make sure the key mataches the name in the header                        
                            String ctrlNum = GlobalVar
                                    .readCtrlNum(rowContainer.get(keyList.get(GlobalVar.CTRL_NUM_CELL_INDEX_V2)));
                            //String ctrlNum = GlobalVar.readCtrlNum(rowContainer.get(keyList.get(GlobalVar.CTRL_NUM_CELL_INDEX_V2))); 
                            if (ctrlNum == null) {
                                JOptionPane.showMessageDialog(null,
                                        "Line " + lineCount + ": Invalid ctrl number received.");
                            }
                            // String lastName = rowContainer.get(keyList.get(GlobalVar.LAST_NAME_CELL_INDEX_V2));
                            String fullSSN = rowContainer.get(keyList.get(GlobalVar.FULL_SSN_CELL_INDEX_V2)); // full ssn                                
                            list.add(ctrlNum + GlobalVar.PRE_PROC_KEY_SYMBOL + fullSSN);

                        }
                    }
                    fis.close();

                } else {

                    JOptionPane.showMessageDialog(null, "XLSX file doesn't contain correct format!");
                }
            } else {
                JOptionPane.showMessageDialog(null, "XLSX file is empty!");
                System.out.println("The xlsx file is empty!");
            }
            // finds the work book in stance for XLSX file

        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "XLSX2BatchHandler.java: Xlsx file not found!");
            Logger.getLogger(XLSX2BatchHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public void rowContainerBuilder(Map<String, String> rowContainer, List<String> keyList,
            Iterator<Cell> cellIterator) {
        int keys = 0;
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            if (keys == GlobalVar.CTRL_NUM_CELL_INDEX_V2) {
                String value = cell.getStringCellValue();
                rowContainer.put(keyList.get(keys), value);
            } else if (keys == GlobalVar.FULL_SSN_CELL_INDEX_V2) {
                int type = cell.getCellType();
                String fullSSN = null;
                if (type == HSSFCell.CELL_TYPE_STRING) {
                    fullSSN = cell.getStringCellValue(); //string type ssn
                    fullSSN = fullSSN.replaceAll("-", "");
                } else {
                    DataFormatter df = new DataFormatter();
                    //SimpleDateFormat fmt = new SimpleDateFormat("yyMMdd");   
                    fullSSN = df.formatCellValue(cell); //return ***-**-****
                }
                fullSSN = GlobalVar.fullSSNgenerator(fullSSN);
                rowContainer.put(keyList.get(keys), fullSSN);
            } else if (keys == GlobalVar.SIGN_OUT_DATE_CELL_INDEX_V2
                    || keys == GlobalVar.SIGN_IN_DATE_CELL_INDEX_V2) {
                DataFormatter df = new DataFormatter();
                String dateString = df.formatCellValue(cell);
                rowContainer.put(keyList.get(keys), dateString);
            } else if (keys == GlobalVar.LEAVE_AREA_CELL_INDEX_V2) {
                int type = cell.getCellType();
                String value = null;
                if (type == HSSFCell.CELL_TYPE_STRING) {
                    value = GlobalVar.getDMOLeaveArea(cell.getStringCellValue());
                } else if (type == HSSFCell.CELL_TYPE_NUMERIC) {
                    value = GlobalVar.getDMOLeaveArea(cell.getNumericCellValue()); //read a double and return string
                }
                rowContainer.put(keyList.get(keys), value);
            } else if (keys == GlobalVar.LEAVE_TYPE_CELL_INDEX_V2) {
                String value = GlobalVar.getDMOLeaveType(cell.getStringCellValue());
                rowContainer.put(keyList.get(keys), value);
            }
            keys++;
        }
    }
}