head	1.10;
access;
symbols
	TIGRIS_1_0_5:1.7.6.1
	TIGRIS_1_0_5RC6:1.7.6.1
	TIGRIS_1_0_5RC5:1.7.6.1
	TIGRIS_1_0_5RC4:1.7.6.1
	TIGRIS_1_0_5RC3:1.7.6.1
	TIGRIS_1_0_5RC2:1.7.6.1
	TIGRIS_1_0_5RC1:1.7.6.1
	TIGRIS_1_0_4:1.7.6.1
	TIGRIS_1_0_3:1.7.6.1
	TIGRIS_1_0_2:1.7.6.1
	TIGRIS_1_0_1:1.7
	TIGRIS_1_0:1.7.0.6
	TIGRIS_1_0_0:1.7
	TIGRIS_1_0_0_RC1:1.7.0.2
	dlr:1.1.1
	HELM_PEER_PORT_BRANCH:1.1.1.1;
locks; strict;
comment	@# @;


1.10
date	2001.05.17.14.51.17;	author leonardr;	state dead;
branches;
next	1.9;

1.9
date	2001.04.19.01.18.00;	author leonardr;	state Exp;
branches;
next	1.8;

1.8
date	2001.04.19.01.04.38;	author leonardr;	state Exp;
branches;
next	1.7;

1.7
date	2001.03.10.01.55.02;	author leonardr;	state Exp;
branches
	1.7.6.1;
next	1.6;

1.6
date	2001.03.09.01.54.31;	author leonardr;	state Exp;
branches;
next	1.5;

1.5
date	2001.03.09.00.28.00;	author leonardr;	state Exp;
branches;
next	1.4;

1.4
date	2001.03.07.01.53.28;	author lucas;	state Exp;
branches;
next	1.3;

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

1.2
date	2001.02.21.03.02.30;	author edk;	state Exp;
branches;
next	1.1;

1.1
date	2001.02.05.16.39.42;	author leonardr;	state dead;
branches
	1.1.1.1
	1.1.2.1;
next	;

1.1.1.1
date	2001.02.21.00.35.38;	author dlr;	state Exp;
branches;
next	;

1.1.2.1
date	2001.02.05.16.39.42;	author leonardr;	state Exp;
branches;
next	1.1.2.2;

1.1.2.2
date	2001.02.05.17.04.10;	author leonardr;	state Exp;
branches;
next	1.1.2.3;

1.1.2.3
date	2001.02.15.20.50.46;	author leonardr;	state Exp;
branches;
next	1.1.2.4;

1.1.2.4
date	2001.02.20.04.24.37;	author dlr;	state Exp;
branches;
next	;

1.7.6.1
date	2001.04.24.16.07.53;	author leonardr;	state Exp;
branches;
next	1.7.6.2;

1.7.6.2
date	2001.05.24.23.31.10;	author leonardr;	state dead;
branches;
next	;


desc
@@


1.10
log
@Refactored a lot of code to get rid of the good-looking but very slow IssueTrackingAccount{Descriptor,Map} classes. Also cleaned up the script-running interface quite a bit.
@
text
@/* ================================================================
 * Copyright (c) 2000-2001 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.tool.task;

import java.io.*;
import java.net.*;

import java.util.*;

import org.apache.turbine.util.Log;

import org.joist.database.*;

import org.tigris.helm.om.*;
import org.tigris.helm.om.UserPeer;
/**
 * IssueTrackingAccountDescriptor is a convenience class used for determining
 * which scripts to run for a set of projects. It maps Projects to
 * IssueTrackingAccountMaps.
 * <p>
 * It looks like:
 * <ul>
 *  <li>Project1 -> IssueTrackingAccountMap1</li>
 *  <li>Project2 -> IssueTrackingAccountMap2</li>
 *  <li>Project3 -> IssueTrackingAccountMap3</li>
 * </ul>
 */
public class IssueTrackingAccountDescriptor extends HashMap
{
    private static final boolean DEBUG = false;

    private Map accountMaps;

    /**
     * Construct an IssueTrackingAccountDescriptor representing a blank slate.
     */
    public IssueTrackingAccountDescriptor()
    {
    }

    /**
     * Construct an IssueTrackingAccountDescriptor for the given project and 
     * all the users who can see that project.
     */
    public IssueTrackingAccountDescriptor(Project project)
        throws Exception
    {
        //All users in this domain, and all users in the host domain
        //are affected.
	List users = new ArrayList();
        users.addAll(project.getDomain().getHost().getSpecialUser().expand());
        users.addAll(project.getDomain().getSpecialUser().expand());

	buildDescriptor(project.expand(), users);
    }

    /**
     * Construct an IssueTrackingAccountDescriptor for the given projects and 
     * user.
     */
    public IssueTrackingAccountDescriptor(List projects, User user)
        throws Exception
    {
	buildDescriptor(projects, user.expand());
    }


    /**
     * Construct an IssueTrackingAccountDescriptor for the given project and 
     * user.
     *
     * @@param project
     * @@param user
     */
    public IssueTrackingAccountDescriptor(Project project, User user)
        throws Exception
    {
	List projects = project.expand();
	List users = user.expand();
	buildDescriptor(projects, users);
    }
    
    /**
     * Construct an IssueTrackingAccountDescriptor for the given projects and
     * users.
     *
     * @@param projects
     * @@param users
     */
    public IssueTrackingAccountDescriptor(List projects, List users)
        throws Exception
    {
	buildDescriptor(projects, users);
    }

    /**
     * Builds a descriptor.
     *
     * @@param projects
     * @@param users
     */
    private void buildDescriptor(List projects, List users)
        throws Exception
    {
        //Expand out all the projects and users first.
        List expandedProjects = new ArrayList();
        for (Iterator i = projects.iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            expandedProjects.add(p);
            if (!p.isBase())
            {
                expandedProjects.addAll(p.expand());
            }
        }
        List expandedUsers = new ArrayList();
        for (Iterator i = users.iterator(); i.hasNext();)
        {
            User u = (User)i.next();
            expandedUsers.addAll(u.expand());
        }
        
        StringBuffer s = new StringBuffer();
        if (DEBUG)
        {
            s.append("\nI am building a descriptor.\n")
                .append(" ").append(expandedProjects.size()).append(" projects, ")
                .append(expandedUsers.size()).append(" users.");
            Log.debug(s.toString());
        }

        for (Iterator i = expandedUsers.iterator(); i.hasNext();)
        {
            User u = (User)i.next();
            for (Iterator j = expandedProjects.iterator(); j.hasNext();)
            {
                Project p = (Project)j.next();
                IssueTrackingAccountMap m = (IssueTrackingAccountMap)get(p);
                if (m == null)
                {
                    m = new IssueTrackingAccountMap(p);
                }
                m.registerAccountForUser(u);
                put(p,m);
            }
        }
    }

    /**
     * Returns a string describing this descriptor which can be dumped
     * to the log.  
     */
    public String dump()
    {
	StringBuffer s = new StringBuffer();
        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            s.append(" Dump of IssueTrackingAccountMap for project ")
                .append(p.getName()).append('\n');
            IssueTrackingAccountMap m = (IssueTrackingAccountMap)get(p);
            s.append(m.dump());
        }
	return s.toString();
    }


    /**
     * Runs whatever scripts are neccessary to bridge the delta
     * between this IssueTrackingAccountDescriptor and another
     * (assumed to be more recent).   
     */
    public void syncTo(IssueTrackingAccountDescriptor newDesc)
        throws Exception
    {
        //HACK; this method should not be neccessary
        syncTo(newDesc, UserPeer.getSuperUser());
    }

    /**
     * Runs whatever scripts are neccessary to bridge the delta
     * between this IssueTrackingAccountDescriptor and another
     * (assumed to be more recent). Also takes a User object
     * representing the user who initiated this action.  
     */
    public void syncTo(IssueTrackingAccountDescriptor newDesc, User onBehalfOf)
        throws Exception
    {
        if (DEBUG)
        {
            StringBuffer s = new StringBuffer();
            s.append("Syncing descriptors.\n").append("Old descriptor:")
                .append(dump())
                .append("\nNew descriptor:").append(newDesc.dump());
            Log.debug(s.toString());
        }

        //Make sure the old descriptor has all the projects in the 
        //new descriptor.
        for (Iterator i = newDesc.keySet().iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            if (!containsProject(p))
            {
                IssueTrackingAccountMap m = new IssueTrackingAccountMap(p);
                put(p,m);
            }            
        } 

        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            IssueTrackingAccountMap map1 =  (IssueTrackingAccountMap)get(p);
            IssueTrackingAccountMap map2 = newDesc.getMapForProject(p);
	    if (DEBUG)
            {
                Log.debug("Syncing maps for project " + p.getName());
            }
	    map1.syncTo(map2, onBehalfOf);
        }
    }

    public boolean containsProject(Project p)
    {
        boolean c = false;
        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p2 = (Project)i.next();
            if (p.equals(p2))
            {
                c = true;
                break;
            }
        }
        return c;
    }

    public IssueTrackingAccountMap getMapForProject(Project p)
    {
        IssueTrackingAccountMap m = null;
        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p2 = (Project)i.next();
            if (p.equals(p2))
            {
                m = (IssueTrackingAccountMap)get(p2);
                break;
            }
        }
        return m;
    }

}
@


1.9
log
@The developer's curse: forgetting to change DEBUG back to false.
@
text
@@


1.8
log
@Because large sites will have more users than there are slots in the ACL cache, iterate over users and then projects (instead of projects and then users) when building account descriptors. This way, an ACL gets made once and used as often as it's ever going to be used in this rebuild, and it's not a big deal if subsequent ACLs bump it out of the cache.
@
text
@d74 1
a74 1
    private static final boolean DEBUG = true;
@


1.7
log
@Fixed problem with duplicate projects showing up.
@
text
@d74 3
a76 1
    private static final boolean DEBUG = false;
d176 1
a176 1
        for (Iterator i = expandedProjects.iterator(); i.hasNext();)
d178 13
a190 5
            Project p = (Project)i.next();
            IssueTrackingAccountMap m =
                new IssueTrackingAccountMap(p, expandedUsers);
            put(p,m);
        }        
@


1.7.6.1
log
@Backported changes to prevent ACL cache thrashing on sites with large numbers of users.
@
text
@a75 2
    private Map accountMaps;

d174 1
a174 1
        for (Iterator i = expandedUsers.iterator(); i.hasNext();)
d176 5
a180 13
            User u = (User)i.next();
            for (Iterator j = expandedProjects.iterator(); j.hasNext();)
            {
                Project p = (Project)j.next();
                IssueTrackingAccountMap m = (IssueTrackingAccountMap)get(p);
                if (m == null)
                {
                    m = new IssueTrackingAccountMap(p);
                }
                m.registerAccountForUser(u);
                put(p,m);
            }
        }
@


1.7.6.2
log
@Ported task running system in from HEAD.
@
text
@@


1.6
log
@Only you can prevent log bloat.
@
text
@d152 5
a156 1
            expandedProjects.addAll(p.expand());
a157 1

d164 1
a164 1

d171 1
a180 5

        if (DEBUG)
        {
            Log.debug(s.toString());
        }
@


1.5
log
@Corrected javadoc.
@
text
@d74 1
a74 1
    private static final boolean DEBUG = true;
d163 6
a168 3
        s.append("\nI am building a descriptor.\n")
            .append(" ").append(expandedProjects.size()).append(" projects, ")
            .append(expandedUsers.size()).append(" users.");
a172 1
            //s.append(p.getName()).append(" ");
d178 1
a178 2
        /*s.append("\n ").append(users.size()).append(" Users. ");
        for (Iterator i = users.iterator(); i.hasNext();)
d180 2
a181 5
            User u = (User)i.next();
            s.append(u.getName()).append(" ");
            }*/

        Log.debug(s.toString());
d224 8
a231 5
        StringBuffer s = new StringBuffer();
        s.append("Syncing descriptors.\n").append("Old descriptor:")
            .append(dump())
            .append("\nNew descriptor:").append(newDesc.dump());
        Log.debug(s.toString());
d250 4
a253 1
	    Log.debug("Syncing maps for project " + p.getName());
@


1.4
log
@Corrected regex madness for Torque upgrade.  -dlr
@
text
@d85 1
a85 1
     * user.
@


1.3
log
@Almost got rid of dependance on hacky one-argument IssueTrackingAccountDescriptor.syncto().
@
text
@d59 1
a59 2
import org.tigris.helm.om.peer.UserPeer;

@


1.2
log
@adding files from the branch to the head
@
text
@d59 1
d205 1
d209 1
a209 1
     * (assumed to be more recent).  
d214 2
a215 1
        syncTo(newDesc, null);
@


1.1
log
@file IssueTrackingAccountDescriptor.java was initially added on branch HELM_PEER_PORT_BRANCH.
@
text
@d1 282
@


1.1.1.1
log
@Replacing the HEAD with the HELM_PEER_PORT_BRANCH.
@
text
@a0 282
/* ================================================================
 * Copyright (c) 2000-2001 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.tool.task;

import java.io.*;
import java.net.*;

import java.util.*;

import org.apache.turbine.util.Log;

import org.joist.database.*;

import org.tigris.helm.om.*;

/**
 * IssueTrackingAccountDescriptor is a convenience class used for determining
 * which scripts to run for a set of projects. It maps Projects to
 * IssueTrackingAccountMaps.
 * <p>
 * It looks like:
 * <ul>
 *  <li>Project1 -> IssueTrackingAccountMap1</li>
 *  <li>Project2 -> IssueTrackingAccountMap2</li>
 *  <li>Project3 -> IssueTrackingAccountMap3</li>
 * </ul>
 */
public class IssueTrackingAccountDescriptor extends HashMap
{
    private static final boolean DEBUG = true;

    /**
     * Construct an IssueTrackingAccountDescriptor representing a blank slate.
     */
    public IssueTrackingAccountDescriptor()
    {
    }

    /**
     * Construct an IssueTrackingAccountDescriptor for the given project and 
     * user.
     */
    public IssueTrackingAccountDescriptor(Project project)
        throws Exception
    {
        //All users in this domain, and all users in the host domain
        //are affected.
	List users = new ArrayList();
        users.addAll(project.getDomain().getHost().getSpecialUser().expand());
        users.addAll(project.getDomain().getSpecialUser().expand());

	buildDescriptor(project.expand(), users);
    }

    /**
     * Construct an IssueTrackingAccountDescriptor for the given projects and 
     * user.
     */
    public IssueTrackingAccountDescriptor(List projects, User user)
        throws Exception
    {
	buildDescriptor(projects, user.expand());
    }


    /**
     * Construct an IssueTrackingAccountDescriptor for the given project and 
     * user.
     *
     * @@param project
     * @@param user
     */
    public IssueTrackingAccountDescriptor(Project project, User user)
        throws Exception
    {
	List projects = project.expand();
	List users = user.expand();
	buildDescriptor(projects, users);
    }
    
    /**
     * Construct an IssueTrackingAccountDescriptor for the given projects and
     * users.
     *
     * @@param projects
     * @@param users
     */
    public IssueTrackingAccountDescriptor(List projects, List users)
        throws Exception
    {
	buildDescriptor(projects, users);
    }

    /**
     * Builds a descriptor.
     *
     * @@param projects
     * @@param users
     */
    private void buildDescriptor(List projects, List users)
        throws Exception
    {
        //Expand out all the projects and users first.
        List expandedProjects = new ArrayList();
        for (Iterator i = projects.iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            expandedProjects.addAll(p.expand());
        }

        List expandedUsers = new ArrayList();
        for (Iterator i = users.iterator(); i.hasNext();)
        {
            User u = (User)i.next();
            expandedUsers.addAll(u.expand());
        }

        StringBuffer s = new StringBuffer();
        s.append("\nI am building a descriptor.\n")
            .append(" ").append(expandedProjects.size()).append(" projects, ")
            .append(expandedUsers.size()).append(" users.");

        for (Iterator i = expandedProjects.iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            //s.append(p.getName()).append(" ");
            IssueTrackingAccountMap m =
                new IssueTrackingAccountMap(p, expandedUsers);
            put(p,m);
        }        

        /*s.append("\n ").append(users.size()).append(" Users. ");
        for (Iterator i = users.iterator(); i.hasNext();)
        {
            User u = (User)i.next();
            s.append(u.getName()).append(" ");
            }*/

        Log.debug(s.toString());
    }

    /**
     * Returns a string describing this descriptor which can be dumped
     * to the log.  
     */
    public String dump()
    {
	StringBuffer s = new StringBuffer();
        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            s.append(" Dump of IssueTrackingAccountMap for project ")
                .append(p.getName()).append('\n');
            IssueTrackingAccountMap m = (IssueTrackingAccountMap)get(p);
            s.append(m.dump());
        }
	return s.toString();
    }

    /**
     * Runs whatever scripts are neccessary to bridge the delta
     * between this IssueTrackingAccountDescriptor and another
     * (assumed to be more recent).  
     */
    public void syncTo(IssueTrackingAccountDescriptor newDesc)
        throws Exception
    {
        syncTo(newDesc, null);
    }

    /**
     * Runs whatever scripts are neccessary to bridge the delta
     * between this IssueTrackingAccountDescriptor and another
     * (assumed to be more recent). Also takes a User object
     * representing the user who initiated this action.  
     */
    public void syncTo(IssueTrackingAccountDescriptor newDesc, User onBehalfOf)
        throws Exception
    {
        StringBuffer s = new StringBuffer();
        s.append("Syncing descriptors.\n").append("Old descriptor:")
            .append(dump())
            .append("\nNew descriptor:").append(newDesc.dump());
        Log.debug(s.toString());

        //Make sure the old descriptor has all the projects in the 
        //new descriptor.
        for (Iterator i = newDesc.keySet().iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            if (!containsProject(p))
            {
                IssueTrackingAccountMap m = new IssueTrackingAccountMap(p);
                put(p,m);
            }            
        } 

        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            IssueTrackingAccountMap map1 =  (IssueTrackingAccountMap)get(p);
            IssueTrackingAccountMap map2 = newDesc.getMapForProject(p);
	    Log.debug("Syncing maps for project " + p.getName());
	    map1.syncTo(map2, onBehalfOf);
        }
    }

    public boolean containsProject(Project p)
    {
        boolean c = false;
        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p2 = (Project)i.next();
            if (p.equals(p2))
            {
                c = true;
                break;
            }
        }
        return c;
    }

    public IssueTrackingAccountMap getMapForProject(Project p)
    {
        IssueTrackingAccountMap m = null;
        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p2 = (Project)i.next();
            if (p.equals(p2))
            {
                m = (IssueTrackingAccountMap)get(p2);
                break;
            }
        }
        return m;
    }

}
@


1.1.2.1
log
@Ported IssueTrackingAccount* tool classes.
@
text
@a0 269
/* ================================================================
 * 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.tool.task;

import java.io.*;
import java.net.*;

import java.util.*;

import org.apache.turbine.util.Log;

import org.joist.database.*;

import org.tigris.helm.om.*;
import org.tigris.helm.DoesNotExist;

/**
 * IssueTrackingAccountDescriptor is a convenience class used for determining
 * which scripts to run for a set of projects. It maps Projects to
 * IssueTrackingAccountMaps.
 *
 * It looks like:
 *  Project1 -> IssueTrackingAccountMap1
 *  Project2 -> IssueTrackingAccountMap2
 *  Project3 -> IssueTrackingAccountMap3
 *  
 */
public class IssueTrackingAccountDescriptor extends HashMap
{

    static private boolean DEBUG = true;

    /**
     * Construct an IssueTrackingAccountDescriptor representing a blank slate.
     */
    public IssueTrackingAccountDescriptor()
    {
    }

    /**
     * Construct an IssueTrackingAccountDescriptor for the given project and 
     * user.
     */
    public IssueTrackingAccountDescriptor(Project project)
        throws Exception, DoesNotExist
    {
        //All users in this domain, and all users in the host domain
        //are affected.
	List users = new ArrayList();
        users.addAll(project.getDomain().getHost().getSpecialUser().expand());
        users.addAll(project.getDomain().getSpecialUser().expand());

	buildDescriptor(project.expand(), users);
    }

    /**
     * Construct an IssueTrackingAccountDescriptor for the given projects and 
     * user.
     */
    public IssueTrackingAccountDescriptor(List projects, User user)
        throws Exception, DoesNotExist
    {
	buildDescriptor(projects, user.expand());
    }


    /**
     * Construct an IssueTrackingAccountDescriptor for the given project and 
     * user.
     */
    public IssueTrackingAccountDescriptor(Project project, User user)
        throws Exception, DoesNotExist
    {
	List projects = project.expand();
	List users = user.expand();
	buildDescriptor(projects, users);
    }
    
    /**
     * Construct an IssueTrackingAccountDescriptor for the given projects and
     * users.
     */
    public IssueTrackingAccountDescriptor(List projects, List users)
        throws Exception, DoesNotExist
    {
	buildDescriptor(projects, users);
    }

    private void buildDescriptor(List projects, List users)
        throws Exception, DoesNotExist
    {
        //Expand out all the projects and users first.
        List expandedProjects = new ArrayList();
        for (Iterator i = projects.iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            expandedProjects.addAll(p.expand());
        }

        List expandedUsers = new ArrayList();
        for (Iterator i = users.iterator(); i.hasNext();)
        {
            User u = (User)i.next();
            expandedUsers.addAll(u.expand());
        }

        StringBuffer s = new StringBuffer();
        s.append("\nI am building a descriptor.\n")
            .append(" ").append(expandedProjects.size()).append(" projects, ")
            .append(expandedUsers.size()).append(" users.");

        for (Iterator i = expandedProjects.iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            //s.append(p.getName()).append(" ");
            IssueTrackingAccountMap m = new IssueTrackingAccountMap(p, expandedUsers);
            put(p,m);
        }        

        /*s.append("\n ").append(users.size()).append(" Users. ");
        for (Iterator i = users.iterator(); i.hasNext();)
        {
            User u = (User)i.next();
            s.append(u.getName()).append(" ");
            }*/

        Log.debug(s.toString());
    }

    /**
     * Returns a string describing this descriptor which can be dumped
     * to the log.  
     */
    public String dump()
    {
	StringBuffer s = new StringBuffer();
        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            s.append(" Dump of IssueTrackingAccountMap for project " + p.getName() + "\n");
            IssueTrackingAccountMap m = (IssueTrackingAccountMap)get(p);
            s.append(m.dump());
        }
	return s.toString();
    }

    /**
     * Runs whatever scripts are neccessary to bridge the delta
     * between this IssueTrackingAccountDescriptor and another
     * (assumed to be more recent).  
     */
    public void syncTo(IssueTrackingAccountDescriptor newDesc)
        throws Exception
    {
        syncTo(newDesc, null);
    }

    /**
     * Runs whatever scripts are neccessary to bridge the delta
     * between this IssueTrackingAccountDescriptor and another
     * (assumed to be more recent). Also takes a User object
     * representing the user who initiated this action.  
     */
    public void syncTo(IssueTrackingAccountDescriptor newDesc, User onBehalfOf)
        throws Exception
    {
        StringBuffer s = new StringBuffer();
        s.append("Syncing descriptors.\n").append("Old descriptor:")
            .append(dump())
            .append("\nNew descriptor:").append(newDesc.dump());
        Log.debug(s.toString());

        //Make sure the old descriptor has all the projects in the 
        //new descriptor.
        for (Iterator i = newDesc.keySet().iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            if (!containsProject(p))
            {
                IssueTrackingAccountMap m = new IssueTrackingAccountMap(p);
                put(p,m);
            }            
        } 

        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            IssueTrackingAccountMap map1 =  (IssueTrackingAccountMap)get(p);
            IssueTrackingAccountMap map2 = newDesc.getMapForProject(p);
	    Log.debug("Syncing maps for project " + p.getName());
	    map1.syncTo(map2, onBehalfOf);
        }
    }

    public boolean containsProject(Project p)
    {
        boolean c = false;
        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p2 = (Project)i.next();
            if (p.equals(p2))
            {
                c = true;
                break;
            }
        }
        return c;
    }

    public IssueTrackingAccountMap getMapForProject(Project p)
    {
        IssueTrackingAccountMap m = null;
        for (Iterator i = keySet().iterator(); i.hasNext();)
        {
            Project p2 = (Project)i.next();
            if (p.equals(p2))
            {
                m = (IssueTrackingAccountMap)get(p2);
                break;
            }
        }
        return m;
    }

}
@


1.1.2.2
log
@Removed an exception that never gets thrown anymore.
@
text
@d59 1
d89 1
a89 1
        throws Exception
d105 1
a105 1
        throws Exception
d116 1
a116 1
        throws Exception
d128 1
a128 1
        throws Exception
d134 1
a134 1
        throws Exception
@


1.1.2.3
log
@Normalized copyright dates.
@
text
@d2 1
a2 1
 * Copyright (c) 2000-2001 CollabNet.  All rights reserved.
@


1.1.2.4
log
@Cleanup.
@
text
@d64 1
a64 1
 * <p>
d66 4
a69 5
 * <ul>
 *  <li>Project1 -> IssueTrackingAccountMap1</li>
 *  <li>Project2 -> IssueTrackingAccountMap2</li>
 *  <li>Project3 -> IssueTrackingAccountMap3</li>
 * </ul>
d73 2
a74 1
    private static final boolean DEBUG = true;
a112 3
     *
     * @@param project
     * @@param user
a124 3
     *
     * @@param projects
     * @@param users
a131 6
    /**
     * Builds a descriptor.
     *
     * @@param projects
     * @@param users
     */
d159 1
a159 2
            IssueTrackingAccountMap m =
                new IssueTrackingAccountMap(p, expandedUsers);
d183 1
a183 2
            s.append(" Dump of IssueTrackingAccountMap for project ")
                .append(p.getName()).append('\n');
@


