/* ======================================================================
   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 java.util.Enumeration;
import org.bodington.database.PrimaryKey;
import org.bodington.server.BuildingServerException;
import org.bodington.server.resources.Resource;

/**
 * Class that encapsulates a quicklink. This is a tool that allows a user to
 * create a link to external websites and internal Bodington resources. In the
 * latter case the <code>resourceId</code> property is stored and converted
 * back to a URL so that the link can be maintained when the resource is moved.
 * If the resource is deleted (i.e. put in the recycling building) a warning is
 * given.
 * @author Antony Corfield
 */
public class QuickLink extends Resource
{
    private static final byte QL_FLAG_IS_OPEN_NEW_WINDOW = 1;
    private static final byte QL_FLAG_IS_SHOW_INTERMEDIATE_PAGE = 2;
    private static final byte QL_FLAG_IS_USE_EXISTING_FRAMESET = 4;

    private PrimaryKey quick_link_id, resource_id_link;
    private String url;
    private byte link_flags;

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

    public int getResourceType()
    {
        return RESOURCE_QUICKLINK;
    }

    public static QuickLink findQuickLink( PrimaryKey key )
        throws BuildingServerException
    {
        return (QuickLink)findPersistentObject( key,
            "org.bodington.server.resources.QuickLink" );
    }

    public static Enumeration findQuickLinks( String where )
        throws BuildingServerException
    {
        return findPersistentObjects( where,
            "org.bodington.server.resources.QuickLink" );
    }

    public static Enumeration findQuickLinks( String where, String order )
        throws BuildingServerException
    {
        return findPersistentObjects( where, order,
            "org.bodington.server.resources.QuickLink" );
    }

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

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

    public PrimaryKey getQuickLinkId()
    {
        return quick_link_id;
    }

    public void setQuickLinkId( PrimaryKey key )
    {
        quick_link_id = key;
        setResourceId( key );
        setUnsaved();
    }

    /**
     * Indicates whether or not this is a Bodington URL. A Bodington URL is one
     * that links to an internal resource. A useful feature of these is that if
     * the resource moves, the link is still valid.
     * @return <code>true</code> if this is a Bodington URL, otherwise
     *         <code>false</code>.
     */
    public boolean isBodURL()
    {
        return resource_id_link != null;
    }

    /**
     * Get the URL. This is the URL which is encapsulated by instances of this
     * class.
     * @return the URL.
     */
    public String getURL()
    {
        return url;
    }

    /**
     * Set the URL. This is the URL which is encapsulated by instances of this
     * class.
     * @param s the URL.
     */
    public void setURL( String s )
    {
        if ( s == null && url == null ) return;
        if ( s != null && url != null && s.equals( url ) ) return;

        url = s;
        setUnsaved();
    }
    
    /**
     * Get the URL. This is the URL which is encapsulated by instances of this
     * class.
     * @return the URL.
     * @deprecated Use {@link #getURL()} instead.
     */
    public String getUrl()
    {
        return getURL();
    }

    /**
     * Set the URL. This is the URL which is encapsulated by instances of this
     * class.
     * @param s the URL.
     * @deprecated Use {@link #setURL(String)} instead.
     */
    public void setUrl( String s )
    {
        setURL( s );
    }
    
    public PrimaryKey getResourceIdLink()
    {
        return resource_id_link;
    }

    public void setResourceIdLink( PrimaryKey resId )
    {
        if ( resId == null && resource_id_link == null ) return;
        if ( resId != null && resource_id_link != null
            && resId.equals( resource_id_link ) ) return;

        resource_id_link = resId;
        setUnsaved();
    }

    /**
     * Set whether the target of the URL will open in a new browser window.
     * @param newWindow the value to be set.
     * @see #isNewWindow()
     */
    public void setNewWindow( boolean newWindow )
    {
        if ( isNewWindow() == newWindow )
            return;
        setLinkFlags( (byte)(link_flags ^ QL_FLAG_IS_OPEN_NEW_WINDOW) );
    }

    /**
     * Indicates whether the target of the URL will open in a new browser
     * window.
     * @return <code>true</code> if the target of the URL will open in a new
     *         browser window, otherwise <code>false</code>.
     */
    public boolean isNewWindow()
    {
        return (link_flags & QL_FLAG_IS_OPEN_NEW_WINDOW) != 0;
    }
    
    /**
     * Set whether an intermediate page will be presented to the user.
     * @param value the value to be set.
     * @see #isShowIntermediatePage()
     */
    public void setShowIntermediatePage( boolean value )
    {
        if ( isShowIntermediatePage() == value )
            return;
        setLinkFlags( (byte)(link_flags ^ QL_FLAG_IS_SHOW_INTERMEDIATE_PAGE) );
    }

    /**
     * Indicates whether an intermediate page will be presented to the user.
     * This intermediate page will contain the (hyper)link itself. 
     * @return <code>true</code> if an intermediate page will be presented to 
     * the user, otherwise <code>false</code>.
     */
    public boolean isShowIntermediatePage()
    {
        return (link_flags & QL_FLAG_IS_SHOW_INTERMEDIATE_PAGE) != 0;
    }
    
    /**
     * Set whether the existing frameset should be used.
     * @param value the value to be set.
     * @see #isUseExistingFrameset()
     */
    public void setUseExistingFrameset( boolean value )
    {
        if ( isUseExistingFrameset() == value )
            return;
        setLinkFlags( (byte)(link_flags ^ QL_FLAG_IS_USE_EXISTING_FRAMESET) );
    }

    /**
     * Indicates whether the existing frameset should be used. This means that
     * the target (<code>href</code>) of the quicklink will be rendered within
     * the main right-hand frame, using or keeping any left-hand and top frame
     * as before.  
     * @return <code>true</code> if the existing frameset should be used, 
     * otherwise <code>false</code>.
     */
    public boolean isUseExistingFrameset()
    {
        return (link_flags & QL_FLAG_IS_USE_EXISTING_FRAMESET) != 0;
    }

    public byte getLinkFlags()
    {
        return link_flags;
    }

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