/* Copyright 2005 Yale University.  All rights reserved. 
*
 *  THIS SOFTWARE IS PROVIDED "AS IS," AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE EXPRESSLY
 *  DISCLAIMED. IN NO EVENT SHALL YALE UNIVERSITY OR ITS EMPLOYEES BE
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED, THE COSTS OF
 *  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 IN ADVANCE OF THE POSSIBILITY OF SUCH
 *  DAMAGE.
 *
 *  Redistribution and use of this software in source or binary forms,
 *  with or without modification, are permitted, provided that the
 *  following conditions are met:
 *
 *  1. Any redistribution must include the above copyright notice and
 *  disclaimer and this list of conditions in any related documentation
 *  and, if feasible, in the redistributed software.
 *
 *  2. Any redistribution must include the acknowledgment, "This product
 *  includes software developed by Yale University," in any related
 *  documentation and, if feasible, in the redistributed software.
 *
 *  3. The names "Yale" and "Yale University" must not be used to endorse
 *  or promote products derived from this software.
 *  */

package edu.yale.its.portal.security.provider;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portal.UserIdentityStoreFactory;
import org.jasig.portal.security.IPerson;
import org.jasig.portal.security.InitialSecurityContextFactory;
import org.jasig.portal.security.PersonFactory;
import org.jasig.portal.security.provider.PersonImpl;

/**
 * @author susan.bramhall@yale.edu
 * @version $Revision: 1.3 $
 * @date Jun 14, 2005
 *
 */
public class YalePersonFactory extends PersonFactory {
    
    /**
     * Creates an empty <code>IPerson</code> implementation. 
     * @return an empty <code>IPerson</code> implementation
     */
    static final Log log = LogFactory.getLog(PersonFactory.class);
    
    public static IPerson createPerson() {
        return new YalePersonImpl();
    }
    /**
     * Creates a <i>guest</i> user.
     * @return <i>guest</i> user
     * @throws Exception
     * Determine guest id and create guest user 
     */
    public static YalePersonImpl createGuestPerson(String userName) throws Exception {
       
        YalePersonImpl person = (YalePersonImpl) createPerson();
        person.setAttribute(IPerson.USERNAME, userName);
        try {
            person.setID(UserIdentityStoreFactory.getUserIdentityStoreImpl().getPortalUID(person));
        } catch (Exception e) {
            log.error("YalePersonImpl Error from getPortalUID setting IPerson ID for " + userName,e);
            throw e;
        }
        person.setSecurityContext(InitialSecurityContextFactory.getInitialContext("root"));
        person.setGuest(true);
        return person;
    }

    public static IPerson createGuestPerson() throws Exception {
        YalePersonImpl person = createGuestPerson(GUEST_USERNAME);
        return person;
    }
}
