KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > checker > ChecksumHistoryDAO


1 package org.dspace.checker;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.sql.Timestamp JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.apache.log4j.Logger;
12 import org.dspace.storage.rdbms.DatabaseManager;
13
14 /**
15  * <p>
16  * This is the data access for the checksum history information. All
17  * update,insert and delete database operations should go through this class for
18  * checksum history operations.
19  * </p>
20  *
21  * @author Jim Downing
22  * @author Grace Carpenter
23  * @author Nathan Sarr
24  *
25  *
26  */

27 public class ChecksumHistoryDAO extends DAOSupport
28 {
29     /**
30      * Query that selects bitstream IDs from most_recent_checksum table that are
31      * not yet in the checksum_history table, and inserts them into
32      * checksum_history.
33      */

34     private static final String JavaDoc 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     /** Query that inserts results of recent check into the history table. */
49     private static final String JavaDoc INSERT_HISTORY = "insert into checksum_history ( bitstream_id, process_start_date, "
50             + " process_end_date, checksum_expected, checksum_calculated, result ) "
51             + " values ( ?, ?, ?, ?, ?, ?);";
52
53     /**
54      * Deletes from the most_recent_checksum where the bitstream id is found
55      */

56     private static final String JavaDoc DELETE_BITSTREAM_HISTORY = "Delete from checksum_history "
57             + "where bitstream_id = ?";
58
59     /**
60      * Logger for the checksum history DAO.
61      */

62     private static final Logger LOG = Logger
63             .getLogger(ChecksumHistoryDAO.class);
64
65     /**
66      * Inserts results of checksum check into checksum_history table for a given
67      * bitstream.
68      *
69      * @param info
70      * the BitstreamInfo representing a checksum check.
71      *
72      * @throws IllegalArgumentException
73      * if the <code>BitstreamInfo</code> is null.
74      */

75     public void insertHistory(BitstreamInfo info)
76     {
77         if (info == null)
78         {
79             throw new IllegalArgumentException JavaDoc(
80                     "BitstreamInfo parameter may not be null");
81         }
82
83         Connection JavaDoc conn = null;
84         PreparedStatement JavaDoc 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 JavaDoc(info
92                     .getProcessStartDate().getTime()));
93             stmt.setTimestamp(3, new java.sql.Timestamp JavaDoc(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 JavaDoc e)
102         {
103             LOG.error("Problem updating checksum row. " + e.getMessage(), e);
104             throw new RuntimeException JavaDoc("Problem updating checksum row. "
105                     + e.getMessage(), e);
106         }
107         finally
108         {
109             cleanup(stmt, conn);
110         }
111     }
112
113     /**
114      * Deletes the bitstream from the bitstream_history table if it exist.
115      *
116      * @param id
117      * the bitstream id.
118      *
119      * @return number of records deleted
120      */

121     protected int deleteHistoryForBitstreamInfo(int id, Connection JavaDoc conn)
122     {
123         PreparedStatement JavaDoc 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 JavaDoc e)
137         {
138             LOG.error("Problem with inserting missing bitstream. "
139                     + e.getMessage(), e);
140             throw new RuntimeException JavaDoc("Problem inserting missing bitstream. "
141                     + e.getMessage(), e);
142         }
143         finally
144         {
145             cleanup(stmt, conn);
146         }
147
148         return numDeleted;
149     }
150
151     /**
152      * @param conn
153      */

154     protected void updateMissingBitstreams(Connection JavaDoc conn) throws SQLException JavaDoc
155     {
156         PreparedStatement JavaDoc stmt = null;
157         try
158         {
159             stmt = conn.prepareStatement(INSERT_MISSING_HISTORY_BITSTREAMS);
160             stmt.executeUpdate();
161         }
162         catch (SQLException JavaDoc e)
163         {
164             LOG.error("Problem updating missing history. " + e.getMessage(), e);
165             throw new RuntimeException JavaDoc("Problem updating missing history. "
166                     + e.getMessage(), e);
167         }
168         finally
169         {
170             cleanup(stmt);
171         }
172     }
173
174     /**
175      * Delete the history records from the database.
176      *
177      * @param retentionDate
178      * any records older than this data are deleted.
179      * @param result
180      * result code records must have for them to be deleted.
181      * @param conn
182      * database connection.
183      * @return number of records deleted.
184      * @throws SQLException
185      * if database error occurs.
186      */

187     protected int deleteHistoryByDateAndCode(Date JavaDoc retentionDate, String JavaDoc result,
188             Connection JavaDoc conn) throws SQLException JavaDoc
189     {
190         PreparedStatement JavaDoc 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 JavaDoc(retentionDate.getTime()));
197             update.setString(2, result);
198             return update.executeUpdate();
199         }
200         finally
201         {
202             cleanup(update);
203         }
204
205     }
206
207     /**
208      * Prune the history records from the database.
209      *
210      * @param interests
211      * set of results and the duration of time before they are
212      * removed from the database
213      *
214      * @return number of bitstreams deleted
215      */

216     public int prune(Map JavaDoc interests)
217     {
218         Connection JavaDoc conn = null;
219         try
220         {
221             conn = DatabaseManager.getConnection();
222             long now = System.currentTimeMillis();
223             int count = 0;
224             for (Iterator JavaDoc iter = interests.keySet().iterator(); iter.hasNext();)
225             {
226                 String JavaDoc result = (String JavaDoc) iter.next();
227                 Long JavaDoc dur = (Long JavaDoc) interests.get(result);
228                 count += deleteHistoryByDateAndCode(new Date JavaDoc(now
229                         - dur.longValue()), result, conn);
230                 conn.commit();
231             }
232             return count;
233         }
234         catch (SQLException JavaDoc e)
235         {
236             LOG.error("Problem pruning results: " + e.getMessage(), e);
237             throw new RuntimeException JavaDoc("Problem pruning results: "
238                     + e.getMessage(), e);
239         }
240         finally
241         {
242             DatabaseManager.freeConnection(conn);
243         }
244     }
245 }
246
Popular Tags