1 package org.dspace.checker; 2 3 import java.sql.Connection ; 4 import java.sql.PreparedStatement ; 5 import java.sql.SQLException ; 6 import java.sql.Timestamp ; 7 import java.util.Date ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 11 import org.apache.log4j.Logger; 12 import org.dspace.storage.rdbms.DatabaseManager; 13 14 27 public class ChecksumHistoryDAO extends DAOSupport 28 { 29 34 private static final String INSERT_MISSING_HISTORY_BITSTREAMS = "insert into checksum_history ( " 35 + "bitstream_id, process_start_date, " 36 + "process_end_date, checksum_expected, " 37 + "checksum_calculated, result ) " 38 + "select most_recent_checksum.bitstream_id, " 39 + "most_recent_checksum.last_process_start_date, " 40 + "most_recent_checksum.last_process_end_date, " 41 + "most_recent_checksum.expected_checksum, most_recent_checksum.expected_checksum, " 42 + "CASE WHEN bitstream.deleted = true THEN 'BITSTREAM_MARKED_DELETED' else 'CHECKSUM_MATCH' END " 43 + "from most_recent_checksum, bitstream where " 44 + "not exists( select 'x' from checksum_history where " 45 + "most_recent_checksum.bitstream_id = checksum_history.bitstream_id ) " 46 + "and most_recent_checksum.bitstream_id = bitstream.bitstream_id;"; 47 48 49 private static final String INSERT_HISTORY = "insert into checksum_history ( bitstream_id, process_start_date, " 50 + " process_end_date, checksum_expected, checksum_calculated, result ) " 51 + " values ( ?, ?, ?, ?, ?, ?);"; 52 53 56 private static final String DELETE_BITSTREAM_HISTORY = "Delete from checksum_history " 57 + "where bitstream_id = ?"; 58 59 62 private static final Logger LOG = Logger 63 .getLogger(ChecksumHistoryDAO.class); 64 65 75 public void insertHistory(BitstreamInfo info) 76 { 77 if (info == null) 78 { 79 throw new IllegalArgumentException ( 80 "BitstreamInfo parameter may not be null"); 81 } 82 83 Connection conn = null; 84 PreparedStatement stmt = null; 85 86 try 87 { 88 conn = DatabaseManager.getConnection(); 89 stmt = conn.prepareStatement(INSERT_HISTORY); 90 stmt.setInt(1, info.getBitstreamId()); 91 stmt.setTimestamp(2, new java.sql.Timestamp (info 92 .getProcessStartDate().getTime())); 93 stmt.setTimestamp(3, new java.sql.Timestamp (info 94 .getProcessEndDate().getTime())); 95 stmt.setString(4, info.getStoredChecksum()); 96 stmt.setString(5, info.getCalculatedChecksum()); 97 stmt.setString(6, info.getChecksumCheckResult()); 98 stmt.executeUpdate(); 99 conn.commit(); 100 } 101 catch (SQLException e) 102 { 103 LOG.error("Problem updating checksum row. " + e.getMessage(), e); 104 throw new RuntimeException ("Problem updating checksum row. " 105 + e.getMessage(), e); 106 } 107 finally 108 { 109 cleanup(stmt, conn); 110 } 111 } 112 113 121 protected int deleteHistoryForBitstreamInfo(int id, Connection conn) 122 { 123 PreparedStatement stmt = null; 124 125 int numDeleted = 0; 126 127 try 128 { 129 conn = DatabaseManager.getConnection(); 130 stmt = conn.prepareStatement(DELETE_BITSTREAM_HISTORY); 131 stmt.setInt(1, id); 132 133 numDeleted = stmt.executeUpdate(); 134 conn.commit(); 135 } 136 catch (SQLException e) 137 { 138 LOG.error("Problem with inserting missing bitstream. " 139 + e.getMessage(), e); 140 throw new RuntimeException ("Problem inserting missing bitstream. " 141 + e.getMessage(), e); 142 } 143 finally 144 { 145 cleanup(stmt, conn); 146 } 147 148 return numDeleted; 149 } 150 151 154 protected void updateMissingBitstreams(Connection conn) throws SQLException 155 { 156 PreparedStatement stmt = null; 157 try 158 { 159 stmt = conn.prepareStatement(INSERT_MISSING_HISTORY_BITSTREAMS); 160 stmt.executeUpdate(); 161 } 162 catch (SQLException e) 163 { 164 LOG.error("Problem updating missing history. " + e.getMessage(), e); 165 throw new RuntimeException ("Problem updating missing history. " 166 + e.getMessage(), e); 167 } 168 finally 169 { 170 cleanup(stmt); 171 } 172 } 173 174 187 protected int deleteHistoryByDateAndCode(Date retentionDate, String result, 188 Connection conn) throws SQLException 189 { 190 PreparedStatement update = null; 191 192 try 193 { 194 update = conn 195 .prepareStatement("DELETE FROM checksum_history WHERE process_end_date<? AND result=?"); 196 update.setTimestamp(1, new Timestamp (retentionDate.getTime())); 197 update.setString(2, result); 198 return update.executeUpdate(); 199 } 200 finally 201 { 202 cleanup(update); 203 } 204 205 } 206 207 216 public int prune(Map interests) 217 { 218 Connection conn = null; 219 try 220 { 221 conn = DatabaseManager.getConnection(); 222 long now = System.currentTimeMillis(); 223 int count = 0; 224 for (Iterator iter = interests.keySet().iterator(); iter.hasNext();) 225 { 226 String result = (String ) iter.next(); 227 Long dur = (Long ) interests.get(result); 228 count += deleteHistoryByDateAndCode(new Date (now 229 - dur.longValue()), result, conn); 230 conn.commit(); 231 } 232 return count; 233 } 234 catch (SQLException e) 235 { 236 LOG.error("Problem pruning results: " + e.getMessage(), e); 237 throw new RuntimeException ("Problem pruning results: " 238 + e.getMessage(), e); 239 } 240 finally 241 { 242 DatabaseManager.freeConnection(conn); 243 } 244 } 245 } 246 | Popular Tags |