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

/**
 * Resource for doing most of the work of storing feeds in the database.
 * @author colin
 * @author buckett
 */
public abstract class FeedResource extends Resource
{

    protected static final int DISPLAY_INLINE = 1;
    protected static final int DISPLAY_TITLE = 2;
    protected static final int DISPLAY_ITEM_TITLES = 4;
    protected static final int DISPLAY_ITEM_CONTENT = 8;
    protected static final int DISPLAY_EXCERPT = 16;
    protected static final int ALLOW_TITLE_LINKS = 32;
    protected static final int DISPLAY_ITEM_DATE = 64;
    protected static final int DISPLAY_AUTHOR = 128;
    protected static final int DISPLAY_ITEM_AUTHORS = 256;
    protected static final int DISPLAY_IMAGE = 512;
    protected static final int DISPLAY_DESCRIPTION = 1024;
    protected static final int DISPLAY_DATE = 2048;

    protected static final String DEFAULT_MAX_NUMBER_OF_ITEMS = "10";
    protected static final String DEFAULT_ITEM_SEPARATION_TAG = "<hr/>";
    
    private String url;
    protected Hashtable paramMap;
    private int displayFlags;
    private String maxNumberOfItems;
    private String itemSeparationTag;
    private boolean paramsChanged;
    
    private FeedCache feedCache;
    
    public FeedResource()
    {
        
    }

    /**
     * @return Returns the feedCache.
     */
    public FeedCache getFeedCache()
    {
        if (feedCache == null)
        {
            feedCache = new FeedCache(this);
        }
        return feedCache;
    }

    public String getURL()
    {
        if (url != null) return url;
    
        return "URL for feed not set.";
    }

    public void setURL(String blogURL)
    {
        if (blogURL != null) this.url = blogURL;
    }
    
    public String getMaxNumberOfItems()
    {
        if (maxNumberOfItems != null) return maxNumberOfItems;
    
        return DEFAULT_MAX_NUMBER_OF_ITEMS;
    }

    public void setMaxNumberOfItems(String maxNumberOfItems)
    {
        if (maxNumberOfItems != null)
            this.maxNumberOfItems = maxNumberOfItems;
        
        paramsChanged = true;
    }

    public String getItemSeparationTag()
    {
        if (itemSeparationTag != null) return itemSeparationTag;
    
        return DEFAULT_ITEM_SEPARATION_TAG;
    }

    /**
     * @param itemSeparationTag String separator to use.
     */
    public void setItemSeparationTag(String itemSeparationTag)
    {
        if (itemSeparationTag != null)
            this.itemSeparationTag = itemSeparationTag;
        
        paramsChanged = true;
    }

    /**
     * Get a hashtable of the set options for use in the XSLT transformation.
     * @return A Hashtable, only when an option is set is an entry put in the table.
     */
    public Hashtable loadTransformParams()
    {
        if (paramsChanged)
        {
            // need to update Hashtable of params to send to XSLT:
            paramMap.clear();
    
            paramMap.put("maxNumberOfItems", getMaxNumberOfItems());
            paramMap.put("itemSeparationTag", getItemSeparationTag());
    
            // XSL params need to be set to some text, or not set at all:
            if (isDisplayImage())
                paramMap.put("isDisplayImage", "true");
    
            if (isDisplayTitle())
                paramMap.put("isDisplayTitle", "true");
    
            if (isDisplayDescription())
                paramMap.put("isDisplayDescription", "true");
    
            if (isDisplayAuthor())
                paramMap.put("isDisplayAuthor", "true");
    
            if (isAllowTitleLinks())
                paramMap.put("isAllowTitleLinks", "true");
    
            if (isDisplayDate())
                paramMap.put("isDisplayDate", "true");
    
            if (isDisplayItemTitles())
                paramMap.put("isDisplayItemTitles", "true");
    
            if (isDisplayItemAuthors())
                paramMap.put("isDisplayItemAuthors", "true");
    
            if (isDisplayItemContent())
                paramMap.put("isDisplayItemContent", "true");
    
            if (isDisplayExcerpt())
                paramMap.put("isDisplayExcerpt", "true");
    
            if (isDisplayItemDate())
                paramMap.put("isDisplayItemDate", "true");
    
            paramsChanged = false;
        }
    
        return paramMap;
    }

    public int getDisplayFlags()
    {
        return displayFlags;
    }

    protected  void setDisplayFlags(int f)
    {
        if (f == displayFlags) return;
        displayFlags = f;
        setUnsaved();
        paramsChanged = true;
    }

    public boolean isDisplayInline()
    {
        return (displayFlags & DISPLAY_INLINE) != 0;
    }

    public void setDisplayInline(boolean b)
    {
        if (b == isDisplayInline())
            return;
    
        setDisplayFlags(displayFlags ^ DISPLAY_INLINE);
    }

    public boolean isDisplayImage()
    {
        return (displayFlags & DISPLAY_IMAGE) != 0;
    }

    public void setDisplayImage(boolean b)
    {
        if (b == isDisplayImage())
            return;
    
        setDisplayFlags(displayFlags ^ DISPLAY_IMAGE);
    }

    public boolean isDisplayTitle()
    {
        return (displayFlags & DISPLAY_TITLE) != 0;
    }

    public void setDisplayTitle(boolean b)
    {
        if (b == isDisplayTitle())
            return;
    
        setDisplayFlags(displayFlags ^ DISPLAY_TITLE);
    }

    public boolean isDisplayDescription()
    {
        return (displayFlags & DISPLAY_DESCRIPTION) != 0;
    }

    public void setDisplayDescription(boolean b)
    {
        if (b == isDisplayDescription())
            return;
    
        setDisplayFlags(displayFlags ^ DISPLAY_DESCRIPTION);
    }

    public boolean isDisplayAuthor()
    {
        return (displayFlags & DISPLAY_AUTHOR) != 0;
    }

    public void setDisplayAuthor(boolean b)
    {
        if (b == isDisplayAuthor())
            return;
    
        setDisplayFlags(displayFlags ^ DISPLAY_AUTHOR);
    }

    public boolean isAllowTitleLinks()
    {
        return (displayFlags & ALLOW_TITLE_LINKS) != 0;
    }

    public void setAllowTitleLinks(boolean b)
    {
        if (b == isAllowTitleLinks())
            return;
        setDisplayFlags(displayFlags ^ ALLOW_TITLE_LINKS);
    }

    public boolean isDisplayDate()
    {
        return (displayFlags & DISPLAY_DATE) != 0;
    }

    public void setDisplayDate(boolean b)
    {
        if (b == isDisplayDate())
            return;
    
        setDisplayFlags(displayFlags ^ DISPLAY_DATE);
    }

    public boolean isDisplayItemTitles()
    {
        return (displayFlags & DISPLAY_ITEM_TITLES) != 0;
    }

    public void setDisplayItemTitles(boolean b)
    {
        if (b == isDisplayItemTitles())
            return;
    
        setDisplayFlags(displayFlags ^ DISPLAY_ITEM_TITLES);
    }

    public boolean isDisplayItemAuthors()
    {
        return (displayFlags & DISPLAY_ITEM_AUTHORS) != 0;
    }

    public void setDisplayItemAuthors(boolean b)
    {
        if (b == isDisplayItemAuthors())
            return;
    
        setDisplayFlags(displayFlags ^ DISPLAY_ITEM_AUTHORS);
    }

    public boolean isDisplayItemContent()
    {
        return (displayFlags & DISPLAY_ITEM_CONTENT) != 0;
    }

    public void setDisplayItemContent(boolean b)
    {
        if (b == isDisplayItemContent())
            return;
    
        setDisplayFlags(displayFlags ^ DISPLAY_ITEM_CONTENT);
    }

    public boolean isDisplayExcerpt()
    {
        return (displayFlags & DISPLAY_EXCERPT) != 0;
    }

    public void setDisplayExcerpt(boolean b)
    {
        if (b == isDisplayExcerpt())
            return;
    
        setDisplayFlags(displayFlags ^ DISPLAY_EXCERPT);
    }

    public boolean isDisplayItemDate()
    {
        return (displayFlags & DISPLAY_ITEM_DATE) != 0;
    }

    public void setDisplayItemDate(boolean b)
    {
        if (b == isDisplayItemDate())
            return;
    
        setDisplayFlags(displayFlags ^ DISPLAY_ITEM_DATE);
    }

}
