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



/**
 * This class is used to represent a metadatum associated with
 * another object in the database.
 * 
 * @author Jon Maber
 * @version 1.0
 */
public class Metadatum extends org.bodington.sqldatabase.SqlPersistentObject
	{
	private PrimaryKey metadatum_id;
	private PrimaryKey object_id;
	private String name;
	private String value;
	

	private MetadatumIndexKey[] ikeys;


	public static Metadatum findMetadatum( PrimaryKey key )
	    throws BuildingServerException
	    {
	    return (Metadatum)findPersistentObject( key, "org.bodington.text.Metadatum" );
	    }
	
	public static Metadatum findMetadatumByIdAndName( PrimaryKey id, String name )
	    throws BuildingServerException
	    {
	    MetadatumIndexKey ikey = new MetadatumIndexKey();
	    ikey.setObjectId( id );
	    ikey.setName( name );
	    return (Metadatum)findPersistentObject( ikey, "org.bodington.text.Metadatum" );
	    }

	public Metadatum()
		{
		ikeys = new MetadatumIndexKey[1];
		ikeys[0] = new MetadatumIndexKey();
		}

    /**
     * Quick constructuor that creates a Metadatum ready to save.
     * @param objectId The primary key of the object to associate the metadata
     * with.
     * @param name The name of the metedatum item.
     * @param value The value to save.
     */
    public Metadatum(PrimaryKey objectId, String name, String value)
        {
        this();
        setObjectId(objectId);
        setName(name);
        setValue(value);
        }

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

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

	public PrimaryKey getMetadatumId()
		{
		return metadatum_id;
		}
		
	public void setMetadatumId( PrimaryKey key )
		{
		metadatum_id = key;
    	setUnsaved();
		}
		
	public PrimaryKey getObjectId()
		{
		return object_id;
		}
		
	public void setObjectId( PrimaryKey key )
		{
		object_id = key;
       	ikeys[0].setObjectId( key );
    	setUnsaved();
		}
		
	public String getName()
		{
		return name;
		}

	public void setName( String m )
		{
		name = m;
       	ikeys[0].setName( m );
    	setUnsaved();
		}
		
	public String getValue()
		{
		return value;
		}

	public void setValue( String m )
		{
		value = m;
    	setUnsaved();
		}
		
	public String toString()
		{
		return getValue();
		}

	public IndexKey[] getIndexKeys()
	    {
	    if ( ikeys[0].getObjectId() == null || ikeys[0].getName() == null)
	        return null;

	    return ikeys;
	    }
	    
	public boolean matchesKey( IndexKey testikey )
	    {
	    return testikey.equals( ikeys[0] );
	    }
	
	}
