1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| package Dao.JDBC;
import Constants.Constants;
import Dao.IReferencesFilesDAO;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JdbcReferencesFilesDAO implements IReferencesFilesDAO {
@Override
public void parseDirectory(String path, Connection conn) {
File actual = new File(path);
String actualPath;
String actualName;
String hash;
String oldHash;
Constants.counter = 0;
try {
PreparedStatement pstmtInsertInReferencesFilesTableDB = conn.prepareStatement("INSERT INTO ReferencesFiles (Name, URL, Signature, Checked) VALUES (?,?,?,?)");
PreparedStatement pstmtGetOldHashIfExist = conn.prepareStatement("SELECT Signature FROM ReferencesFiles WHERE URL=?");
PreparedStatement pstmtUpdateCheckedColumn = conn.prepareStatement("UPDATE ReferencesFiles SET Checked=1 WHERE URL=?");
PreparedStatement pstmtUpdateSignatureAndCheckedColumn = conn.prepareStatement("UPDATE ReferencesFiles SET Signature=?, Checked=1 WHERE URL=?");
try {
for (File f : actual.listFiles()) {
actualPath = f.getAbsolutePath();
actualName = f.getName();
if (!f.getName().equals(".snapshot")) {
if (f.isDirectory()) {
parseDirectory(actualPath, conn);
} else {
Constants.counter++;
hash = Utils.HashUtils.hashFile(actualPath);
oldHash = getOldHashIfExist(actualPath, conn, pstmtGetOldHashIfExist);
if (oldHash != null) {
if (oldHash.equals(hash)) {
updateCheckedColumn(actualPath, conn, pstmtUpdateCheckedColumn);
} else {
Utils.ReportUtils.WriteInFile("Reference File MODIFIED : " + actualPath, Constants.REFERENCES_REPORTING_PATH);
updateSignatureAndCheckedColumn(hash, actualPath, conn, pstmtUpdateSignatureAndCheckedColumn);
}
} else {
Utils.ReportUtils.WriteInFile("Reference File ADDED : " + actualPath, Constants.REFERENCES_REPORTING_PATH);
insertInReferencesFilesTableDB(actualName, actualPath, hash, conn, pstmtInsertInReferencesFilesTableDB);
}
System.out.print(".");
if (((Constants.counter) % 100) == 0) {
executeBatch(pstmtGetOldHashIfExist);
executeBatch(pstmtInsertInReferencesFilesTableDB);
executeBatch(pstmtUpdateCheckedColumn);
executeBatch(pstmtUpdateSignatureAndCheckedColumn);
System.out.print("\n" + Constants.counter + "files found");
}
}
}
}
executeBatch(pstmtGetOldHashIfExist);
executeBatch(pstmtInsertInReferencesFilesTableDB);
executeBatch(pstmtUpdateCheckedColumn);
executeBatch(pstmtUpdateSignatureAndCheckedColumn);
System.out.print("\n" + Constants.counter + "files found");
} catch (Exception e) {
System.out.println(" Problem on " + path);
Utils.ReportUtils.WriteInFile("Problem on " + path, Constants.LOGS_PATH);
} finally {
try {
pstmtInsertInReferencesFilesTableDB.close();
pstmtGetOldHashIfExist.close();
pstmtUpdateCheckedColumn.close();
pstmtUpdateSignatureAndCheckedColumn.close();
} catch (SQLException ex) {
System.out.println("ERROR");
}
}
} catch (Exception e) {
throw new RuntimeException();
}
}
@Override
public void insertInReferencesFilesTableDB(String name, String url, String hash, Connection conn, PreparedStatement pstmt) {
try {
pstmt.clearParameters();
pstmt.setString(1, name);
pstmt.setString(2, url);
pstmt.setString(3, hash);
pstmt.setInt(4, 1);
pstmt.addBatch();
} catch (SQLException e) {
System.out.println(" Impossible to add " + name + " in ReferencesFiles Table !");
Utils.ReportUtils.WriteInFile("Problem adding " + url + " file", Constants.LOGS_PATH);
}
}
@Override
public String getOldHashIfExist(String path, Connection conn, PreparedStatement pstmt) {
ResultSet rs = null;
try {
pstmt.clearParameters();
pstmt.setString(1, path);
rs = pstmt.executeQuery();
if (rs.first()) {
String hash = rs.getString(1);
return hash;
} else {
return null;
}
} catch (Exception e) {
System.out.println("Problem checking if file of reference exist in DB (" + path + ")");
Utils.ReportUtils.WriteInFile("Problem checking if file of reference exist in DB (" + path + ")", Constants.LOGS_PATH);
return null;
} finally {
try {
rs.close();
} catch (SQLException ex) {
}
}
}
@Override
public void updateCheckedColumn(String url, Connection conn, PreparedStatement pstmt) {
try {
pstmt.clearParameters();
pstmt.setString(1, url);
pstmt.addBatch();
} catch (SQLException e) {
System.out.println("Problem updating last checked date (" + url + ")");
}
}
@Override
public void updateSignatureAndCheckedColumn(String url, String newHash, Connection conn, PreparedStatement pstmt) {
try {
pstmt.clearParameters();
pstmt.setString(1, newHash);
pstmt.setString(2, url);
pstmt.addBatch();
} catch (SQLException e) {
System.out.println("Problem updating signature (" + url + ")");
}
}
@Override
public void checkDeletedFiles(Connection conn, PreparedStatement pstmt) {
ResultSet rs = null;
try {
pstmt.clearParameters();
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("!!! Reference DELETED !!! -> " + rs.getString(1));
deleteRecord(rs.getString(1), conn);
Utils.ReportUtils.WriteInFile("Reference File DELETED " + rs.getString(1), Constants.REFERENCES_REPORTING_PATH);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Problem checking deletedFiles");
Utils.ReportUtils.WriteInFile("Problem checking deleted files", Constants.LOGS_PATH);
} finally {
try {
rs.close();
} catch (SQLException ex) {
}
}
}
@Override
public void deleteRecord(String path, Connection conn) {
try (PreparedStatement pstmt = conn.prepareStatement("DELETE FROM ReferencesFiles WHERE URL = ?")) {
pstmt.setString(1, path);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println("Problem deleting " + path);
Utils.ReportUtils.WriteInFile("Problem deleting " + path, Constants.LOGS_PATH);
}
}
public void executeBatch (PreparedStatement pstmt) {
try {
pstmt.executeBatch();
} catch (SQLException ex) {
System.out.println("Impossible to execute batch");
}
}
} |
Partager