/*
 * ====================================================================== The
 * Bodington System Software License, Version 1.0 Copyright (c) 2001 The
 * University of Leeds. All rights reserved. Redistribution and use in source
 * and binary forms, with or without modification, are permitted provided that
 * the following conditions are met: 1. Redistributions of source code must
 * retain the above copyright notice, this list of conditions and the following
 * disclaimer. 2. Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution. 3. The
 * end-user documentation included with the redistribution, if any, must include
 * the following acknowledgement: "This product includes software developed by
 * the University of Leeds (http://www.bodington.org/)." Alternately, this
 * acknowledgement may appear in the software itself, if and wherever such
 * third-party acknowledgements normally appear. 4. The names "Bodington",
 * "Nathan Bodington", "Bodington System", "Bodington Open Source Project", and
 * "The University of Leeds" must not be used to endorse or promote products
 * derived from this software without prior written permission. For written
 * permission, please contact d.gardner@leeds.ac.uk. 5. The name "Bodington" may
 * not appear in the name of products derived from this software without prior
 * written permission of the University of Leeds. THIS SOFTWARE IS PROVIDED ``AS
 * IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * TITLE, THE IMPLIED WARRANTIES OF QUALITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF LEEDS OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * ========================================================= This software was
 * originally created by the University of Leeds and may contain voluntary
 * contributions from others. For more information on the Bodington Open Source
 * Project, please see http://bodington.org/
 * ======================================================================
 */

package org.bodington.server.resources;

import org.bodington.database.PrimaryKey;
import org.bodington.server.BuildingServerException;
import java.util.logging.*;
import java.util.Hashtable;

public class BlogResource extends Resource // TODO  extend NewsFeedResource? DB won't allow it
{
    private static int DEFAULT_FLAG_SETTINGS = 494;//TODO still correct?

    // These could be in a more logical order, but altering their values would
    // affect NewsFeeds that exist already...
    private static final int DISPLAY_INLINE = 1; // default is off
    private static final int DISPLAY_TITLE = 2; // default is on
    private static final int DISPLAY_ENTRY_TITLES = 4; // default is on
    private static final int DISPLAY_ENTRY_CONTENT = 8; // default is on
    private static final int DISPLAY_EXCERPT = 16; // default is off
    private static final int ALLOW_TITLE_LINKS = 32; // default is on
    private static final int DISPLAY_ENTRY_DATE = 64; // default is on
    private static final int DISPLAY_AUTHOR = 128; // default is on
    private static final int DISPLAY_ENTRY_AUTHORS = 256; // default is on
    private static final int DISPLAY_IMAGE = 512; // default is off
    private static final int DISPLAY_DESCRIPTION = 1024; // default is off
    private static final int DISPLAY_DATE = 2048; // default is off

//  String value used in XSL stylesheet:
    private static final String DEFAULT_MAX_NUMBER_OF_ENTRIES = "10";
    private static final String DEFAULT_ENTRY_SEPARATION_TAG = "&lt;hr /&gt;";

    private PrimaryKey blog_id;
    private String blogURL;
    private Hashtable paramMap;
    private int blog_flags;
    private String maxNumberOfEntries;
    private String entrySeparationTag;
    private boolean flagsChanged;

    /** @todo complete logging.... */

    public BlogResource() throws BuildingServerException
    {
        paramMap = new Hashtable();
        setBlogFlags(DEFAULT_FLAG_SETTINGS);
    }

    public int getResourceType()
    {
        return RESOURCE_BLOG;
    }

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

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

    public PrimaryKey getBlogId()
    {
        return blog_id;
    }

    public void setBlogId(PrimaryKey key)
    {
        blog_id = key;
        setResourceId(key);
    }

    // Following group of properties saved to database:
    public String getBlogURL()
    {
        if (blogURL != null) return blogURL;

        return "URL for feed not set.";
    }

    public void setBlogURL(String blogURL)
    {
        if (blogURL != null) this.blogURL = blogURL;
    }

    public String getMaxNumberOfEntries()
    {
        if (maxNumberOfEntries != null) return maxNumberOfEntries;

        return DEFAULT_MAX_NUMBER_OF_ENTRIES;
    }

    public void setMaxNumberOfEntries(String maxNumberOfEntries)
    {
        if (maxNumberOfEntries != null)
        {
            this.maxNumberOfEntries = maxNumberOfEntries;
        }
    }

    // TODO For persistence this needs a modification to the database, to store
    // the user's choice from the user interface, but it can wait till after the
    // WebAuth fuss is over...
    public String getEntrySeparationTag()
    {
        if (entrySeparationTag != null) return entrySeparationTag;

        return DEFAULT_ENTRY_SEPARATION_TAG;
    }

    public void setEntrySeparationTag(String entrySeparationTag)
    {
        if (entrySeparationTag != null)
            this.entrySeparationTag = entrySeparationTag;
    }

    public Hashtable loadTransformParams()
    {
        if (flagsChanged)
        {
            // need to update Hashtable of params to send to XSLT:
            paramMap.clear();

            paramMap.put("maxNumberOfEntries", getMaxNumberOfEntries());
            paramMap.put("entrySeparationTag", getEntrySeparationTag());

            // 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 (isDisplayEntryTitles())
                paramMap.put("isDisplayEntryTitles", "true");

            if (isDisplayEntryAuthors())
                paramMap.put("isDisplayEntryAuthors", "true");

            if (isDisplayEntryContent())
                paramMap.put("isDisplayEntryContent", "true");

            if (isDisplayExcerpt())
                paramMap.put("isDisplayExcerpt", "true");

            if (isDisplayEntryDate())
                paramMap.put("isDisplayEntryDate", "true");

            flagsChanged = false;
        }

        return paramMap;
    }

    // Remaining properties saved to database as a set of flags:

    public int getBlogFlags()
    {
        return blog_flags;
    }

    public void setBlogFlags(int f)
    {
        if (f == blog_flags) return;
        blog_flags = f;
        setUnsaved();
        flagsChanged = true;
    }

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

    public void setDisplayInline(boolean b)
    {
        if (b == isDisplayInline())
            return;

        setBlogFlags(blog_flags ^ DISPLAY_INLINE);
    }

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

    public void setDisplayImage(boolean b)
    {
        if (b == isDisplayImage())
            return;

        setBlogFlags(blog_flags ^ DISPLAY_IMAGE);
    }

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

    public void setDisplayTitle(boolean b)
    {
        if (b == isDisplayTitle())
            return;

        setBlogFlags(blog_flags ^ DISPLAY_TITLE);
    }

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

    public void setDisplayDescription(boolean b)
    {
        if (b == isDisplayDescription())
            return;

        setBlogFlags(blog_flags ^ DISPLAY_DESCRIPTION);
    }

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

    public void setDisplayAuthor(boolean b)
    {
        if (b == isDisplayAuthor())
            return;

        setBlogFlags(blog_flags ^ DISPLAY_AUTHOR);
    }

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

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

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

    public void setDisplayDate(boolean b)
    {
        if (b == isDisplayEntryDate())
            return;

        setBlogFlags(blog_flags ^ DISPLAY_DATE);
    }

    public boolean isDisplayEntryTitles()
    {
        return (blog_flags & DISPLAY_ENTRY_TITLES) != 0;
    }

    public void setDisplayEntryTitles(boolean b)
    {
        if (b == isDisplayEntryTitles())
            return;

        setBlogFlags(blog_flags ^ DISPLAY_ENTRY_TITLES);
    }

    public boolean isDisplayEntryAuthors()
    {
        return (blog_flags & DISPLAY_ENTRY_AUTHORS) != 0;
    }

    public void setDisplayEntryAuthors(boolean b)
    {
        if (b == isDisplayEntryAuthors())
            return;

        setBlogFlags(blog_flags ^ DISPLAY_ENTRY_AUTHORS);
    }

    public boolean isDisplayEntryContent()
    {
        return (blog_flags & DISPLAY_ENTRY_CONTENT) != 0;
    }

    public void setDisplayEntryContent(boolean b)
    {
        if (b == isDisplayEntryContent())
            return;

        setBlogFlags(blog_flags ^ DISPLAY_ENTRY_CONTENT);
    }

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

    public void setDisplayExcerpt(boolean b)
    {
        if (b == isDisplayExcerpt())
            return;

        setBlogFlags(blog_flags ^ DISPLAY_EXCERPT);
    }

    public boolean isDisplayEntryDate()
    {
        return (blog_flags & DISPLAY_ENTRY_DATE) != 0;
    }

    public void setDisplayEntryDate(boolean b)
    {
        if (b == isDisplayEntryDate())
            return;

        setBlogFlags(blog_flags ^ DISPLAY_ENTRY_DATE);
    }
}