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: DatabaseTaskoptions.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.scheduler.taskoptionmanagers;
009:
010: import com.uwyn.rife.database.queries.*;
011: import com.uwyn.rife.scheduler.taskoptionmanagers.exceptions.*;
012:
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.database.DbQueryManager;
017: import com.uwyn.rife.database.DbRowProcessor;
018: import com.uwyn.rife.database.exceptions.DatabaseException;
019: import com.uwyn.rife.scheduler.Scheduler;
020: import com.uwyn.rife.scheduler.Taskoption;
021: import com.uwyn.rife.scheduler.TaskoptionManager;
022: import com.uwyn.rife.scheduler.exceptions.TaskoptionManagerException;
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025: import java.util.ArrayList;
026: import java.util.Collection;
027:
028: public abstract class DatabaseTaskoptions extends DbQueryManager
029: implements TaskoptionManager {
030: private Scheduler mScheduler = null;
031:
032: protected DatabaseTaskoptions(Datasource datasource) {
033: super (datasource);
034: }
035:
036: public void setScheduler(Scheduler scheduler) {
037: mScheduler = scheduler;
038: }
039:
040: public Scheduler getScheduler() {
041: return mScheduler;
042: }
043:
044: public abstract boolean install() throws TaskoptionManagerException;
045:
046: public abstract boolean remove() throws TaskoptionManagerException;
047:
048: protected boolean _install(CreateTable createTableTaskoption)
049: throws TaskoptionManagerException {
050: assert createTableTaskoption != null;
051:
052: try {
053: executeUpdate(createTableTaskoption);
054: } catch (DatabaseException e) {
055: throw new InstallTaskoptionsErrorException(e);
056: }
057:
058: return true;
059: }
060:
061: protected boolean _remove(DropTable dropTableTaskoption)
062: throws TaskoptionManagerException {
063: assert dropTableTaskoption != null;
064:
065: try {
066: executeUpdate(dropTableTaskoption);
067: } catch (DatabaseException e) {
068: throw new RemoveTaskoptionsErrorException(e);
069: }
070:
071: return true;
072: }
073:
074: protected boolean _addTaskoption(Insert addTaskoption,
075: DbPreparedStatementHandler handler,
076: final Taskoption taskoption)
077: throws TaskoptionManagerException {
078: assert addTaskoption != null;
079:
080: if (null == taskoption)
081: throw new IllegalArgumentException(
082: "taskoption can't be null.");
083:
084: boolean result = false;
085:
086: try {
087: if (0 == executeUpdate(addTaskoption, handler)) {
088: throw new AddTaskoptionErrorException(taskoption);
089: }
090: result = true;
091: } catch (DatabaseException e) {
092: throw new AddTaskoptionErrorException(taskoption, e);
093: }
094:
095: return result;
096: }
097:
098: protected boolean _updateTaskoption(Update updateTaskoption,
099: DbPreparedStatementHandler handler,
100: final Taskoption taskoption)
101: throws TaskoptionManagerException {
102: assert updateTaskoption != null;
103:
104: if (null == taskoption)
105: throw new IllegalArgumentException(
106: "taskoption can't be null.");
107:
108: boolean result = false;
109: try {
110: if (0 == executeUpdate(updateTaskoption, handler)) {
111: throw new UpdateTaskoptionErrorException(taskoption);
112: }
113: result = true;
114: } catch (DatabaseException e) {
115: throw new UpdateTaskoptionErrorException(taskoption, e);
116: }
117:
118: return result;
119: }
120:
121: protected Taskoption _getTaskoption(Select getTaskoption,
122: ProcessTaskoption processTaskoption, final int taskId,
123: final String name) throws TaskoptionManagerException {
124: assert getTaskoption != null;
125:
126: if (taskId < 0)
127: throw new IllegalArgumentException(
128: "taskid can't be negative.");
129: if (null == name)
130: throw new IllegalArgumentException("name can't be null.");
131: if (0 == name.length())
132: throw new IllegalArgumentException("name can't be empty.");
133:
134: Taskoption taskoption = null;
135:
136: try {
137: executeFetchFirst(getTaskoption, processTaskoption,
138: new DbPreparedStatementHandler() {
139: public void setParameters(
140: DbPreparedStatement statement) {
141: statement.setInt("task_id", taskId)
142: .setString("name", name);
143: }
144: });
145: taskoption = processTaskoption.getTaskoption();
146: } catch (DatabaseException e) {
147: throw new GetTaskoptionErrorException(taskId, name, e);
148: }
149:
150: return taskoption;
151: }
152:
153: protected Collection<Taskoption> _getTaskoptions(
154: Select getTaskoptions, ProcessTaskoption processTaskoption,
155: final int taskId) throws TaskoptionManagerException {
156: assert getTaskoptions != null;
157:
158: if (taskId < 0)
159: throw new IllegalArgumentException(
160: "taskid can't be negative.");
161:
162: ArrayList<Taskoption> taskoptions = new ArrayList<Taskoption>();
163: processTaskoption.setCollection(taskoptions);
164:
165: try {
166: executeFetchAll(getTaskoptions, processTaskoption,
167: new DbPreparedStatementHandler() {
168: public void setParameters(
169: DbPreparedStatement statement) {
170: statement.setInt("task_id", taskId);
171: }
172: });
173: } catch (DatabaseException e) {
174: throw new GetTaskoptionsErrorException(taskId, e);
175: }
176:
177: assert taskoptions != null;
178:
179: return taskoptions;
180: }
181:
182: protected boolean _removeTaskoption(Delete removeTaskoption,
183: Taskoption taskoption) throws TaskoptionManagerException {
184: if (null == taskoption)
185: throw new IllegalArgumentException(
186: "taskoption can't be null.");
187:
188: return _removeTaskoption(removeTaskoption, taskoption
189: .getTaskId(), taskoption.getName());
190: }
191:
192: protected boolean _removeTaskoption(Delete removeTaskoption,
193: final int taskId, final String name)
194: throws TaskoptionManagerException {
195: assert removeTaskoption != null;
196:
197: if (taskId < 0)
198: throw new IllegalArgumentException(
199: "taskid can't be negative.");
200: if (null == name)
201: throw new IllegalArgumentException("name can't be null.");
202: if (0 == name.length())
203: throw new IllegalArgumentException("name can't be empty.");
204:
205: boolean result = false;
206:
207: try {
208: if (0 != executeUpdate(removeTaskoption,
209: new DbPreparedStatementHandler() {
210: public void setParameters(
211: DbPreparedStatement statement) {
212: statement.setInt("task_id", taskId)
213: .setString("name", name);
214: }
215: })) {
216: result = true;
217: }
218: } catch (DatabaseException e) {
219: throw new RemoveTaskoptionErrorException(taskId, name, e);
220: }
221:
222: return result;
223: }
224:
225: protected class ProcessTaskoption extends DbRowProcessor {
226: protected Collection<Taskoption> mCollection = null;
227: protected Taskoption mTaskoption = null;
228:
229: public ProcessTaskoption() {
230: }
231:
232: public void setCollection(Collection<Taskoption> collection) {
233: mCollection = collection;
234: }
235:
236: public boolean processRow(ResultSet resultSet)
237: throws SQLException {
238: assert resultSet != null;
239:
240: mTaskoption = new Taskoption();
241:
242: mTaskoption.setTaskId(resultSet.getInt("task_id"));
243: mTaskoption.setName(resultSet.getString("name"));
244: mTaskoption.setValue(resultSet.getString("value"));
245:
246: if (mCollection != null) {
247: mCollection.add(mTaskoption);
248: }
249:
250: return true;
251: }
252:
253: public Taskoption getTaskoption() {
254: return mTaskoption;
255: }
256: }
257: }
|