/*
 * Copyright (C) 2003-2004 Red Hat Inc. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
package com.arsdigita.search.filters;

import com.arsdigita.search.FilterSpecification;
import com.arsdigita.kernel.Party;
import com.arsdigita.kernel.permissions.PermissionManager;
import com.arsdigita.kernel.permissions.PrivilegeDescriptor;
import com.arsdigita.domain.DomainObjectFactory;
import com.arsdigita.persistence.OID;


/**
 * A filter specification for supplying a party
 * and privilege to filter against.
 */
public class PermissionFilterSpecification extends FilterSpecification {
    
    public final static String PARTY = "party";
    public final static String PRIVILEGE = "privilege";

    /**
     * Creates a new permission filter spec with
     * 'The Public' party and the READ privilege
     */
    public PermissionFilterSpecification() {
        this(lookupPublic());
    }
    
    /**
     * Creates a new permission filter spec for a
     * party with READ privilege
     * @param party the party to check permissions for
     */
    public PermissionFilterSpecification(Party party) {
        this(party, PrivilegeDescriptor.READ);
    }
    
    /**
     * Creates a new permission filter spec for a
     * party with a specified privilege
     * @param party the party to check permissions for
     * @param privilege the privilege to check
     */
    public PermissionFilterSpecification(Party party,
                                         PrivilegeDescriptor privilege) {
        super(new Object[] { PARTY, party, PRIVILEGE, privilege },
              new PermissionFilterType());
    }
    
    /**
     * Returns the party to check permissions for
     * @return the party to check
     */
    public Party getParty() {
        return (Party)get(PARTY);
    }
    
    /**
     * Returns the privilege to check against
     * @return the privilege
     */
    public PrivilegeDescriptor getPrivilege() {
        return (PrivilegeDescriptor)get(PRIVILEGE);
    }

    private static Party lookupPublic() {
        OID oid = new OID(Party.BASE_DATA_OBJECT_TYPE,
                          PermissionManager.VIRTUAL_PUBLIC_ID);
        
        return (Party)DomainObjectFactory.newInstance(oid);
    }
}
