/** * The contents of this file are subject to the license and copyright * detailed in the LICENSE and NOTICE files at the root of the source * tree and available online at * * http://www.dspace.org/license/ */ package org.dspace.embargo; import java.io.IOException; import java.sql.SQLException; import java.util.Date; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; import org.apache.log4j.Logger; import org.dspace.authorize.AuthorizeException; import org.dspace.content.DCDate; import org.dspace.content.DCValue; import org.dspace.content.DSpaceObject; import org.dspace.content.Item; import org.dspace.content.ItemIterator; import org.dspace.content.MetadataSchema; import org.dspace.core.ConfigurationManager; import org.dspace.core.Constants; import org.dspace.core.Context; import org.dspace.core.PluginManager; import org.dspace.handle.HandleManager; /** * Public interface to the embargo subsystem. *
* Configuration properties: (with examples)
*
# DC metadata field to hold the user-supplied embargo terms
*
embargo.field.terms = dc.embargo.terms
*
# DC metadata field to hold computed "lift date" of embargo
*
embargo.field.lift = dc.date.available
*
# String to indicate indefinite (forever) embargo in terms
*
embargo.terms.open = Indefinite
*
# implementation of embargo setter plugin
*
plugin.single.org.dspace.embargo.EmbargoSetter = edu.my.Setter
*
# implementation of embargo lifter plugin
*
plugin.single.org.dspace.embargo.EmbargoLifter = edu.my.Lifter
*
* @author Larry Stone
* @author Richard Rodgers
*/
public class EmbargoManager
{
/** Special date signalling an Item is to be embargoed forever.
** The actual date is the first day of the year 10,000 UTC.
**/
public static final DCDate FOREVER = new DCDate("10000-01-01");
/** log4j category */
private static Logger log = Logger.getLogger(EmbargoManager.class);
// Metadata field components for user-supplied embargo terms
// set from the DSpace configuration by init()
private static String terms_schema = null;
private static String terms_element = null;
private static String terms_qualifier = null;
// Metadata field components for lift date, encoded as a DCDate
// set from the DSpace configuration by init()
private static String lift_schema = null;
private static String lift_element = null;
private static String lift_qualifier = null;
// plugin implementations
// set from the DSpace configuration by init()
private static EmbargoSetter setter = null;
private static EmbargoLifter lifter = null;
/**
* Put an Item under embargo until the specified lift date.
* Calls EmbargoSetter plugin to adjust Item access control policies.
*
* @param context the DSpace context
* @param item the item to embargo
* @param lift date on which the embargo is to be lifted.
*/
public static void setEmbargo(Context context, Item item, DCDate lift)
throws SQLException, AuthorizeException, IOException
{
init();
// if lift is null, we might be restoring an item from an AIP
DCDate myLift = lift;
if (myLift == null)
{
if ((myLift = recoverEmbargoDate(item)) == null)
{
return;
}
}
String slift = myLift.toString();
boolean ignoreAuth = context.ignoreAuthorization();
try
{
context.setIgnoreAuthorization(true);
item.clearMetadata(lift_schema, lift_element, lift_qualifier, Item.ANY);
item.addMetadata(lift_schema, lift_element, lift_qualifier, null, slift);
log.info("Set embargo on Item "+item.getHandle()+", expires on: "+slift);
setter.setEmbargo(context, item);
item.update();
}
finally
{
context.setIgnoreAuthorization(ignoreAuth);
}
}
/**
* Get the embargo lift date for an Item, if any. This looks for the
* metadata field configured to hold embargo terms, and gives it
* to the EmbargoSetter plugin's method to interpret it into
* an absolute timestamp. This is intended to be called at the time
* the Item is installed into the archive.
*
* Note that the plugin is *always* called, in case it gets its cue for * the embargo date from sources other than, or in addition to, the * specified field. * * @param context the DSpace context * @param item the item to embargo * @return lift date on which the embargo is to be lifted, or null if none */ public static DCDate getEmbargoDate(Context context, Item item) throws SQLException, AuthorizeException, IOException { init(); DCValue terms[] = item.getMetadata(terms_schema, terms_element, terms_qualifier, Item.ANY); DCDate result = null; // Its poor form to blindly use an object that could be null... if (terms == null) return null; result = setter.parseTerms(context, item, terms.length > 0 ? terms[0].value : null); if (result == null) return null; // new DCDate(non-date String) means toDate() will return null Date liftDate = result.toDate(); if (liftDate == null) { throw new IllegalArgumentException( "Embargo lift date is uninterpretable: " + result.toString()); } // sanity check: do not allow an embargo lift date in the past. if (liftDate.before(new Date())) { throw new IllegalArgumentException( "Embargo lift date must be in the future, but this is in the past: " + result.toString()); } return result; } /** * Lift the embargo on an item which is assumed to be under embargo. * Call the plugin to manage permissions in its own way, then delete * the administrative metadata fields that dictated embargo date. * * @param context the DSpace context * @param item the item on which to lift the embargo */ public static void liftEmbargo(Context context, Item item) throws SQLException, AuthorizeException, IOException { init(); lifter.liftEmbargo(context, item); item.clearMetadata(lift_schema, lift_element, lift_qualifier, Item.ANY); // set the dc.date.available value to right now item.clearMetadata(MetadataSchema.DC_SCHEMA, "date", "available", Item.ANY); item.addMetadata(MetadataSchema.DC_SCHEMA, "date", "available", null, DCDate.getCurrent().toString()); log.info("Lifting embargo on Item "+item.getHandle()); item.update(); } /** * Command-line service to scan for every Item with an expired embargo, * and then lift that embargo. *
* Options: *