/*
* InstallItem.java
*
* Version: $Revision: 3705 $
*
* Date: $Date: 2009-04-11 18:02:24 +0100 (Sat, 11 Apr 2009) $
*
* 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.sql.SQLException;
import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
import org.dspace.handle.HandleManager;
import uk.ac.jorum.exceptions.CriticalException;
import uk.ac.jorum.exceptions.NonCriticalException;
import uk.ac.jorum.utils.ExceptionLogger;
/**
* Support to install item in the archive
*
* @author dstuve
* @version $Revision: 3705 $
*/
public class InstallItem
{
/** log4j category */
private static Logger log = Logger.getLogger(InstallItem.class);
/**
* Take an InProgressSubmission and turn it into a fully-archived Item,
* creating a new Handle
*
* @param c
* DSpace Context
* @param is
* submission to install
*
* @return the fully archived Item
*/
public static Item installItem(Context c, InProgressSubmission is)
throws SQLException, IOException, AuthorizeException
{
return installItem(c, is, null);
}
/**
* Take an InProgressSubmission and turn it into a fully-archived Item.
*
* @param c current context
* @param is
* submission to install
* @param suppliedHandle
* the existing Handle to give the installed item
*
* @return the fully archived Item
*/
public static Item installItem(Context c, InProgressSubmission is,
String suppliedHandle) throws SQLException,
IOException, AuthorizeException
{
Item item = is.getItem();
String handle;
// create accession date
DCDate now = DCDate.getCurrent();
item.addDC("date", "accessioned", null, now.toString());
item.addDC("date", "available", null, now.toString());
// create issue date if not present
DCValue[] currentDateIssued = item.getDC("date", "issued", Item.ANY);
if (currentDateIssued.length == 0)
{
item.addDC("date", "issued", null, now.toString());
}
// if no previous handle supplied, create one
if (suppliedHandle == null)
{
// create handle
handle = HandleManager.createHandle(c, item);
}
else
{
handle = HandleManager.createHandle(c, item, suppliedHandle);
}
String handleref = HandleManager.getCanonicalForm(handle);
// Add handle as identifier.uri DC value, first check that identifier dosn't allready exist
boolean identifierExists = false;
DCValue[] identifiers = item.getDC("identifier", "uri", Item.ANY);
for (DCValue identifier : identifiers)
if (handleref.equals(identifier.value))
identifierExists = true;
if (!identifierExists)
item.addDC("identifier", "uri", null, handleref);
String provDescription = "Made available in DSpace on " + now
+ " (GMT). " + getBitstreamProvenanceMessage(item);
if (currentDateIssued.length != 0)
{
DCDate d = new DCDate(currentDateIssued[0].value);
provDescription = provDescription + " Previous issue date: "
+ d.toString();
}
// Add provenance description
item.addDC("description", "provenance", "en", provDescription);
// create collection2item mapping
is.getCollection().addItem(item);
// set owning collection
item.setOwningCollection(is.getCollection());
// set in_archive=true
item.setArchived(true);
// save changes ;-)
item.update();
// remove in-progress submission
is.deleteWrapper();
// GWaller 9/11/09 IssueID #73 Call post install hook which will generate preview page for content packages
// NOTE: this must be called before 'inheritCollectionDefaultPolicies' so that the user still has privs to create the preview bundle!
try{
item.postInstallHook(c);
} catch (NonCriticalException e){
ExceptionLogger.logException(log, e);
} catch (CriticalException e){
ExceptionLogger.logException(log, e);
// Action must be taken - we received a critical error.
// As the item is already installed at this point, it should be withdrawn so a user cannot see it.
// This must be taken as the item coudl be in an invalid state and should be corrected before making it live again.
item.withdraw();
}
// remove the item's policies and replace them with
// the defaults from the collection
item.inheritCollectionDefaultPolicies(is.getCollection());
return item;
}
/**
* Generate provenance-worthy description of the bitstreams contained in an
* item.
*
* @param myitem the item generate description for
*
* @return provenance description
*/
public static String getBitstreamProvenanceMessage(Item myitem)
throws SQLException
{
// Get non-internal format bitstreams
Bitstream[] bitstreams = myitem.getNonInternalBitstreams();
// Create provenance description
String mymessage = "No. of bitstreams: " + bitstreams.length + "\n";
// Add sizes and checksums of bitstreams
for (int j = 0; j < bitstreams.length; j++)
{
mymessage = mymessage + bitstreams[j].getName() + ": "
+ bitstreams[j].getSize() + " bytes, checksum: "
+ bitstreams[j].getChecksum() + " ("
+ bitstreams[j].getChecksumAlgorithm() + ")\n";
}
return mymessage;
}
}