head	1.4;
access;
symbols;
locks; strict;
comment	@# @;


1.4
date	2001.01.26.05.24.05;	author edk;	state dead;
branches;
next	1.3;

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

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

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


desc
@@


1.4
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
@/* ================================================================
 * 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 COLLABNET 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.webmacro.util.*;

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

import com.workingdogs.village.*;

import org.joist.util.*;
import org.joist.database.Criterion;
import org.joist.database.DataManager;
import org.joist.database.TableManager;

import org.tigris.helm.DoesNotExist;
import org.tigris.helm.AlreadyExists;

/**
 * Manages the Role table.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@version $Id: RoleManager.java,v 1.3 2001/01/25 01:25:21 leonardr Exp $
 *
 * Copyright (c) 2000 CollabNet, Inc.
 */

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

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

    /**
     * The name field.
     */
    public static final String ROLE_NAME = "name";   

    /**
     * The description field.
     */
    public static final String ROLE_DESCRIPTION = "description";   

    /**
     * The type field.
     */
    public static final String ROLE_TYPE = "type";   

    /**
     * Whether or not the role is requestable.
     */
    public static final String REQUESTABLE = "requestable";   

    /**
     * Denotes a host role.
     */
    public static final String HOST_ROLE_TYPE = "Host";   

    /**
     * Denotes a domain role.
     */
    public static final String DOMAIN_ROLE_TYPE = "Domain";   

    /**
     * Denotes a project role.
     */
    public static final String PROJECT_ROLE_TYPE = "Project";   

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

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

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

    /**
     * Returns a KeyDef object appropriate for this table.
     */
    private KeyDef keydef()
    {
        return new KeyDef().addAttrib(ROLE_ID);
    }

    ////////////////////////////////////////////////////////////////
    // Retrieval methods.
    ////////////////////////////////////////////////////////////////

    /**
     * Maps a role ID to a role name.
     *
     * @@param roleID   The ID of the role whose name to get.
     * @@return	         The role name.
     *
     * @@exception DBException if a database error ocurs.
     * @@exception DoesNotExist if there is no role with the specified ID.
     */
    public String getRoleName(String roleID)
	throws DBException, DoesNotExist
    {
        Record role = getRole(roleID);
        String name = null;
        try {
            name = role.getValue(ROLE_NAME).asString();
        } catch (DataSetException e) {
            throw new DBException(e);
        }
        return name;
    }

    /**
     * Maps a role name to a role ID.
     *
     * @@param roleName   The name of the role whose ID to get.
     * @@return	         The role ID.
     *
     * @@exception DBException if a database error ocurs.
     * @@exception DoesNotExist if there is no role with the specified name.
     */
    public String getRoleID(String roleName)
        throws DBException, DoesNotExist
    {
        Record role = getRoleByName(roleName);
        String id = null;
        try {
            id = role.getValue(ROLE_ID).asString();
        } catch (DataSetException e) {
            throw new DBException(e);
        }
        return id;
    }

    /**
     * Gets the role with a given ID.
     *
     * @@param	connection    A JDBC database connection
     * @@return	a Record for the specified role.
     *
     * @@exception DBException if a database error ocurs.
     * @@exception DoesNotExist if there is no role with the specified ID.
     */
    public Record getRole (String roleID)
        throws DBException, DoesNotExist
    {        
        Criterion c = new Criterion(ROLE_ID, roleID);
        Record role = getFirstMatchingRole(c);   
        if (role == null)
        {
            throw new DoesNotExist("Role with ID " + roleID);
        }
        return role;
    }

    /**
     * Gets the role with a given name.
     *
     * @@param	name  Name of the role to retrieve.
     * @@return	a Record for the specified role.
     *
     * @@exception DBException if a database error ocurs.
     * @@exception DoesNotExist if there is no role with the specified name.
     */
    public Record getRoleByName (String name)
        throws DBException, DoesNotExist
    {        
        Criterion c = new Criterion(ROLE_NAME, name);
        Record role = getFirstMatchingRole(c);   
        if (role == null)
        {
            throw new DoesNotExist("Role with name " + name);
        }
        return role;
    }

    /**
     * Returns a dataset of all roles.
     *
     * @@return	 a DataSet
     * @@exception DBException if a database error occurs.
     */
    public DataSet listRoles()
	throws DBException
    {        
        return getRoles(null);
    }

    /**
     * Returns a dataset of all the roles of a specified type.
     *
     * @@param type The types of role to list.
     * @@return	   The matching roles.
     *
     * @@exception DBException if a database error occurs.
     */
    public DataSet listRolesByType(String type)
	throws DBException
    {        
        Criterion c = null;
        if (type != null)
        {
            c = new Criterion(ROLE_TYPE, type);
        }
        DataSet roles = getRoles(c);
        return roles;
    }

    /**
     * Returns the first role matching a given Criterion. 
     *
     * @@param criterion Criterion to use in select.
     * @@return Null if no matching role, first role in recordset otherwise.
     *
     * @@exception DBException if a database error occurs.
     *
     */
    private Record getFirstMatchingRole(Criterion criterion)
        throws DBException
    {
        Record role = null;
        try {
            DataSet roles = getRoles(criterion);
            if (role != null && roles.containsRecord(0))
            {
                role = roles.getRecord(0);
            }
        } catch (DataSetException e) {
            throw new DBException(e);
        }
        return role;
    }

    /**
     * Returns roles matching a given criterion, sorted by name.
     *
     * @@param criterion Criterion to use in select.
     *
     * @@exception DBException if a database error occurs.
     */
    public DataSet getRoles(Criterion criterion)
    throws DBException
    {
        DataSet s = null;
        try {
            s = runSelect(TABLE_NAME, criterion, TABLE_NAME + "." + ROLE_NAME);
        } catch (Exception e) {
            throw new DBException(e);
        }
        return s;
    }

    ////////////////////////////////////////////////////////////////
    // Mutator methods.
    ////////////////////////////////////////////////////////////////

    /**
     * Adds a new role to the database
     *
     * @@param name        The name of the new role.
     * @@param description The description of the new role.
     * @@param type        The type of the new role.
     * @@param requestable Whether or not the role can be requested by users.
     * @@return 	          The database record for the newly added role.
     *
     * @@exception DBException if a database error ocurrs.
     * @@exception AlreadyExists if a role with the given name already exists.
     */
    public Record addRole (String name, String description,
                           String type, Boolean requestable)
        throws DBException, AlreadyExists
    {
        Criterion c = new Criterion(ROLE_NAME, name);
        Record role = getFirstMatchingRole(c);
        if (role != null)
        {
            throw new AlreadyExists("Role " + name);
        }

        try {
            role = updateRole(null, name, description, type, requestable);
        } catch (DoesNotExist ignore) {}
        return role;
    }

    /**
     * Updates an existing role or adds a new role to the database 
     * (depending on whether given a <code>roleID</code> or not).
     *
     * @@param roleID      The database key of the role to update, or 
     *                     <code>null</code> if the role is new.
     * @@param name        The name of the role.
     * @@param description The description of the role.
     * @@param type        The type of the role.
     * @@param requestable Whether or not the role can be requested by users.
     * @@return	          The database record for the role updated.
     *
     * @@exception DBException if a database error ocurrs.
     * @@exception DoesNotExist if the roleID is invalid..
     */
    public Record updateRole (String roleID, String name, String description,
                              String type, Boolean requestable)
	throws DBException, DoesNotExist
    {
        DBConnection dbConn = null;
        Record role = null;
        TableDataSet roles = null;

        try
        {
            dbConn = TurbineDB.getConnection();
            Connection connection = dbConn.getConnection();           

            if (roleID != null)
            {
                role = getRole(roleID);
            } else {
                roles = new TableDataSet(connection, TABLE_NAME, keydef());
                role = roles.addRecord();                
            }

            // Do update.
            if (name != null) role.setValue(ROLE_NAME, name);
            if (description != null) role.setValue(ROLE_DESCRIPTION, description);
            if (type != null) role.setValue(ROLE_TYPE, type);
            if (requestable != null) role.setValue(REQUESTABLE, requestable.booleanValue());
            role.save();

            if (roleID == null)
            {
                // NOTE: There is no way to refresh a Village Record which has
                // been created using TableDataSet.addRecord().
                roleID = getLastInsertedID(connection).toString();
                role.setValue(ROLE_ID, roleID);
            }
        } catch (Exception e) {
            throw new DBException(e);
        } finally {
            try {
                TurbineDB.releaseConnection(dbConn);
            } catch (Exception e) {
                throw new DBException(e);
            }
            registerToClose(roles);
        }

	return role;
    }

    /**
     *  Deletes an existing role record from the database
     *
     * @@param	roleID    the ID of the role to delete
     * @@exception	DBException if a database error occurs.
     */
    public void deleteRole (String roleID)
	throws DBException
    {
        Record role = null;
        try {
            role = getRole(roleID);
        } catch (DoesNotExist ignore) {}

        if (role != null)
        {
            try {
                role.markToBeDeleted();
                role.save();
            } catch (DataSetException e) {
                throw new DBException(e);
            } catch (SQLException e) {
                throw new DBException(e);
            }
        }
    }
}
@


1.3
log
@Removed JDBC Connection from getRoleID() params.
@
text
@d71 1
a71 1
 * @@version $Id: RoleManager.java,v 1.2 2001/01/24 22:24:36 leonardr Exp $
@


1.2
log
@Moved TableManagers to new keydef constructor and keydef-less runSelect.
@
text
@d71 1
a71 1
 * @@version $Id: RoleManager.java,v 1.1 2001/01/24 21:41:48 leonardr Exp $
d187 1
a187 1
    public String getRoleID(Connection connection, String roleName)
@


1.1
log
@Ported UserManager and RoleManager to Helm. Started moving other TableManagers toward a grab-connection-on-demand model. Massive breakage ensues. dlr and I are working on it.
@
text
@d71 1
a71 1
 * @@version $Id: RoleManager.java,v 1.18 2000/12/12 21:45:16 leonardr Exp $
d311 1
a311 1
            s = runSelect(TABLE_NAME, keydef(), criterion, TABLE_NAME + "." + ROLE_NAME);
@

