KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2004-2005, Hewlett-Packard Company and Massachusetts
3  * Institute of Technology. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Hewlett-Packard Company nor the name of the
17  * Massachusetts Institute of Technology nor the names of their
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32  * DAMAGE.
33  */

34 package org.dspace.checker;
35
36 import java.io.IOException JavaDoc;
37 import java.io.OutputStreamWriter JavaDoc;
38 import java.text.DateFormat JavaDoc;
39 import java.util.Date JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42
43 import org.apache.log4j.Logger;
44 import org.dspace.core.I18N;
45
46 /**
47  *
48  * Simple Reporter implementation.
49  *
50  * @author Jim Downing
51  * @author Grace Carpenter
52  * @author Nathan Sarr
53  *
54  * @todo estimate string buffer sizes.
55  */

56 public class SimpleReporterImpl implements SimpleReporter
57 {
58     /** log4j logger. */
59     private static Logger LOG = Logger.getLogger(SimpleReporterImpl.class);
60
61     /** Utility date format which includes hours minutes and seconds. */
62     private static final DateFormat JavaDoc DATE_FORMAT_MAX = DateFormat
63             .getDateInstance(DateFormat.MEDIUM);
64
65     /** Utility date format which only includes Month/day/year. */
66     private static final DateFormat JavaDoc DATE_FORMAT_MIN = DateFormat
67             .getDateInstance(DateFormat.SHORT);
68
69     /** The reporter access object to be used. */
70     private ReporterDAO reporter = null;
71
72     private String JavaDoc msg(String JavaDoc key)
73     {
74         return I18N.message(key, SimpleReporterImpl.class);
75     }
76
77     /**
78      * Main Constructor.
79      *
80      * @param reporter
81      * reporter to select the information
82      */

83     public SimpleReporterImpl()
84     {
85         this.reporter = new ReporterDAO();
86     }
87
88     /**
89      * Sends the Deleteted bitstream report to an administrator. for the
90      * specified date range.
91      *
92      * @param startDate
93      * the start date for the range
94      * @param endDate
95      * the end date for the range
96      * @param osw
97      * the output stream writer to write to.
98      *
99      * @return number of bitstreams found
100      *
101      * @throws IOException
102      * if io error occurs
103      */

104     public int getDeletedBitstreamReport(Date JavaDoc startDate, Date JavaDoc endDate,
105             OutputStreamWriter JavaDoc osw) throws IOException JavaDoc
106     {
107         // get all the bitstreams marked deleted for today
108
List JavaDoc history = reporter.getBitstreamResultTypeReport(startDate,
109                 endDate, ChecksumCheckResults.BITSTREAM_MARKED_DELETED);
110
111         osw.write("\n");
112         osw.write(msg("deleted-bitstream-intro"));
113         osw.write(DATE_FORMAT_MIN.format(startDate));
114         osw.write(" ");
115         osw.write(msg("date-range-to"));
116         osw.write(" ");
117         osw.write(DATE_FORMAT_MIN.format(endDate));
118         osw.write("\n\n\n");
119
120         if (history.size() == 0)
121         {
122             osw.write("\n\n");
123             osw.write(msg("no-bitstreams-to-delete"));
124             osw.write("\n");
125         }
126         else
127         {
128             printHistoryRecords(history, osw);
129         }
130
131         return history.size();
132     }
133
134     /**
135      * Send the checksum changed report for the specified date range.
136      *
137      * @param startDate
138      * the start date for the range
139      * @param endDate
140      * the end date for the range
141      * @param osw
142      * the output stream writer to write to.
143      *
144      * @return number of bitstreams found
145      *
146      * @throws IOException
147      * if io error occurs
148      */

149     public int getChangedChecksumReport(Date JavaDoc startDate, Date JavaDoc endDate,
150             OutputStreamWriter JavaDoc osw) throws IOException JavaDoc
151     {
152         // get all the bitstreams marked deleted for today
153
List JavaDoc history = reporter.getBitstreamResultTypeReport(startDate,
154                 endDate, ChecksumCheckResults.CHECKSUM_NO_MATCH);
155
156         osw.write("\n");
157         osw.write(msg("checksum-did-not-match"));
158         osw.write(" ");
159         osw.write("\n");
160         osw.write(DATE_FORMAT_MIN.format(startDate));
161         osw.write(" ");
162         osw.write(msg("date-range-to"));
163         osw.write(" ");
164         osw.write(DATE_FORMAT_MIN.format(endDate));
165         osw.write("\n\n\n");
166
167         if (history.size() == 0)
168         {
169             osw.write("\n\n");
170             osw.write(msg("no-changed-bitstreams"));
171             osw.write("\n");
172         }
173         else
174         {
175             printHistoryRecords(history, osw);
176         }
177
178         return history.size();
179     }
180
181     /**
182      * Send the bitstream not found report for the specified date range.
183      *
184      * @param startDate
185      * the start date for the range.
186      * @param endDate
187      * the end date for the range.
188      * @param osw
189      * the output stream writer to write to.
190      *
191      * @return number of bitstreams found
192      *
193      * @throws IOException
194      * if io error occurs
195      */

196     public int getBitstreamNotFoundReport(Date JavaDoc startDate, Date JavaDoc endDate,
197             OutputStreamWriter JavaDoc osw) throws IOException JavaDoc
198     {
199         // get all the bitstreams marked deleted for today
200
List JavaDoc history = reporter.getBitstreamResultTypeReport(startDate,
201                 endDate, ChecksumCheckResults.BITSTREAM_NOT_FOUND);
202
203         osw.write("\n");
204         osw.write(msg("bitstream-not-found-report"));
205         osw.write(DATE_FORMAT_MIN.format(startDate));
206         osw.write(" ");
207         osw.write(msg("date-range-to"));
208         osw.write(" ");
209         osw.write(DATE_FORMAT_MIN.format(endDate));
210         osw.write("\n\n\n");
211
212         if (history.size() == 0)
213         {
214             osw.write("\n\n");
215             osw.write(msg("no-bitstreams-changed"));
216             osw.write("\n");
217         }
218         else
219         {
220             printHistoryRecords(history, osw);
221         }
222
223         return history.size();
224     }
225
226     /**
227      * Send the bitstreams that were set to not be processed report for the
228      * specified date range.
229      *
230      * @param startDate
231      * the start date for the range
232      * @param endDate
233      * the end date for the range
234      * @param osw
235      * the output stream writer to write to.
236      *
237      * @return number of bitstreams found
238      *
239      * @throws IOException
240      * if io error occurs
241      */

242     public int getNotToBeProcessedReport(Date JavaDoc startDate, Date JavaDoc endDate,
243             OutputStreamWriter JavaDoc osw) throws IOException JavaDoc
244     {
245         // get all the bitstreams marked deleted for today
246
List JavaDoc history = reporter.getNotProcessedBitstreamsReport(startDate,
247                 endDate);
248
249         osw.write("\n");
250         osw.write(msg("bitstream-will-no-longer-be-processed"));
251         osw.write(" ");
252         osw.write(DATE_FORMAT_MIN.format(startDate));
253         osw.write(" ");
254         osw.write(msg("date-range-to"));
255         osw.write(" ");
256         osw.write(DATE_FORMAT_MIN.format(endDate));
257         osw.write("\n\n\n");
258
259         if (history.size() == 0)
260         {
261             osw.write("\n\n");
262             osw.write(msg("no-bitstreams-to-no-longer-be-processed"));
263             osw.write("\n");
264         }
265         else
266         {
267             printHistoryRecords(history, osw);
268         }
269
270         return history.size();
271     }
272
273     /**
274      * Get any bitstreams that are not checked by the checksum checker.
275      *
276      * @param osw
277      * the OutputStreamWriter to write to
278      *
279      * @return the number of unchecked bitstreams
280      *
281      * @throws IOException
282      * if io error occurs
283      */

284     public int getUncheckedBitstreamsReport(OutputStreamWriter JavaDoc osw)
285             throws IOException JavaDoc
286     {
287         // get all the bitstreams marked deleted for today
288
List JavaDoc bitstreams = reporter.getUnknownBitstreams();
289
290         osw.write("\n");
291         osw.write(msg("unchecked-bitstream-report"));
292         osw.write(DATE_FORMAT_MIN.format(new Date JavaDoc()));
293         osw.write("\n\n\n");
294
295         if (bitstreams.size() == 0)
296         {
297             osw.write("\n\n");
298             osw.write(msg("no-unchecked-bitstreams"));
299             osw.write("\n");
300         }
301         else
302         {
303             osw.write(msg("howto-add-unchecked-bitstreams"));
304             osw.write("\n\n\n");
305             this.printDSpaceInfoRecords(bitstreams, osw);
306         }
307
308         return bitstreams.size();
309     }
310
311     /**
312      * Create a list of the found history records.
313      *
314      * @param history
315      * the list of history records to be iterated over.
316      * @param osw
317      * the output stream writer to write to.
318      *
319      * @throws IOException
320      * if io error occurs
321      */

322     private void printHistoryRecords(List JavaDoc history, OutputStreamWriter JavaDoc osw)
323             throws IOException JavaDoc
324     {
325         Iterator JavaDoc iter = history.iterator();
326         while (iter.hasNext())
327         {
328             ChecksumHistory historyInfo = (ChecksumHistory) iter.next();
329             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(1000);
330             buf.append("------------------------------------------------ \n");
331             buf.append(msg("bitstream-id")).append(" = ").append(
332                     historyInfo.getBitstreamId()).append("\n");
333             buf.append(msg("process-start-date")).append(" = ").append(
334                     DATE_FORMAT_MAX.format(historyInfo.getProcessStartDate()))
335                     .append("\n");
336             buf.append(msg("process-end-date")).append(" = ").append(
337                     DATE_FORMAT_MAX.format(historyInfo.getProcessEndDate()))
338                     .append("\n");
339             buf.append(msg("checksum-expected")).append(" = ").append(
340                     historyInfo.getChecksumExpected()).append("\n");
341             buf.append(msg("checksum-calculated")).append(" = ").append(
342                     historyInfo.getChecksumCalculated()).append("\n");
343             buf.append(msg("result")).append(" = ").append(
344                     historyInfo.getResult()).append("\n");
345             buf.append("----------------------------------------------- \n\n");
346             osw.write(buf.toString());
347         }
348     }
349
350     /**
351      * Create a list of the found history records.
352      *
353      * @param bitstreams
354      * the list of history records to be iterated over.
355      * @param osw
356      * the output stream to write to.
357      *
358      * @throws IOException
359      * if io error occurs
360      */

361     private void printDSpaceInfoRecords(List JavaDoc bitstreams, OutputStreamWriter JavaDoc osw)
362             throws IOException JavaDoc
363     {
364         Iterator JavaDoc iter = bitstreams.iterator();
365
366         while (iter.hasNext())
367         {
368             DSpaceBitstreamInfo info = (DSpaceBitstreamInfo) iter.next();
369             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(1000);
370             buf.append("------------------------------------------------ \n");
371             buf.append(msg("format-id")).append(" = ").append(
372                     info.getBitstreamFormatId()).append("\n");
373             buf.append(msg("deleted")).append(" = ").append(info.getDeleted())
374                     .append("\n");
375             buf.append(msg("bitstream-id")).append(" = ").append(
376                     info.getBitstreamId()).append("\n");
377             buf.append(msg("checksum-algorithm")).append(" = ").append(
378                     info.getChecksumAlgorithm()).append("\n");
379             buf.append(msg("internal-id")).append(" = ").append(
380                     info.getInternalId()).append("\n");
381             buf.append(msg("name")).append(" = ").append(info.getName())
382                     .append("\n");
383             buf.append(msg("size")).append(" = ").append(info.getSize())
384                     .append("\n");
385             buf.append(msg("source")).append(" = ").append(info.getSource())
386                     .append("\n");
387             buf.append(msg("checksum")).append(" = ").append(
388                     info.getStoredChecksum()).append("\n");
389             buf.append(msg("store-number")).append(" = ").append(
390                     info.getStoreNumber()).append("\n");
391             buf.append(msg("description")).append(" = ").append(
392                     info.getUserFormatDescription()).append("\n");
393             buf.append("----------------------------------------------- \n\n");
394             osw.write(buf.toString());
395         }
396     }
397 }
398
Popular Tags