Package pdfrobot.engine.robot

Source Code of pdfrobot.engine.robot.Robot

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfrobot.engine.robot;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import pdfrobot.engine.parser.PdfFileRule;
import pdfrobot.engine.parser.PdfFolderParser;
import pdfrobot.engine.parser.SerializedFileRules;

/**
* A pdf parsing robot. This one actually does the work (representing a working thread). Robots
* are controlled by a robbot manager. Robots are for all practical purposes worker threads.
* @author hedsttor
*/
public class Robot implements Runnable {
    //private File[] pdfFiles;
    private String fileSep;
    private int index;
    private RobotCallBack callBack;

    /**
     * Constructor
     * @param index The thread index of this robot (given by the robot-manager).
     * @param callBack The interface used by the robot to notify progress.
     */
    public Robot(int index, RobotCallBack callBack) {
        this.index = index;
        this.callBack = callBack;
    }

    /*
     * Class initialization.
     */
    private void init() {
        fileSep = System.getProperty("file.separator");
    }

    /**
     * Starts execution of this robot.
     * @throws IOException
     */
    public void start() throws IOException {
        init();

        List<PdfFileRule> rules = new SerializedFileRules().getRules();
        PdfFolderParser pdfFolderParser = new PdfFolderParser(index);

        for (PdfFileRule pdfFileRule : rules) {           
            File f;
            while( (f = pdfFolderParser.parseFile(pdfFileRule)) != null) {
                Logger.getLogger(Robot.class.getName()).log(Level.INFO, index+" - Moving file "+f.getName()+" to "+pdfFileRule.getDestinationSubFolder().getAbsolutePath());
                moveFile(f, pdfFileRule.getDestinationSubFolder());               
            }                       
        }
        callBack.done(index);
    }

    /*
     * Runnable method - Calls start().
     */
    public void run() {
        try {
            start();
        } catch (IOException ex) {
            Logger.getLogger(Robot.class.getName()).log(Level.SEVERE, Integer.toString(index), ex);
        }

    }

    /**
     * Helper method... Moves a collection of pdf files.
     * @param parsedFiles A collection containing the parsed files.
     * @param  destinationSubFolder The destination folder.
     */
    private void moveFiles(File[] parsedFiles, File destinationSubFolder) throws FileNotFoundException, IOException {
       
        for (int i = 0; i < parsedFiles.length; i++) {
            moveFile(parsedFiles[i], destinationSubFolder);
        }
    }

    /**
     * Moves a single file
     * @param fileToMove The file to move.
     * @param destinationFolder The destination
     */
    private void moveFile(File fileToMove, File destinationFolder) throws IOException {
        File movedFile = new File(destinationFolder.getAbsolutePath() + fileSep + fileToMove.getName());
        if(movedFile.exists()) {
            Logger.getLogger(Robot.class.getName()).log(Level.WARNING, "Unable to move file "+fileToMove.getAbsolutePath()+
                    " to "+
                    movedFile.getAbsolutePath()+
                    " destination file already exists. Putting file in Quarentine...");
            File quarentine = new File("Quarentined"+fileSep+fileToMove.getName());
            move(fileToMove, quarentine);
            if(fileToMove.exists())
                Filetracker.getInstance().addFileForDeletion(fileToMove);
            return;
        }
       
        move(fileToMove, movedFile);                     
        boolean deleted = fileToMove.delete();
        if(!deleted) {
            Logger.getLogger(Robot.class.getName()).log(Level.WARNING, "Unable to delete file "+fileToMove.getAbsolutePath()+" queing file for later deletion.");
            Filetracker.getInstance().addFileForDeletion(fileToMove);
        }
           
    }
   
    private void move(File origin, File destination) throws FileNotFoundException, IOException {
        FileInputStream fileInputStream = new FileInputStream(origin);
        FileOutputStream fileOutputStream = new FileOutputStream(destination);
        byte[] buffer = new byte[1024];
        int readBytes;

        while ((readBytes = fileInputStream.read(buffer)) > 0) {
            fileOutputStream.write(buffer, 0, readBytes);
        }

        fileInputStream.close();
        fileOutputStream.close();
    }
}
TOP

Related Classes of pdfrobot.engine.robot.Robot

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.