📄 item.java
字号:
/* * Item.java * * Version: $Revision: 1.71 $ * * Date: $Date: 2006/05/26 14:16:44 $ * * 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.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.sql.SQLException;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import java.util.Map;import org.apache.log4j.Logger;import org.dspace.authorize.AuthorizeException;import org.dspace.authorize.AuthorizeManager;import org.dspace.authorize.ResourcePolicy;import org.dspace.browse.Browse;import org.dspace.core.ConfigurationManager;import org.dspace.core.Constants;import org.dspace.core.Context;import org.dspace.core.LogManager;import org.dspace.eperson.EPerson;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;/** * Class representing an item in DSpace. * <P> * This class holds in memory the item Dublin Core metadata, the bundles in the * item, and the bitstreams in those bundles. When modifying the item, if you * modify the Dublin Core or the "in archive" flag, you must call * <code>update</code> for the changes to be written to the database. * Creating, adding or removing bundles or bitstreams has immediate effect in * the database. * * @author Robert Tansley * @author Martin Hald * @version $Revision: 1.71 $ */public class Item extends DSpaceObject{ /** * Wild card for Dublin Core metadata qualifiers/languages */ public final static String ANY = "*"; /** log4j category */ private static Logger log = Logger.getLogger(Item.class); /** Our context */ private Context ourContext; /** The table row corresponding to this item */ private TableRow itemRow; /** The e-person who submitted this item */ private EPerson submitter; /** The bundles in this item - kept in sync with DB */ private List bundles; /** The Dublin Core metadata - a list of DCValue objects. */ private List dublinCore; /** Handle, if any */ private String handle; /** * True if the Dublin Core has changed since reading from the DB or the last * update() */ private boolean dublinCoreChanged; /** * Construct an item with the given table row * * @param context * the context this object exists in * @param row * the corresponding row in the table * @throws SQLException */ Item(Context context, TableRow row) throws SQLException { ourContext = context; itemRow = row; dublinCoreChanged = false; dublinCore = new ArrayList(); // Get Dublin Core metadata TableRowIterator tri = DatabaseManager.queryTable(ourContext, "MetadataValue", "SELECT * FROM MetadataValue WHERE item_id= ? " + " ORDER BY metadata_field_id, place", itemRow.getIntColumn("item_id")); while (tri.hasNext()) { TableRow resultRow = tri.next(); // Get the associated metadata field and schema information int fieldID = resultRow.getIntColumn("metadata_field_id"); MetadataField field = MetadataField.find(context, fieldID); if (field == null) { log.error("Loading item - cannot found metadata field " + fieldID); } else { MetadataSchema schema = MetadataSchema.find( context, field.getSchemaID()); // Make a DCValue object DCValue dcv = new DCValue(); dcv.element = field.getElement(); dcv.qualifier = field.getQualifier(); dcv.value = resultRow.getStringColumn("text_value"); dcv.language = resultRow.getStringColumn("text_lang"); //dcv.namespace = schema.getNamespace(); dcv.schema = schema.getName(); // Add it to the list dublinCore.add(dcv); } } // close the TableRowIterator to free up resources tri.close(); // Get our Handle if any handle = HandleManager.findHandle(context, this); // Cache ourselves context.cache(this, row.getIntColumn("item_id")); } /** * Get an item from the database. The item, its Dublin Core metadata, and * the bundle and bitstream metadata are all loaded into memory. * * @param context * DSpace context object * @param id * Internal ID of the item * @return the item, or null if the internal ID is invalid. * @throws SQLException */ public static Item find(Context context, int id) throws SQLException { // First check the cache Item fromCache = (Item) context.fromCache(Item.class, id); if (fromCache != null) { return fromCache; } TableRow row = DatabaseManager.find(context, "item", id); if (row == null) { if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_item", "not_found,item_id=" + id)); } return null; } // not null, return item if (log.isDebugEnabled()) { log.debug(LogManager.getHeader(context, "find_item", "item_id=" + id)); } return new Item(context, row); } /** * Create a new item, with a new internal ID. This method is not public, * since items need to be created as workspace items. Authorisation is the * responsibility of the caller. * * @param context * DSpace context object * @return the newly created item * @throws SQLException * @throws AuthorizeException */ static Item create(Context context) throws SQLException, AuthorizeException { TableRow row = DatabaseManager.create(context, "item"); Item i = new Item(context, row); // Call update to give the item a last modified date. OK this isn't // amazingly efficient but creates don't happen that often. context.setIgnoreAuthorization(true); i.update(); context.setIgnoreAuthorization(false); HistoryManager.saveHistory(context, i, HistoryManager.CREATE, context .getCurrentUser(), context.getExtraLogInfo()); log.info(LogManager.getHeader(context, "create_item", "item_id=" + row.getIntColumn("item_id"))); return i; } /** * Get all the items in the archive. Only items with the "in archive" flag * set are included. The order of the list is indeterminate. * * @param context * DSpace context object * @return an iterator over the items in the archive. * @throws SQLException */ public static ItemIterator findAll(Context context) throws SQLException { String myQuery = "SELECT * FROM item WHERE in_archive='1'"; TableRowIterator rows = DatabaseManager.queryTable(context, "item", myQuery); return new ItemIterator(context, rows); } /** * Find all the items in the archive by a given submitter. The order is * indeterminate. Only items with the "in archive" flag set are included. * * @param context * DSpace context object * @param eperson * the submitter * @return an iterator over the items submitted by eperson * @throws SQLException */ public static ItemIterator findBySubmitter(Context context, EPerson eperson) throws SQLException { String myQuery = "SELECT * FROM item WHERE in_archive='1' AND submitter_id=" + eperson.getID(); TableRowIterator rows = DatabaseManager.queryTable(context, "item", myQuery); return new ItemIterator(context, rows); } /** * Get the internal ID of this item. In general, this shouldn't be exposed * to users * * @return the internal identifier */ public int getID() { return itemRow.getIntColumn("item_id"); } /** * @see org.dspace.content.DSpaceObject#getHandle() */ public String getHandle() { return handle; } /** * Find out if the item is part of the main archive * * @return true if the item is in the main archive */ public boolean isArchived() { return itemRow.getBooleanColumn("in_archive"); } /** * Find out if the item has been withdrawn * * @return true if the item has been withdrawn */ public boolean isWithdrawn() { return itemRow.getBooleanColumn("withdrawn"); } /** * Get the date the item was last modified, or the current date if * last_modified is null * * @return the date the item was last modified, or the current date if the * column is null. */ public Date getLastModified() { Date myDate = itemRow.getDateColumn("last_modified"); if (myDate == null) { myDate = new Date(); } return myDate; } /** * Set the "is_archived" flag. This is public and only * <code>WorkflowItem.archive()</code> should set this. * * @param isArchived * new value for the flag */ public void setArchived(boolean isArchived) { itemRow.setColumn("in_archive", isArchived); } /** * Set the owning Collection for the item * * @param c * Collection */ public void setOwningCollection(Collection c) { itemRow.setColumn("owning_collection", c.getID()); } /** * Get the owning Collection for the item * * @return Collection that is the owner of the item * @throws SQLException */ public Collection getOwningCollection() throws java.sql.SQLException
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -