/* ======================================================================
   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.resources;

import org.bodington.database.PrimaryKey;
import org.bodington.server.BuildingServerException;
import org.bodington.server.HomeSessionImpl;
import org.bodington.server.realm.User;

/**
 * Class for a users home. But then do we need to have title and descriptions? But the 
 * this breaks the search as only stuff that ends up in the metadata gets searched. 
 * @author buckett
 */
public class HomeResource extends Resource
{
    
    private PrimaryKey homeResourceId;
    private PrimaryKey  userPk;
    private transient User user;
    

    public static HomeResource findHomeResource(PrimaryKey pk)
        throws BuildingServerException
    {
        return (HomeResource) findPersistentObject(pk, HomeResource.class
            .getName());
    }
    
    public static HomeResource findHomeResource(User user, Resource container)
        throws BuildingServerException
     {
        return (HomeResource)findPersistentObject("user_id = "+ user.getUserId()+
            " AND parent_resource_id = "+ container.getResourceId(),
            HomeResource.class.getName());
     }
        
    public PrimaryKey getPrimaryKey()
    {
        return getHomeResourceId();
    }
    
    public void setPrimaryKey(PrimaryKey pk)
    {
        setHomeResourceId(pk);
    }
    
    public PrimaryKey getHomeResourceId()
    {
        return homeResourceId;
    }

    public void setHomeResourceId(PrimaryKey homeContainerId)
    {
        this.homeResourceId = homeContainerId;
        setResourceId(homeContainerId);
    }
    
    public String getTitle()
    {
        return "Home Space for "+ getUser();
    }
    
    public User getUser()
    {
        if (user == null)
        {
            try
            {
                user = User.findUser(userPk);
            }
            catch (BuildingServerException bse)
            {}  
        }
        return user;
    }
    
    public void setUserId(PrimaryKey pk)
    {
        this.userPk = pk;
        this.user = null;
        setUnsaved();
    }
    
    public PrimaryKey getUserId()
    {
        return this.userPk;
    }

    public int getResourceType()
    {
        return RESOURCE_HOME;
    }
    
    public Class sessionClass()
    {
    	return HomeSessionImpl.class;
    }

}
