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
| 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;
PreparedStatement pstmt = null;
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, pstmt);
if (oldHash != null) {
if (oldHash.equals(hash)) {
updateCheckedColumn(actualPath, conn, pstmt);
} else {
Utils.ReportUtils.WriteInFile("Reference File MODIFIED : " + actualPath, Constants.REFERENCES_REPORTING_PATH);
updateSignatureAndCheckedColumn(hash, actualPath, conn, pstmt);
}
} else {
Utils.ReportUtils.WriteInFile("Reference File ADDED : " + actualPath, Constants.REFERENCES_REPORTING_PATH);
insertInReferencesFilesTableDB(actualName, actualPath, hash, conn, pstmt);
}
System.out.print(".");
if (((Constants.counter) % 100) == 0) {
System.out.print("\n" + Constants.counter + "files found");
}
}
}
}
} catch (NullPointerException 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 = conn.prepareStatement("INSERT INTO ReferencesFiles (Name, URL, Signature, Checked) VALUES (?,?,?,?)");
pstmt.setString(1, name);
pstmt.setString(2, url);
pstmt.setString(3, hash);
pstmt.setInt(4, 1);
pstmt.executeUpdate();
} 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 = conn.prepareStatement("SELECT Signature FROM ReferencesFiles WHERE URL=?");
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 = conn.prepareStatement("UPDATE ReferencesFiles SET Checked=1 WHERE URL=?");
pstmt.setString(1, url);
pstmt.executeUpdate();
} 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 = conn.prepareStatement("UPDATE ReferencesFiles SET Signature=?, Checked=1 WHERE URL=?");
pstmt.setString(1, newHash);
pstmt.setString(2, url);
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
System.out.println("Problem updating signature (" + url + ")");
}
}
@Override
public void checkDeletedFiles(Connection conn, PreparedStatement pstmt) {
ResultSet rs = null;
try {
pstmt = conn.prepareStatement("SELECT URL FROM ReferencesFiles WHERE (Checked = 0)");
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("!!! Reference DELETED !!! -> " + rs.getString(1));
deleteRecord(rs.getString(1), conn, pstmt);
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, PreparedStatement pstmt) {
try {
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);
}
}
} |
Partager