java-检查文件是否成功索引并更新表状态

vu8f3i0k  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(303)

假设我想索引一个文件。文件存储在 filequeue table。表结构如下所示:

UniqueID   FilePath                 Status     
 1          C:\Folder1\abc.pdf       Active
 2          C:\Folder1\def.pdf       Active
 3          C:\Folder1\efg.pdf       Error

有四种不同的状态:活动、处理、成功和错误
活动:当文件插入到表中等待索引过程时
处理:索引过程开始时,表状态更新为处理。
成功:索引过程完成后,表状态应更新为“正在处理”。
错误:如果由于某种原因,处理失败。
因为某种原因 .pdf 不存在。当我扫描这个表时,它将检索所有状态为active的filepath,并开始迭代它们中的每一个,然后执行index函数。在此过程中,它会将状态更新为“正在处理”,如果没有问题,则更新为“完成”。
但是,它将抛出一个错误 FileNotFoundException.pdf 这很好,因为文件不存在,但它仍会将状态更新为 Complete . 它应该更新为错误状态。
我在考虑使用if-else语句,它看起来是这样的:

public void doScan_DB() throws Exception {
  boolean fileprocessstatus=false;
        try {

            Statement statement = con.connect().createStatement();
            ResultSet rs = statement.executeQuery("select * from filequeue where Status='Active'");

            while (rs.next()) {
                // get the filepath of the PDF document
                String path1 = rs.getString(2);
                // while running the process, update status : Processing
                updateProcess_DB();

             // call the index function
                Indexing conn = new Indexing();
                conn.doScan(path1);

                fileProcessStatus =true;
                // After completing the process, update status: Complete
                if(fileProcessStatus=true){
                    updateComplete_DB();
                }else{

                    //call function to update status to error if index fails
                }

                }

        }catch(SQLException|IOException e){
            e.printStackTrace();

        }

我的doscan()方法:

public void doScan(String path) throws Exception{

     /*   File folder = new File("D:\\PDF1");
        File[] listOfFiles = folder.listFiles();

        for (File file : listOfFiles) {
            if (file.isFile()) {
                //  HashSet<String> uniqueWords = new HashSet<>();

                String path = "D:\\PDF1\\" + file.getName();*/
        ArrayList<String> list = new ArrayList<String>();
        try (PDDocument document = PDDocument.load(new File(path))) {

            if (!document.isEncrypted()) {

                PDFTextStripper tStripper = new PDFTextStripper();
                String pdfFileInText = tStripper.getText(document);
                String lines[] = pdfFileInText.split("\\r?\\n");
                for (String line : lines) {
                    String[] words = line.split(" ");
                    // words.replaceAll("([\\W]+$)|(^[\\W]+)", ""));

                    for (String word : words) {
                        // check if one or more special characters at end of string then remove OR
                        // check special characters in beginning of the string then remove
                        // uniqueWords.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
                        list.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
                        // uniqueWords.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
                    }

                }

            }
        } catch (IOException e) {
            System.err.println("Exception while trying to read pdf document - " + e);

        }

        String[] words1 =list.toArray(new String[list.size()]);
        // String[] words2 =uniqueWords.toArray(new String[uniqueWords.size()]);

        // MysqlAccessIndex connection = new MysqlAccessIndex();

        index(words1,path);

        System.out.println("Completed");

    }

}

updateerror\u db():

public void updateError_DB(){

        try{

            Statement statement = con.connect().createStatement();
            statement.execute("update filequeue SET STATUS ='Error' where STATUS ='Processing' ");

        }catch(Exception e){

            e.printStackTrace();

        }

    }

updatecomplete\u db():

public void updateComplete_DB() {

        try {

            Statement statement = con.connect().createStatement();
            statement.execute("update filequeue SET STATUS ='Complete' where STATUS ='Processing' ");

        } catch (Exception e) {
            e.printStackTrace();

        }

    }

但是,它并不能真正解决正确更新状态的问题。有没有办法达到我想要的?

yjghlzjz

yjghlzjz1#

这是基于我对你的情况的理解的解决方案。
doscan\u db()方法:

public void doScan_DB() throws Exception {
            boolean fileprocessstatus = false;
            try {

                    Statement statement = con.connect().createStatement();
                    ResultSet rs = statement.executeQuery("select * from filequeue where Status='Active'");

                    while (rs.next()) {
                            //Get the uniqueID of active filepath
                            String uniqueID = rs.getString(1);
                            // get the filepath of the PDF document
                            String path1 = rs.getString(2);

                            // while running the process, update status : Processing
                            updateProcess_DB(uniqueID);

                            // call the index function
                            Indexing conn = new Indexing();
                            if (conn.doScan(path1)) {
                                    updateComplete_DB(uniqueID);
                            } else {
                                    updateError_DB(uniqueID);
                            }

                    }

            } catch (SQLException | IOException e) {
                    e.printStackTrace();

            }
    }

doscan()方法:

public boolean doScan(String path) {

            /*
             * File folder = new File("D:\\PDF1"); File[] listOfFiles = folder.listFiles();
             * 
             * for (File file : listOfFiles) { if (file.isFile()) { // HashSet<String>
             * uniqueWords = new HashSet<>();
             * 
             * String path = "D:\\PDF1\\" + file.getName();
             */
            ArrayList<String> list = new ArrayList<String>();
            boolean isSuccess = true;
            try {
                    File f = new File(path);
                    if (!f.exists()) {
                            isSuccess = false;
                    } else {
                            PDDocument document = PDDocument.load(f);
                            if (!document.isEncrypted()) {

                                    PDFTextStripper tStripper = new PDFTextStripper();
                                    String pdfFileInText = tStripper.getText(document);
                                    String lines[] = pdfFileInText.split("\\r?\\n");
                                    for (String line : lines) {
                                            String[] words = line.split(" ");
                                            // words.replaceAll("([\\W]+$)|(^[\\W]+)", ""));

                                            for (String word : words) {
                                                    // check if one or more special characters at end of string then
                                                    // remove OR
                                                    // check special characters in beginning of the string then
                                                    // remove
                                                    // uniqueWords.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
                                                    list.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
                                                    // uniqueWords.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
                                            }

                                    }

                            }
                    }
                    String[] words1 = list.toArray(new String[list.size()]);
                    // String[] words2 =uniqueWords.toArray(new String[uniqueWords.size()]);

                    // MysqlAccessIndex connection = new MysqlAccessIndex();

                    index(words1, path);
            } catch (Exception e) {
                    System.err.println("Exception while trying to read pdf document - " + e);
                    isSuccess = false;
            }
            return isSuccess;
    }

现在是所有的update方法。在这里,我将使用jdbc preparedstatement代替jdbc语句。我假设你的唯一ID是int类型。
注意:根据您的环境进行必要的更改

public void updateComplete_DB(String uniqueID) {

    try {
         String sql="UPDATE filequeue SET STATUS ='Complete' WHERE STATUS ='Processing' AND UniqueID=?";
         PreparedStatement statement = con.connect().prepareStatement(sql);

         statement.setInt(1,Integer.parseInt(uniqueID));
         int rows = statement.executeUpdate();
         System.out.prrintln("No. of rows updated:"+rows)

    } catch (Exception e) {
        e.printStackTrace();

    }

}

public void updateProcess_DB(String uniqueID) {

    try {
         String sql="UPDATE filequeue SET STATUS ='Processing' WHERE UniqueID=?";
         PreparedStatement statement = con.connect().prepareStatement(sql);

         statement.setInt(1,Integer.parseInt(uniqueID));
         int rows = statement.executeUpdate();
         System.out.prrintln("No. of rows updated:"+rows)

    } catch (Exception e) {
        e.printStackTrace();

    }

}
public void updateError_DB(String uniqueID) {

    try {
         String sql="UPDATE filequeue SET STATUS ='Error' WHERE STATUS ='Processing' AND UniqueID=?";
         PreparedStatement statement = con.connect().prepareStatement(sql);

         statement.setInt(1,Integer.parseInt(uniqueID));
         int rows = statement.executeUpdate();
         System.out.prrintln("No. of rows updated:"+rows)

    } catch (Exception e) {
        e.printStackTrace();

    }

}

相关问题