01: /*
02: * Copyright 2006 Pentaho Corporation. All rights reserved.
03: * This software was developed by Pentaho Corporation and is provided under the terms
04: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
05: * this file except in compliance with the license. If you need a copy of the license,
06: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
07: * BI Platform. The Initial Developer is Pentaho Corporation.
08: *
09: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11: * the license for the specific language governing your rights and limitations.
12: *
13: * Created Feb 21, 2006
14: * @author wseyler
15: */
16:
17: package org.pentaho.plugin.core;
18:
19: import java.io.OutputStream;
20: import java.util.Calendar;
21: import java.util.Date;
22:
23: import org.apache.commons.logging.Log;
24: import org.apache.commons.logging.LogFactory;
25: import org.pentaho.core.repository.IContentRepository;
26: import org.pentaho.messages.Messages;
27: import org.pentaho.plugin.ComponentBase;
28: import org.pentaho.repository.content.ContentRepository;
29:
30: public class ContentRepositoryCleaner extends ComponentBase {
31:
32: private static final long serialVersionUID = 1L;
33:
34: private static final String AGE = "days_old"; //$NON-NLS-1$
35:
36: private static final String DELETE_COUNT = "delete_count"; //$NON-NLS-1$
37:
38: protected boolean validateAction() {
39: if (!isDefinedInput(AGE)) {
40: return false;
41: }
42: return true;
43: }
44:
45: protected boolean validateSystemSettings() {
46: return true;
47: }
48:
49: public void done() {
50: }
51:
52: protected boolean executeAction() throws Throwable {
53: // get daysback off the input
54: int daysBack = Integer.parseInt(getInputValue(AGE).toString());
55: // make sure it's a negative number
56: daysBack = Math.abs(daysBack) * -1;
57:
58: // get todays calendar
59: Calendar calendar = Calendar.getInstance();
60: // subtract (by adding a negative number) the daysback amount
61: calendar.add(Calendar.DATE, daysBack);
62: // create the new date for the content repository to use
63: Date agedDate = new Date(calendar.getTimeInMillis());
64: // get the content repository and tell it to remove the items older than
65: // agedDate
66: IContentRepository contentRepository = ContentRepository
67: .getInstance(getSession());
68: int deleteCount = contentRepository
69: .deleteContentOlderThanDate(agedDate);
70: // return the number of files deleted
71: setOutputValue(DELETE_COUNT, Integer.toString(deleteCount));
72:
73: OutputStream feedbackOutputStream = getFeedbackOutputStream();
74: if (feedbackOutputStream != null) { // We have a feedback stream so we'll send some messages to it.
75: feedbackOutputStream.write(Messages.getString(
76: "ContentRepositoryCleaner.INFO_0001").getBytes()); //$NON-NLS-1$
77: feedbackOutputStream.write(Integer.toString(deleteCount)
78: .getBytes());
79: feedbackOutputStream.write(Messages.getString(
80: "ContentRepositoryCleaner.INFO_0002").getBytes()); //$NON-NLS-1$
81: feedbackOutputStream.write(Integer.toString(
82: Math.abs(daysBack)).getBytes());
83: feedbackOutputStream.write(Messages.getString(
84: "ContentRepositoryCleaner.INFO_0003").getBytes()); //$NON-NLS-1$
85: }
86: return true;
87: }
88:
89: public boolean init() {
90: return true;
91: }
92:
93: public Log getLogger() {
94: return LogFactory.getLog(ContentRepositoryCleaner.class);
95: }
96:
97: }
|