/* ======================================================================
   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 java.util.Enumeration;

public class SentientLink extends Resource
    {

	private static final byte FLAG_NEW_WINDOW = 1;

        private PrimaryKey sentient_link_id;
	private String url, editUrl;
	private byte link_flags;

	public Class sessionClass()
	{
	  return org.bodington.server.resources.SentientLinkSessionImpl.class;
	}

	public int getResourceType() {
	  /** @todo */
//	  return Resource.RESOURCE_LINK;
	   return Resource.RESOURCE_SENTIENTLINK;
        }

	public static SentientLink findSentientLink( PrimaryKey key )
	    throws BuildingServerException
	{
	  return (SentientLink)findPersistentObject( key, "org.bodington.server.resources.SentientLink" );
	}
	public static Enumeration findSentientLinks( String where )
	    throws BuildingServerException
	{
	  return findPersistentObjects( where, "org.bodington.server.resources.SentientLink" );
	}
	public static Enumeration findSentientLinks( String where, String order )
	    throws BuildingServerException
	{
	  return findPersistentObjects( where, order, "org.bodington.server.resources.SentientLink" );
	}


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

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


	public PrimaryKey getSentientLinkId()
	{
	  return sentient_link_id;
	}

	public void setSentientLinkId(PrimaryKey key)
	{
	  sentient_link_id = key;
	  setResourceId( key );
	  setUnsaved();
	}


	public String getUrl()
	{
	  if ( url == null)
	    setUrl("");
	  return url;
	}


	public void setUrl( String s )
	{
	if ( s==null && url==null )
		return;
	if ( s!=null && url!=null && s.equals( url ) )
		return;

	url = s;
	setUnsaved();
	}

	public String getEditUrl()
	{
	  if ( editUrl == null)
	    setEditUrl("");
	  return editUrl;
	}


	public void setEditUrl( String s )
	{
	if ( s==null && editUrl==null )
		return;
	if ( s!=null && editUrl!=null && s.equals( editUrl ) )
		return;

	editUrl = s;
	setUnsaved();
	}

	public void setNewWindow(boolean newWindow)
	{
	  if ( isNewWindow() == newWindow )
	    return;

	  setLinkFlags( (byte)(getLinkFlags() ^ FLAG_NEW_WINDOW) );
	  setUnsaved();
	}

	public boolean isNewWindow()
	{
	  return (link_flags & FLAG_NEW_WINDOW) != 0;
	}

	public byte getLinkFlags()
	{
	  return link_flags;
	}

	public void setLinkFlags( byte f )
	{
	  if ( f==link_flags )
	    return;
	  link_flags=f;
	  setUnsaved();
	}
}
