?? collection.java
字號:
/* * Collection.java * * Version: $Revision: 1.56 $ * * Date: $Date: 2005/11/17 19:02:04 $ * * 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.content;import java.io.IOException;import java.io.InputStream;import java.sql.*;import java.util.ArrayList;import java.util.List;import org.apache.log4j.Logger;import org.dspace.authorize.AuthorizeException;import org.dspace.authorize.AuthorizeManager;import org.dspace.authorize.ResourcePolicy;import org.dspace.core.ConfigurationManager;import org.dspace.core.Constants;import org.dspace.core.Context;import org.dspace.core.LogManager;import org.dspace.eperson.Group;import org.dspace.handle.HandleManager;import org.dspace.history.HistoryManager;import org.dspace.search.DSIndexer;import org.dspace.storage.rdbms.DatabaseManager;import org.dspace.storage.rdbms.TableRow;import org.dspace.storage.rdbms.TableRowIterator;import org.dspace.workflow.WorkflowItem;/** * Class representing a collection. * <P> * The collection's metadata (name, introductory text etc), workflow groups, and * default group of submitters are loaded into memory. Changes to metadata are * not written to the database until <code>update</code> is called. If you * create or remove a workflow group, the change is only reflected in the * database after calling <code>update</code>. The default group of * submitters is slightly different - creating or removing this has instant * effect. * * @author Robert Tansley * @version $Revision: 1.56 $ */public class Collection extends DSpaceObject{ /** log4j category */ private static Logger log = Logger.getLogger(Collection.class); /** Our context */ private Context ourContext; /** The table row corresponding to this item */ private TableRow collectionRow; /** The logo bitstream */ private Bitstream logo; /** The item template */ private Item template; /** Our Handle */ private String handle; /** * Groups corresponding to workflow steps - NOTE these start from one, so * workflowGroups[0] corresponds to workflow_step_1. */ private Group[] workflowGroup; /** The default group of submitters */ private Group submitters; /** The default group of administrators */ private Group admins; /** * Construct a collection with the given table row * * @param context * the context this object exists in * @param row * the corresponding row in the table * @throws SQLException */ Collection(Context context, TableRow row) throws SQLException { ourContext = context; collectionRow = row; // Get the logo bitstream if (collectionRow.isColumnNull("logo_bitstream_id")) { logo = null; } else { logo = Bitstream.find(ourContext, collectionRow .getIntColumn("logo_bitstream_id")); } // Get the template item if (collectionRow.isColumnNull("template_item_id")) { template = null; } else { template = Item.find(ourContext, collectionRow .getIntColumn("template_item_id")); } // Get the relevant groups workflowGroup = new Group[3]; workflowGroup[0] = groupFromColumn("workflow_step_1"); workflowGroup[1] = groupFromColumn("workflow_step_2"); workflowGroup[2] = groupFromColumn("workflow_step_3"); submitters = groupFromColumn("submitter"); admins = groupFromColumn("admin"); // Get our Handle if any handle = HandleManager.findHandle(context, this); // Cache ourselves context.cache(this, row.getIntColumn("collection_id")); } /** * Get a collection from the database. Loads in the metadata * * @param context * DSpace context object * @param id * ID of the collection * * @return the collection, or null if the ID is invalid. * @throws SQLException */ public static Collection find(Context context, int id) throws SQLException { // First check the cache Collection fromCache = (Collection) context.fromCache(Collection.class, id); if (fromCache != null) { return fromCache; } TableRow row = DatabaseManager.find(context, "collection", id); if (row == null) { if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_collection", "not_found,collection_id=" + id)); } return null; } // not null, return Collection if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_collection", "collection_id=" + id)); } return new Collection(context, row); } /** * Create a new collection, with a new ID. This method is not public, and * does not check authorisation. * * @param context * DSpace context object * * @return the newly created collection * @throws SQLException * @throws AuthorizeException */ static Collection create(Context context) throws SQLException, AuthorizeException { TableRow row = DatabaseManager.create(context, "collection"); Collection c = new Collection(context, row); c.handle = HandleManager.createHandle(context, c); // create the default authorization policy for collections // of 'anonymous' READ Group anonymousGroup = Group.find(context, 0); ResourcePolicy myPolicy = ResourcePolicy.create(context); myPolicy.setResource(c); myPolicy.setAction(Constants.READ); myPolicy.setGroup(anonymousGroup); myPolicy.update(); // now create the default policies for submitted items myPolicy = ResourcePolicy.create(context); myPolicy.setResource(c); myPolicy.setAction(Constants.DEFAULT_ITEM_READ); myPolicy.setGroup(anonymousGroup); myPolicy.update(); myPolicy = ResourcePolicy.create(context); myPolicy.setResource(c); myPolicy.setAction(Constants.DEFAULT_BITSTREAM_READ); myPolicy.setGroup(anonymousGroup); myPolicy.update(); HistoryManager.saveHistory(context, c, HistoryManager.CREATE, context .getCurrentUser(), context.getExtraLogInfo()); log.info(LogManager.getHeader(context, "create_collection", "collection_id=" + row.getIntColumn("collection_id")) + ",handle=" + c.handle); return c; } /** * Get all collections in the system. These are alphabetically sorted by * collection name. * * @param context * DSpace context object * * @return the collections in the system * @throws SQLException */ public static Collection[] findAll(Context context) throws SQLException { TableRowIterator tri = DatabaseManager.query(context, "collection", "SELECT * FROM collection ORDER BY name"); List collections = new ArrayList(); while (tri.hasNext()) { TableRow row = tri.next(); // First check the cache Collection fromCache = (Collection) context.fromCache( Collection.class, row.getIntColumn("collection_id")); if (fromCache != null) { collections.add(fromCache); } else { collections.add(new Collection(context, row)); } } // close the TableRowIterator to free up resources tri.close(); Collection[] collectionArray = new Collection[collections.size()]; collectionArray = (Collection[]) collections.toArray(collectionArray); return collectionArray; } /** * Get all the items in this collection. The order is indeterminate. * * @return an iterator over the items in the collection. * @throws SQLException */ public ItemIterator getItems() throws SQLException { String myQuery = "SELECT item.* FROM item, collection2item WHERE " + "item.item_id=collection2item.item_id AND " + "collection2item.collection_id=" + getID() + " AND item.in_archive='1'"; TableRowIterator rows = DatabaseManager.query(ourContext, "item", myQuery); return new ItemIterator(ourContext, rows); } /** * Get the internal ID of this collection * * @return the internal identifier */ public int getID() { return collectionRow.getIntColumn("collection_id"); } public String getHandle() { return handle; } /** * Get the value of a metadata field * * @param field * the name of the metadata field to get * * @return the value of the metadata field * * @exception IllegalArgumentException * if the requested metadata field doesn't exist */ public String getMetadata(String field) { return collectionRow.getStringColumn(field); } /** * Set a metadata value * * @param field * the name of the metadata field to get * @param value * value to set the field to * * @exception IllegalArgumentException * if the requested metadata field doesn't exist */ public void setMetadata(String field, String value) { collectionRow.setColumn(field, value); } /** * Get the logo for the collection. <code>null</code> is return if the * collection does not have a logo. * * @return the logo of the collection, or <code>null</code> */ public Bitstream getLogo() { return logo; } /** * Give the collection a logo. Passing in <code>null</code> removes any * existing logo. You will need to set the format of the new logo bitstream * before it will work, for example to "JPEG". Note that * <code>update(/code> will need to be called for the change to take
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -