head	1.11;
access;
symbols
	TIGRIS_0_9_2_4:1.5
	TIGRIS_0_9_2_3:1.5
	HELM_PEER_PORT_BRANCH:1.10.0.2
	TURBINE_PEER:1.10
	TIGRIS_0_9_2_1:1.10
	TIGRIS_0_9_2:1.5.0.2
	TIGRIS_0_9_0:1.4.0.4
	TIGRIS_0_8_4:1.4.0.2;
locks; strict;
comment	@# @;


1.11
date	2001.02.21.03.23.56;	author edk;	state dead;
branches;
next	1.10;

1.10
date	2001.01.26.18.29.00;	author edk;	state Exp;
branches
	1.10.2.1;
next	1.9;

1.9
date	2001.01.26.05.24.05;	author edk;	state dead;
branches;
next	1.8;

1.8
date	2001.01.25.02.42.47;	author edk;	state Exp;
branches;
next	1.7;

1.7
date	2001.01.24.22.24.36;	author leonardr;	state Exp;
branches;
next	1.6;

1.6
date	2001.01.24.20.42.46;	author dlr;	state Exp;
branches;
next	1.5;

1.5
date	2001.01.05.01.10.55;	author dlr;	state Exp;
branches;
next	1.4;

1.4
date	2000.12.02.01.45.41;	author leonardr;	state Exp;
branches;
next	1.3;

1.3
date	2000.12.01.19.46.19;	author leonardr;	state Exp;
branches;
next	1.2;

1.2
date	2000.11.30.23.56.41;	author leonardr;	state Exp;
branches;
next	1.1;

1.1
date	2000.11.30.03.19.39;	author leonardr;	state Exp;
branches;
next	;

1.10.2.1
date	2001.01.28.01.02.17;	author dlr;	state dead;
branches;
next	;


desc
@@


1.11
log
@delete stuff from the head which is obsolete in the branch (more changes
are still needed for a clean compile)
@
text
@/* ================================================================
 * Copyright (c) 2000 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.collab.net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */
package org.tigris.helm.database;

import java.util.*;
import java.sql.*;

import org.apache.turbine.services.db.TurbineDB;
import org.apache.turbine.util.db.pool.DBConnection;

import org.webmacro.util.*;
import org.webmacro.servlet.*;

import com.workingdogs.village.*;

import org.joist.database.*;
import org.joist.util.*;

/**
 * Manages the task table.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@version $Id: TaskManager.java,v 1.10 2001/01/26 18:29:00 edk Exp $
 *
 * Copyright (c) 2000 CollabNet, Inc.  
 */

public class TaskManager extends TableManager
{
    /**
     * The name of the database table that this <code>TableManager</code> 
     * manages.
     */
    public static final String TABLE_NAME = "HELM_TASK";

    /**
     * The primary database key field.
     */
    public static final String TASK_ID = "TASK_ID";

    /**
     * The time at which the task was put into the database.
     */
    public static final String TIMESTAMP = "TIMESTAMP";   

    /**
     * UserID of the user for whom the task is being run.
     */
    public static final String ON_BEHALF_OF = "ON_BEHALF_OF";   

    /**
     * The Tigris component in charge of completing the task.
     */
    public static final String COMPONENT = "COMPONENT";   

    /**
     * The object to be operated on by the task. Optional.
     */
    public static final String AFFECTED_OBJECT_ID  = "AFFECTED_OBJECT_ID";   

    /**
     * The command to be run.
     */
    public static final String COMMAND  = "COMMAND";   

    /**
     * Any additional arguments to the task.
     */
    public static final String ARGUMENTS  = "ARGUMENTS";   

    /**
     * The logger instance for all instances of this class.
     */
    private static Log log = new Log("taskMgr", "TaskManager");

    /**
     * Whether we want debugging information.
     */
    private static boolean debug = true;

    /**
     * Creates a new instance.
     *
     * @@param dataMgr The manager of this instance.
     */
    public TaskManager(DataManager dataMgr)
    {
        super(dataMgr);
    }    

    /**
     * Adds a task to the table.
     *
     * @@param connection    The JDBC database connection to use
     * @@param      The ID for the list's project
     * @@param name          The name for the list
     * @@param description   A short description of the list
     * @@param type          The type of the list
     *
     * @@exception SQLException
     * @@exception DataSetException
     */
    public Record addTask(Connection connection, String onBehalfOf, 
                          String component, String affectedObjectID, 
                          String command, String arguments)
        throws DataSetException, SQLException, SecurityException
    {
        String action = "Adding task";
        log.info(action + ": " + command);
        TableDataSet tasks = null;
        Record task = null;
        try {            
            tasks = new TableDataSet(connection, TABLE_NAME, 
                                     new KeyDef().addAttrib(TASK_ID));        
            task = tasks.addRecord();
            if (onBehalfOf != null) task.setValue(ON_BEHALF_OF, onBehalfOf);
            if (component != null) task.setValue(COMPONENT, component);
            
            if (affectedObjectID != null) task.setValue(AFFECTED_OBJECT_ID, 
                                                        affectedObjectID);
            if (command != null) task.setValue(COMMAND, command);
            if (arguments != null) task.setValue(ARGUMENTS, arguments);
            
            task.save();
            // NOTE: There is no way to refresh a Village Record which has
            // been created using TableDataSet.addRecord().
            String taskID = getLastInsertedID(connection).toString();
            task.setValue(TASK_ID, taskID);
        } catch (DataSetException e) {
            log.exception(e);
            if (debug) log.debug(action + ": " + e.getMessage());
            throw e;
        } catch (SQLException e) {
            log.exception(e);
            if (debug) log.debug(action + ": " + e.getMessage());
            throw e;
        } finally {
            registerToClose(tasks);
        }
        
        return task;
    }

    /**
     * Removes a task by ID.
     *
     * @@param	connection    A JDBC database connection
     * @@param   taskID        The task to remove.
     * @@return	 a String
     * @@exception	SQLException
     * @@exception	DataSetException
     */
    public void removeTask(Connection connection, String taskID)
	throws SQLException, DataSetException
    {        
        Record r = getTaskByID(connection, taskID);
        r.markToBeDeleted();
        r.save();
    }

    /**
     * Returns all tasks currently in the queue.
     *
     * @@param	connection    A JDBC database connection
     * @@return	 a String
     * @@exception	SQLException
     * @@exception	DataSetException
     */
    public DataSet getTasks(Connection connection)
	throws SQLException, DataSetException
    {        
        return getTasks(connection, null, null, null, null);
    }

    /**
     * Returns all tasks charged to a particular component.
     *
     * @@param	connection    A JDBC database connection
     * @@param	component     The component to get actions for
     * @@return	 a String
     * @@exception	SQLException
     * @@exception	DataSetException
     */
    public DataSet getTasksForComponent 
        (Connection connection, String component)
	throws SQLException, DataSetException
    {        
        return getTasks(connection, null, null, component, null);
    }

    /**
     * Gets a task by ID.
     *
     * @@param	connection    A JDBC database connection
     * @@param	component     The ID to get a record for
     * @@return	 a String
     * @@exception	SQLException
     * @@exception	DataSetException
     */
    public Record getTaskByID (Connection connection, String taskID)
	throws SQLException, DataSetException
    {        
        DataSet tasks = getTasks(connection, taskID, null, null, null);        
        if (tasks.size() == 0)
        {
            throw new DataSetException("No such task " + taskID);
        }
        return tasks.getRecord(0);
    }

    /**
     * Retrieves tasks by any of the relevant fields.
     *
     * @@param	connection    A JDBC database connection
     * @@return	 a DataSet
     * @@exception	SQLException
     * @@exception	DataSetException
     */
    private TableDataSet getTasks(Connection connection, String taskID, 
                                  String onBehalfOf, String component, 
                                  String affectedObjectID)
        throws SQLException, DataSetException
    {
        TableDataSet tasks = null;
        try {
            tasks = new TableDataSet(connection, TABLE_NAME,
                                     new KeyDef().addAttrib(TASK_ID));        
            tasks.where(buildWhereClause(taskID, onBehalfOf, component,
                                           affectedObjectID));
            tasks.order(TIMESTAMP + " ASC, " + TASK_ID + " ASC");
            tasks.fetchRecords();
	}
	catch (DataSetException e)
	{
	    log.exception(e);
	    if (debug) log.debug("Getting Tasks: " + e.getMessage());
	    throw e;
	}
	catch (SQLException e)
	{
	    log.exception(e);
	    if (debug) log.debug("Getting Tasks: " + e.getMessage());
	    throw e;
	}
	finally
	{
	    registerToClose(tasks);
	}
        return tasks;
    }

    /**
     * Creates a SQL <code>where</code> clause.
     */
    protected static String buildWhereClause 
        (String taskID, String onBehalfOf, String component, 
         String affectedObjectID)
    {
        StringBuffer whereClause = new StringBuffer();
        boolean firstClause = true;

        if (taskID != null)
        {
            whereClause.append(TASK_ID).append('=').append(taskID);
            firstClause = false;
        }

        if (onBehalfOf != null)
        {
            if (!firstClause)
            {
                whereClause.append(" AND ");
            }
            whereClause.append(ON_BEHALF_OF).append('=').append(onBehalfOf);
            firstClause = false;
        }

        if (component != null)
        {
            if (!firstClause)
            {
                whereClause.append(" AND ");
            }
            whereClause.append(COMPONENT).append('=')
                .append(escapeAndQuoteSQL(component));
            firstClause = false;
        }

        if (onBehalfOf != null)
        {
            if (!firstClause)
            {
                whereClause.append(" AND ");
            }
            whereClause.append(AFFECTED_OBJECT_ID).append('=')
                .append(affectedObjectID);
            firstClause = false;
        }
        return whereClause.toString();
    }

}
@


1.10
log
@readding all the files as part of the process of returning to the way
things were as of tuesday
@
text
@d66 1
a66 1
 * @@version $Id: TaskManager.java,v 1.5 2001/01/05 01:10:55 dlr Exp $
@


1.10.2.1
log
@Porting to the Turbine Peer system.  -dlr and leonardr
@
text
@d66 1
a66 1
 * @@version $Id: TaskManager.java,v 1.10 2001/01/26 18:29:00 edk Exp $
@


1.9
log
@revert this stuff to its state as of 2001/01/24 12:42:00 GMT -- with
the following exceptions, which have some different modifications so
they'll work with changes which have happened in joist:

ProjectNewsEdit.java
SkinManager.java
SkinStyleElementValueManager.java
StyleElementManager.java
StyleManager.java

it compiles.  i dunno if it works, though.
@
text
@a61 2
import org.tigris.helm.DoesNotExist;

d66 1
a66 1
 * @@version $Id: TaskManager.java,v 1.8 2001/01/25 02:42:47 edk Exp $
a114 7
     * The default ordering of records from thsi table
     */
    public final static String DEFAULT_ORDER_BY = 
        TABLE_NAME + "." + TIMESTAMP + " ASC, " +
        TABLE_NAME + "." + TASK_ID + " ASC";

    /**
d131 1
a131 1
        super(dataMgr, new KeyDef().addAttrib(TASK_ID));
d143 2
a144 2
     * @@exception DBException
     * @@exception SecurityException
d146 1
a146 1
    public Record addTask(String onBehalfOf, 
d149 1
a149 1
        throws DBException, SecurityException
a154 1
        DBConnection dbConn = null;
d156 2
a157 4
            dbConn = TurbineDB.getConnection();
            Connection connection = dbConn.getConnection();

            tasks = new TableDataSet(connection, TABLE_NAME, keydef);
d172 8
a179 2
        } catch (Exception e) {
            throw new DBException(e);
a180 5
            try {
                TurbineDB.releaseConnection(dbConn);
            } catch (Exception e) {
                throw new DBException(e);
            }
d190 1
d193 2
a194 2
     *
     * @@exception DBException
d196 2
a197 2
    public void removeTask(String taskID)
	throws DBException, DoesNotExist
d199 3
a201 14
        try
        {
            Record r = getTaskByID(taskID);
            r.markToBeDeleted();
            r.save();
        }
        catch (DataSetException e)
        {
            throw new DBException(e);
        }
        catch (SQLException e)
        {
            throw new DBException(e);
        }
d207 1
d209 2
a210 2
     *
     * @@exception DBException
d212 2
a213 2
    public DataSet getTasks()
	throws DBException
d215 1
a215 1
        return getTasks(null);
d221 1
d227 3
a229 2
    public DataSet getTasksForComponent (String component)
	throws DBException
d231 1
a231 2
        Criterion c = new Criterion(COMPONENT, component);
        return getTasks(c);
d237 1
d240 2
a241 2
     *
     * @@exception DBException
d243 2
a244 2
    public Record getTaskByID (String taskID)
	throws DBException, DoesNotExist
d246 2
a247 3
        Criterion c = new Criterion(TASK_ID, taskID);
        Record r = getFirstMatchingTask(c);
        if (r == null)
d249 1
a249 1
            throw new DoesNotExist ("Task " + taskID);
d251 1
a251 1
        return r;
d255 1
a255 1
     * Retrieves tasks using the supplied Criterion object.
d257 4
a260 4
     * @@param criterion Criterion to use for select
     * @@return	        a DataSet
     *
     * @@exception DBException
d262 4
a265 2
    private DataSet getTasks(Criterion criterion)
        throws DBException
d267 26
a292 8
        DataSet s = null;
        try
        {
            s = runSelect(TABLE_NAME, criterion, DEFAULT_ORDER_BY);
        } catch (Exception e) {
            throw new DBException(e);
        }
        return s;
d296 1
a296 4
     * Retrieves the first task matching a given Criterion.
     *
     * @@param criterion Criterion to use for select
     * @@return          A Record with the first match, or null if none match
d298 3
a300 2
    private Record getFirstMatchingTask(Criterion criterion)
        throws DBException
d302 22
a323 4
        Record r = null;
        try {
            DataSet s = getTasks(criterion);
            if (s != null && s.containsRecord(0))
d325 1
a325 1
                r = s.getRecord(0);
d327 3
d331 2
a332 1
        catch (DataSetException e)
d334 7
a340 1
            throw new DBException(e);
d342 1
a342 1
        return r;
d344 1
@


1.8
log
@more changes to match the new pattern
@
text
@d68 1
a68 1
 * @@version $Id: TaskManager.java,v 1.7 2001/01/24 22:24:36 leonardr Exp $
@


1.7
log
@Moved TableManagers to new keydef constructor and keydef-less runSelect.
@
text
@d62 2
d68 1
a68 1
 * @@version $Id: TaskManager.java,v 1.6 2001/01/24 20:42:46 dlr Exp $
d117 7
d155 1
a155 1
    public Record addTask(Connection connection, String onBehalfOf, 
d158 1
a158 1
        throws DBExceptionn, SecurityException
d164 1
d166 3
d184 1
a184 3
        } catch (DataSetException e) {
            throw new DBException(e);
        } catch (SQLException e) {
d187 5
a200 1
     * @@param	connection    A JDBC database connection
d206 2
a207 2
    public void removeTask(Connection connection, String taskID)
	throws DBException
d211 1
a211 1
            Record r = getTaskByID(connection, taskID);
d219 4
a227 1
     * @@param	connection    A JDBC database connection
d232 1
a232 1
    public DataSet getTasks(Connection connection)
d235 1
a235 1
        return getTasks(connection, null, null, null, null);
a240 1
     * @@param	connection    A JDBC database connection
d246 1
a246 2
    public DataSet getTasksForComponent 
        (Connection connection, String component)
d249 2
a250 1
        return getTasks(connection, null, null, component, null);
a255 1
     * @@param	connection    A JDBC database connection
d261 2
a262 2
    public Record getTaskByID (Connection connection, String taskID)
	throws DBException
d264 3
a266 2
        DataSet tasks = getTasks(connection, taskID, null, null, null);        
        if (tasks.size() == 0)
d268 1
a268 1
            throw new DBException("No such task: " + taskID);
d270 1
a270 1
        return tasks.getRecord(0);
d274 1
a274 1
     * Retrieves tasks by any of the relevant fields.
d276 2
a277 2
     * @@param	connection    A JDBC database connection
     * @@return	 a DataSet
d281 1
a281 3
    private TableDataSet getTasks(Connection connection, String taskID, 
                                  String onBehalfOf, String component, 
                                  String affectedObjectID)
d284 8
a291 21
        TableDataSet tasks = null;
        try {
            tasks = new TableDataSet(connection, TABLE_NAME, keydef);
            tasks.where(buildWhereClause(taskID, onBehalfOf, component,
                                           affectedObjectID));
            tasks.order(TIMESTAMP + " ASC, " + TASK_ID + " ASC");
            tasks.fetchRecords();
	}
	catch (DataSetException e)
	{
	    throw new DBException(e);
	}
	catch (SQLException e)
	{
	    throw new DBException(e);
	}
	finally
	{
	    registerToClose(tasks);
	}
        return tasks;
d295 4
a298 1
     * Creates a SQL <code>where</code> clause.
d300 2
a301 3
    protected static String buildWhereClause 
        (String taskID, String onBehalfOf, String component, 
         String affectedObjectID)
d303 4
a306 12
        StringBuffer whereClause = new StringBuffer();
        boolean firstClause = true;

        if (taskID != null)
        {
            whereClause.append(TASK_ID).append('=').append(taskID);
            firstClause = false;
        }

        if (onBehalfOf != null)
        {
            if (!firstClause)
d308 1
a308 1
                whereClause.append(" AND ");
a309 2
            whereClause.append(ON_BEHALF_OF).append('=').append(onBehalfOf);
            firstClause = false;
d311 1
a311 2

        if (component != null)
d313 1
a313 18
            if (!firstClause)
            {
                whereClause.append(" AND ");
            }
            whereClause.append(COMPONENT).append('=')
                .append(escapeAndQuoteSQL(component));
            firstClause = false;
        }

        if (onBehalfOf != null)
        {
            if (!firstClause)
            {
                whereClause.append(" AND ");
            }
            whereClause.append(AFFECTED_OBJECT_ID).append('=')
                .append(affectedObjectID);
            firstClause = false;
d315 1
a315 1
        return whereClause.toString();
@


1.6
log
@Making use of DBException.
@
text
@d66 1
a66 1
 * @@version $Id: TaskManager.java,v 1.5 2001/01/05 01:10:55 dlr Exp $
d131 1
a131 1
        super(dataMgr);
d156 1
a156 2
            tasks = new TableDataSet(connection, TABLE_NAME, 
                                     new KeyDef().addAttrib(TASK_ID));        
d271 1
a271 2
            tasks = new TableDataSet(connection, TABLE_NAME,
                                     new KeyDef().addAttrib(TASK_ID));        
@


1.5
log
@Migrated to latest Turbine CVS to avoid connection release bug (fixed long ago in Turbine CVS--we just had a bunk version).
@
text
@d66 1
a66 1
 * @@version $Id: TaskManager.java,v 1.4 2000/12/02 01:45:41 leonardr Exp $
d143 2
a144 2
     * @@exception SQLException
     * @@exception DataSetException
d149 1
a149 1
        throws DataSetException, SQLException, SecurityException
d173 1
a173 3
            log.exception(e);
            if (debug) log.debug(action + ": " + e.getMessage());
            throw e;
d175 1
a175 3
            log.exception(e);
            if (debug) log.debug(action + ": " + e.getMessage());
            throw e;
d189 2
a190 2
     * @@exception	SQLException
     * @@exception	DataSetException
d193 1
a193 1
	throws SQLException, DataSetException
d195 10
a204 3
        Record r = getTaskByID(connection, taskID);
        r.markToBeDeleted();
        r.save();
d212 2
a213 2
     * @@exception	SQLException
     * @@exception	DataSetException
d216 1
a216 1
	throws SQLException, DataSetException
d232 1
a232 1
	throws SQLException, DataSetException
d243 2
a244 2
     * @@exception	SQLException
     * @@exception	DataSetException
d247 1
a247 1
	throws SQLException, DataSetException
d252 1
a252 1
            throw new DataSetException("No such task " + taskID);
d262 2
a263 2
     * @@exception	SQLException
     * @@exception	DataSetException
d268 1
a268 1
        throws SQLException, DataSetException
d281 1
a281 3
	    log.exception(e);
	    if (debug) log.debug("Getting Tasks: " + e.getMessage());
	    throw e;
d285 1
a285 3
	    log.exception(e);
	    if (debug) log.debug("Getting Tasks: " + e.getMessage());
	    throw e;
a342 1

@


1.4
log
@Changes to fix horrible mistakes in removing projects from project groups, among other things.
@
text
@d51 1
a51 1
import org.apache.turbine.services.db.PoolBrokerService;
d66 1
a66 1
 * @@version $Id: TaskManager.java,v 1.3 2000/12/01 19:46:19 leonardr Exp $
@


1.3
log
@Logging cleanup.
@
text
@d66 1
a66 1
 * @@version $Id: TaskManager.java,v 1.2 2000/11/30 23:56:41 leonardr Exp $
d246 6
a251 1
        return getTasks(connection, taskID, null, null, null).getRecord(0);
@


1.2
log
@Mega patch which makes script data persistent and which provides a general framework for tasks to be run (which will be used by anzu eventually)
@
text
@d66 1
a66 1
 * @@version $Id: TaskManager.java,v 1.1 2000/11/30 03:19:39 leonardr Exp $
d152 1
a152 1
        log.info(action + ": " + command + "/" + arguments);
@


1.1
log
@The Task business object will replace QueueHandler and take over the world!
@
text
@d66 1
a66 1
 * @@version $Id: ActionManager.java,v 1.1.1.1 2000/11/12 21:14:51 dlr Exp $
d152 1
d188 17
d264 2
a265 1
            tasks = new TableDataSet(connection, TABLE_NAME);
d268 1
a268 1
            tasks.order(TIMESTAMP + " ASC");
@

