001: /*
002: * Copyright (c) 2004-2005, Hewlett-Packard Company and Massachusetts
003: * Institute of Technology. All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are
007: * met:
008: *
009: * - Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * - Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * - Neither the name of the Hewlett-Packard Company nor the name of the
017: * Massachusetts Institute of Technology nor the names of their
018: * contributors may be used to endorse or promote products derived from
019: * this software without specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
024: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
025: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
028: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
029: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
030: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
031: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
032: * DAMAGE.
033: */
034: package org.dspace.checker;
035:
036: import java.sql.Connection;
037: import java.sql.PreparedStatement;
038: import java.sql.ResultSet;
039: import java.sql.SQLException;
040: import java.util.Date;
041: import java.util.LinkedList;
042: import java.util.List;
043:
044: import org.apache.log4j.Logger;
045: import org.dspace.core.ConfigurationManager;
046: import org.dspace.storage.rdbms.DatabaseManager;
047:
048: /**
049: * This class will report information on the checksum checker process.
050: *
051: * @author Jim Downing
052: * @author Grace Carpenter
053: * @author Nathan Sarr
054: *
055: *
056: */
057: public class ReporterDAO extends DAOSupport {
058: /**
059: * Select the most recent bitstream information for a given date range with
060: * the specified status. This select is from the checksum history and
061: * checksum results tables.
062: */
063: public static final String DATE_RANGE_BITSTREAMS = "select bitstream_id, last_process_start_date, last_process_end_date, "
064: + "expected_checksum, current_checksum, result_description "
065: + "from most_recent_checksum, checksum_results "
066: + "where most_recent_checksum.result = checksum_results.result_code "
067: + "and most_recent_checksum.result= ? "
068: + "and most_recent_checksum.last_process_start_date >= ? "
069: + "and most_recent_checksum.last_process_start_date < ? "
070: + "order by bitstream_id";
071:
072: /**
073: *
074: * Find all bitstreams that were set to not be processed for the specified
075: * date range.
076: *
077: */
078: public static final String DATE_RANGE_NOT_PROCESSED_BITSTREAMS = "select most_recent_checksum.bitstream_id, "
079: + "most_recent_checksum.last_process_start_date, most_recent_checksum.last_process_end_date, "
080: + "most_recent_checksum.expected_checksum, most_recent_checksum.current_checksum, "
081: + "result_description "
082: + "from checksum_results, most_recent_checksum "
083: + "where most_recent_checksum.to_be_processed = false "
084: + "and most_recent_checksum.result = checksum_results.result_code "
085: + "and most_recent_checksum.last_process_start_date >= ? "
086: + "and most_recent_checksum.last_process_start_date < ? "
087: + "order by most_recent_checksum.bitstream_id";
088:
089: public static final String DATE_RANGE_NOT_PROCESSED_BITSTREAMS_ORACLE = "select most_recent_checksum.bitstream_id, "
090: + "most_recent_checksum.last_process_start_date, most_recent_checksum.last_process_end_date, "
091: + "most_recent_checksum.expected_checksum, most_recent_checksum.current_checksum, "
092: + "result_description "
093: + "from checksum_results, most_recent_checksum "
094: + "where most_recent_checksum.to_be_processed = 0 "
095: + "and most_recent_checksum.result = checksum_results.result_code "
096: + "and most_recent_checksum.last_process_start_date >= ? "
097: + "and most_recent_checksum.last_process_start_date < ? "
098: + "order by most_recent_checksum.bitstream_id";
099:
100: /**
101: * Find all bitstreams that the checksum checker is unaware of
102: */
103: public static final String FIND_UNKNOWN_BITSTREAMS = "select bitstream.deleted, bitstream.store_number, bitstream.size_bytes, "
104: + "bitstreamformatregistry.short_description, bitstream.bitstream_id, "
105: + "bitstream.user_format_description, bitstream.internal_id, "
106: + "bitstream.source, bitstream.checksum_algorithm, bitstream.checksum, "
107: + "bitstream.name, bitstream.description "
108: + "from bitstream left outer join bitstreamformatregistry on "
109: + "bitstream.bitstream_format_id = bitstreamformatregistry.bitstream_format_id "
110: + "where not exists( select 'x' from most_recent_checksum "
111: + "where most_recent_checksum.bitstream_id = bitstream.bitstream_id )";
112:
113: /**
114: * Usual Log4J Logger.
115: */
116: private static final Logger LOG = Logger
117: .getLogger(ReporterDAO.class);
118:
119: /**
120: * Default constructor
121: */
122: public ReporterDAO() {
123: ;
124: }
125:
126: /**
127: * Select the most recent bitstream for a given date range with the
128: * specified status.
129: *
130: * @param startDate
131: * the start date range
132: * @param endDate
133: * the end date range.
134: * @param resultCode
135: * the result code
136: *
137: * @return a list of BitstreamHistoryInfo objects
138: */
139: public List getBitstreamResultTypeReport(Date startDate,
140: Date endDate, String resultCode) {
141: List bitstreamHistory = new LinkedList();
142:
143: Connection conn = null;
144: PreparedStatement prepStmt = null;
145: ResultSet rs = null;
146:
147: try {
148: // create the connection and execute the statement
149: conn = DatabaseManager.getConnection();
150:
151: prepStmt = conn.prepareStatement(DATE_RANGE_BITSTREAMS);
152:
153: prepStmt.setString(1, resultCode);
154: prepStmt.setDate(2, new java.sql.Date(startDate.getTime()));
155: prepStmt.setDate(3, new java.sql.Date(endDate.getTime()));
156:
157: rs = prepStmt.executeQuery();
158:
159: // add the bitstream history objects
160: while (rs.next()) {
161: bitstreamHistory.add(new ChecksumHistory(rs
162: .getInt("bitstream_id"), rs
163: .getTimestamp("last_process_start_date"), rs
164: .getTimestamp("last_process_end_date"), rs
165: .getString("expected_checksum"), rs
166: .getString("current_checksum"), rs
167: .getString("result_description")));
168: }
169: } catch (SQLException e) {
170: LOG.warn(
171: "Bitstream history could not be found for specified type "
172: + e.getMessage(), e);
173: } finally {
174: cleanup(prepStmt, conn, rs);
175: }
176:
177: return bitstreamHistory;
178: }
179:
180: /**
181: * Find all bitstreams that were set to not be processed for the specified
182: * date range.
183: *
184: * @param startDate
185: * the start of the date range
186: * @param endDate
187: * the end of the date range
188: * @return a list of BitstreamHistoryInfo objects
189: */
190: public List getNotProcessedBitstreamsReport(Date startDate,
191: Date endDate) {
192: List bitstreamHistory = new LinkedList();
193:
194: Connection conn = null;
195: PreparedStatement prepStmt = null;
196: ResultSet rs = null;
197:
198: try {
199: // create the connection and execute the statement
200: conn = DatabaseManager.getConnection();
201:
202: if ("oracle".equals(ConfigurationManager
203: .getProperty("db.name")))
204: prepStmt = conn
205: .prepareStatement(DATE_RANGE_NOT_PROCESSED_BITSTREAMS_ORACLE);
206: else
207: prepStmt = conn
208: .prepareStatement(DATE_RANGE_NOT_PROCESSED_BITSTREAMS);
209:
210: prepStmt.setDate(1, new java.sql.Date(startDate.getTime()));
211: prepStmt.setDate(2, new java.sql.Date(endDate.getTime()));
212:
213: rs = prepStmt.executeQuery();
214:
215: // add the bitstream history objects
216: while (rs.next()) {
217: bitstreamHistory.add(new ChecksumHistory(rs
218: .getInt("bitstream_id"), rs
219: .getTimestamp("last_process_start_date"), rs
220: .getTimestamp("last_process_end_date"), rs
221: .getString("expected_checksum"), rs
222: .getString("current_checksum"), rs
223: .getString("result_description")));
224: }
225: } catch (SQLException e) {
226: LOG.warn(
227: "Bitstream history could not be found for specified type "
228: + e.getMessage(), e);
229: } finally {
230: cleanup(prepStmt, conn, rs);
231: }
232:
233: return bitstreamHistory;
234: }
235:
236: /**
237: * Find all bitstreams that the checksum checker is currently not aware of
238: *
239: * @return a List of DSpaceBitstreamInfo objects
240: */
241: public List getUnknownBitstreams() {
242: List unknownBitstreams = new LinkedList();
243:
244: Connection conn = null;
245: PreparedStatement prepStmt = null;
246: ResultSet rs = null;
247:
248: try {
249: // create the connection and execute the statement
250: conn = DatabaseManager.getConnection();
251:
252: prepStmt = conn.prepareStatement(FIND_UNKNOWN_BITSTREAMS);
253:
254: rs = prepStmt.executeQuery();
255:
256: // add the bitstream history objects
257: while (rs.next()) {
258: unknownBitstreams.add(new DSpaceBitstreamInfo(rs
259: .getBoolean("deleted"), rs
260: .getInt("store_number"), rs
261: .getInt("size_bytes"), rs
262: .getString("short_description"), rs
263: .getInt("bitstream_id"), rs
264: .getString("user_format_description"), rs
265: .getString("internal_id"), rs
266: .getString("source"), rs
267: .getString("checksum_algorithm"), rs
268: .getString("checksum"), rs.getString("name"),
269: rs.getString("description")));
270: }
271: } catch (SQLException e) {
272: LOG.warn(
273: "Bitstream history could not be found for specified type "
274: + e.getMessage(), e);
275: } finally {
276: cleanup(prepStmt, conn, rs);
277: }
278:
279: return unknownBitstreams;
280: }
281: }
|