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


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

1.4
date	2001.08.27.08.41.27;	author dlr;	state Exp;
branches;
next	1.3;

1.3
date	2001.08.11.09.08.02;	author dlr;	state Exp;
branches;
next	1.2;

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

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


desc
@@


1.5
log
@Updated for latest Turbine API changes.
@
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.net.URL;
import java.net.MalformedURLException;
import java.util.*;

import org.apache.torque.util.BasePeer;
import org.apache.torque.util.Criteria;
import org.apache.commons.util.ObjectUtils;
import org.apache.commons.util.StringUtils;
import org.apache.turbine.ParameterParser;
import org.apache.turbine.util.Log;
import org.apache.commons.util.exception.NestableException;
import org.apache.torque.pool.DBConnection;

import org.tigris.helm.om.*;
import org.tigris.helm.security.AccessControlList;
import org.tigris.helm.security.ACLException;
import org.tigris.helm.tool.TagValidator;
import org.tigris.helm.tool.audit.AuditLog;

/** 
 * Business object representing a license.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 */
public class License extends BaseLicense 
{
    /**
     * Description of the audit log entry created when a license is added.  
     */
    private static final String AUDIT_LOG_ON_ADD = "License added: ";

    /**
     * Description of the audit log entry created when a license is
     * edited.  
     */
    private static final String AUDIT_LOG_ON_EDIT = "License edited: ";

    /**
     * Description of the audit log entry created when a license is deleted.
     */
    private static final String AUDIT_LOG_ON_DELETE = "License deleted: ";

    /**
     * Returns the projects using this license.
     */
    public List projectsUsing()
        throws Exception
    {
        Criteria c = new Criteria(1)
            .add(ProjectPeer.LICENSE_ID, getID());
        return ProjectPeer.doSelect(c);
    }

    /**
     * Returns true iff this license is in a deletable state.
     */
    public boolean isDeletable()
        throws Exception
    {
        return (projectsUsing().size() == 0);
    }

    /**
     * Returns true iff the description of this license consists
     * entirely of a URL.
     */
    public boolean describedByURL()
    {
        boolean isURL = true;
        try 
        {
            URL test = new URL(getDescription());
        }
        catch (MalformedURLException e)
        {
            isURL = false;
        }
        return isURL;
    }

    /**
     * Returns true iff the description of this license consists
     * of HTML.
     */
    public boolean describedByHTML()
    {
        return new TagValidator().containsHTML(getDescription());
    }

    /**
     * Create a new License with the specified name and description.
     */
    public static License add(AccessControlList acl, Project project,
                               String name, String description)
        throws ACLException, Exception
    {
        License license = new License();
        license.setName(name);
        license.setDescription(description);
        license.setProject(project);
        license.save(acl, project);
        AuditLog.logEvent
            (acl.getUser(), project, AUDIT_LOG_ON_ADD + license.getName());
        return license;
    }

    /**
     * Set this license's pattern and/or description.
     */
    public void update (AccessControlList acl, String name, 
                        String description)
        throws ACLException, Exception
    {
        Project project = getProject();
        String oldString = getName();
        if (name != null)
        {
            setName(name);
        }
        if (description != null)
        {
            setDescription(description);
        }
        save(acl, project);
        AuditLog.logEvent(acl.getUser(), project, AUDIT_LOG_ON_EDIT 
                          + oldString + " -> " + getName());
    }

    /**
     * Deletes a license.
     */
    public void delete(AccessControlList acl)
        throws ACLException, Exception
    {
        acl.assertPermission("Licenses - Administer", getProject());

        Project associatedProject = getProject();
        String name = getName();

        int times = projectsUsing().size();
        if (!isDeletable())
        {            
            StringBuffer err = new StringBuffer
                ("This license is currently in use by ")
                .append(times).append("project").append(times==1?"":"s")
                .append(". You cannot delete a license which is in use.");
            throw new Exception(err.toString());
        }
        
        //Delete the license.
        Criteria thisLicense = new Criteria(1).add
            (LicensePeer.LICENSE_ID, getID());
        LicensePeer.doDelete(thisLicense);

        AuditLog.logEvent(acl.getUser(), associatedProject, 
                          AUDIT_LOG_ON_DELETE + name);
    }

    //
    // Overridden methods.
    //

    private void save(AccessControlList acl, Project project)
        throws Exception, ACLException
    {
        acl.assertPermission("Licenses - Administer", project);
        super.save();
        uncache();
    }
}
@


1.4
log
@Updated for Turbine API change.
@
text
@d57 1
a57 1
import org.apache.fulcrum.util.parser.ParameterParser;
@


1.3
log
@Upgrading to Turbine 3.0-dev, Fulcrum 1.0, and Torque 1.0.
@
text
@d57 1
a57 1
import org.apache.turbine.util.ParameterParser;
@


1.2
log
@Removed Joist dependancies.
@
text
@d53 4
a56 5
import org.apache.turbine.om.*;
import org.apache.turbine.om.peer.BasePeer;
import org.apache.turbine.util.db.Criteria;
import org.apache.turbine.util.ObjectUtils;
import org.apache.turbine.util.StringUtils;
d59 2
a60 2
import org.apache.turbine.util.TurbineException;
import org.apache.turbine.util.db.pool.DBConnection;
@


1.1
log
@Licenses are now contained in a database-managed table rather than in a custom Java class (yay!)
@
text
@a52 2
import org.joist.util.TagValidator;

d66 1
@

