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.taskoptionmanagers.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.Taskoption;
017: import com.uwyn.rife.scheduler.exceptions.TaskoptionManagerException;
018: import com.uwyn.rife.scheduler.taskoptionmanagers.DatabaseTaskoptions;
019: import com.uwyn.rife.scheduler.taskoptionmanagers.exceptions.DuplicateTaskoptionException;
020: import com.uwyn.rife.scheduler.taskoptionmanagers.exceptions.InexistentTaskIdException;
021: import java.util.Collection;
022:
023: public class generic extends DatabaseTaskoptions {
024: protected CreateTable mCreateTableTaskoption = null;
025: protected DropTable mDropTableTaskoption = null;
026: protected Insert mAddTaskoption = null;
027: protected Select mGetTaskoption = null;
028: protected Select mGetTaskoptions = null;
029: protected Update mUpdateTaskoption = null;
030: protected Delete mRemoveTaskoption = null;
031:
032: public generic(Datasource datasource) {
033: super (datasource);
034:
035: mCreateTableTaskoption = new CreateTable(getDatasource())
036: .table(RifeConfig.Scheduler.getTableTaskoption())
037: .column("task_id", Integer.class, CreateTable.NOTNULL)
038: .column(
039: "name",
040: String.class,
041: RifeConfig.Scheduler
042: .getTaskoptionNameMaximumLength(),
043: CreateTable.NOTNULL).column(
044: "value",
045: String.class,
046: RifeConfig.Scheduler
047: .getTaskoptionValueMaximumLength(),
048: CreateTable.NOTNULL).primaryKey(
049: RifeConfig.Scheduler.getTableTaskoption()
050: .toUpperCase()
051: + "_PK",
052: new String[] { "task_id", "name" }).foreignKey(
053: RifeConfig.Scheduler.getTableTaskoption()
054: .toUpperCase()
055: + "_TASKID_FK",
056: RifeConfig.Scheduler.getTableTask(), "task_id",
057: "id", null, CreateTable.CASCADE);
058:
059: mDropTableTaskoption = new DropTable(getDatasource())
060: .table(mCreateTableTaskoption.getTable());
061:
062: mAddTaskoption = new Insert(getDatasource()).into(
063: mCreateTableTaskoption.getTable()).fieldParameter(
064: "task_id").fieldParameter("name").fieldParameter(
065: "value");
066:
067: mGetTaskoption = new Select(getDatasource()).from(
068: mCreateTableTaskoption.getTable()).whereParameter(
069: "task_id", "=").whereParameterAnd("name", "=");
070:
071: mGetTaskoptions = new Select(getDatasource()).from(
072: mCreateTableTaskoption.getTable()).whereParameter(
073: "task_id", "=");
074:
075: mUpdateTaskoption = new Update(getDatasource()).table(
076: mCreateTableTaskoption.getTable()).fieldParameter(
077: "value").whereParameter("task_id", "=")
078: .whereParameterAnd("name", "=");
079:
080: mRemoveTaskoption = new Delete(getDatasource()).from(
081: mCreateTableTaskoption.getTable()).whereParameter(
082: "task_id", "=").whereParameterAnd("name", "=");
083: }
084:
085: public boolean install() throws TaskoptionManagerException {
086: return _install(mCreateTableTaskoption);
087: }
088:
089: public boolean remove() throws TaskoptionManagerException {
090: return _remove(mDropTableTaskoption);
091: }
092:
093: public boolean addTaskoption(final Taskoption taskoption)
094: throws TaskoptionManagerException {
095: try {
096: return _addTaskoption(mAddTaskoption,
097: new DbPreparedStatementHandler() {
098: public void setParameters(
099: DbPreparedStatement statement) {
100: statement.setInt("task_id",
101: taskoption.getTaskId()).setString(
102: "name", taskoption.getName())
103: .setString("value",
104: taskoption.getValue());
105: }
106: }, taskoption);
107: } catch (TaskoptionManagerException e) {
108: if (null != e.getCause() && null != e.getCause().getCause()) {
109: String message = e.getCause().getCause().getMessage()
110: .toUpperCase();
111: if (-1 != message.indexOf(mCreateTableTaskoption
112: .getForeignKeys().get(0).getName())) {
113: throw new InexistentTaskIdException(taskoption
114: .getTaskId());
115: } else if (-1 != message.indexOf(mCreateTableTaskoption
116: .getPrimaryKeys().get(0).getName())) {
117: throw new DuplicateTaskoptionException(taskoption
118: .getTaskId(), taskoption.getName());
119: }
120: }
121:
122: throw e;
123: }
124: }
125:
126: public boolean updateTaskoption(final Taskoption taskoption)
127: throws TaskoptionManagerException {
128: try {
129: return _updateTaskoption(mUpdateTaskoption,
130: new DbPreparedStatementHandler() {
131: public void setParameters(
132: DbPreparedStatement statement) {
133: statement.setInt("task_id",
134: taskoption.getTaskId()).setString(
135: "name", taskoption.getName())
136: .setString("value",
137: taskoption.getValue());
138: }
139: }, taskoption);
140: } catch (TaskoptionManagerException e) {
141: if (null != e.getCause() && null != e.getCause().getCause()) {
142: String message = e.getCause().getCause().getMessage()
143: .toUpperCase();
144: if (-1 != message.indexOf(mCreateTableTaskoption
145: .getForeignKeys().get(0).getName())) {
146: throw new InexistentTaskIdException(taskoption
147: .getTaskId());
148: } else if (-1 != message.indexOf(mCreateTableTaskoption
149: .getPrimaryKeys().get(0).getName())) {
150: throw new DuplicateTaskoptionException(taskoption
151: .getTaskId(), taskoption.getName());
152: }
153: }
154:
155: throw e;
156: }
157: }
158:
159: public Taskoption getTaskoption(int taskId, String name)
160: throws TaskoptionManagerException {
161: return _getTaskoption(mGetTaskoption, new ProcessTaskoption(),
162: taskId, name);
163: }
164:
165: public Collection<Taskoption> getTaskoptions(int taskId)
166: throws TaskoptionManagerException {
167: return _getTaskoptions(mGetTaskoptions,
168: new ProcessTaskoption(), taskId);
169: }
170:
171: public boolean removeTaskoption(Taskoption taskoption)
172: throws TaskoptionManagerException {
173: return _removeTaskoption(mRemoveTaskoption, taskoption);
174: }
175:
176: public boolean removeTaskoption(int taskId, String name)
177: throws TaskoptionManagerException {
178: return _removeTaskoption(mRemoveTaskoption, taskId, name);
179: }
180: }
|