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
| public class JdbcReferencesFilesDAO implements IReferencesFilesDAO {
PreparedStatement pstmtInsertInReferencesFilesTableDB;
PreparedStatement pstmtGetOldHashIfExist;
PreparedStatement pstmtUpdateCheckedColumn ;
PreparedStatement pstmtUpdateSignatureAndCheckedColumn;
@Override
public void parse(String path, Connection conn){
try {
conn.setAutoCommit(false);
pstmtInsertInReferencesFilesTableDB = conn.prepareStatement("INSERT INTO ReferencesFiles (Name, URL, Signature, Checked) VALUES (?,?,?,?)");
pstmtGetOldHashIfExist = conn.prepareStatement("SELECT Signature FROM ReferencesFiles WHERE URL=?");
pstmtUpdateCheckedColumn = conn.prepareStatement("UPDATE ReferencesFiles SET Checked=1 WHERE URL=?");
pstmtUpdateSignatureAndCheckedColumn = conn.prepareStatement("UPDATE ReferencesFiles SET Signature=?, Checked=1 WHERE URL=?");
parseDirectory(path, conn);
executeBatch(pstmtGetOldHashIfExist);
executeBatch(pstmtInsertInReferencesFilesTableDB);
executeBatch(pstmtUpdateCheckedColumn);
executeBatch(pstmtUpdateSignatureAndCheckedColumn);
pstmtInsertInReferencesFilesTableDB.close();
pstmtGetOldHashIfExist.close();
pstmtUpdateCheckedColumn.close();
pstmtUpdateSignatureAndCheckedColumn.close();
System.out.print("\n" + Constants.counter + "files found");
conn.commit();
} catch (Exception e) {
System.out.println("Fatal error");
}
}
@Override
public void parseDirectory(String path, Connection conn) {
File actual = new File(path);
String actualPath;
String actualName;
String hash;
String oldHash;
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");
conn.commit();
}
}
}
}
} catch (Exception e) {
System.out.println(" Problem on " + path);
Utils.ReportUtils.WriteInFile("Problem on " + path, Constants.LOGS_PATH);
}
}
@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) {
try {
pstmt.clearParameters();
pstmt.setString(1, path);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.first()) {
String hash = rs.getString(1);
return hash;
} else {
return null;
}
}
} catch (Exception e) {
e.printStackTrace();
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;
}
}
@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 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