Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial working ExportBitstreams curation task

  • Loading branch information...
commit 1b01ff80f6851eff822f33946d44f2b1092509c5 1 parent deb732e
helix84 authored
118 dspace-api/src/main/java/org/dspace/ctask/general/ExportBitstreams.java
... ... @@ -0,0 +1,118 @@
  1 +/**
  2 + * The contents of this file are subject to the license and copyright
  3 + * detailed in the LICENSE and NOTICE files at the root of the source
  4 + * tree and available online at
  5 + *
  6 + * http://www.dspace.org/license/
  7 + */
  8 +package org.dspace.ctask.general;
  9 +
  10 +import java.io.InputStream;
  11 +import java.io.OutputStream;
  12 +import java.io.FileOutputStream;
  13 +import java.io.BufferedOutputStream;
  14 +import java.io.BufferedWriter;
  15 +import java.io.Writer;
  16 +import java.io.OutputStreamWriter;
  17 +
  18 +import java.io.IOException;
  19 +import java.sql.SQLException;
  20 +import java.util.HashMap;
  21 +import java.util.Map;
  22 +
  23 +import org.dspace.content.Bitstream;
  24 +import org.dspace.content.BitstreamFormat;
  25 +import org.dspace.content.Bundle;
  26 +import org.dspace.content.DSpaceObject;
  27 +import org.dspace.content.Item;
  28 +import org.dspace.core.Context;
  29 +import org.dspace.curate.AbstractCurationTask;
  30 +import org.dspace.curate.Curator;
  31 +import org.dspace.curate.Distributive;
  32 +import org.dspace.authorize.AuthorizeException;
  33 +
  34 +/**
  35 + * ExportBitstreams exports all bitstreams found within the specified
  36 + * DSO and saves them to disk with their bitstream name.
  37 + * This version ignores authorizations, so it will export even
  38 + * restricted bitstreams.
  39 + *
  40 + * @author Ivan Masár <helix84@centrum.sk>
  41 + */
  42 +@Distributive
  43 +public class ExportBitstreams extends AbstractCurationTask
  44 +{
  45 + /**
  46 + * Perform the curation task upon passed DSO
  47 + *
  48 + * @param dso the DSpace object
  49 + * @throws IOException
  50 + */
  51 + @Override
  52 + public int perform(DSpaceObject dso) throws IOException
  53 + {
  54 + try {
  55 + // ignore authorization; is there a simpler way?
  56 + Context context = new Context();
  57 + context.turnOffAuthorisationSystem();
  58 + DSpaceObject dso2 = dso.find(context, dso.getType(), dso.getID());
  59 +
  60 + distribute(dso2);
  61 +
  62 + context.restoreAuthSystemState();
  63 + } catch (SQLException e) {
  64 + }
  65 +
  66 + return Curator.CURATE_SUCCESS;
  67 + }
  68 +
  69 + @Override
  70 + protected void performItem(Item item) throws SQLException, IOException
  71 + {
  72 +
  73 + StringBuilder sb = new StringBuilder();
  74 + for (Bundle bundle : item.getBundles())
  75 + {
  76 + for (Bitstream bs : bundle.getBitstreams()) // alternatively, use bundle.getPrimaryBitstreamID()
  77 + {
  78 + String handle = item.getHandle();
  79 + String h = handle.substring(handle.indexOf("/")+1);
  80 + String filename = bs.getName();
  81 + if (filename.indexOf("_dp.") > 0) {
  82 + try {
  83 + InputStream file = bs.retrieve();
  84 + OutputStream out = null;
  85 +// Writer out = null;
  86 +
  87 + // TODO: proper UTF-8 filenames
  88 + try {
  89 + String fullpath = "/dspace/exports/exportbitstream/" + filename.substring(0, filename.indexOf(".")) + "_" + h + filename.substring(filename.indexOf("."));
  90 +// out = new FileOutputStream(fullpath);
  91 + out = new BufferedOutputStream(new FileOutputStream(fullpath));
  92 +// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fullpath), "UTF-8"));
  93 + int c;
  94 +
  95 + while ((c = file.read()) != -1) {
  96 + out.write(c);
  97 + }
  98 +
  99 + sb.append(filename + "\n");
  100 +// TODO: reporting and status
  101 + report(filename + "\n");
  102 + } finally {
  103 + if (out != null) {
  104 + out.close();
  105 + }
  106 + }
  107 + } catch (AuthorizeException e) {
  108 +// sb.append("AuthorizeException\n\n");
  109 + report("AuthorizeException\n\n");
  110 + }
  111 + }
  112 + }
  113 + }
  114 +
  115 +// report(sb.toString());
  116 + setResult(sb.toString());
  117 + }
  118 +}
6 dspace/config/modules/curate.cfg
@@ -12,6 +12,7 @@ plugin.named.org.dspace.curate.CurationTask = \
12 12 org.dspace.ctask.general.RequiredMetadata = requiredmetadata, \
13 13 org.dspace.ctask.general.ClamScan = vscan, \
14 14 org.dspace.ctask.general.MicrosoftTranslator = translate, \
  15 + org.dspace.ctask.general.ExportBitstreams = exportbitstreams, \
15 16 org.dspace.ctask.general.MetadataValueLinkChecker = checklinks
16 17 # add new tasks here
17 18
@@ -19,10 +20,10 @@ plugin.named.org.dspace.curate.CurationTask = \
19 20 plugin.single.org.dspace.curate.TaskQueue = org.dspace.curate.FileTaskQueue
20 21
21 22 # directory location of curation task queues
22   -taskqueue.dir = ${dspace.dir}/ctqueues
  23 +taskqueue.dir = /dspace/ctqueues
23 24
24 25 # (optional) directory location of scripted (non-java) tasks
25   -# script.dir = ${dspace.dir}/ctscripts
  26 +# script.dir = /dspace/ctscripts
26 27
27 28 # Friendly names for curation tasks to appear in admin UI
28 29 # Also acts as a filter - i.e. tasks not enumerated here can still
@@ -31,6 +32,7 @@ ui.tasknames = \
31 32 profileformats = Profile Bitstream Formats, \
32 33 requiredmetadata = Check for Required Metadata, \
33 34 checklinks = Check Links in Metadata
  35 + exportbitstreams = Export Bitstreams
34 36
35 37 # Tasks may be organized into named groups which display together in UI drop-downs
36 38 # ui.taskgroups = \

0 comments on commit 1b01ff8

Please sign in to comment.
Something went wrong with that request. Please try again.