head	1.8;
access;
symbols
	TIGRIS_0_9_2_4:1.1
	TIGRIS_0_9_2_3:1.1
	HELM_PEER_PORT_BRANCH:1.7.0.2
	TURBINE_PEER:1.7
	TIGRIS_0_9_2_1:1.7
	TIGRIS_0_9_2:1.1.0.4
	TIGRIS_0_9_0:1.1.0.2;
locks; strict;
comment	@# @;


1.8
date	2001.02.21.03.23.56;	author edk;	state dead;
branches;
next	1.7;

1.7
date	2001.01.26.18.29.00;	author edk;	state Exp;
branches
	1.7.2.1;
next	1.6;

1.6
date	2001.01.26.05.24.05;	author edk;	state dead;
branches;
next	1.5;

1.5
date	2001.01.25.02.57.26;	author stack;	state Exp;
branches;
next	1.4;

1.4
date	2001.01.25.02.46.16;	author stack;	state Exp;
branches;
next	1.3;

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

1.2
date	2001.01.24.20.42.46;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2000.12.15.00.31.03;	author dlr;	state Exp;
branches;
next	;

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


desc
@@


1.8
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 Collab.Net (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 Collab.Net.
 * 
 * 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 Collab.Net.
 */


package org.tigris.helm.database;

import java.sql.*;

import org.webmacro.util.*;

import com.workingdogs.village.*;

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

import org.tigris.helm.DoesNotExist;
import org.tigris.helm.tool.TigrisUpdate;
import org.tigris.helm.database.ProjectManager;
import org.tigris.helm.project.Project;

/**
 * Manages views of version control modules.
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@version $Id: VCViewManager.java,v 1.7 2001/01/26 18:29:00 edk Exp $
 */
public class VCViewManager extends TableManager
{
    /**
     * Open the log file.
     */
    private static Log log = new Log("vcMgr", "VCViewManager");

    /**
     * Whether to log debugging information.
     */
    private static final boolean debug = true;

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

    /**
     * The auto-incremented database key field.
     */
    public static final String VIEW_ID = "VIEW_ID";

    /**
     * The field containing the domain with which this view is
     * associated (also part of the database key).
     */
    public static final String DOMAIN_ID = "DOMAIN_ID";

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

    /**
     * Creates a new instance.
     *
     * @@param dataMgr The associated <code>DataManager</code>.
     */
    public VCViewManager(DataManager dataMgr)
    {
        super(dataMgr);
    }
    
    // TODO: Update methods to support HELM_VC_VIEW table.

    /**
     * Updates an existing view or adds a new view to the database 
     * (depending on whether given a <code>viewID</code> or not).
     *
     * TODO: Updating a view via this method doesn't yet call the
     * scripts.
     *
     * @@param conn  The JDBC database connection to use.
     * @@param viewID      The database key of the module to update, or 
     *                     <code>null</code> if the module is new.
     * @@param domainID   The ID of the project associated with the module
     * @@param name       The name of the module
     *
     * @@return	          The system-assigned module ID for the module.
     *
     * @@exception SQLException
     * @@exception DataSetException
     */
    public Record updateVC (Connection connection, String viewID,
                            String domainID, String name)
	throws SQLException, DataSetException
    {
	Record module = null;

        String action = 
            (viewID == null ? "Adding VC: " : "Updating VC: ");
        TableDataSet modules = null;
	try
	{
            modules = new TableDataSet(connection, TABLE_NAME,
                    new KeyDef().addAttrib(VIEW_ID));
            if (viewID != null) {
                modules.where(buildWhereClause(viewID, domainID, name));
                modules.fetchRecords();
                if (debug) log.info("Updating record for existing module");
                module = modules.getRecord(0);
            } else {
                if (debug) log.info("Adding record for new module");
                module = modules.addRecord();
            }

            module.setValue(DOMAIN_ID, domainID);
            module.setValue(NAME, name);
            module.save();
            if (viewID == null)
            {
                // NOTE: There is no way to refresh a Village Record which has
                // been created using TableDataSet.addRecord().
                module.setValue(VIEW_ID, getLastInsertedID(connection)
                                .toString());
            }
        }
	catch (DataSetException e)
	{
	    log.exception(e);
	    if (debug) log.warning(action + e.getMessage());
	    throw e;
	}
	catch (SQLException e)
	{
	    log.exception(e);
	    if (debug) log.warning(action + e.getMessage());
	    throw e;
	}
	finally
	{
	    registerToClose(modules);
	}
        
	return module;
    }

    /**
     * Deletes an existing module from the database
     *
     * @@param	  connection    the JDBC database connection
     * @@param	  moduleID       the ID of the module to delete
     * @@exception SQLException
     */
    public void  deleteVC (Connection connection, String moduleID)
	throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        String action = "Deleting module: ";
	try
	{
            Record r = getVC(connection, moduleID);
            String domainID = r.getValue(VCViewManager.DOMAIN_ID).toString();
        
            String moduleName = r.getValue(NAME).toString();
            r.markToBeDeleted();
            r.save();

            Project project = new Project(connection, getDataManager(),
                                          domainID);
            TigrisUpdate.deleteModule(connection, getDataManager(), null, 
                                      project, moduleName);
	}
	catch (DataSetException e)
	{
	    log.exception(e);
	    if (debug) log.error(action + e.getMessage());
	    throw e;
	}
	catch (SQLException e)
	{
	    log.exception(e);
	    if (debug) log.error(action + e.getMessage());
	    throw e;
	}
    }

    /**
     * Deletes all of a project's modules.
     *
     * @@param	  connection    the JDBC database connection
     * @@param	  domainID     the ID of the project to clear out
     * @@exception SQLException
     * @@exception DataSetException
     */
    public void  deleteVCsForProject (Connection connection, String 
                                          domainID)
	throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
    {
        String action = "Deleting VCs for Project: ";       

        String moduleName = null;
	TableDataSet modules = null;
	try
	{
            KeyDef kd = new KeyDef().addAttrib(VIEW_ID);
	    modules = new TableDataSet(connection, TABLE_NAME,kd);
	    modules.where(buildWhereClause(null, domainID, null));
	    modules.fetchRecords();
            for (int i = 0; i < modules.size(); i++)
	    {
                Record r = modules.getRecord(i);
                moduleName = r.getValue(VCViewManager.NAME).toString();
                r.markToBeDeleted();
                r.save();

                Project p = new Project(connection, getDataManager(),
                                        domainID);
                TigrisUpdate.deleteModule(connection, getDataManager(), null,
                                          p, moduleName);
	    }
	}
	catch (DataSetException e)
	{
	    log.exception(e);
	    if (debug) log.error(action + e.getMessage());
	    throw e;
	}
	catch (SQLException e)
	{
	    log.exception(e);
	    if (debug) log.error(action + e.getMessage());
	    throw e;
	}
	finally
	{
	    registerToClose(modules);
	}
    }

    /**
     *  Retrieves a module with known ID from the database.
     *
     * @@param	connection    A JDBC database connection
     * @@param	moduleID       The moduleID to obtain 
     * @@return	 a Record
     * @@exception	SQLException
     * @@exception	DataSetException
     */
    public Record getVC (Connection connection, String viewID)
	throws SQLException, DataSetException, JoistDatabaseException
    {
        TableDataSet views = getVCs(connection, viewID, null, null);
        if (views != null && views.containsRecord(0))
        {
            return views.getRecord(0);
        }
        else
        {
            throw new JoistDatabaseException ("No such module: " + viewID);
        }
    }

    /**
     * Retrieves the ID for a module with known name
     *
     * @@param	connection    A JDBC database connection
     * @@param	moduleName     The module name to identify
     * @@return	 a String
     * @@exception	SQLException
     * @@exception	DataSetException
     */
    public String getVCID (Connection connection, String moduleName)
	throws SQLException, DataSetException, JoistDatabaseException
    {
        TableDataSet views = getVCs(connection, null, null, moduleName);
            
        if (views.containsRecord(0))
        {
            return views.getRecord(0).getValue(VIEW_ID).asString();
        }
        else
        {
            throw new JoistDatabaseException ("No such module: " + moduleName);
        }
    }

    /**
     *  Retrieves a list of views for a given project
     *
     * @@param	connection    A JDBC database connection
     * @@param   domainID     A project ID
     * @@return	 a DataSet
     * @@exception	SQLException
     * @@exception	DataSetException
     */
    public final DataSet getVCsForProject (Connection connection, 
                                               String domainID)
	throws SQLException, DataSetException
    {
        return getVCs(connection, null, domainID, null);
    }    

    /**
     *  Retrieves views by any of their fields
     *
     * @@param	connection    A JDBC database connection
     * @@param   moduleID      A module ID (can be null)
     * @@param   domainID     A project ID (can be null)
     * @@param   name          A module name (can be null)
     * @@param   description   A module description (can be null)
     * @@param   isWeb         "Y", "N", or null
     * @@return	 a DataSet
     * @@exception	SQLException
     * @@exception	DataSetException
     */
    public TableDataSet getVCs (Connection connection, String viewID, 
                                String domainID, String name)
        throws SQLException, DataSetException
    {
        TableDataSet views = null;

        try
        {
            views = new TableDataSet(connection, TABLE_NAME);
            views.where(buildWhereClause(viewID, domainID, name));
            views.fetchRecords();
	}
	catch (DataSetException e)
	{
	    log.exception(e);
	    if (debug) log.error("Getting VC: " + e.getMessage());
	    throw e;
	}
	catch (SQLException e)
	{
	    log.exception(e);
	    if (debug) log.error("Getting VC: " + e.getMessage());
	    throw e;
	}
	finally
	{
	    registerToClose(views);
	}

        return views;
    }

    /**
     * Creates a SQL <code>where</code> clause.
     */
    protected static String buildWhereClause (String viewID, 
                                              String domainID,
                                              String name)
    {
        StringBuffer whereClause = new StringBuffer();
        boolean firstClause = true;
        
        if (viewID != null)
        {
            whereClause.append(VIEW_ID).append('=').append(viewID);
            firstClause = false;
        }

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

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

        return whereClause.toString();
    }
}
@


1.7
log
@readding all the files as part of the process of returning to the way
things were as of tuesday
@
text
@d70 1
a70 1
 * @@version $Id: VCViewManager.java,v 1.1 2000/12/15 00:31:03 dlr Exp $
@


1.7.2.1
log
@Porting to the Turbine Peer system.  -dlr and leonardr
@
text
@d70 1
a70 1
 * @@version $Id: VCViewManager.java,v 1.7 2001/01/26 18:29:00 edk Exp $
@


1.6
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
@d70 1
a70 1
 * @@version $Id: VCViewManager.java,v 1.5 2001/01/25 02:57:26 stack Exp $
d113 1
a113 1
        super(dataMgr, new KeyDef().addAttrib(VIEW_ID).addAttrib(DOMAIN_ID));
d133 2
a134 1
     * @@exception DBException
d138 1
a138 1
	throws DBException
d172 3
a174 1
	    throw new DBException(e);
d178 3
a180 1
	    throw new DBException(e);
d195 1
a195 2
     *
     * @@exception DBException
d198 2
a199 1
	throws DoesNotExist, DBException
d201 1
d211 3
a213 2
            Project project = new Project(getDataManager(), domainID);
            TigrisUpdate.deleteModule(getDataManager(), null, 
d218 3
a220 1
	    throw new DBException(e);
d224 3
a226 1
	    throw new DBException(e);
d235 2
a236 2
     *
     * @@exception DBException
d240 2
a241 1
	throws DoesNotExist, DBException
d243 2
d260 3
a262 2
                Project p = new Project(getDataManager(), domainID);
                TigrisUpdate.deleteModule(getDataManager(), null,
d268 3
a270 1
	    throw new DBException(e);
d274 3
a276 1
	    throw new DBException(e);
d294 1
a294 1
	throws DBException
d299 1
a299 13
            Record record = null;

            try 
            { 
                record = views.getRecord(0); 
            }

            catch (DataSetException dse)
            { 
                throw new DBException( dse ); 
            }

            return record;
d303 1
a303 1
            throw new DBException ("No such module: " + viewID);
d313 2
a314 2
     *
     * @@exception DBException
d317 1
a317 1
	throws DBException
d323 1
a323 10
            String str = null;
            try
            {
                str = views.getRecord(0).getValue(VIEW_ID).asString(); 
            }
            catch (DataSetException dse)
            {
                throw new DBException( dse );
            }
            return str;
d327 1
a327 1
            throw new DBException ("No such module: " + moduleName);
d337 2
a338 2
     *
     * @@exception DBException
d341 2
a342 2
                                           String domainID)
	throws DBException
d357 2
a358 2
     *
     * @@exception DBException
d362 1
a362 1
        throws DBException
d374 3
a376 1
	    throw new DBException(e);
d380 3
a382 1
	    throw new DBException(e);
@


1.5
log
@TigrisUpdate.deleteModule no longer takes a connection. Fixed
@
text
@d70 1
a70 1
 * @@version $Id: VCViewManager.java,v 1.4 2001/01/25 02:46:16 stack Exp $
@


1.4
log
@Reconciled calls to new Project to match changed Project constructor signature.  Converted DataSetExceptions to DBException
@
text
@d70 1
a70 1
 * @@version $Id: VCViewManager.java,v 1.3 2001/01/24 22:24:36 leonardr Exp $
d206 1
a206 1
            TigrisUpdate.deleteModule(connection, getDataManager(), null, 
d247 1
a247 1
                TigrisUpdate.deleteModule(connection, getDataManager(), null,
@


1.3
log
@Moved TableManagers to new keydef constructor and keydef-less runSelect.
@
text
@d70 1
a70 1
 * @@version $Id: VCViewManager.java,v 1.2 2001/01/24 20:42:46 dlr Exp $
d205 1
a205 2
            Project project = new Project(connection, getDataManager(),
                                          domainID);
d246 1
a246 2
                Project p = new Project(connection, getDataManager(),
                                        domainID);
d280 13
a292 1
            return views.getRecord(0);
d316 10
a325 1
            return views.getRecord(0).getValue(VIEW_ID).asString();
@


1.2
log
@Making use of DBException.
@
text
@d70 1
a70 1
 * @@version $Id: VCViewManager.java,v 1.1 2000/12/15 00:31:03 dlr Exp $
d113 1
a113 1
        super(dataMgr);
@


1.1
log
@Manages views of version control modules.
@
text
@d70 1
a70 1
 * @@version $Id: $
d133 1
a133 2
     * @@exception SQLException
     * @@exception DataSetException
d137 1
a137 1
	throws SQLException, DataSetException
d171 1
a171 3
	    log.exception(e);
	    if (debug) log.warning(action + e.getMessage());
	    throw e;
d175 1
a175 3
	    log.exception(e);
	    if (debug) log.warning(action + e.getMessage());
	    throw e;
d190 2
a191 1
     * @@exception SQLException
d194 1
a194 2
	throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
a195 1
        String action = "Deleting module: ";
d212 1
a212 3
	    log.exception(e);
	    if (debug) log.error(action + e.getMessage());
	    throw e;
d216 1
a216 3
	    log.exception(e);
	    if (debug) log.error(action + e.getMessage());
	    throw e;
d225 2
a226 2
     * @@exception SQLException
     * @@exception DataSetException
d230 1
a230 2
	throws SQLException, DataSetException, JoistDatabaseException,
               DoesNotExist
a231 2
        String action = "Deleting VCs for Project: ";       

d255 1
a255 3
	    log.exception(e);
	    if (debug) log.error(action + e.getMessage());
	    throw e;
d259 1
a259 3
	    log.exception(e);
	    if (debug) log.error(action + e.getMessage());
	    throw e;
d277 1
a277 1
	throws SQLException, DataSetException, JoistDatabaseException
d286 1
a286 1
            throw new JoistDatabaseException ("No such module: " + viewID);
d296 2
a297 2
     * @@exception	SQLException
     * @@exception	DataSetException
d300 1
a300 1
	throws SQLException, DataSetException, JoistDatabaseException
d310 1
a310 1
            throw new JoistDatabaseException ("No such module: " + moduleName);
d320 2
a321 2
     * @@exception	SQLException
     * @@exception	DataSetException
d324 2
a325 2
                                               String domainID)
	throws SQLException, DataSetException
d340 2
a341 2
     * @@exception	SQLException
     * @@exception	DataSetException
d345 1
a345 1
        throws SQLException, DataSetException
d357 1
a357 3
	    log.exception(e);
	    if (debug) log.error("Getting VC: " + e.getMessage());
	    throw e;
d361 1
a361 3
	    log.exception(e);
	    if (debug) log.error("Getting VC: " + e.getMessage());
	    throw e;
@

