001: package org.sakaiproject.component.app.scheduler.jobs;
002:
003: import java.sql.Connection;
004: import java.sql.Date;
005: import java.sql.PreparedStatement;
006: import java.sql.ResultSet;
007: import java.sql.SQLException;
008: import java.sql.Statement;
009: import java.sql.Timestamp;
010: import java.util.Calendar;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import org.quartz.Job;
015: import org.quartz.JobExecutionContext;
016: import org.quartz.JobExecutionException;
017: import org.sakaiproject.component.cover.ServerConfigurationService;
018: import org.sakaiproject.db.cover.SqlService;
019:
020: public class SakaiEventArchiveJob implements Job {
021:
022: private static final Log LOG = LogFactory
023: .getLog(SakaiEventArchiveJob.class);
024:
025: // default is to move any events older than 24 hours
026: private static final boolean ARCHIVE_ENABLED = true;
027: private static final String DEFAULT_ARCHIVE_LENGTH = "86400000";
028:
029: public void execute(JobExecutionContext arg0)
030: throws JobExecutionException {
031:
032: boolean archiveEnabled = ServerConfigurationService.getBoolean(
033: "scheduler.event.archive.enabled", ARCHIVE_ENABLED);
034:
035: long archiveLength = Long.parseLong(ServerConfigurationService
036: .getString("scheduler.event.archive.length",
037: DEFAULT_ARCHIVE_LENGTH));
038:
039: Connection sakaiConnection = null;
040: PreparedStatement sakaiStatement = null;
041: String sql;
042:
043: Timestamp archiveDate = new Timestamp(System
044: .currentTimeMillis()
045: - archiveLength);
046:
047: LOG.info("archiveDate=" + archiveDate.toString());
048:
049: // TODO: checkToSeeIfArchiveTablesExist();
050: // Make separate statements for HSQL, MySQL, Oracle
051:
052: try {
053:
054: sakaiConnection = SqlService.borrowConnection();
055: sakaiConnection.setAutoCommit(false);
056:
057: // move session entries older than <date> to archive table
058: sql = "INSERT INTO SAKAI_SESSION_ARCHIVE (SELECT * FROM SAKAI_SESSION WHERE SESSION_END IS NOT NULL AND SESSION_END < ?)";
059: LOG.info("sql=" + sql);
060:
061: sakaiStatement = sakaiConnection.prepareStatement(sql);
062: sakaiStatement.setTimestamp(1, archiveDate);
063: sakaiStatement.execute(sql);
064:
065: sql = "DELETE FROM SAKAI_SESSION WHERE SESSION_END IS NOT NULL AND SESSION_END < ?";
066: LOG.info("sql=" + sql);
067:
068: //sakaiStatement = sakaiConnection.prepareStatement(sql);
069: //sakaiStatement.setTimestamp(1, archiveDate);
070: //sakaiStatement.execute(sql);
071:
072: sakaiConnection.commit();
073:
074: // move events older than <date> to archive table
075: sql = "INSERT INTO SAKAI_EVENT_ARCHIVE (SELECT * FROM SAKAI_EVENT WHERE EVENT_DATE < ?)";
076: LOG.info("sql=" + sql);
077:
078: sakaiStatement = sakaiConnection.prepareStatement(sql);
079: sakaiStatement.setTimestamp(1, archiveDate);
080: sakaiStatement.execute(sql);
081:
082: sql = "DELETE FROM SAKAI_EVENT WHERE EVENT_DATE < ?";
083: LOG.info("sql=" + sql);
084:
085: //sakaiStatement = sakaiConnection.prepareStatement(sql);
086: //sakaiStatement.setTimestamp(1, archiveDate);
087: //sakaiStatement.execute(sql);
088:
089: sakaiConnection.commit();
090:
091: } catch (SQLException e) {
092: LOG.error("SQLException: " + e);
093: } finally {
094: try {
095: if (sakaiStatement != null)
096: sakaiStatement.close();
097: if (sakaiConnection != null)
098: SqlService.returnConnection(sakaiConnection);
099: } catch (SQLException e) {
100: LOG.error("SQLException in finally block: " + e);
101: }
102: }
103:
104: }
105:
106: }
|