/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000-2001 The Apache Software Foundation.  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 acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" and
 *     "Apache Jetspeed" must not be used to endorse or promote products
 *    derived from this software without prior written permission. For
 *    written permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache" or
 *    "Apache Jetspeed", nor may "Apache" appear in their name, without
 *    prior written permission of the Apache Software Foundation.
 *
 * 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 THE APACHE SOFTWARE FOUNDATION 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 the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.jetspeed.om.security.ldap;

import javax.naming.directory.BasicAttributes;
import org.apache.jetspeed.om.security.Permission;
import org.apache.jetspeed.services.JetspeedLDAP;
import org.apache.jetspeed.services.ldap.LDAPURL;
import org.apache.jetspeed.services.security.PermissionException;

/**
 *
 * @author <a href="mailto:ender@kilicoglu.nom.tr">Ender KILICOGLU</a>
 * @author <a href="mailto:sami.leino@netorek.fi">Sami Leino</a>
 *
 * @version $Id: LDAPPermission.java,v 1.4 2003/01/28 04:49:43 taylor Exp $ 
 * 
 */
public class LDAPPermission extends BaseLDAPObject implements Permission {

    // ---------------------------- Constants ----------------------------

    protected static final String OBJECT_CLASS          = "jetspeedpermission";
    protected static final String ORGANIZATIONAL_UNIT   = "ou=permissions";

    protected static final String ATTR_PERMISSION_NAME  = "permissionname";
    protected static final String ATTR_PERMISSION_ID    = "uid";

    // ------------------------- Member variables ------------------------

    protected String name                               = null;
    protected String id                                 = null;
    protected boolean isNew                             = true;

    // --------------------------- Constructors --------------------------

    public LDAPPermission()
    {
        isNew = true;
    }

    public LDAPPermission(String id)
    {
        this.setId(id);
        isNew = true;
    }

    public LDAPPermission(String name, boolean isNew)
    {
		name = super.createId(name);
        super.ldapurl = JetspeedLDAP.buildURL(ATTR_PERMISSION_ID + "=" + name + "," + ORGANIZATIONAL_UNIT);
        this.isNew = isNew;

        if (isNew)
        {
            this.setName(name);
            super.myAttrs = new BasicAttributes();
            myAttrs.put(ATTR_PERMISSION_ID, id);
            myAttrs.put(ATTR_PERMISSION_NAME, name);
            super.setObjectClass(OBJECT_CLASS);
        }
        else
        {
            super.myAttrs = JetspeedLDAP.read(ldapurl);
            this.id = super.getutil(ATTR_PERMISSION_ID);
            this.name = super.getutil(ATTR_PERMISSION_NAME);
        }
    }

    public LDAPPermission(LDAPURL ldapurl)
    {
        super.ldapurl = ldapurl;
        super.myAttrs = JetspeedLDAP.read(ldapurl);
        this.id = super.getutil(ATTR_PERMISSION_ID);
        this.name = super.getutil(ATTR_PERMISSION_NAME);
    }

    // --------------------- Persistence operations ----------------------

    public void update(boolean create)
	throws PermissionException
    {
        removeutil("createTimeStamp", false);
        removeutil("modifyTimeStamp", false);

        if (create)
        {
            if (JetspeedLDAP.addEntry(super.ldapurl, super.myAttrs) == false) throw new PermissionException("Could not insert permission in LDAP!");
        }
        else if (JetspeedLDAP.exists(super.ldapurl))
        {
            JetspeedLDAP.deleteAttrs(super.ldapurl, super.rmAttrs);
            if (JetspeedLDAP.updateEntry(super.ldapurl, super.myAttrs) == false) throw new PermissionException("Could not update permission in LDAP!");
        }
    }

    // ------------------------ Accessor methods -------------------------

    /**
     * Get the name of the Permission
     *
     * @return the name of the permission.
     */
    public String getName()
    {
        return name;
    }
 
    /**
     * Set the name of the Permission
     *
     * @param permissionName the name of the Permission.
     */
    public void setName(String permissionName)
    {
    	setId(permissionName);
        name = super.createId(permissionName);
    }

    /**
     * Get the id of the Permission
     *
     * @return the id of the permission.
     */
    public String getId()
    {
        return id;
    }

    /**
     * Set the id of the Permission
     *
     * @param id the new id for the permission
     */
    public void setId(String id)
    {      
        if (this.id == null)
        {
            this.id = super.createId(id);
        }
    }

    public boolean isNew()
    {
        return isNew;
    }

    void setNew(boolean isNew)
    {
        this.isNew = isNew;
    }

}