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


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

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

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

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

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

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


desc
@@


1.6
log
@Made project deletions soft.
@
text
@/* ================================================================
 * Copyright (c) 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.om;

import java.lang.*;
import java.util.*;

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

import org.tigris.helm.AlreadyExists;
import org.tigris.helm.om.*;
import org.tigris.helm.om.profile.ProjectProfile;
import org.tigris.helm.security.AccessControlList;
import org.tigris.helm.security.ACLException;
import org.tigris.helm.tool.DBFlag;
import org.tigris.helm.tool.audit.AuditLog;
import org.tigris.helm.tool.paginate.PeerBackedList;

/** 
 * A project which can have other projects associated with it.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 */
public class AssociationTargetProject extends Project
{
    
    /**
     * The types of project which can be associated with this project.
     */
    protected static String[] VALID_ASSOCIATION_TYPES = {REGULAR_TYPE};

    /**
     * The permission required to associate a project with this
     * project.
     */
    private static String PROJECT_ASSOCIABILITY_PERMISSION 
        = "Project - Associate With Project";

    /**
     * Prefix of the audit log entry to create upon a project's association
     * with this project.
     */
    private static final String AUDIT_LOG_ON_ASSOCIATE 
        = "Project associated with project";

    /**
     * Prefix of the audit log entry to create upon a project's
     * disassociation from this project.  
     */
    private static final String AUDIT_LOG_ON_DISASSOCIATE
        = "Project disassociated from project";

    /**
     * Returns the types of project which can be associated with this
     * project. 
     */
    protected String[] getValidAssociationTypes()
    {
        return this.VALID_ASSOCIATION_TYPES;
    }

    /**
     * Returns the permission neccessary to associate a project with
     * this project.  
     */
    protected String getProjectAssociabilityPermission()
    {
        return this.PROJECT_ASSOCIABILITY_PERMISSION;
    }

    /**
     * Throws an exception if the given project is not of a type
     * which can be associated with this project.
     */
    public void assertAssociatableTypeOf(Project p) 
        throws NestableException
    {
        if (isOfAssociatableType(p))
        {
            throw new NestableException
                ("A project of type " + p.getType()
                 + " cannot be associated with a project of type " 
                 + getType());
        }
    }

    /**
     * Returns true iff the given project is of a type which can
     * be associated with this project.
     */
    public boolean isOfAssociatableType(Project p)
    {
        boolean ok = false;
        String type = p.getType();
        String[] validTypes = getValidAssociationTypes();
        for (int i = 0; i < validTypes.length; i++)
        {
            if (type.equals(validTypes[i]))
            {
                ok = true;
                break;
            }
        }
        return ok;
    }
    
    /**
     * Associates the given project with this project.
     */
    public void associate(AccessControlList acl, Project toAssociate) 
        throws ACLException, Exception
    {
        acl.assertPermission("Project - Associate With Project", toAssociate);
        acl.assertPermission(getProjectAssociabilityPermission(), this);
        associate(toAssociate);

        toAssociate.assertNondeletion();

        User onBehalfOf = acl.getUser();
        StringBuffer auditLogMessage = 
            new StringBuffer(this.AUDIT_LOG_ON_ASSOCIATE);
        auditLogMessage.append(toAssociate.getName()).append(" -> ")
            .append(getName());
        AuditLog.logEvent(onBehalfOf, auditLogMessage.toString());
    }

    /**
     * Associates the given project with this project. Does not
     * do permission checks or audit logging.
     */    
    protected void associate(Project toAssociate) 
        throws Exception
    {
        if (!hasDirectChild(toAssociate))
        {
            addChild(toAssociate);
        }
    }

    /**
     * Disassociates the given project from this project.
     */
    public void disassociate(AccessControlList acl, Project toDisassociate) 
        throws ACLException, Exception
    {
        acl.assertPermission("Project - Associate Project", toDisassociate);
        acl.assertPermission(getProjectAssociabilityPermission(), 
                             this);

        toDisassociate.assertNondeletion();

        disassociate(toDisassociate);

        User onBehalfOf = acl.getUser();
        StringBuffer auditLogMessage = 
            new StringBuffer(this.AUDIT_LOG_ON_DISASSOCIATE);
        auditLogMessage.append(toDisassociate.getName()).append(" <- ")
            .append(getName());
        AuditLog.logEvent(onBehalfOf, auditLogMessage.toString());
    }

    /**
     * Disassociates the given project from this project. Does not
     * do permission checks or audit logging.
     */    
    protected void disassociate(Project toDisassociate) 
        throws Exception
    {
        if (hasDirectChild(toDisassociate))
        {
            removeChild(toDisassociate);
        }
    }

    /**
     * Adds a new project of this type, with some initial elements.
     */
    public static AssociationTargetProject add 
        (AccessControlList acl, ProjectProfile profile, Project parent, 
         String name, String isPublic, String shortDescription, 
         String longDescription, List initialElements)
        throws AlreadyExists, ACLException, Exception
    {
        License license = null;
        String useIndex = DBFlag.OFF;
        String status = "";
        String aliasURL = "";
        User proposedBy = acl.getUser();
        User projectOwner = acl.getUser();        
        AssociationTargetProject newProject =
            (AssociationTargetProject)Project.add
            (acl, profile, parent, name, license, isPublic, 
             shortDescription, longDescription, useIndex, status, aliasURL,
             proposedBy, projectOwner);
        for (Iterator i = initialElements.iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            newProject.associate(p);
        }
        return newProject;
    }         

    /**
     * Change the description information of a category.
     */
    public void updateInfo(AccessControlList acl, String shortDescription,
                           String longDescription)
        throws ACLException, Exception
    {
        assertEditPermission(acl);
        setShortDescription(shortDescription);
        setLongDescription(longDescription);
        save();
    }

    /**
     * Deletes the project.
     */
    public void delete(AccessControlList acl) 
        throws Exception
    {
        acl.assertPermission("Project - Delete", getDomain());
        for (Iterator i = getAssociatedProjects().iterator(); i.hasNext();)
        {
            Project p = (Project)i.next();
            disassociate(p);            
        }
        super.delete(acl);
    }

    /**
     * Returns the projects associated with this project. Does
     * not do ACL-based filtering.
     */
    protected List getAssociatedProjects()
        throws Exception
    {
        return getAssociatedProjects(null);
    }

    /**
     * Returns the projects associated with this project and viewable
     * by the provided ACL.
     */
    public List getAssociatedProjects(AccessControlList acl) 
        throws Exception
    {
        return getAssociatedProjects(null, null, acl);
    }

    /**
     * Returns the projects associated with this project and viewable
     * by the provided ACL.
     */
    public PeerBackedList getAssociatedProjects
        (Integer itemsPerPage, Criteria filter, AccessControlList acl) 
        throws Exception
    {
        return getAssociatedProjects(itemsPerPage, null, filter, acl);
    }

    /**
     * Returns the projects associated with this project and viewable
     * by the provided ACL.
     */
    public PeerBackedList getAssociatedProjects
        (Integer itemsPerPage, Criteria base, Criteria filter,
         AccessControlList acl)
        throws Exception
    {
        return getDirectChildren(itemsPerPage, base, filter, acl,
                                 getValidAssociationTypes());
    }

    /**
     * Returns the number of projects associated with this project.
     */
    public int getNumberOfAssociations(AccessControlList acl) 
        throws Exception
    {
        return getAssociatedProjects(acl).size();
    }

    /**
     * Returns true iff this project has an association with the
     * given project.
     */
    public boolean hasAssociationWith(Project p) 
        throws Exception
    {
        return getAssociatedProjects().contains(p);
    }

    /**
     * Returns the projects which can be associated with this project.
     */
    public PeerBackedList getAssociableProjects
        (Integer itemsPerPage, Criteria filter, AccessControlList acl)
        throws Exception
    {
        List associatedProjects = getAssociatedProjects();
        String[] associatedIDs = new String[associatedProjects.size()];
        for (int i = 0; i < associatedIDs.length; i++)
        {
            Project p = (Project)associatedProjects.get(0);
            associatedIDs[i] = p.getID();
        }

        Criteria base = new Criteria(2);
        if (associatedIDs.length > 0)
        {
            base.addNotIn(ProjectPeer.PROJECT_ID, associatedIDs);
        }
        String[] types = getValidAssociationTypes();
        if (types.length > 0)
        {
            base.addIn(ProjectPeer.TYPE, types);
        }
        base.setDistinct();
        PeerBackedList unfiltered = new PeerBackedList
            (acl, ProjectPeer.class, itemsPerPage, base, filter);
        String perm = getProjectAssociabilityPermission();
        if (!perm.equals("Project - View"))
        {
            List toRemove = new ArrayList(unfiltered.size());
            unfiltered.fillListToPage(new Integer(0));
            for (Iterator i = unfiltered.iterator(); i.hasNext();)
            {
                Project p = (Project)i.next();
                if (!acl.hasPermission(perm, p))
                {
                    toRemove.add(p);
                }
            }
            unfiltered.removeAll(toRemove);            
        }
        return unfiltered;
    }

    /**
     * @@deprecated use getAssociatedProjects instead.
     */
    protected List getProjects() 
        throws Exception
    {
        return getAssociatedProjects();
    }

    /**
     * @@deprecated use getAssociatedProjects instead.
     */
    public List getProjects(AccessControlList acl) 
        throws Exception
    {
        return getAssociatedProjects(acl);
    }

    /**
     * @@deprecated use getAssociatedProjects instead.
     */
    public PeerBackedList getProjects
        (Integer itemsPerPage, Criteria filter, AccessControlList acl) 
        throws Exception
    {
        return getAssociatedProjects(itemsPerPage, filter, acl);
    }

    /**
     * @@deprecated use associate instead.
     */
    public void addProject(AccessControlList acl, Project p) 
        throws ACLException, Exception
    {
        associate(acl, p);
    }

    /**
     * @@deprecated use disassociate instead.
     */
    public void removeProject(AccessControlList acl, Project p) 
        throws ACLException, Exception
    {
        disassociate(acl, p);
    }

    /**
     * @@deprecated use hasAssociationWith instead.
     */
    public boolean containsProject(Project p) 
        throws Exception
    {
        return hasAssociationWith(p);
    }

    /**
     * @@deprecated use getNumberOfAssociations instead.
     */ 
    public final int getNumberOfProjects(AccessControlList acl) 
        throws Exception
    {
        return getNumberOfAssociations(acl);
    }
}

@


1.5
log
@Upgrading to Turbine 3.0-dev, Fulcrum 1.0, and Torque 1.0.
@
text
@d163 2
d195 3
@


1.4
log
@Corrected order of permissions.
@
text
@d53 2
a54 2
import org.apache.turbine.util.TurbineException;
import org.apache.turbine.util.db.Criteria;
d122 1
a122 1
        throws TurbineException
d126 1
a126 1
            throw new TurbineException
@


1.3
log
@Licenses are now contained in a database-managed table rather than in a custom Java class (yay!)
@
text
@d159 2
a160 2
        acl.assertPermission("Project - Associate Project", this);
        acl.assertPermission(getProjectAssociabilityPermission(), toAssociate);
d190 1
a190 1
        acl.assertPermission("Project - Associate Project", this);
d192 1
a192 1
                             toDisassociate);
@


1.2
log
@How about calling the Project method instead of going into infinite recursion.
@
text
@d225 1
a225 1
        String license = "";
@


1.1
log
@Argh, forgot to commit earlier.
@
text
@d61 1
d225 6
d232 4
a235 3
            (AssociationTargetProject)AssociationTargetProject.add
            (acl, profile, parent, name, isPublic, 
             shortDescription, longDescription, initialElements);
@

