Permalink
Browse files

initial working ExportBitstreams curation task

  • Loading branch information...
1 parent deb732e commit 1b01ff80f6851eff822f33946d44f2b1092509c5 @helix84 committed Aug 30, 2012
View
118 dspace-api/src/main/java/org/dspace/ctask/general/ExportBitstreams.java
@@ -0,0 +1,118 @@
+/**
+ * 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.ctask.general;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.FileOutputStream;
+import java.io.BufferedOutputStream;
+import java.io.BufferedWriter;
+import java.io.Writer;
+import java.io.OutputStreamWriter;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.dspace.content.Bitstream;
+import org.dspace.content.BitstreamFormat;
+import org.dspace.content.Bundle;
+import org.dspace.content.DSpaceObject;
+import org.dspace.content.Item;
+import org.dspace.core.Context;
+import org.dspace.curate.AbstractCurationTask;
+import org.dspace.curate.Curator;
+import org.dspace.curate.Distributive;
+import org.dspace.authorize.AuthorizeException;
+
+/**
+ * ExportBitstreams exports all bitstreams found within the specified
+ * DSO and saves them to disk with their bitstream name.
+ * This version ignores authorizations, so it will export even
+ * restricted bitstreams.
+ *
+ * @author Ivan Masár <helix84@centrum.sk>
+ */
+@Distributive
+public class ExportBitstreams extends AbstractCurationTask
+{
+ /**
+ * Perform the curation task upon passed DSO
+ *
+ * @param dso the DSpace object
+ * @throws IOException
+ */
+ @Override
+ public int perform(DSpaceObject dso) throws IOException
+ {
+ try {
+ // ignore authorization; is there a simpler way?
+ Context context = new Context();
+ context.turnOffAuthorisationSystem();
+ DSpaceObject dso2 = dso.find(context, dso.getType(), dso.getID());
+
+ distribute(dso2);
+
+ context.restoreAuthSystemState();
+ } catch (SQLException e) {
+ }
+
+ return Curator.CURATE_SUCCESS;
+ }
+
+ @Override
+ protected void performItem(Item item) throws SQLException, IOException
+ {
+
+ StringBuilder sb = new StringBuilder();
+ for (Bundle bundle : item.getBundles())
+ {
+ for (Bitstream bs : bundle.getBitstreams()) // alternatively, use bundle.getPrimaryBitstreamID()
+ {
+ String handle = item.getHandle();
+ String h = handle.substring(handle.indexOf("/")+1);
+ String filename = bs.getName();
+ if (filename.indexOf("_dp.") > 0) {
+ try {
+ InputStream file = bs.retrieve();
+ OutputStream out = null;
+// Writer out = null;
+
+ // TODO: proper UTF-8 filenames
+ try {
+ String fullpath = "/dspace/exports/exportbitstream/" + filename.substring(0, filename.indexOf(".")) + "_" + h + filename.substring(filename.indexOf("."));
+// out = new FileOutputStream(fullpath);
+ out = new BufferedOutputStream(new FileOutputStream(fullpath));
+// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fullpath), "UTF-8"));
+ int c;
+
+ while ((c = file.read()) != -1) {
+ out.write(c);
+ }
+
+ sb.append(filename + "\n");
+// TODO: reporting and status
+ report(filename + "\n");
+ } finally {
+ if (out != null) {
+ out.close();
+ }
+ }
+ } catch (AuthorizeException e) {
+// sb.append("AuthorizeException\n\n");
+ report("AuthorizeException\n\n");
+ }
+ }
+ }
+ }
+
+// report(sb.toString());
+ setResult(sb.toString());
+ }
+}
View
6 dspace/config/modules/curate.cfg
@@ -12,17 +12,18 @@ plugin.named.org.dspace.curate.CurationTask = \
org.dspace.ctask.general.RequiredMetadata = requiredmetadata, \
org.dspace.ctask.general.ClamScan = vscan, \
org.dspace.ctask.general.MicrosoftTranslator = translate, \
+ org.dspace.ctask.general.ExportBitstreams = exportbitstreams, \
org.dspace.ctask.general.MetadataValueLinkChecker = checklinks
# add new tasks here
## task queue implementation
plugin.single.org.dspace.curate.TaskQueue = org.dspace.curate.FileTaskQueue
# directory location of curation task queues
-taskqueue.dir = ${dspace.dir}/ctqueues
+taskqueue.dir = /dspace/ctqueues
# (optional) directory location of scripted (non-java) tasks
-# script.dir = ${dspace.dir}/ctscripts
+# script.dir = /dspace/ctscripts
# Friendly names for curation tasks to appear in admin UI
# Also acts as a filter - i.e. tasks not enumerated here can still
@@ -31,6 +32,7 @@ ui.tasknames = \
profileformats = Profile Bitstream Formats, \
requiredmetadata = Check for Required Metadata, \
checklinks = Check Links in Metadata
+ exportbitstreams = Export Bitstreams
# Tasks may be organized into named groups which display together in UI drop-downs
# ui.taskgroups = \

0 comments on commit 1b01ff8

Please sign in to comment.