⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subscribe.java

📁 dspace 用j2ee架构的一个数字图书馆.开源程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Subscribe.java * * Version: $Revision: 1.9 $ * * Date: $Date: 2005/07/28 17:20:40 $ * * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts * Institute of Technology.  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 Hewlett-Packard Company nor the name of the * Massachusetts Institute of Technology nor the names of their * 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 org.dspace.eperson;import java.io.IOException;import java.sql.SQLException;import java.util.ArrayList;import java.util.Date;import java.util.List;import javax.mail.MessagingException;import org.apache.log4j.Logger;import org.dspace.authorize.AuthorizeException;import org.dspace.authorize.AuthorizeManager;import org.dspace.content.Collection;import org.dspace.content.DCDate;import org.dspace.content.DCValue;import org.dspace.content.Item;import org.dspace.core.ConfigurationManager;import org.dspace.core.Context;import org.dspace.core.Email;import org.dspace.core.LogManager;import org.dspace.handle.HandleManager;import org.dspace.search.Harvest;import org.dspace.search.HarvestedItemInfo;import org.dspace.storage.rdbms.DatabaseManager;import org.dspace.storage.rdbms.TableRow;import org.dspace.storage.rdbms.TableRowIterator;/** * Class defining methods for sending new item e-mail alerts to users *  * @author Robert Tansley * @version $Revision: 1.9 $ */public class Subscribe{    /** log4j logger */    private static Logger log = Logger.getLogger(Subscribe.class);    /**     * Subscribe an e-person to a collection. An e-mail will be sent every day a     * new item appears in the collection.     *      * @param context     *            DSpace context     * @param eperson     *            EPerson to subscribe     * @param collection     *            Collection to subscribe to     */    public static void subscribe(Context context, EPerson eperson,            Collection collection) throws SQLException, AuthorizeException    {        // Check authorisation. Must be administrator, or the eperson.        if (AuthorizeManager.isAdmin(context)                || ((context.getCurrentUser() != null) && (context                        .getCurrentUser().getID() == eperson.getID())))        {            // already subscribed?            TableRowIterator r = DatabaseManager.query(context,                    "SELECT * FROM subscription WHERE eperson_id="                            + eperson.getID() + " AND collection_id="                            + collection.getID());            if (!r.hasNext())            {                // Not subscribed, so add them                TableRow row = DatabaseManager.create(context, "subscription");                row.setColumn("eperson_id", eperson.getID());                row.setColumn("collection_id", collection.getID());                DatabaseManager.update(context, row);                log.info(LogManager.getHeader(context, "subscribe",                        "eperson_id=" + eperson.getID() + ",collection_id="                                + collection.getID()));            }        }        else        {            throw new AuthorizeException(                    "Only admin or e-person themselves can subscribe");        }    }    /**     * Unsubscribe an e-person to a collection. Passing in <code>null</code>     * for the collection unsubscribes the e-person from all collections they     * are subscribed to.     *      * @param context     *            DSpace context     * @param eperson     *            EPerson to unsubscribe     * @param collection     *            Collection to unsubscribe from     */    public static void unsubscribe(Context context, EPerson eperson,            Collection collection) throws SQLException, AuthorizeException    {        // Check authorisation. Must be administrator, or the eperson.        if (AuthorizeManager.isAdmin(context)                || ((context.getCurrentUser() != null) && (context                        .getCurrentUser().getID() == eperson.getID())))        {            if (collection == null)            {                // Unsubscribe from all                DatabaseManager.updateQuery(context,                        "DELETE FROM subscription WHERE eperson_id="                                + eperson.getID());            }            else            {                DatabaseManager.updateQuery(context,                        "DELETE FROM subscription WHERE eperson_id="                                + eperson.getID() + " AND collection_id="                                + collection.getID());                log.info(LogManager.getHeader(context, "unsubscribe",                        "eperson_id=" + eperson.getID() + ",collection_id="                                + collection.getID()));            }        }        else        {            throw new AuthorizeException(                    "Only admin or e-person themselves can unsubscribe");        }    }    /**     * Find out which collections an e-person is subscribed to     *      * @param context     *            DSpace context     * @param eperson     *            EPerson     * @return array of collections e-person is subscribed to     */    public static Collection[] getSubscriptions(Context context, EPerson eperson)            throws SQLException    {        TableRowIterator tri = DatabaseManager.query(context,                "SELECT collection_id FROM subscription WHERE eperson_id="                        + eperson.getID());        List collections = new ArrayList();        while (tri.hasNext())        {            TableRow row = tri.next();            collections.add(Collection.find(context, row                    .getIntColumn("collection_id")));        }        Collection[] collArray = new Collection[collections.size()];        return (Collection[]) collections.toArray(collArray);    }    /**     * Is that e-person subscribed to that collection?     *      * @param context     *            DSpace context     * @param eperson     *            find out if this e-person is subscribed     * @param collection     *            find out if subscribed to this collection     * @return <code>true</code> if they are subscribed     */    public static boolean isSubscribed(Context context, EPerson eperson,            Collection collection) throws SQLException    {        TableRowIterator tri = DatabaseManager.query(context,                "SELECT * FROM subscription WHERE eperson_id="                        + eperson.getID() + " AND collection_id="                        + collection.getID());        return tri.hasNext();    }    /**     * Process subscriptions. This must be invoked only once a day. Messages are

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -