Skip to content

Instantly share code, notes, and snippets.

@aschweer
Last active April 9, 2017 22:29
Show Gist options
  • Save aschweer/d53aad5bf4f181df4be98de97f16e4dc to your computer and use it in GitHub Desktop.
Save aschweer/d53aad5bf4f181df4be98de97f16e4dc to your computer and use it in GitHub Desktop.
DSpace 5.x patch for e-mailing curation reports of queued tasks. Code changes and e-mail template.
commit fa122611f2551d418f6ed92aacdd5a30bedca88e
Author: Andrea Schweer <schweer@waikato.ac.nz>
Date: Tue May 5 17:13:10 2015 +1200
|#6120 Send curation report when queue is worked off (configurable)
diff --git a/dspace-api/src/main/java/org/dspace/curate/CurationCli.java b/dspace-api/src/main/java/org/dspace/curate/CurationCli.java
index af9f8e0..8af7c89 100644
--- a/dspace-api/src/main/java/org/dspace/curate/CurationCli.java
+++ b/dspace-api/src/main/java/org/dspace/curate/CurationCli.java
@@ -7,8 +7,7 @@
*/
package org.dspace.curate;
-import java.io.BufferedReader;
-import java.io.FileReader;
+import java.io.*;
import java.util.Iterator;
import org.apache.commons.cli.CommandLine;
@@ -17,11 +16,13 @@ import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
+import org.apache.commons.lang.StringUtils;
import org.dspace.content.Site;
-import org.dspace.core.Context;
-import org.dspace.core.PluginManager;
+import org.dspace.core.*;
import org.dspace.eperson.EPerson;
+import javax.mail.MessagingException;
+
/**
* CurationCli provides command-line access to Curation tools and processes.
*
@@ -54,6 +55,7 @@ public class CurationCli
"transaction scope to impose: use 'object', 'curation', or 'open'. If absent, 'open' applies");
options.addOption("v", "verbose", false,
"report activity to stdout");
+ options.addOption("E", "email-report", false, "if given, e-mail result of curation to curating eperson.");
options.addOption("h", "help", false, "help");
CommandLine line = parser.parse(options, args);
@@ -66,6 +68,7 @@ public class CurationCli
String reporterName = null;
String limit = null;
String scope = null;
+ boolean emailReport = false;
boolean verbose = false;
if (line.hasOption('h'))
@@ -126,6 +129,11 @@ public class CurationCli
verbose = true;
}
+ if (line.hasOption("E"))
+ {
+ emailReport = true;
+ }
+
// now validate the args
if (idName == null && taskQueueName == null)
{
@@ -253,33 +261,71 @@ public class CurationCli
}
// use current time as our reader 'ticket'
long ticket = System.currentTimeMillis();
- Iterator<TaskQueueEntry> entryIter = queue.dequeue(taskQueueName, ticket).iterator();
- while (entryIter.hasNext())
- {
- TaskQueueEntry entry = entryIter.next();
- if (verbose)
- {
+ for (TaskQueueEntry entry : queue.dequeue(taskQueueName, ticket)) {
+ if (verbose) {
System.out.println("Curating id: " + entry.getObjectId());
}
curator.clear();
+
+ // make eperson who queued task the effective user
+ EPerson agent = EPerson.findByEmail(c, entry.getEpersonId());
+ if (agent != null) {
+ c.setCurrentUser(agent);
+ }
+
+ File reportFile = null;
+ PrintStream reportStream = null;
+
+ if (emailReport) {
+ reportFile = File.createTempFile("curation-report-" + entry.getSubmitTime(), ".log");
+ reportStream = new PrintStream(new FileOutputStream(reportFile));
+ curator.setReporter(reportStream);
+ }
+
// does entry relate to a DSO or workflow object?
- if (entry.getObjectId().indexOf("/") > 0)
- {
- for (String task : entry.getTaskNames())
- {
+ if (entry.getObjectId().indexOf("/") > 0) {
+ for (String task : entry.getTaskNames()) {
curator.addTask(task);
}
curator.curate(c, entry.getObjectId());
+ } else {
+ WorkflowCurator.curate(curator, c, entry.getObjectId());
}
- else
+
+ if (reportStream != null)
{
- // make eperson who queued task the effective user
- EPerson agent = EPerson.findByEmail(c, entry.getEpersonId());
- if (agent != null)
+ reportStream.flush();
+ reportStream.close();
+ }
+ if (reportFile != null) {
+ if (c.getCurrentUser() != null)
{
- c.setCurrentUser(agent);
+ try
+ {
+ Email email = Email.getEmail(I18nUtil.getEmailFilename(c.getCurrentLocale(), "curation_report"));
+ email.addRecipient(c.getCurrentUser().getEmail());
+ email.addArgument(StringUtils.join(entry.getTaskNames(), ", "));
+ email.addArgument(entry.getObjectId());
+ StringBuilder resultBuilder = new StringBuilder();
+ for (String task : entry.getTaskNames())
+ {
+ if (resultBuilder.length() > 0)
+ {
+ resultBuilder.append("\n");
+ }
+ resultBuilder.append(curator.getResult(task));
+ }
+ email.addArgument(resultBuilder.toString());
+ email.addAttachment(reportFile, reportFile.getName());
+ email.send();
+ }
+ catch (IOException | MessagingException e)
+ {
+ System.err.println("Could not e-mail report to current user: " + e.getMessage());
+ }
}
- WorkflowCurator.curate(curator, c, entry.getObjectId());
+ //noinspection ResultOfMethodCallIgnored
+ reportFile.delete();
}
}
queue.release(taskQueueName, ticket, true);
diff --git a/dspace-api/src/main/java/org/dspace/curate/Curator.java b/dspace-api/src/main/java/org/dspace/curate/Curator.java
index be02aab..5b73444 100644
--- a/dspace-api/src/main/java/org/dspace/curate/Curator.java
+++ b/dspace-api/src/main/java/org/dspace/curate/Curator.java
@@ -8,6 +8,7 @@
package org.dspace.curate;
import java.io.IOException;
+import java.io.PrintStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
@@ -61,7 +62,7 @@ public class Curator
private Map<String, TaskRunner> trMap = new HashMap<String, TaskRunner>();
private List<String> perfList = new ArrayList<String>();
private TaskQueue taskQ = null;
- private String reporter = null;
+ private PrintStream reporter = null;
private Invoked iMode = null;
private TaskResolver resolver = new TaskResolver();
private int cacheLimit = Integer.MAX_VALUE;
@@ -150,11 +151,23 @@ public class Curator
*/
public Curator setReporter(String reporter)
{
- this.reporter = reporter;
+ if ("-".equals(reporter))
+ {
+ setReporter(System.out);
+ }
+ else
+ {
+ setReporter((PrintStream) null);
+ }
return this;
}
-
- /**
+
+ public Curator setReporter(PrintStream stream) {
+ this.reporter = stream;
+ return this;
+ }
+
+ /**
* Sets an upper limit for the number of objects in the context cache
* used in a curation, if context accessible. Note that for many forms of
* invocation, the context is not accessible. If limit is reached,
@@ -317,11 +330,10 @@ public class Curator
*/
public void report(String message)
{
- // Stub for now
- if ("-".equals(reporter))
- {
- System.out.println(message);
- }
+ if (reporter != null)
+ {
+ reporter.println(message);
+ }
}
/**
diff --git a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/administrative/FlowItemUtils.java b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/administrative/FlowItemUtils.java
index e9a7c3e..ed215a6 100644
--- a/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/administrative/FlowItemUtils.java
+++ b/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/administrative/FlowItemUtils.java
@@ -7,8 +7,7 @@
*/
package org.dspace.app.xmlui.aspect.administrative;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
@@ -31,9 +30,7 @@ import org.dspace.authorize.ResourcePolicy;
import org.dspace.content.*;
import org.dspace.content.Collection;
import org.dspace.content.authority.Choices;
-import org.dspace.core.ConfigurationManager;
-import org.dspace.core.Constants;
-import org.dspace.core.Context;
+import org.dspace.core.*;
import org.dspace.curate.Curator;
import org.dspace.embargo.EmbargoManager;
import org.dspace.embargo.ThesisEmbargoUtils;
@@ -42,6 +39,7 @@ import org.dspace.statistics.SolrLogger;
import org.dspace.submit.step.AccessStep;
import org.dspace.workflow.WorkflowItem;
+import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
/**
@@ -1173,12 +1171,24 @@ public class FlowItemUtils
throws AuthorizeException, IOException, SQLException, Exception
{
String task = request.getParameter("curate_task");
+ boolean emailReport = "true".equals(request.getParameter("notify"));
+
Curator curator = FlowCurationUtils.getCurator(task);
+ File reportFile = null;
+ PrintStream reportStream = null;
+
try
{
Item item = Item.find(context, itemID);
if (item != null)
{
+ if (emailReport)
+ {
+ reportFile = File.createTempFile("curation-report-" + task + "-" + item.getHandle().replaceFirst("/", "_") + "-", ".log");
+ reportStream = new PrintStream(new FileOutputStream(reportFile));
+ curator.setReporter(reportStream);
+ }
+
//Call curate(context,ID) to ensure a Task Performer (Eperson) is set in Curator
curator.curate(context, item.getHandle());
}
@@ -1189,6 +1199,34 @@ public class FlowItemUtils
curator.setResult(task, e.getMessage());
return FlowCurationUtils.getRunFlowResult(task, curator, false);
}
+ finally {
+ if (reportStream != null) {
+ reportStream.flush();
+ reportStream.close();
+ }
+ if (emailReport && reportFile != null && context.getCurrentUser() != null)
+ {
+ try
+ {
+ Email email = Email.getEmail(I18nUtil.getEmailFilename(context.getCurrentLocale(), "curation_report"));
+ email.addRecipient(context.getCurrentUser().getEmail());
+ email.addArgument(FlowCurationUtils.getUITaskName(task));
+ email.addArgument(itemID);
+ email.addArgument(curator.getResult(task));
+ if (reportFile.length() > 0)
+ {
+ email.addAttachment(reportFile, reportFile.getName());
+ }
+ email.send();
+ //noinspection ResultOfMethodCallIgnored
+ reportFile.delete();
+ }
+ catch (IOException | MessagingException e)
+ {
+ // ignore for now
+ }
+ }
+ }
}
/**
# {0} Curation task name
# {1} Item ID
# {2} Curation result
Subject: DSpace: Curation report
This is an automated message to inform you about the outcome of a curation task
recently performed by you via the administrative user interface.
Curation task(s): {0}
Handle/ID: {1}
The curation task''s result is:
{2}
If the curation task produced reporting output, it is attached to this e-mail.
DSpace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment