/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.server.events;

import org.bodington.server.*;
import org.bodington.server.resources.Resource;
import org.bodington.sqldatabase.*;
import org.bodington.database.*;
import org.bodington.server.realm.User;

import java.util.Enumeration;


/**
 * Class that is used to track which resources a user is interested in recieving
 * notifications about.
 * @author Jon Maber
 */
public class ResourceEventUser extends org.bodington.sqldatabase.SqlPersistentObject
	{

	/**
	 * Primary key of this entry.
	 */
	private PrimaryKey resource_event_user_id;

	/**
	 * The user_id of the user this entry refers to.
	 */
	private PrimaryKey user_id;
	private PrimaryKey resource_id;


	

	public static ResourceEventUser findResourceEventUser( PrimaryKey key )
	    throws BuildingServerException
	    {
	    return (ResourceEventUser)findPersistentObject( key, "org.bodington.server.events.ResourceEventUser" );
	    }
	
	public static ResourceEventUser findResourceEventUser( String where )
	    throws BuildingServerException
	    {
	    return (ResourceEventUser)findPersistentObject( where, "org.bodington.server.events.ResourceEventUser" );
	    }
	
	public static Enumeration findResourceEventUsers( String where )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, "org.bodington.server.events.ResourceEventUser" );
	    }


    public void setPrimaryKey(PrimaryKey key)
	    {
        setResourceEventUserId( key );
    	}

    public PrimaryKey getPrimaryKey()
    	{
        return getResourceEventUserId();
    	}

    public void setResourceEventUserId( PrimaryKey key )
	    {
	    resource_event_user_id = key;
	    setUnsaved();
	    }

    public PrimaryKey getResourceEventUserId()
	    {
        return resource_event_user_id;
	    }
	

    public void setUserId( PrimaryKey key )
	    {
	    user_id = key;
	    setUnsaved();
	    }

    public PrimaryKey getUserId()
	    {
        return user_id;
	    }
	
	public void setUser( User u )
		{
		setUserId( u.getUserId() );
		}
	public User getUser()
		throws BuildingServerException
		{
		return User.findUser( user_id );
		}



	public PrimaryKey getResourceId()
		{
		return resource_id;
		}
		
    public void setResourceId(PrimaryKey key)
    	{
    	resource_id = key;
        setUnsaved();
    	}

	public Resource getResource()
		throws BuildingServerException
		{
		if ( resource_id == null )
			return null;
		return (Resource)Resource.findResource( resource_id );
		}

	public void setResource( Resource r )
		{
		setResourceId( r.getResourceId() );
		}

	}
