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

import org.bodington.database.PrimaryKey;
import org.bodington.database.IndexKey;

public class MetadatumIndexKey
    implements IndexKey
    {
    PrimaryKey object_id;
    String name;

    MetadatumIndexKey()
        {
        object_id = null;
        name = null;
        }
    
    public boolean cacheNull()
        {
        return true;
        }
        
    public String whereClause()
        {
        StringBuffer where = new StringBuffer();
        
        where.append( "object_id = " );
        if ( object_id == null )
            where.append( "NULL" );
        else
            where.append( object_id.toString() );
        where.append( " AND name = " );
        if ( name == null )
            where.append( "NULL" );
        else
            {
            where.append( "'" );
            where.append( name );
            where.append( "'" );
            }
        
        return where.toString();
        }
    
    public void setObjectId(PrimaryKey key)
    	{
    	object_id = key;
    	}

    public PrimaryKey getObjectId()
    	{
    	return object_id;
	    }

    public void setName(String n)
    	{
    	name = n;
    	}

    public String getName()
    	{
    	return name;
	    }

    
    public boolean equals(Object obj)
        {
        if ( !(obj instanceof MetadatumIndexKey) )
            return false;

        MetadatumIndexKey other = (MetadatumIndexKey)obj;
        
        if ( other.object_id==null || this.object_id==null || 
             other.name==null || this.name==null    )
            return false;
        
        return    other.object_id.equals( this.object_id   ) && 
                other.name.equals( this.name );
        }

    public int hashCode()
        {
        int a = object_id.intValue();
        int b = name.hashCode();
        return a ^ b;
        }
    
    public String toString()
        {
        return "Object ID: "+ object_id+ " Name: "+ name;
        }

    }
    
