/*
 * Restrict.java
 *
 * Created on 03 September 2004, 09:47
 *
 * Version: $Revision: 1.3 $
 *
 * Date: $Date: 2006/09/29 17:19:59 $
 *
 * Copyright (c) 2004, The University of Edinburgh.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - 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.
 *
 * - Neither the name of the University of Edinburgh, or the names of the
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR 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.
 */

//package ac.ed.dspace.submit;

package org.dspace.app.webui.util;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.dspace.core.LogManager;

import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.content.Item;
import org.dspace.content.Bundle;
import org.dspace.content.Bitstream;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.storage.rdbms.DatabaseManager;
import org.dspace.storage.rdbms.TableRow;
import org.dspace.storage.rdbms.TableRowIterator;

//import ac.ed.dspace.submit.Licence;
/**
 * Apply the required restrictions to an item
 *
 * @author  Richard Jones
 */
public class Restrict {
    
    /** Creates a new instance of RestrictRelease */
    //public  Restrict() 
    //{
    // }

    private static Logger log = Logger.getLogger(Restrict.class);
    
    /**
     * apply one of the available restrictions to the item
     *
     * @param item          the item to have restrictions applied to
     * @param restriction   the integer identifier of the restriction type
     */
    public static void apply(Context context, Item item, int restriction)
        throws SQLException, AuthorizeException, IOException
    {
        switch (restriction)
        {
            case 0:
                generalRestrict(context, item, 3);
                break;
            case 1:
                generalRestrict(context, item, 6);
                break;
            case 2:
                generalRestrict(context, item, 12);
                break;
            case 3:
                domainRestrict(item, 1);
                break;
            case 4:
                domainRestrict(item, 2);
                break;
            case 5:
                totalRestrict(item);
                break;
            default:
                break;
        }
    }
    
    /**
     * apply a general restriction to the item
     *
     * @param item          the item to have restrictions applied to
     * @param months         the number of months to restrict for
     */
    public static void generalRestrict(Context context, Item item, int months)
        throws SQLException, AuthorizeException, IOException
    {
        // we can't actually do policy restrictions, but we withdraw the item
        item.withdraw();
        
        // create a date object with the release date in it
        Calendar release = Calendar.getInstance();
        release.setTime(new Date());
        release.add(release.MONTH, months);
        Date releaseDate = release.getTime();
        
	log.warn(LogManager.getHeader(context, "in generalRestrict ",
				      "releaseDate" + releaseDate));

        // update the database to contain the date of release
        TableRow row = DatabaseManager.create(context, "umrestricted");
        
        row.setColumn("item_id", item.getID());
        row.setColumn("release_date", releaseDate);
        
        DatabaseManager.update(context, row);
    }
    
     /**
     * apply a total restriction to the item
     *
     * @param item          the item to have restrictions applied to
     * @param yeras         the number of years to restrict for
     */
    public static void totalRestrict(Item item)
        throws SQLException, AuthorizeException, IOException
    {
        // we can't actually do policy restrictions, but we withdraw the item
        item.withdraw();
        
        // this item is not marked for release at any point, so we don't
        // enter it into the database
        
        // QUESTION: should items that are withdrawn in this manner be 
        // registered somewhere or are they OK just as withdrawn?
        
    }
    
    /**
     * apply a domain restriction to the item
     *
     * @param item          the item to have restrictions applied to
     * @param yeras         the number of years to restrict for
     */
    public static void domainRestrict(Item item, int years)
    {
        // there is no code here for the moment - we need to decide how best
        // to do domain restrictions
        return;
    }
    
    /**
     * release all restrictions on a given item
     *
     * @param item          the item to have restrictions applied to
     * @param context       the context of the request
     */
    public static void release(Context context, Item item)
        throws SQLException, AuthorizeException, IOException
    {
        // get the item ID as a string
        String itemID = Integer.toString(item.getID());
        
        // delete the database entry
        DatabaseManager.deleteByValue(context, "umrestricted", "item_id", 
                                      itemID);
        
        System.out.println("Item released: " + itemID);
        
        // un-withdraw the item
        item.reinstate();
    }
    
    /**
     * release all restrictions on a given item
     *
     * @param item          the item to have restrictions applied to
     * @param context       the context of the request
     */
    public static void release(Context context, Item[] items)
        throws SQLException, AuthorizeException, IOException
    {
        for (int i = 0; i < items.length; i++)
        {
            release(context, items[i]);
        }
    }
    /**
     * find all of the items that are restricted
     *
     * @param context          the context of the request
     *
     * @return      an array of items.
     */
    public static Item[] getRestricted(Context context)
        throws SQLException, AuthorizeException, IOException
    {
        String query="SELECT * FROM umrestricted";
        
        TableRowIterator tri = DatabaseManager.queryTable(context, 
                                                    "umrestricted", query);
        
        List itemList = new ArrayList();
        
        while (tri.hasNext())
        {
            TableRow row = tri.next();
            Item item = Item.find(context, row.getIntColumn("item_id"));
            itemList.add(item);
        }
        
        Item[] itemArray = new Item[itemList.size()];
        itemArray = (Item[]) itemList.toArray(itemArray);

        return itemArray;
    }
    
    /**
     * find all of the items that are due to be derestricted
     *
     * @param context          the context of the request
     *
     * @return      an array of items.
     */
    public static Item[] getAvailable(Context context)
        throws SQLException, AuthorizeException, IOException
    {

        String query="SELECT * FROM umrestricted " +
                      "WHERE release_date < current_timestamp";
        
        
        TableRowIterator tri = DatabaseManager.queryTable(context, 
                                                    "umrestricted", query);
        
        List itemList = new ArrayList();
        
        while (tri.hasNext())
        {
            TableRow row = tri.next();
            Item item = Item.find(context, row.getIntColumn("item_id"));
            itemList.add(item);
        }
        
        Item[] itemArray = new Item[itemList.size()];
        itemArray = (Item[]) itemList.toArray(itemArray);

        return itemArray;
    }
    
}
