head	1.21;
access;
symbols
	TIGRIS_1_1_0RC2:1.12.6.3
	TIGRIS_1_1_0RC1:1.12.6.3
	TIGRIS_1_1:1.12.6.3.0.2
	TIGRIS_1_0_8:1.12.6.3
	TIGRIS_1_0_8RC3:1.12.6.3
	TIGRIS_1_0_8RC2:1.12.6.3
	TIGRIS_1_0_8RC1:1.12.6.3
	TIGRIS_1_0_7:1.12.6.3
	TIGRIS_1_0_7RC3:1.12.6.3
	TIGRIS_1_0_7RC2:1.12.6.3
	TIGRIS_1_0_7RC1:1.12.6.3
	TIGRIS_1_0_6:1.12.6.2
	TIGRIS_1_0_6RC5:1.12.6.2
	TIGRIS_1_0_6RC4:1.12.6.1
	TIGRIS_1_0_6RC3:1.12
	TIGRIS_1_0_6RC2:1.12
	TIGRIS_1_0_6RC1:1.12
	TIGRIS_1_0_5:1.12
	TIGRIS_1_0_5RC6:1.12
	TIGRIS_1_0_5RC5:1.12
	TIGRIS_1_0_5RC4:1.12
	TIGRIS_1_0_5RC3:1.12
	TIGRIS_1_0_5RC2:1.12
	TIGRIS_1_0_5RC1:1.12
	TIGRIS_1_0_4:1.12
	TIGRIS_1_0_3:1.12
	TIGRIS_1_0_2:1.12
	TIGRIS_1_0_1:1.12
	TIGRIS_1_0:1.12.0.6
	TIGRIS_1_0_0:1.12
	TIGRIS_1_0_0_RC1:1.12.0.2
	dlr:1.1.1
	HELM_PEER_PORT_BRANCH:1.1.1.1;
locks; strict;
comment	@# @;


1.21
date	2001.08.11.09.08.02;	author dlr;	state Exp;
branches;
next	1.20;

1.20
date	2001.07.29.23.13.05;	author edk;	state Exp;
branches;
next	1.19;

1.19
date	2001.07.19.18.42.22;	author dlr;	state Exp;
branches;
next	1.18;

1.18
date	2001.06.18.22.12.20;	author leonardr;	state Exp;
branches;
next	1.17;

1.17
date	2001.06.14.20.23.01;	author leonardr;	state Exp;
branches;
next	1.16;

1.16
date	2001.06.14.04.42.57;	author edk;	state Exp;
branches;
next	1.15;

1.15
date	2001.06.14.00.06.55;	author leonardr;	state Exp;
branches;
next	1.14;

1.14
date	2001.06.14.00.04.45;	author leonardr;	state Exp;
branches;
next	1.13;

1.13
date	2001.05.23.01.50.13;	author dlr;	state Exp;
branches;
next	1.12;

1.12
date	2001.03.14.00.04.13;	author leonardr;	state Exp;
branches
	1.12.6.1;
next	1.11;

1.11
date	2001.03.12.22.47.54;	author dlr;	state Exp;
branches;
next	1.10;

1.10
date	2001.03.12.22.45.49;	author dlr;	state Exp;
branches;
next	1.9;

1.9
date	2001.03.10.03.50.40;	author edk;	state Exp;
branches;
next	1.8;

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

1.7
date	2001.03.08.17.06.52;	author edk;	state Exp;
branches;
next	1.6;

1.6
date	2001.03.07.19.47.18;	author edk;	state Exp;
branches;
next	1.5;

1.5
date	2001.03.07.01.53.21;	author lucas;	state Exp;
branches;
next	1.4;

1.4
date	2001.02.21.16.23.46;	author edk;	state Exp;
branches;
next	1.3;

1.3
date	2001.02.21.15.42.09;	author edk;	state Exp;
branches;
next	1.2;

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

1.1
date	2001.02.18.02.02.09;	author dlr;	state dead;
branches
	1.1.1.1
	1.1.2.1;
next	;

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

1.1.2.1
date	2001.02.18.02.02.09;	author dlr;	state Exp;
branches;
next	1.1.2.2;

1.1.2.2
date	2001.02.18.02.03.51;	author dlr;	state Exp;
branches;
next	1.1.2.3;

1.1.2.3
date	2001.02.18.08.10.09;	author dlr;	state Exp;
branches;
next	1.1.2.4;

1.1.2.4
date	2001.02.18.08.33.48;	author dlr;	state Exp;
branches;
next	1.1.2.5;

1.1.2.5
date	2001.02.18.09.14.35;	author dlr;	state Exp;
branches;
next	1.1.2.6;

1.1.2.6
date	2001.02.20.09.49.20;	author edk;	state Exp;
branches;
next	;

1.12.6.1
date	2001.06.13.17.43.41;	author edk;	state Exp;
branches;
next	1.12.6.2;

1.12.6.2
date	2001.06.14.00.06.26;	author leonardr;	state Exp;
branches;
next	1.12.6.3;

1.12.6.3
date	2001.06.14.04.33.28;	author edk;	state Exp;
branches;
next	;


desc
@@


1.21
log
@Upgrading to Turbine 3.0-dev, Fulcrum 1.0, and Torque 1.0.
@
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.om;

import java.util.*;

import org.apache.turbine.util.Log;
import org.apache.commons.util.exception.NestableException;

import org.tigris.helm.tool.HelmProperties;

import org.tigris.anzu.client.MailingAliasSpec;
import org.tigris.anzu.client.AddressManagerSpec;

import org.tigris.helm.DoesNotExist;
import org.tigris.helm.om.*;
import org.tigris.helm.security.AccessControlList;
import org.tigris.helm.tool.task.TaskException;
import org.tigris.helm.tool.DBFlag;

/**
 * Anzu task.
 *
 * @@author <a href="mailto:edk@@collab.net">Ed Korthof</a>
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 */
public class AnzuTask extends Task
{
    /**
     * The name of the component which runs our tasks.
     */
    public static String COMPONENT_NAME = "Anzu";

    /**
     * Whether or not in debug mode.
     */
    private static final boolean DEBUG = false;

    /**
     * Adds a mailing list.
     */
    public static void addMailingList(User onBehalfOf, String list,
                                      Project project, String type,
                                      String description, String proprietary)
        throws Exception
    {
        List args = new ArrayList(3);
        args.add(list);
        args.add(type);
        args.add(description);
        args.add(proprietary);
        add(onBehalfOf, project.getID(), "addlist", args);
    }

    /**
     * Deletes a mailing list.
     */
    public static void deleteMailingList(User onBehalfOf, String list,
                                         Project project)
        throws DoesNotExist, Exception
    {
        List args = new ArrayList(3);
        args.add(project.getDomainName());
        args.add(project.getName());
        args.add(list);
        add(onBehalfOf, project.getID(), "deletelist", args);
    }

    /**
     * Adds a project.
     */
    public static void addProject(User onBehalfOf, Domain domain,
                                  Project project)
        throws Exception
    {
        add(onBehalfOf, domain, "addproject", project);
    }

    /**
     * Locks a project
     */
    public static void lockProject(User onBehalfOf, Domain domain,
                                   Project project)
        throws Exception
    {
        add(onBehalfOf, domain, "disableproject", project);
    }

    /**
     * Unlocks a project
     */
    public static void unlockProject(User onBehalfOf, Domain domain,
                                     Project project)
        throws Exception
    {
        add(onBehalfOf, domain, "enableproject", project);
    }

    /**
     * Deletes a project.
     */
    public static void deleteProject(User onBehalfOf, Domain domain,
                                     Project project)
        throws Exception
    {
        add(onBehalfOf, domain, "deleteproject", project);
    }

    /**
     * Sets an owner's alias.
     */
    public static void setOwnerAlias(User onBehalfOf, Project project,
                                     boolean exists)
        throws Exception
    {
        add(onBehalfOf, COMPONENT_NAME, project.getID(), "owneralias",
            (exists ? "true" : "false"));
    }

    /**
     * Add a task to be performed. Takes a List of arguments--use
     * one of the wrapper methods to make a list.
     */
    protected static Task add (User onBehalfOf, String affectedObjectID,
                               String command, List arguments)
        throws Exception
    {
        return Task.add(onBehalfOf, COMPONENT_NAME, affectedObjectID,
                        command, arguments);
    }
    
    /**
     * Adds a task to be performed.
     */
    protected static final void add(User onBehalfOf, Domain domain,
                                    String cmd, Project project)
        throws Exception
    {
        Task.add(onBehalfOf, COMPONENT_NAME, domain.getID(), cmd,
                 project.getName());
    }

    /**
     * Runs this task.
     */
    public static void runTask(Task task)
        throws TaskException
    {
        if (!task.getComponent().equals(COMPONENT_NAME)) {
            throw new TaskException
                ("AnzuTask cannot run a task for component " +
                 task.getComponent());
        }

        TaskException ue = null;
        String command = task.getCommand();
        String objectID = String.valueOf(task.getAffectedObjectId());

        if (DEBUG) 
        {
            Log.debug("AnzuTask called: " + command + " / " +
                      objectID);
        }

        try {
            if (command != null && objectID != null)
            {
                if (command.equals("addlist")) {
                    AnzuTask.addList(task, objectID);
                }
                else if (command.equals("deletelist")) {
                    AnzuTask.deleteList(task, objectID);
                }
                else if (command.equals("addproject")) {
                    AnzuTask.addProject(task, objectID);
                }
                else if (command.equals("deleteproject")) {
                    AnzuTask.deleteProject(task, objectID);
                }
                else if (command.equals("owneralias")) {
                    AnzuTask.setOwnerAlias(task, objectID);
                }
                else if (command.equals("disableproject")) {
                    AnzuTask.disableProject(task, objectID);
                }
                else if (command.equals("enableproject")) {
                    AnzuTask.enableProject(task, objectID);
                }
                else {
                    Log.error("unknown command type in AnzuTask : " + command);
                }
            }
        }
        catch (TaskException e) {
            ue = e;
        }
        try {
            task.setAsDone();
        }
        catch (Exception e) {
            Log.error("Couldn't set Anzu task as done: " + e.getMessage(), e);
        }
        if (ue != null) {
            throw ue;
        }
    }

    private static void deleteList(Task task, String projectID)
        throws TaskException
    {
        String domain = null;
        String project = null;
        String list = null;
        try {
            List args = task.readArgsArray();
            domain = (String)args.get(0);
            project = (String)args.get(1);
            list = (String)args.get(2);

            MailingList ml = MailingListPeer.getInstance
                (ProjectPeer.getInstance(projectID), list);
            ml.delete();
        }
        catch (Exception e) {
            throw new TaskException("Exception in AnzuTask deletelist (" +
                    list + "@@" + project + "." + domain + " )", e);
        }
    }

    private static void setOwnerAlias(Task task, String projectID)
        throws TaskException
    {
        Project project = null;
        try {
            project = ProjectPeer.getInstance(projectID);
        }
        catch (Exception e) {
            // maybe the project is dead already.  it's a silly fringe case,
            // but perhaps it'd happen if someone tries to create lots and
            // lots of projects and the admin tries to cancel the projects
            // as fast as possible.
            Log.error("Unable to retrieve project " + projectID + 
                    " while trying to setOwnerAlias", e);
        }
        if (project != null)
        {
            try {
                String domain = project.getDomainName();
                boolean exists = task.getArguments().equals("true");
    
                List owners = project.getOwners();
                StringBuffer address = new StringBuffer();
                Iterator i = owners.iterator();
                while (i.hasNext()) {
                    if (address.length() > 0)
                        address.append("\n");
                    String ownerEmail = ((User)i.next()).getDomainEmail();
                    address.append(ownerEmail);
                }
                // TODO : handle error case where address.length() == 0 ?
                if (DEBUG) Log.info("set owner alias: " + address.toString());
    
                MailingAliasSpec ma = new MailingAliasSpec(domain,
                        project.getName(), "owner");
                ma.setDestination(address.toString());
                if (exists) 
                    ma.update();
                else
                    ma.create();
            }
            catch (Exception e) {
                Log.error("Unable to set owner email alias: " +
                        e.getMessage(), e);
                throw new TaskException("Unable to set owner email alias: " +
                        e.getMessage() + " for project " + projectID);
            }
        }
    }

    private static void addProject(Task task, String domainID)
        throws TaskException
    {
        String projectName = null;
        try 
        {
            projectName = task.getArguments();
            Domain domain = (Domain)ProjectPeer.getInstance(domainID);
            String domainName = domain.getName();
            AddressManagerSpec mgr = new AddressManagerSpec();
            mgr.addProject(domainName, projectName);
        }
        catch (Exception e) {
            Log.error("Unable to add anzu project: " + e.getMessage(), e);
            throw new TaskException("unable to add anzu project " +
                    projectName + " : " + e.getMessage());
        }
    }
    
    private static void deleteProject(Task task, String domainID)
        throws TaskException
    {
        String projectName = null;
        try 
        {
            projectName = task.getArguments();
            Domain domain = (Domain)ProjectPeer.getInstance(domainID);
            String domainName = domain.getName();
            AddressManagerSpec mgr = new AddressManagerSpec();
            mgr.deleteProject(domainName, projectName);
        }
        catch (Exception e) {
            Log.error("Unable to delete anzu project: " + e.getMessage(), e);
            throw new TaskException("Unable to delete anzu project " +
                    projectName + " : " + e.getMessage());
        }
    }

    private static void enableProject(Task task, String domainID)
        throws TaskException
    {
        String projectName = null;
        try 
        {
            projectName = task.getArguments();
            Domain domain = (Domain)ProjectPeer.getInstance(domainID);
            String domainName = domain.getName();
            AddressManagerSpec mgr = new AddressManagerSpec();
            mgr.enableProject(domainName, projectName);
        }
        catch (Exception e) 
        {
            Log.error("Unable to enable anzu project: " + e.getMessage(), e);
            throw new TaskException("unable to enable anzu project " + 
                    projectName + " : " + e.getMessage());
        }
    }

    private static void disableProject(Task task, String domainID)
        throws TaskException
    {
        String projectName = null;
        try 
        {
            projectName = task.getArguments();
            Domain domain = (Domain)ProjectPeer.getInstance(domainID);
            String domainName = domain.getName();
            AddressManagerSpec mgr = new AddressManagerSpec();
            mgr.disableProject(domainName, projectName);
        }
        catch (Exception e) 
        {
            Log.error("Unable to disable anzu project: " + e.getMessage(), e);
            throw new TaskException("unable to disable anzu project: " + 
                    projectName + " : " + e.getMessage());
        }
    }
    
    private static void addList(Task task, String projectID)
        throws TaskException
    {
        Project project = null;
        try
        { // work around a silly race condition
            project = ProjectPeer.getInstance(projectID);
        }
        catch (Exception e)
        {
            Log.error("exception getting project in addList, proejctID " +
                    projectID, e);
        }
        if (project != null)
        {
            try 
            {
                List args = task.readArgsArray();
                String list = (String)args.get(0);
                String type = (String)args.get(1);
                String description = (String)args.get(2);
                String proprietary = (String)args.get(3);
                AccessControlList acl = AccessControlList.getSuperUserACL();

                MailingList ml = MailingList.add
                    (acl, project, list, description, type);
                ml.setPrivate(DBFlag.booleanValueOf(proprietary));
                ml.setOwner(project.getProjectOwnerEmailAlias());
                ml.save(acl);
            }
            catch (Exception e) 
            {
                throw new TaskException("Exception in AnzuTask addlist, " +
                                        "projectID: " + projectID , e);
            }
        }
    }
}
@


1.20
log
@set the owner of the default mailing lists to be the owner alias
@
text
@d52 1
a52 1
import org.apache.turbine.util.TurbineException;
d435 1
a435 1
                        "projectID: " + projectID , e);
@


1.19
log
@Updated in light of the port of Joist JoistProperties class to Helm (as class HelmProperties).
@
text
@a201 1
        String userID = String.valueOf(task.getOnBehalfOf());
d206 1
a206 13
                      objectID + " / " + userID);
        }

        User user = null;
        if (userID != null && !userID.equals("0")) 
        {
            try 
            {
                user = UserPeer.getInstance(userID);
            }
            catch (Exception ignore) 
            {
            }
d210 1
a210 1
            if (command != null && objectID != null && userID != null)
d213 1
a213 1
                    AnzuTask.addList(task, objectID, user);
d402 1
a402 1
    private static void addList(Task task, String projectID, User user)
d427 1
a427 2
                    (acl, ProjectPeer.getInstance(projectID),
                     list, description, type);
d429 1
a429 4
                if (user != null)
                {
                    ml.setOwner(user);
                }
@


1.18
log
@Huge patch of the day: Got rid of redundant HELM_DOMAIN table. The project representation of the domain and the host is now the canonical one. Host and Domain now subclass ProjectGroup. The canonical representation of a project's membership in a domain group is now the corresponding mapping in HELM_PROJECT_PROJECTGROUP. Schema changes: No more HELM_DOMAIN table (obviously); no more DOMAIN_ID in HELM_PROJECT; HELM_USER and HELM_VC_VIEW now have foreign keys in HELM_PROJECT instead of HELM_DOMAIN.
@
text
@d54 1
a54 1
import org.joist.util.JoistProperties;
@


1.17
log
@Added import.
@
text
@d344 1
a344 1
            Domain domain = DomainPeer.getInstance(domainID);
d363 1
a363 1
            Domain domain = DomainPeer.getInstance(domainID);
d382 1
a382 1
            Domain domain = DomainPeer.getInstance(domainID);
d402 1
a402 1
            Domain domain = DomainPeer.getInstance(domainID);
@


1.16
log
@* provide better error messages (some information about the project/list)
* avoid sending error messages in an unlikely but non-fatal error
  situation: because both project creation and project deletion are only
  partially asynchronos, it's possible that the project has been
  disapproved or deleted by the time we try to run tasks related to it.
  handle that case cleanly.
@
text
@d59 1
d61 1
a62 1
import org.tigris.helm.DoesNotExist;
@


1.15
log
@A look at the code indicates that it's better to create the list
without any owner.
@
text
@a52 1
import org.apache.turbine.util.TurbineException;
d267 3
d272 3
a274 3
            String domain = (String)args.get(0);
            String project = (String)args.get(1);
            String list = (String)args.get(2);
d281 2
a282 1
            throw new TaskException("Exception in AnzuTask deletelist", e);
d289 1
d291 1
a291 23
            Project project = ProjectPeer.getInstance(projectID);
            String domain = project.getDomainName();
            boolean exists = task.getArguments().equals("true");

            List owners = project.getOwners();
            StringBuffer address = new StringBuffer();
            Iterator i = owners.iterator();
            while (i.hasNext()) {
                if (address.length() > 0)
                    address.append("\n");
                String ownerEmail = ((User)i.next()).getDomainEmail();
                address.append(ownerEmail);
            }
            // TODO : handle error case where address.length() == 0 ?
            if (DEBUG) Log.info("set owner alias: " + address.toString());

            MailingAliasSpec ma = new MailingAliasSpec(domain,
                    project.getName(), "owner");
            ma.setDestination(address.toString());
            if (exists) 
                ma.update();
            else
                ma.create();
d294 39
a332 3
            Log.error("Unable to set owner email alias: " + e.getMessage(), e);
            throw new TaskException("Unable to set owner email alias: " +
                                    e.getMessage());
d339 1
d342 1
a342 1
            String projectName = task.getArguments();
d350 2
a351 2
            throw new TaskException("unable to add anzu project: " + 
                                    e.getMessage());
d358 1
d361 1
a361 1
            String projectName = task.getArguments();
d369 2
a370 2
            throw new TaskException("Unable to delete anzu project: " +
                                    e.getMessage());
d377 1
d380 1
a380 1
            String projectName = task.getArguments();
d389 2
a390 2
            throw new TaskException("unable to enable anzu project: " + 
                                    e.getMessage());
d397 1
d400 1
a400 1
            String projectName = task.getArguments();
d410 1
a410 1
                                    e.getMessage());
d417 6
a422 1
        try 
d424 2
a425 12
            List args = task.readArgsArray();
            String list = (String)args.get(0);
            String type = (String)args.get(1);
            String description = (String)args.get(2);
            String proprietary = (String)args.get(3);

            MailingList ml = MailingList.add
                (user.getACL(), ProjectPeer.getInstance(projectID),
                 list, description, type);
            ml.setPrivate(DBFlag.booleanValueOf(proprietary));
            ml.setOwner(user);
            ml.save(user.getACL());
d427 1
a427 1
        catch (Exception e) 
d429 24
a452 1
            throw new TaskException("Exception in AnzuTask addlist", e);
@


1.14
log
@If the user is defunct, use the superuser instead. This is a quick fix
which Ed will probably change later).
@
text
@d211 2
a212 1
        if (userID != null && !userID.equals("0")) {
d217 1
a217 1
            catch (Exception e) 
a218 1
                user = UserPeer.getSuperUser();
@


1.13
log
@Ported Helm from WebMacro to Velocity.
@
text
@d212 2
a213 1
            try {
d216 3
a218 2
            catch (Exception e) {
                throw new TaskException("Invalid userID : " + userID);
@


1.12
log
@Gated a lot of task debug information which was annoying dlr.
@
text
@d52 2
@


1.12.6.1
log
@make the user creating the project more optional; use root's acl to avoid
permission problems.  (this will be done more cleanly in the HEAD, but it's
a minimal set of changes for the branch).
@
text
@a58 1
import org.tigris.helm.security.AccessControlList;
d214 1
a214 1
                user = null;
d219 1
a219 1
            if (command != null && objectID != null)
a397 1
            AccessControlList acl = AccessControlList.getSuperUserACL();
d400 1
a400 1
                (acl, ProjectPeer.getInstance(projectID),
d403 2
a404 5
            if (user != null)
            {
                ml.setOwner(user);
            }
            ml.save(acl);
@


1.12.6.2
log
@Got rid of error when a user gets deleted before their tasks get run.
@
text
@d210 2
a211 4
        if (userID != null && !userID.equals("0")) 
        {
            try 
            {
d214 2
a215 2
            catch (Exception ignore) 
            {
@


1.12.6.3
log
@* better logging messages
* avoid sending mail to the host admin if the project can't be found during
  AnzuTasks -- the likely cause is that the project was deleted before the
  creation tasks ran.  we still report the error in the log files.
@
text
@a266 3
        String domain = null;
        String project = null;
        String list = null;
d269 3
a271 3
            domain = (String)args.get(0);
            project = (String)args.get(1);
            list = (String)args.get(2);
d278 1
a278 2
            throw new TaskException("Exception in AnzuTask deletelist (" +
                    list + "@@" + project + "." + domain + " )", e);
a284 1
        Project project = null;
d286 23
a308 1
            project = ProjectPeer.getInstance(projectID);
d311 3
a313 39
            // maybe the project is dead already.  it's a silly fringe case,
            // but perhaps it'd happen if someone tries to create lots and
            // lots of projects and the admin tries to cancel the projects
            // as fast as possible.
            Log.error("Unable to retrieve project " + projectID + 
                    " while trying to setOwnerAlias", e);
        }
        if (project != null)
        {
            try {
                String domain = project.getDomainName();
                boolean exists = task.getArguments().equals("true");
    
                List owners = project.getOwners();
                StringBuffer address = new StringBuffer();
                Iterator i = owners.iterator();
                while (i.hasNext()) {
                    if (address.length() > 0)
                        address.append("\n");
                    String ownerEmail = ((User)i.next()).getDomainEmail();
                    address.append(ownerEmail);
                }
                // TODO : handle error case where address.length() == 0 ?
                if (DEBUG) Log.info("set owner alias: " + address.toString());
    
                MailingAliasSpec ma = new MailingAliasSpec(domain,
                        project.getName(), "owner");
                ma.setDestination(address.toString());
                if (exists) 
                    ma.update();
                else
                    ma.create();
            }
            catch (Exception e) {
                Log.error("Unable to set owner email alias: " +
                        e.getMessage(), e);
                throw new TaskException("Unable to set owner email alias: " +
                        e.getMessage() + " for project " + projectID);
            }
a319 1
        String projectName = null;
d322 1
a322 1
            projectName = task.getArguments();
d330 2
a331 2
            throw new TaskException("unable to add anzu project " +
                    projectName + " : " + e.getMessage());
a337 1
        String projectName = null;
d340 1
a340 1
            projectName = task.getArguments();
d348 2
a349 2
            throw new TaskException("Unable to delete anzu project " +
                    projectName + " : " + e.getMessage());
a355 1
        String projectName = null;
d358 1
a358 1
            projectName = task.getArguments();
d367 2
a368 2
            throw new TaskException("unable to enable anzu project " + 
                    projectName + " : " + e.getMessage());
a374 1
        String projectName = null;
d377 1
a377 1
            projectName = task.getArguments();
d386 2
a387 2
            throw new TaskException("unable to disable anzu project " + 
                    projectName + " : " + e.getMessage());
d394 1
a394 6
        Project project = null;
        try
        { // work around a silly race condition
            project = ProjectPeer.getInstance(projectID);
        }
        catch (Exception e)
d396 16
a411 2
            Log.error("exception getting project in addList, proejctID " +
                    projectID, e);
d413 1
a413 1
        if (project != null)
d415 1
a415 24
            try 
            {
                List args = task.readArgsArray();
                String list = (String)args.get(0);
                String type = (String)args.get(1);
                String description = (String)args.get(2);
                String proprietary = (String)args.get(3);
                AccessControlList acl = AccessControlList.getSuperUserACL();

                MailingList ml = MailingList.add
                    (acl, ProjectPeer.getInstance(projectID),
                     list, description, type);
                ml.setPrivate(DBFlag.booleanValueOf(proprietary));
                if (user != null)
                {
                    ml.setOwner(user);
                }
                ml.save(acl);
            }
            catch (Exception e) 
            {
                throw new TaskException("Exception in AnzuTask addlist, " +
                        "projectID: " + projectID , e);
            }
@


1.11
log
@Disentangled Anzu and Helm imports.
@
text
@d79 1
a79 1
    private static final boolean DEBUG = true;
d202 2
a203 1
        if (DEBUG) {
d317 2
a318 2
        Log.error ("in addProject now " + domainID);
        try {
d335 2
a336 1
        try {
d353 2
a354 2
        Log.error ("in enableProject now " + domainID);
        try {
d361 2
a362 1
        catch (Exception e) {
d372 2
a373 2
        Log.error ("in disableProject now " + domainID);
        try {
d380 2
a381 1
        catch (Exception e) {
d391 2
a392 1
        try {
d406 2
a407 1
        catch (Exception e) {
@


1.10
log
@Formatted for 80 columns.
@
text
@d55 3
a60 3
import org.tigris.anzu.client.MailingAliasSpec;
import org.tigris.anzu.client.AddressManagerSpec;

a101 1
            
d125 1
a125 1
            Project project)
d135 1
a135 1
            Project project)
@


1.9
log
@cleanup/update TODOs in comments slightly.
@
text
@d178 2
a179 2
    protected static final void add (User onBehalfOf, Domain domain, String cmd,
                                     Project project)
d182 2
a183 3
        Task.add(onBehalfOf, COMPONENT_NAME,
                 domain.getID(),
                 cmd, project.getName());
@


1.8
log
@More renaming madness as the act of 'project disabling' becomes 'project locking'
@
text
@a395 2
            // TODO : should we validate the projectID?  it
            // got in the database, it should be valid ...
@


1.7
log
@* Domain.getSpecialProject().getID() is really *not* what we want, since
  the ID stored is later used to construct a project.  It was just
  coincidence that this has worked so far.
* add methods for (re)enableProject / disableProject
@
text
@d123 1
a123 1
     * Disables a project
d125 1
a125 1
    public static void disableProject(User onBehalfOf, Domain domain,
d133 1
a133 1
     * (re-)Enable a project
d135 1
a135 1
    public static void enableProject(User onBehalfOf, Domain domain,
@


1.6
log
@use the private toggle for private mailing lists, rather than
{type}-private
@
text
@d123 20
d167 2
a168 2
    protected static Task add (User onBehalfOf, String affectedObjectID, 
                            String command, List arguments)
d178 2
a179 2
    protected static final void add(User onBehalfOf, Domain domain, String cmd,
                                  Project project)
d182 2
a183 1
        Task.add(onBehalfOf, COMPONENT_NAME, domain.getSpecialProject().getID(),
d237 9
d350 36
@


1.5
log
@Corrected regex madness for Torque upgrade.  -dlr
@
text
@d61 2
d86 1
a86 1
                                      String description)
d93 1
d328 1
d335 1
@


1.4
log
@we're passed a Task, not an AnzuTask; but we can use it anyway, so
long as we don't cast it.  make various methods static (providing the
Task as an argument) as part of this.
@
text
@a54 1
import org.tigris.helm.om.peer.*;
@


1.3
log
@remove TODO notes which have been done
@
text
@d167 1
a167 1
    public static void runTask(Task t)
d170 1
a170 1
        if (!t.getComponent().equals(COMPONENT_NAME)) {
d172 2
a173 1
                ("AnzuTask cannot run a task for component " + t.getComponent());
a174 1
        AnzuTask anzuTask = (AnzuTask)t;
d177 3
a179 3
        String command = anzuTask.getCommand();
        String objectID = String.valueOf(anzuTask.getAffectedObjectId());
        String userID = String.valueOf(anzuTask.getOnBehalfOf());
d200 1
a200 1
                    anzuTask.addList(objectID, user);
d203 1
a203 1
                    anzuTask.deleteList(objectID);
d206 1
a206 1
                    anzuTask.addProject(objectID);
d209 1
a209 1
                    anzuTask.deleteProject(objectID);
d212 1
a212 1
                    anzuTask.setOwnerAlias(objectID);
d220 1
a220 1
            anzuTask.setAsDone();
d230 1
a230 1
    private void deleteList(String projectID)
d234 1
a234 1
            List args = readArgsArray();
d248 1
a248 1
    private void setOwnerAlias(String projectID)
d254 1
a254 1
            boolean exists = getArguments().equals("true");
d283 1
a283 1
    private void addProject(String domainID)
d286 1
d288 1
a288 1
            String projectName = getArguments();
d301 1
a301 1
    private void deleteProject(String domainID)
d305 1
a305 1
            String projectName = getArguments();
d318 1
a318 1
    private void addList(String projectID, User user)
d322 1
a322 1
            List args = readArgsArray();
@


1.2
log
@adding files from the branch to the head
@
text
@a289 2
            // TODO : we should do this through a different class
            // (AddressManager client library) -- once it exists
a306 2
            // TODO : we should do this through a different class
            // (AddressManager client library) -- once it exists
@


1.1
log
@file AnzuTask.java was initially added on branch HELM_PEER_PORT_BRANCH.
@
text
@d1 342
@


1.1.1.1
log
@Replacing the HEAD with the HELM_PEER_PORT_BRANCH.
@
text
@a0 342
/* ================================================================
 * 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.om;

import java.util.*;

import org.apache.turbine.util.Log;

import org.joist.util.JoistProperties;

import org.tigris.helm.om.peer.*;
import org.tigris.helm.om.*;
import org.tigris.helm.tool.task.TaskException;
import org.tigris.helm.DoesNotExist;
import org.tigris.anzu.client.MailingAliasSpec;
import org.tigris.anzu.client.AddressManagerSpec;

/**
 * Anzu task.
 *
 * @@author <a href="mailto:edk@@collab.net">Ed Korthof</a>
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 */
public class AnzuTask extends Task
{
    /**
     * The name of the component which runs our tasks.
     */
    public static String COMPONENT_NAME = "Anzu";

    /**
     * Whether or not in debug mode.
     */
    private static final boolean DEBUG = true;

    /**
     * Adds a mailing list.
     */
    public static void addMailingList(User onBehalfOf, String list,
                                      Project project, String type,
                                      String description)
        throws Exception
    {
        List args = new ArrayList(3);
        args.add(list);
        args.add(type);
        args.add(description);
        add(onBehalfOf, project.getID(), "addlist", args);
    }

    /**
     * Deletes a mailing list.
     */
    public static void deleteMailingList(User onBehalfOf, String list,
                                         Project project)
            
        throws DoesNotExist, Exception
    {
        List args = new ArrayList(3);
        args.add(project.getDomainName());
        args.add(project.getName());
        args.add(list);
        add(onBehalfOf, project.getID(), "deletelist", args);
    }

    /**
     * Adds a project.
     */
    public static void addProject(User onBehalfOf, Domain domain,
                                  Project project)
        throws Exception
    {
        add(onBehalfOf, domain, "addproject", project);
    }

    /**
     * Deletes a project.
     */
    public static void deleteProject(User onBehalfOf, Domain domain,
                                     Project project)
        throws Exception
    {
        add(onBehalfOf, domain, "deleteproject", project);
    }

    /**
     * Sets an owner's alias.
     */
    public static void setOwnerAlias(User onBehalfOf, Project project,
                                     boolean exists)
        throws Exception
    {
        add(onBehalfOf, COMPONENT_NAME, project.getID(), "owneralias",
            (exists ? "true" : "false"));
    }

    /**
     * Add a task to be performed. Takes a List of arguments--use
     * one of the wrapper methods to make a list.
     */
    protected static Task add (User onBehalfOf, String affectedObjectID, 
                            String command, List arguments)
        throws Exception
    {
        return Task.add(onBehalfOf, COMPONENT_NAME, affectedObjectID,
                        command, arguments);
    }
    
    /**
     * Adds a task to be performed.
     */
    protected static final void add(User onBehalfOf, Domain domain, String cmd,
                                  Project project)
        throws Exception
    {
        Task.add(onBehalfOf, COMPONENT_NAME, domain.getSpecialProject().getID(),
                 cmd, project.getName());
    }

    /**
     * Runs this task.
     */
    public static void runTask(Task t)
        throws TaskException
    {
        if (!t.getComponent().equals(COMPONENT_NAME)) {
            throw new TaskException
                ("AnzuTask cannot run a task for component " + t.getComponent());
        }
        AnzuTask anzuTask = (AnzuTask)t;

        TaskException ue = null;
        String command = anzuTask.getCommand();
        String objectID = String.valueOf(anzuTask.getAffectedObjectId());
        String userID = String.valueOf(anzuTask.getOnBehalfOf());

        if (DEBUG) {
            Log.debug("AnzuTask called: " + command + " / " +
                      objectID + " / " + userID);
        }

        User user = null;
        if (userID != null && !userID.equals("0")) {
            try {
                user = UserPeer.getInstance(userID);
            }
            catch (Exception e) {
                throw new TaskException("Invalid userID : " + userID);
            }
        }

        try {
            if (command != null && objectID != null && userID != null)
            {
                if (command.equals("addlist")) {
                    anzuTask.addList(objectID, user);
                }
                else if (command.equals("deletelist")) {
                    anzuTask.deleteList(objectID);
                }
                else if (command.equals("addproject")) {
                    anzuTask.addProject(objectID);
                }
                else if (command.equals("deleteproject")) {
                    anzuTask.deleteProject(objectID);
                }
                else if (command.equals("owneralias")) {
                    anzuTask.setOwnerAlias(objectID);
                }
            }
        }
        catch (TaskException e) {
            ue = e;
        }
        try {
            anzuTask.setAsDone();
        }
        catch (Exception e) {
            Log.error("Couldn't set Anzu task as done: " + e.getMessage(), e);
        }
        if (ue != null) {
            throw ue;
        }
    }

    private void deleteList(String projectID)
        throws TaskException
    {
        try {
            List args = readArgsArray();
            String domain = (String)args.get(0);
            String project = (String)args.get(1);
            String list = (String)args.get(2);

            MailingList ml = MailingListPeer.getInstance
                (ProjectPeer.getInstance(projectID), list);
            ml.delete();
        }
        catch (Exception e) {
            throw new TaskException("Exception in AnzuTask deletelist", e);
        }
    }

    private void setOwnerAlias(String projectID)
        throws TaskException
    {
        try {
            Project project = ProjectPeer.getInstance(projectID);
            String domain = project.getDomainName();
            boolean exists = getArguments().equals("true");

            List owners = project.getOwners();
            StringBuffer address = new StringBuffer();
            Iterator i = owners.iterator();
            while (i.hasNext()) {
                if (address.length() > 0)
                    address.append("\n");
                String ownerEmail = ((User)i.next()).getDomainEmail();
                address.append(ownerEmail);
            }
            // TODO : handle error case where address.length() == 0 ?
            if (DEBUG) Log.info("set owner alias: " + address.toString());

            MailingAliasSpec ma = new MailingAliasSpec(domain,
                    project.getName(), "owner");
            ma.setDestination(address.toString());
            if (exists) 
                ma.update();
            else
                ma.create();
        }
        catch (Exception e) {
            Log.error("Unable to set owner email alias: " + e.getMessage(), e);
            throw new TaskException("Unable to set owner email alias: " +
                                    e.getMessage());
        }
    }

    private void addProject(String domainID)
        throws TaskException
    {
        try {
            String projectName = getArguments();
            Domain domain = DomainPeer.getInstance(domainID);
            String domainName = domain.getName();
            // TODO : we should do this through a different class
            // (AddressManager client library) -- once it exists
            AddressManagerSpec mgr = new AddressManagerSpec();
            mgr.addProject(domainName, projectName);
        }
        catch (Exception e) {
            Log.error("Unable to add anzu project: " + e.getMessage(), e);
            throw new TaskException("unable to add anzu project: " + 
                                    e.getMessage());
        }
    }
    
    private void deleteProject(String domainID)
        throws TaskException
    {
        try {
            String projectName = getArguments();
            Domain domain = DomainPeer.getInstance(domainID);
            String domainName = domain.getName();
            // TODO : we should do this through a different class
            // (AddressManager client library) -- once it exists
            AddressManagerSpec mgr = new AddressManagerSpec();
            mgr.deleteProject(domainName, projectName);
        }
        catch (Exception e) {
            Log.error("Unable to delete anzu project: " + e.getMessage(), e);
            throw new TaskException("Unable to delete anzu project: " +
                                    e.getMessage());
        }
    }

    private void addList(String projectID, User user)
        throws TaskException
    {
        try {
            List args = readArgsArray();
            String list = (String)args.get(0);
            String type = (String)args.get(1);
            String description = (String)args.get(2);

            // TODO : should we validate the projectID?  it
            // got in the database, it should be valid ...
            MailingList ml = MailingList.add
                (user.getACL(), ProjectPeer.getInstance(projectID),
                 list, description, type);
            ml.setOwner(user);
            ml.save(user.getACL());
        }
        catch (Exception e) {
            throw new TaskException("Exception in AnzuTask addlist", e);
        }
    }
}
@


1.1.2.1
log
@This compiles, but that's all I'd count on it for.
@
text
@a0 341
/* ================================================================
 * 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.om;

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

import org.apache.turbine.util.Log;

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

import org.tigris.helm.om.peer.*;
import org.tigris.helm.om.*;
import org.tigris.helm.tool.task.TaskException;
import org.tigris.helm.DoesNotExist;
import org.tigris.anzu.client.MailingAliasSpec;
import org.tigris.anzu.client.AddressManagerSpec;

/**
 * Anzu task.
 *
 * @@author <a href="mailto:edk@@collab.net">Ed Korthof</a>
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 */
public class AnzuTask extends Task
{
    /**
     * The name of the component which runs our tasks.
     */
    public static String COMPONENT_NAME = "Anzu";

    private static final boolean DEBUG = true;

    protected AnzuTask (String onBehalfOf, String affectedObjectID,
                        String command, String arguments)
                        //throws Exception
    {
//         super(onBehalfOf, COMPONENT_NAME, affectedObjectID, command,
//               arguments);
    }

    public static void addMailingList(String onBehalfOfID, String list,
                                      String projectID, String type,
                                      String description)
                                      //throws Exception
    {
        ArrayList args = new ArrayList();
        args.add(list);
        args.add(type);
        args.add(description);
        String arguments = null;
        try {
             arguments = writeArgsArray(args);
        }
        catch (Exception e) {
            Log.warn("Unable to store Task args in a string: " +
                     e.getMessage(), e);
            arguments = list + " " + type + " " + description;
        }
        // this creates the entry and stores it in the db
        new AnzuTask(onBehalfOfID, projectID, "addlist", arguments);
    }

    public static void deleteMailingList(Connection conn, DataManager dMgr,
            String onBehalfOfID, String list, String projectID)
        throws DoesNotExist, Exception
    {
        ArrayList args = new ArrayList();
        Project p = ProjectPeer.getInstance(projectID);
        String projectName = p.getName();
        String domainName = p.getDomainName();
        args.add(domainName);
        args.add(projectName);
        args.add(list);
        String arguments = null;
        try {
             arguments = writeArgsArray(args);
        }
        catch (Exception e) {
            Log.warn("Unable to store args in a string: " +
                      e.getMessage(), e);
            arguments = domainName + " " + projectName + " " + list;
        }
        // this creates the entry and stores it in the db
        new AnzuTask(onBehalfOfID, projectID, "deletelist", arguments);
    }

    public static void addProject(Connection conn, DataManager dMgr,
            String onBehalfOfID, String domainID, String projectName)
            //throws 
    {
        new AnzuTask(onBehalfOfID, domainID, "addproject", projectName);
    }

    public static void deleteProject(Connection conn, DataManager dMgr,
            String onBehalfOfID, String domainID, String project)
            //throws 
    {
        new AnzuTask(onBehalfOfID, domainID, "deleteproject", project);
    }

    public static void setOwnerAlias(Connection conn, DataManager dMgr,
            String onBehalfOfID, String projectID, boolean exists)
            //throws 
    {
        new AnzuTask(onBehalfOfID, projectID, "owneralias",
                     (exists ? "true" : "false"));
    }

    public void runTask()
        throws TaskException
    {
        String command = null;
        String objectID = null;
        String userID = null;
        try {
            command = getCommand();
            objectID = String.valueOf(getAffectedObjectId());
            userID = String.valueOf(getOnBehalfOf());
        }
        catch (Exception e) {
            Log.error(e);
        }

        if (DEBUG)
        {
            Log.debug("AnzuTask called: " + command + " / " +
                      objectID + " / " + userID);
        }

        User user = null;
        if (userID != null && !userID.equals("0")) {
            try {
                user = UserPeer.getInstance(userID);
            }
            catch (Exception e) {
                throw new TaskException("invalid userID : " + userID);
            }
        }
        TaskException ue = null;
        try {
            if (command != null && objectID != null && userID != null)
            {
                if (command.equals("addlist")) {
                    addList(objectID, user);
                } else if (command.equals("deletelist")) {
                    deleteList(objectID);
                } else if (command.equals("addproject")) {
                    addProject(objectID);
                } else if (command.equals("deleteproject")) {
                    deleteProject(objectID);
                } else if (command.equals("owneralias")) {
                    setOwnerAlias(objectID);
                }
            }
        } catch (TaskException e) {
            ue = e;
        }
        try {
            setAsDone();
        }
        catch (Exception e) {
            Log.error("Couldn't set Anzu task as done: " + e.getMessage(), e);
        }
        if (ue != null) { throw ue; }
    }

    private void deleteList(String projectID) throws TaskException
    {
        TaskException ue = null;
        try {
            String argsArray[] = readArgs();
            if (DEBUG) {
                for (int i = 0 ; i < argsArray.length ; ++i) {
                    Log.info("argsArray[" + i + "] = " + argsArray[i]);
                }
            }
            String domain = argsArray[0];
            String project = argsArray[1];
            String list = argsArray[2];

            MailingList ml = MailingListPeer.getInstance
                (ProjectPeer.getInstance(projectID), list);
            ml.delete();
        } catch (Exception e) {
            Log.error(e);
            if (DEBUG) Log.info("exception in AnzuTask deletelist: " +
                    e.getMessage());
            ue = new TaskException("exception in AnzuTask deletelist: "+
                    e.getMessage());
        }
        if (ue != null) {
            throw ue;
        }
    }

    private void setOwnerAlias(String projectID) throws TaskException
    {
        try {
            Project project = ProjectPeer.getInstance(projectID);
            String domain = project.getDomainName();
            boolean exists = getArguments().equals("true");

            List owners = project.getOwners();
            StringBuffer address = new StringBuffer();
            Iterator i = owners.iterator();
            while (i.hasNext()) {
                if (address.length() > 0)
                    address.append("\n");
                String ownerEmail = ((User)i.next()).getDomainEmail();
                address.append(ownerEmail);
            }
            // TODO : handle error case where address.length() == 0 ?
            if (DEBUG) Log.info("set owner alias: " + address.toString());

            MailingAliasSpec ma = new MailingAliasSpec(domain,
                    project.getName(), "owner");
            ma.setDestination(address.toString());
            if (exists) 
                ma.update();
            else
                ma.create();
        } catch (Exception e) {
            Log.error("Unable to set owner email alias: " + e.getMessage(), e);
            throw new TaskException("Unable to set owner email alias: " +
                                    e.getMessage());
        }
    }

    private void addProject(String domainID) throws TaskException
    {
        try {
            String projectName = getArguments();
            Domain domain = DomainPeer.getInstance(domainID);
            String domainName = domain.getName();
            // TODO : we should do this through a different class
            // (AddressManager client library) -- once it exists
            AddressManagerSpec mgr = new AddressManagerSpec();
            mgr.addProject(domainName, projectName);
        }
        catch (Exception e) {
            Log.error("Unable to add anzu project: " + e.getMessage(), e);
            throw new TaskException("unable to add anzu project: " + 
                                    e.getMessage());
        }
    }
    
    private void deleteProject(String domainID) throws TaskException
    {
        try {
            String projectName = getArguments();
            Domain domain = DomainPeer.getInstance(domainID);
            String domainName = domain.getName();
            // TODO : we should do this through a different class
            // (AddressManager client library) -- once it exists
            AddressManagerSpec mgr = new AddressManagerSpec();
            mgr.deleteProject(domainName, projectName);
        }
        catch (Exception e) {
            Log.error("Unable to delete anzu project: " + e.getMessage(), e);
            throw new TaskException("Unable to delete anzu project: " +
                                    e.getMessage());
        }
    }

    private void addList(String projectID, User user) throws TaskException {
        TaskException ue = null;
        try {
            String argsArray[] = readArgs();
            if (DEBUG) {
                for (int i = 0 ; i < argsArray.length ; ++i) {
                    Log.info("argsArray["+i+"] = " + argsArray[i]);
                }
            }
            String list = argsArray[0];
            String type = argsArray[1];
            String description = argsArray[2];

            // TODO : should we validate the projectID?  it
            // got in the database, it should be valid ...
            // TODO: FIXME
//             MailingList ml =
//                 MailingList.add(ProjectPeer.getInstance(projectID),
//                                 list, description, type);
//             ml.setOwner(user);
//             ml.create();
        } catch (Exception e) {
            Log.error("Exception in AnzuTask addlist: " + e.getMessage(), e);
            ue = new TaskException("Exception in AnzuTask addlist: " +
                                   e.getMessage());
        }
        if (ue != null) {
            throw ue;
        }
    }
}
@


1.1.2.2
log
@Don't open and close block on the same line.
@
text
@a76 3
    /**
     * Whether or not in debug mode.
     */
d201 1
a201 2
        }
        catch (TaskException e) {
d230 1
a230 2
        }
        catch (Exception e) {
d268 1
a268 2
        }
        catch (Exception e) {
d332 1
a332 2
        }
        catch (Exception e) {
@


1.1.2.3
log
@Most--if not all--of the task methods are now implemented.
@
text
@d49 1
d54 1
d82 10
a91 5
    /**
     * Adds a mailing list.
     */
    public static void addMailingList(User onBehalfOf, String list,
                                      Project project, String type,
d93 1
a93 1
        throws Exception
d95 1
a95 1
        List args = new ArrayList(3);
d99 11
a109 1
        add(onBehalfOf, project.getID(), "addlist", args);
d112 2
a113 6
    /**
     * Deletes a mailing list.
     */
    public static void deleteMailingList(User onBehalfOf, String list,
                                         Project project)
            
d116 6
a121 3
        List args = new ArrayList(3);
        args.add(project.getDomainName());
        args.add(project.getName());
d123 11
a133 11
        add(onBehalfOf, project.getID(), "deletelist", args);
    }

    /**
     * Adds a project.
     */
    public static void addProject(User onBehalfOf, Domain domain,
                                  Project project)
        throws Exception
    {
        add(onBehalfOf, domain, "addproject", project);
d136 3
a138 6
    /**
     * Deletes a project.
     */
    public static void deleteProject(User onBehalfOf, Domain domain,
                                     Project project)
        throws Exception
d140 1
a140 1
        add(onBehalfOf, domain, "deleteproject", project);
d143 3
a145 6
    /**
     * Sets an owner's alias.
     */
    public static void setOwnerAlias(User onBehalfOf, Project project,
                                     boolean exists)
        throws Exception
d147 1
a147 2
        add(onBehalfOf, COMPONENT_NAME, project.getID(), "owneralias",
            (exists ? "true" : "false"));
d150 3
a152 7
    /**
     * Add a task to be performed. Takes a List of arguments--use
     * one of the wrapper methods to make a list.
     */
    protected static Task add (User onBehalfOf, String affectedObjectID, 
                            String command, List arguments)
        throws Exception
d154 2
a155 13
        return Task.add(onBehalfOf, COMPONENT_NAME, affectedObjectID,
                        command, arguments);
    }
    
    /**
     * Adds a task to be performed.
     */
    protected static final void add(User onBehalfOf, Domain domain, String cmd,
                                  Project project)
        throws Exception
    {
        Task.add(onBehalfOf, COMPONENT_NAME, domain.getSpecialProject().getID(),
                 cmd, project.getName());
a157 3
    /**
     * Runs this task.
     */
@


1.1.2.4
log
@* Ported runTask() method and supporting cast.
* Improved error handling via TaskException (a la TurbineException).
@
text
@d167 1
a167 1
    public static void runTask(Task t)
d170 10
a179 3
        if (!t.getComponent().equals(COMPONENT_NAME)) {
            throw new TaskException
                ("AnzuTask cannot run a task for component " + t.getComponent());
a180 6
        AnzuTask anzuTask = (AnzuTask)t;

        TaskException ue = null;
        String command = anzuTask.getCommand();
        String objectID = String.valueOf(anzuTask.getAffectedObjectId());
        String userID = String.valueOf(anzuTask.getOnBehalfOf());
d182 2
a183 1
        if (DEBUG) {
d194 1
a194 1
                throw new TaskException("Invalid userID : " + userID);
d197 1
a197 1

d202 9
a210 13
                    anzuTask.addList(objectID, user);
                }
                else if (command.equals("deletelist")) {
                    anzuTask.deleteList(objectID);
                }
                else if (command.equals("addproject")) {
                    anzuTask.addProject(objectID);
                }
                else if (command.equals("deleteproject")) {
                    anzuTask.deleteProject(objectID);
                }
                else if (command.equals("owneralias")) {
                    anzuTask.setOwnerAlias(objectID);
d218 1
a218 1
            anzuTask.setAsDone();
d223 1
a223 3
        if (ue != null) {
            throw ue;
        }
d226 1
a226 2
    private void deleteList(String projectID)
        throws TaskException
d228 1
d230 9
a238 4
            List args = readArgsArray();
            String domain = (String)args.get(0);
            String project = (String)args.get(1);
            String list = (String)args.get(2);
d245 8
a252 1
            throw new TaskException("Exception in AnzuTask deletelist", e);
d256 1
a256 2
    private void setOwnerAlias(String projectID)
        throws TaskException
d290 1
a290 2
    private void addProject(String domainID)
        throws TaskException
d308 1
a308 2
    private void deleteProject(String domainID)
        throws TaskException
d326 2
a327 3
    private void addList(String projectID, User user)
        throws TaskException
    {
d329 9
a337 4
            List args = readArgsArray();
            String list = (String)args.get(0);
            String type = (String)args.get(1);
            String description = (String)args.get(2);
d349 6
a354 1
            throw new TaskException("Exception in AnzuTask addlist", e);
@


1.1.2.5
log
@Activated the actual list creation of the addList() instance method.
@
text
@d332 6
a337 5
            MailingList ml = MailingList.add
                (user.getACL(), ProjectPeer.getInstance(projectID),
                 list, description, type);
            ml.setOwner(user);
            ml.create(user.getACL());
@


1.1.2.6
log
@* port MailingList* to the peer system
* add some necessary methods to MailingList, update AnzuTask to use those
* this compiles, but some methods still need to be implemented, so it
  doesn't all work
@
text
@d336 1
a336 1
            ml.save(user.getACL());
@


