/* ======================================================================
   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.IndexKey;
import org.bodington.database.PrimaryKey;
import org.bodington.sqldatabase.SqlDatabase;

/**
 * Class so that we can do index lookups on the parent resource and
 * the name of the child. This should mean that a resource doesn't have to 
 * load all it's children to find one of them.
 * @author buckett
 */
public class ResourceParentIndexKey implements IndexKey
{
    
    private PrimaryKey parent;
    private String child;
    private int hash = 0;

    public ResourceParentIndexKey ()
    {}
    
    public ResourceParentIndexKey (PrimaryKey parent, String child)
    {
        setParent(parent);
        setChild(child);
    }
    
    public void setParent(PrimaryKey parent)
    {
        this.parent = parent;
        this.hash = 0;
    }
    
    public void setChild (String child)
    {
        this.child = child;
        this.hash = 0;
    }
    
    public String whereClause()
    {
        if (parent == null)
            return "parent_resource_id IS NULL AND name = "+ SqlDatabase.quotedSQL(child);
        else
            return "parent_resource_id = "+ parent+ " AND name = "+ SqlDatabase.quotedSQL(child);
    }

    /**
     * We want to cache <code>nulls</code> so that lookups for files in 
     * resources are cached.
     */
    public boolean cacheNull()
    {
        return true;
    }
    
    public int hashCode()
    {
        if (hash == 0)
        {
            if (parent == null)
                hash = child.hashCode();
            else
                hash = parent.intValue() ^ child.hashCode();
        }
        return hash;
    }
    
    public boolean equals (Object other)
    {
        if (other instanceof ResourceParentIndexKey)
        {
            ResourceParentIndexKey otherIndex = (ResourceParentIndexKey)other;
            boolean sameChild = this.child.equals(otherIndex.child);
            boolean sameParent = (this.parent != null &&
                otherIndex.parent != null && this.parent.equals(otherIndex.parent))
                ||
                (this.parent == null && otherIndex.parent == null);
            return sameChild && sameParent;
        }
        return false;
    }
    
    public String toString ()
    {
        return "Parent: "+ parent+ ", Child: "+ child;
    }

}
