Skip to content

Instantly share code, notes, and snippets.

@smkiewel
Last active September 11, 2019 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smkiewel/f78b1ded9e4f9be38272e1ff44346e15 to your computer and use it in GitHub Desktop.
Save smkiewel/f78b1ded9e4f9be38272e1ff44346e15 to your computer and use it in GitHub Desktop.
UGAEmbargoSetter
/**
* 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 edu.uga.dspace.embargo;
import java.sql.SQLException;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.AuthorizeManager;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.DCDate;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.embargo.*;
import org.dspace.eperson.Group;
import org.dspace.license.CreativeCommons;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.Calendar;
/**
* Georgetown specific embargo policies
*
* "Common" - allow GU access, embargo to the world
* "Restrictive" - embargo to all
*
* Standard policies
* common-6-months
* common-1-year
* common-2-years
* restrictive-6-months
* restrictive-1-year
* restrictive-2-years
*
* Special cases
* common-forever
* restrictive-forever
* common-custom
* restrictive-custom
*/
public class UGAEmbargoSetter extends DefaultEmbargoSetter
{
private static Logger log = Logger.getLogger(UGAEmbargoSetter.class);
public UGAEmbargoSetter()
{
super();
}
private void addUGARead(Context context, DSpaceObject dso) throws SQLException, AuthorizeException {
Group ugaread = Group.findByName(context, "uga-ip");
if (ugaread == null) {
log.error("Group (uga-ip) cannot be found");
return;
}
Group[] groups = AuthorizeManager.getAuthorizedGroups(context, dso, Constants.READ);
for(Group g: groups) {
if (g.equals(ugaread)) {
return;
}
}
AuthorizeManager.addPolicy(context, dso, Constants.READ, ugaread);
}
/**
* Parse the terms into a definite date. Terms are expected to consist of
* either: a token (value configured in 'embargo.terms.open' property) to indicate
* indefinite embargo, or a literal lift date formatted in ISO 8601 format (yyyy-mm-dd)
*
* @param context the DSpace context
* @param item the item to embargo
* @param terms the embargo terms
* @return parsed date in DCDate format
*/
public DCDate parseTerms(Context context, Item item, String terms)
throws SQLException, AuthorizeException, IOException
{
if (terms != null && terms.length() > 0)
{
// Set READ for the uga-ip community
for (Bundle bn : item.getBundles())
{
// Skip the LICENSE and METADATA bundles, they stay world-readable
String bnn = bn.getName();
if (!(bnn.equals(Constants.LICENSE_BUNDLE_NAME) || bnn.equals(Constants.METADATA_BUNDLE_NAME) || bnn.equals(CreativeCommons.CC_BUNDLE_NAME)))
{
addUGARead(context, bn);
for (Bitstream bs : bn.getBitstreams())
{
addUGARead(context, bs);
}
}
}
if ("FOREVER".equals(terms))
{
return EmbargoManager.FOREVER;
}
else if (terms.equals(""))
{
return null;
}
else
{
Calendar cal = new GregorianCalendar();
if (terms.equals("common-6-months") || terms.equals("restrictive-6-months")) {
cal.add(Calendar.MONTH, 6);
} else if (terms.equals("common-1-year") || terms.equals("restrictive-1-year")) {
cal.add(Calendar.YEAR, 1);
} else if (terms.equals("common-2-years") || terms.equals("restrictive-2-years")) {
cal.add(Calendar.YEAR, 2);
} else if (terms.equals("common-custom") || terms.equals("restrictive-custom")) {
String dctag = ConfigurationManager.getProperty("embargo.field.customdate");
if (dctag==null)
return null;
String vals = item.getMetadata(dctag);
if (vals != null) {
String s = vals;
if (s.isEmpty()) {
log.warn("Embargo custom date is blank. The item will not be embargoed");
return null;
}
DCDate ndate = new DCDate(s);
if (ndate.toDate().compareTo(new Date()) <= 0) {
log.warn("Embargo date for item " + item.getHandle() +" is in the past. Value will be ignored.");
return null;
}
DCDate dcd = new DCDate(s);
if (s.length() >= 10) {
cal.set(dcd.getYear(), dcd.getMonth()-1, dcd.getDay() + 1);
} else if (s.length() >= 7) {
cal.set(dcd.getYear(), dcd.getMonth()-1, 2);
} else {
cal.set(dcd.getYear(), 0, 2);
}
return new DCDate(showCal(cal));
} else {
log.warn("Embargo custom date is missing. The item will not be embargoed");
return null;
}
}
return new DCDate(showCal(cal));
}
}
return null;
}
public String showCal(Calendar cal) {
return ""+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH)+1) + "-" + cal.get(Calendar.DAY_OF_MONTH);
}
/**
* Enforce embargo by turning off all read access to bitstreams in
* this Item.
*
* @param context the DSpace context
* @param item the item to embargo
*/
public void setEmbargo(Context context, Item item)
throws SQLException, AuthorizeException, IOException
{
DCDate liftDate = EmbargoManager.getEmbargoTermsAsDate(context, item);
if(liftDate == null) {
java.util.logging.Logger.getLogger(UGAEmbargoSetter.class.getName()).log(Level.INFO, "WARNING: Lift Date for item '" + item.getName() +
"' could not be retrieved. This is most likely due the custom-date and/or terms metadata missing while \n\tthe lift-date metadatum is "
+ "present. In the past, this has caused the import process to fail on an item. To prevent this, the program is skipping \n\tthe setEmbargo() "
+ "call on this item. Please manually ensure that embargo is properly set on this item.");
return;
}
for (Bundle bn : item.getBundles())
{
// Skip the LICENSE and METADATA bundles, they stay world-readable
String bnn = bn.getName();
if (!(bnn.equals(Constants.LICENSE_BUNDLE_NAME) || bnn.equals(Constants.METADATA_BUNDLE_NAME) || bnn.equals(CreativeCommons.CC_BUNDLE_NAME)))
{
//AuthorizeManager.removePoliciesActionFilter(context, bn, Constants.READ);
generatePolicies(context, liftDate.toDate(), null, bn, item.getOwningCollection());
for (Bitstream bs : bn.getBitstreams())
{
//AuthorizeManager.removePoliciesActionFilter(context, bs, Constants.READ);
generatePolicies(context, liftDate.toDate(), null, bs, item.getOwningCollection());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment