001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: generic.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.scheduler.taskmanagers.databasedrivers;
009:
010: import com.uwyn.rife.database.queries.*;
011:
012: import com.uwyn.rife.config.RifeConfig;
013: import com.uwyn.rife.database.Datasource;
014: import com.uwyn.rife.database.DbPreparedStatement;
015: import com.uwyn.rife.database.DbPreparedStatementHandler;
016: import com.uwyn.rife.scheduler.Task;
017: import com.uwyn.rife.scheduler.exceptions.TaskManagerException;
018: import com.uwyn.rife.scheduler.taskmanagers.DatabaseTasks;
019: import java.util.Collection;
020:
021: public class generic extends DatabaseTasks {
022: protected CreateSequence mCreateSequenceTask = null;
023: protected CreateTable mCreateTableTask = null;
024: protected DropSequence mDropSequenceTask = null;
025: protected DropTable mDropTableTask = null;
026: protected SequenceValue mGetTaskId = null;
027: protected Insert mAddTask = null;
028: protected Select mGetTask = null;
029: protected Select mGetTasksToProcess = null;
030: protected Select mGetScheduledTasks = null;
031: protected Update mUpdateTask = null;
032: protected Delete mRemoveTask = null;
033: protected Update mActivateTask = null;
034: protected Update mDesactivateTask = null;
035:
036: public generic(Datasource datasource) {
037: super (datasource);
038:
039: mCreateSequenceTask = new CreateSequence(getDatasource())
040: .name(RifeConfig.Scheduler.getSequenceTask());
041:
042: mCreateTableTask = new CreateTable(getDatasource()).table(
043: RifeConfig.Scheduler.getTableTask()).column("id",
044: int.class, CreateTable.NOTNULL).column("type",
045: String.class,
046: RifeConfig.Scheduler.getTaskTypeMaximumLength(),
047: CreateTable.NOTNULL).column("planned", long.class,
048: CreateTable.NOTNULL).column("frequency", String.class,
049: RifeConfig.Scheduler.getTaskFrequencyMaximumLength(),
050: CreateTable.NULL).column("busy", boolean.class)
051: .defaultValue("busy", false).primaryKey(
052: RifeConfig.Scheduler.getTableTask()
053: .toUpperCase()
054: + "_PK", "id");
055:
056: mDropSequenceTask = new DropSequence(getDatasource())
057: .name(mCreateSequenceTask.getName());
058:
059: mDropTableTask = new DropTable(getDatasource())
060: .table(mCreateTableTask.getTable());
061:
062: mGetTaskId = new SequenceValue(getDatasource()).name(
063: mCreateSequenceTask.getName()).next();
064:
065: mAddTask = new Insert(getDatasource()).into(
066: mCreateTableTask.getTable()).fieldParameter("id")
067: .fieldParameter("type").fieldParameter("planned")
068: .fieldParameter("frequency").fieldParameter("busy");
069:
070: mGetTask = new Select(getDatasource()).from(
071: mCreateTableTask.getTable()).whereParameter("id", "=");
072:
073: mGetTasksToProcess = new Select(getDatasource()).from(
074: mCreateTableTask.getTable()).whereParameter("planned",
075: "<").whereAnd("busy", "=", false);
076:
077: mGetScheduledTasks = new Select(getDatasource()).from(
078: mCreateTableTask.getTable()).whereParameter("planned",
079: ">=").whereAnd("busy", "=", false);
080:
081: mUpdateTask = new Update(getDatasource()).table(
082: mCreateTableTask.getTable()).fieldParameter("type")
083: .fieldParameter("planned").fieldParameter("frequency")
084: .fieldParameter("busy").whereParameter("id", "=");
085:
086: mRemoveTask = new Delete(getDatasource()).from(
087: mCreateTableTask.getTable()).whereParameter("id", "=");
088:
089: mActivateTask = new Update(getDatasource()).table(
090: mCreateTableTask.getTable()).field("busy", true)
091: .whereParameter("id", "=");
092:
093: mDesactivateTask = new Update(getDatasource()).table(
094: mCreateTableTask.getTable()).field("busy", false)
095: .whereParameter("id", "=");
096: }
097:
098: public boolean install() throws TaskManagerException {
099: return _install(mCreateSequenceTask, mCreateTableTask);
100: }
101:
102: public boolean remove() throws TaskManagerException {
103: return _remove(mDropSequenceTask, mDropTableTask);
104: }
105:
106: public int addTask(final Task task) throws TaskManagerException {
107: return _addTask(mGetTaskId, mAddTask,
108: new DbPreparedStatementHandler() {
109: public void setParameters(
110: DbPreparedStatement statement) {
111: statement.setBean(task);
112: }
113: }, task);
114: }
115:
116: public boolean updateTask(final Task task)
117: throws TaskManagerException {
118: return _updateTask(mUpdateTask,
119: new DbPreparedStatementHandler() {
120: public void setParameters(
121: DbPreparedStatement statement) {
122: statement.setBean(task);
123: }
124: }, task);
125: }
126:
127: public Task getTask(int id) throws TaskManagerException {
128: return _getTask(mGetTask, new ProcessTask(), id);
129: }
130:
131: public Collection<Task> getTasksToProcess()
132: throws TaskManagerException {
133: return _getTasksToProcess(mGetTasksToProcess, new ProcessTask());
134: }
135:
136: public Collection<Task> getScheduledTasks()
137: throws TaskManagerException {
138: return _getScheduledTasks(mGetScheduledTasks, new ProcessTask());
139: }
140:
141: public boolean removeTask(int id) throws TaskManagerException {
142: return _removeTask(mRemoveTask, id);
143: }
144:
145: public boolean rescheduleTask(Task task, long interval,
146: String frequency) throws TaskManagerException {
147: return _rescheduleTask(task, interval, frequency);
148: }
149:
150: public boolean concludeTask(Task task) throws TaskManagerException {
151: return _concludeTask(task);
152: }
153:
154: public boolean activateTask(int id) throws TaskManagerException {
155: return _activateTask(mActivateTask, id);
156: }
157:
158: public boolean deactivateTask(int id) throws TaskManagerException {
159: return _desactivateTask(mDesactivateTask, id);
160: }
161: }
|