maven-failsafe-plugin
@@ -291,7 +297,6 @@
-
org.hibernate
@@ -304,13 +309,28 @@
- org.hibernate
- hibernate-ehcache
+ org.hibernate
+ hibernate-ehcache
+
+
+ org.hibernate
+ hibernate-jpamodelgen
+
+
+ org.hibernate
+ hibernate-validator-cdi
+ ${hibernate-validator.version}
org.springframework
spring-orm
+
+ org.glassfish
+ javax.el
+ 3.0.1-b10
+
+
org.dspace
handle
@@ -331,16 +351,6 @@
org.apache.jena
apache-jena-libs
pom
-
-
- com.fasterxml.jackson.core
- jackson-core
-
-
- com.fasterxml.jackson.core
- jackson-databind
-
-
commons-cli
@@ -351,8 +361,8 @@
commons-codec
- commons-collections
- commons-collections
+ org.apache.commons
+ commons-collections4
org.apache.commons
@@ -385,7 +395,7 @@
javax.servlet
- servlet-api
+ javax.servlet-api
provided
@@ -415,8 +425,8 @@
pdfbox
- org.apache.pdfbox
- fontbox
+ org.apache.pdfbox
+ fontbox
org.bouncycastle
@@ -496,16 +506,12 @@
h2
test
-
- org.databene
- contiperf
- test
-
org.mockito
mockito-core
test
+
org.rometools
rome-modules
@@ -569,9 +575,9 @@
commons-configuration
- com.maxmind.geoip
- geoip-api
- 1.3.0
+ com.maxmind.geoip2
+ geoip2
+ 2.11.0
org.apache.ant
@@ -584,9 +590,9 @@
- org.elasticsearch
- elasticsearch
- 1.4.0
+ org.apache.lucene
+ lucene-core
+ 4.10.4
@@ -672,7 +678,6 @@
joda-time
joda-time
- 2.9.2
javax.inject
@@ -697,7 +702,7 @@
org.glassfish.jersey.core
jersey-client
- 2.22.1
+ ${jersey.version}
@@ -709,27 +714,19 @@
joda-time
joda-time
-
- com.fasterxml.jackson.core
- jackson-databind
-
-
+
+
- com.fasterxml.jackson.core
- jackson-core
- 2.7.0
+ org.dspace
+ orcid-jaxb-api
+ 2.1.0
- com.fasterxml.jackson.core
- jackson-databind
- 2.7.0
-
-
- com.fasterxml.jackson.core
- jackson-annotations
- 2.7.0
+ org.json
+ json
+ 20180130
diff --git a/dspace-api/src/main/java/org/apache/solr/handler/extraction/ExtractingParams.java b/dspace-api/src/main/java/org/apache/solr/handler/extraction/ExtractingParams.java
index 014d4e0d6b..c2015cdb19 100644
--- a/dspace-api/src/main/java/org/apache/solr/handler/extraction/ExtractingParams.java
+++ b/dspace-api/src/main/java/org/apache/solr/handler/extraction/ExtractingParams.java
@@ -19,147 +19,145 @@ package org.apache.solr.handler.extraction;
/**
* The various Solr Parameters names to use when extracting content.
- *
**/
public interface ExtractingParams {
- /**
- * Map all generated attribute names to field names with lowercase and underscores.
- */
- public static final String LOWERNAMES = "lowernames";
+ /**
+ * Map all generated attribute names to field names with lowercase and underscores.
+ */
+ public static final String LOWERNAMES = "lowernames";
- /**
- * if true, ignore TikaException (give up to extract text but index meta data)
- */
- public static final String IGNORE_TIKA_EXCEPTION = "ignoreTikaException";
+ /**
+ * if true, ignore TikaException (give up to extract text but index meta data)
+ */
+ public static final String IGNORE_TIKA_EXCEPTION = "ignoreTikaException";
- /**
- * The param prefix for mapping Tika metadata to Solr fields.
- *
- * To map a field, add a name like:
- *
fmap.title=solr.title
- *
- * In this example, the tika "title" metadata value will be added to a Solr field named "solr.title"
- *
- *
- */
- public static final String MAP_PREFIX = "fmap.";
+ /**
+ * The param prefix for mapping Tika metadata to Solr fields.
+ *
+ * To map a field, add a name like:
+ *
fmap.title=solr.title
+ *
+ * In this example, the tika "title" metadata value will be added to a Solr field named "solr.title"
+ */
+ public static final String MAP_PREFIX = "fmap.";
- /**
- * The boost value for the name of the field. The boost can be specified by a name mapping.
- *
- * For example
- *
- * map.title=solr.title
- * boost.solr.title=2.5
- *
- * will boost the solr.title field for this document by 2.5
- *
- */
- public static final String BOOST_PREFIX = "boost.";
+ /**
+ * The boost value for the name of the field. The boost can be specified by a name mapping.
+ *
+ * For example
+ *
+ * map.title=solr.title
+ * boost.solr.title=2.5
+ *
+ * will boost the solr.title field for this document by 2.5
+ */
+ public static final String BOOST_PREFIX = "boost.";
- /**
- * Pass in literal values to be added to the document, as in
- *
- * literal.myField=Foo
- *
- *
- */
- public static final String LITERALS_PREFIX = "literal.";
+ /**
+ * Pass in literal values to be added to the document, as in
+ *
+ * literal.myField=Foo
+ *
+ */
+ public static final String LITERALS_PREFIX = "literal.";
- /**
- * Restrict the extracted parts of a document to be indexed
- * by passing in an XPath expression. All content that satisfies the XPath expr.
- * will be passed to the {@link org.apache.solr.handler.extraction.SolrContentHandler}.
- *
- * See Tika's docs for what the extracted document looks like.
- *
- * @see #CAPTURE_ELEMENTS
- */
- public static final String XPATH_EXPRESSION = "xpath";
+ /**
+ * Restrict the extracted parts of a document to be indexed
+ * by passing in an XPath expression. All content that satisfies the XPath expr.
+ * will be passed to the {@link org.apache.solr.handler.extraction.SolrContentHandler}.
+ *
+ * See Tika's docs for what the extracted document looks like.
+ *
+ * @see #CAPTURE_ELEMENTS
+ */
+ public static final String XPATH_EXPRESSION = "xpath";
- /**
- * Only extract and return the content, do not index it.
- */
- public static final String EXTRACT_ONLY = "extractOnly";
+ /**
+ * Only extract and return the content, do not index it.
+ */
+ public static final String EXTRACT_ONLY = "extractOnly";
- /**
- * Content output format if extractOnly is true. Default is "xml", alternative is "text".
- */
- public static final String EXTRACT_FORMAT = "extractFormat";
+ /**
+ * Content output format if extractOnly is true. Default is "xml", alternative is "text".
+ */
+ public static final String EXTRACT_FORMAT = "extractFormat";
- /**
- * Capture attributes separately according to the name of the element, instead of just adding them to the string buffer
- */
- public static final String CAPTURE_ATTRIBUTES = "captureAttr";
+ /**
+ * Capture attributes separately according to the name of the element, instead of just adding them to the string
+ * buffer
+ */
+ public static final String CAPTURE_ATTRIBUTES = "captureAttr";
- /**
- * Literal field values will by default override other values such as metadata and content. Set this to false to revert to pre-4.0 behaviour
- */
- public static final String LITERALS_OVERRIDE = "literalsOverride";
+ /**
+ * Literal field values will by default override other values such as metadata and content. Set this to false to
+ * revert to pre-4.0 behaviour
+ */
+ public static final String LITERALS_OVERRIDE = "literalsOverride";
- /**
- * Capture the specified fields (and everything included below it that isn't capture by some other capture field) separately from the default. This is different
- * then the case of passing in an XPath expression.
- *
- * The Capture field is based on the localName returned to the {@link org.apache.solr.handler.extraction.SolrContentHandler}
- * by Tika, not to be confused by the mapped field. The field name can then
- * be mapped into the index schema.
- *
- * For instance, a Tika document may look like:
- *
- * <html>
- * ...
- * <body>
- * <p>some text here. <div>more text</div></p>
- * Some more text
- * </body>
- *
- * By passing in the p tag, you could capture all P tags separately from the rest of the t
- * Thus, in the example, the capture of the P tag would be: "some text here. more text"
- *
- */
- public static final String CAPTURE_ELEMENTS = "capture";
+ /**
+ * Capture the specified fields (and everything included below it that isn't capture by some other capture field)
+ * separately from the default. This is different
+ * then the case of passing in an XPath expression.
+ *
+ * The Capture field is based on the localName returned to the
+ * {@link org.apache.solr.handler.extraction.SolrContentHandler}
+ * by Tika, not to be confused by the mapped field. The field name can then
+ * be mapped into the index schema.
+ *
+ * For instance, a Tika document may look like:
+ *
+ * <html>
+ * ...
+ * <body>
+ * <p>some text here. <div>more text</div></p>
+ * Some more text
+ * </body>
+ *
+ * By passing in the p tag, you could capture all P tags separately from the rest of the t
+ * Thus, in the example, the capture of the P tag would be: "some text here. more text"
+ */
+ public static final String CAPTURE_ELEMENTS = "capture";
- /**
- * The type of the stream. If not specified, Tika will use mime type detection.
- */
- public static final String STREAM_TYPE = "stream.type";
+ /**
+ * The type of the stream. If not specified, Tika will use mime type detection.
+ */
+ public static final String STREAM_TYPE = "stream.type";
- /**
- * Optional. The file name. If specified, Tika can take this into account while
- * guessing the MIME type.
- */
- public static final String RESOURCE_NAME = "resource.name";
+ /**
+ * Optional. The file name. If specified, Tika can take this into account while
+ * guessing the MIME type.
+ */
+ public static final String RESOURCE_NAME = "resource.name";
- /**
- * Optional. The password for this resource. Will be used instead of the rule based password lookup mechanisms
- */
- public static final String RESOURCE_PASSWORD = "resource.password";
+ /**
+ * Optional. The password for this resource. Will be used instead of the rule based password lookup mechanisms
+ */
+ public static final String RESOURCE_PASSWORD = "resource.password";
- /**
- * Optional. If specified, the prefix will be prepended to all Metadata, such that it would be possible
- * to setup a dynamic field to automatically capture it
- */
- public static final String UNKNOWN_FIELD_PREFIX = "uprefix";
+ /**
+ * Optional. If specified, the prefix will be prepended to all Metadata, such that it would be possible
+ * to setup a dynamic field to automatically capture it
+ */
+ public static final String UNKNOWN_FIELD_PREFIX = "uprefix";
- /**
- * Optional. If specified and the name of a potential field cannot be determined, the default Field specified
- * will be used instead.
- */
- public static final String DEFAULT_FIELD = "defaultField";
+ /**
+ * Optional. If specified and the name of a potential field cannot be determined, the default Field specified
+ * will be used instead.
+ */
+ public static final String DEFAULT_FIELD = "defaultField";
- /**
- * Optional. If specified, loads the file as a source for password lookups for Tika encrypted documents.
- *
- * File format is Java properties format with one key=value per line.
- * The key is evaluated as a regex against the file name, and the value is the password
- * The rules are evaluated top-bottom, i.e. the first match will be used
- * If you want a fallback password to be always used, supply a .*=<defaultmypassword> at the end
- */
- public static final String PASSWORD_MAP_FILE = "passwordsFile";
+ /**
+ * Optional. If specified, loads the file as a source for password lookups for Tika encrypted documents.
+ *
+ * File format is Java properties format with one key=value per line.
+ * The key is evaluated as a regex against the file name, and the value is the password
+ * The rules are evaluated top-bottom, i.e. the first match will be used
+ * If you want a fallback password to be always used, supply a .*=<defaultmypassword> at the end
+ */
+ public static final String PASSWORD_MAP_FILE = "passwordsFile";
}
diff --git a/dspace-api/src/main/java/org/dspace/administer/CommunityFiliator.java b/dspace-api/src/main/java/org/dspace/administer/CommunityFiliator.java
index 70b525347b..261e623130 100644
--- a/dspace-api/src/main/java/org/dspace/administer/CommunityFiliator.java
+++ b/dspace-api/src/main/java/org/dspace/administer/CommunityFiliator.java
@@ -17,7 +17,7 @@ import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
-import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections4.CollectionUtils;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Community;
import org.dspace.content.factory.ContentServiceFactory;
@@ -30,13 +30,12 @@ import org.dspace.handle.service.HandleService;
/**
* A command-line tool for setting/removing community/sub-community
* relationships. Takes community DB Id or handle arguments as inputs.
- *
+ *
* @author rrodgers
* @version $Revision$
*/
-public class CommunityFiliator
-{
+public class CommunityFiliator {
protected CommunityService communityService;
protected HandleService handleService;
@@ -47,12 +46,10 @@ public class CommunityFiliator
}
/**
- *
* @param argv the command line arguments given
* @throws Exception if error
*/
- public static void main(String[] argv) throws Exception
- {
+ public static void main(String[] argv) throws Exception {
// create an options object and populate it
CommandLineParser parser = new PosixParser();
@@ -60,11 +57,11 @@ public class CommunityFiliator
options.addOption("s", "set", false, "set a parent/child relationship");
options.addOption("r", "remove", false,
- "remove a parent/child relationship");
+ "remove a parent/child relationship");
options.addOption("p", "parent", true,
- "parent community (handle or database ID)");
+ "parent community (handle or database ID)");
options.addOption("c", "child", true,
- "child community (handle or databaseID)");
+ "child community (handle or databaseID)");
options.addOption("h", "help", false, "help");
CommandLine line = parser.parse(options, argv);
@@ -73,57 +70,48 @@ public class CommunityFiliator
String parentID = null;
String childID = null;
- if (line.hasOption('h'))
- {
+ if (line.hasOption('h')) {
HelpFormatter myhelp = new HelpFormatter();
myhelp.printHelp("CommunityFiliator\n", options);
System.out
- .println("\nestablish a relationship: CommunityFiliator -s -p parentID -c childID");
+ .println("\nestablish a relationship: CommunityFiliator -s -p parentID -c childID");
System.out
- .println("remove a relationship: CommunityFiliator -r -p parentID -c childID");
+ .println("remove a relationship: CommunityFiliator -r -p parentID -c childID");
System.exit(0);
}
- if (line.hasOption('s'))
- {
+ if (line.hasOption('s')) {
command = "set";
}
- if (line.hasOption('r'))
- {
+ if (line.hasOption('r')) {
command = "remove";
}
- if (line.hasOption('p')) // parent
- {
+ if (line.hasOption('p')) { // parent
parentID = line.getOptionValue('p');
}
- if (line.hasOption('c')) // child
- {
+ if (line.hasOption('c')) { // child
childID = line.getOptionValue('c');
}
// now validate
// must have a command set
- if (command == null)
- {
+ if (command == null) {
System.out
- .println("Error - must run with either set or remove (run with -h flag for details)");
+ .println("Error - must run with either set or remove (run with -h flag for details)");
System.exit(1);
}
- if ("set".equals(command) || "remove".equals(command))
- {
- if (parentID == null)
- {
+ if ("set".equals(command) || "remove".equals(command)) {
+ if (parentID == null) {
System.out.println("Error - a parentID must be specified (run with -h flag for details)");
System.exit(1);
}
- if (childID == null)
- {
+ if (childID == null) {
System.out.println("Error - a childID must be specified (run with -h flag for details)");
System.exit(1);
}
@@ -135,71 +123,57 @@ public class CommunityFiliator
// we are superuser!
c.turnOffAuthorisationSystem();
- try
- {
+ try {
// validate and resolve the parent and child IDs into commmunities
Community parent = filiator.resolveCommunity(c, parentID);
Community child = filiator.resolveCommunity(c, childID);
- if (parent == null)
- {
+ if (parent == null) {
System.out.println("Error, parent community cannot be found: "
- + parentID);
+ + parentID);
System.exit(1);
}
- if (child == null)
- {
+ if (child == null) {
System.out.println("Error, child community cannot be found: "
- + childID);
+ + childID);
System.exit(1);
}
- if ("set".equals(command))
- {
+ if ("set".equals(command)) {
filiator.filiate(c, parent, child);
- }
- else
- {
+ } else {
filiator.defiliate(c, parent, child);
}
- }
- catch (SQLException sqlE)
- {
+ } catch (SQLException sqlE) {
System.out.println("Error - SQL exception: " + sqlE.toString());
- }
- catch (AuthorizeException authE)
- {
+ } catch (AuthorizeException authE) {
System.out.println("Error - Authorize exception: "
- + authE.toString());
- }
- catch (IOException ioE)
- {
+ + authE.toString());
+ } catch (IOException ioE) {
System.out.println("Error - IO exception: " + ioE.toString());
}
}
/**
- *
- * @param c context
+ * @param c context
* @param parent parent Community
- * @param child child community
- * @throws SQLException if database error
+ * @param child child community
+ * @throws SQLException if database error
* @throws AuthorizeException if authorize error
- * @throws IOException if IO error
+ * @throws IOException if IO error
*/
public void filiate(Context c, Community parent, Community child)
- throws SQLException, AuthorizeException, IOException
- {
+ throws SQLException, AuthorizeException, IOException {
// check that a valid filiation would be established
// first test - proposed child must currently be an orphan (i.e.
// top-level)
- Community childDad = CollectionUtils.isNotEmpty(child.getParentCommunities()) ? child.getParentCommunities().iterator().next() : null;
+ Community childDad = CollectionUtils.isNotEmpty(child.getParentCommunities()) ? child.getParentCommunities()
+ .iterator().next() : null;
- if (childDad != null)
- {
+ if (childDad != null) {
System.out.println("Error, child community: " + child.getID()
- + " already a child of: " + childDad.getID());
+ + " already a child of: " + childDad.getID());
System.exit(1);
}
@@ -207,12 +181,10 @@ public class CommunityFiliator
// child
List parentDads = parent.getParentCommunities();
- for (int i = 0; i < parentDads.size(); i++)
- {
- if (parentDads.get(i).getID().equals(child.getID()))
- {
+ for (int i = 0; i < parentDads.size(); i++) {
+ if (parentDads.get(i).getID().equals(child.getID())) {
System.out
- .println("Error, circular parentage - child is parent of parent");
+ .println("Error, circular parentage - child is parent of parent");
System.exit(1);
}
}
@@ -223,39 +195,34 @@ public class CommunityFiliator
// complete the pending transaction
c.complete();
System.out.println("Filiation complete. Community: '" + parent.getID()
- + "' is parent of community: '" + child.getID() + "'");
+ + "' is parent of community: '" + child.getID() + "'");
}
/**
- *
- * @param c context
+ * @param c context
* @param parent parent Community
- * @param child child community
- * @throws SQLException if database error
+ * @param child child community
+ * @throws SQLException if database error
* @throws AuthorizeException if authorize error
- * @throws IOException if IO error
+ * @throws IOException if IO error
*/
public void defiliate(Context c, Community parent, Community child)
- throws SQLException, AuthorizeException, IOException
- {
+ throws SQLException, AuthorizeException, IOException {
// verify that child is indeed a child of parent
List parentKids = parent.getSubcommunities();
boolean isChild = false;
- for (int i = 0; i < parentKids.size(); i++)
- {
- if (parentKids.get(i).getID().equals(child.getID()))
- {
+ for (int i = 0; i < parentKids.size(); i++) {
+ if (parentKids.get(i).getID().equals(child.getID())) {
isChild = true;
break;
}
}
- if (!isChild)
- {
+ if (!isChild) {
System.out
- .println("Error, child community not a child of parent community");
+ .println("Error, child community not a child of parent community");
System.exit(1);
}
@@ -269,37 +236,33 @@ public class CommunityFiliator
// complete the pending transaction
c.complete();
System.out.println("Defiliation complete. Community: '" + child.getID()
- + "' is no longer a child of community: '" + parent.getID()
- + "'");
+ + "' is no longer a child of community: '" + parent.getID()
+ + "'");
}
/**
* Find a community by ID
- * @param c context
+ *
+ * @param c context
* @param communityID community ID
* @return Community object
* @throws SQLException if database error
*/
protected Community resolveCommunity(Context c, String communityID)
- throws SQLException
- {
+ throws SQLException {
Community community = null;
- if (communityID.indexOf('/') != -1)
- {
+ if (communityID.indexOf('/') != -1) {
// has a / must be a handle
community = (Community) handleService.resolveToObject(c,
- communityID);
+ communityID);
// ensure it's a community
if ((community == null)
- || (community.getType() != Constants.COMMUNITY))
- {
+ || (community.getType() != Constants.COMMUNITY)) {
community = null;
}
- }
- else
- {
+ } else {
community = communityService.find(c, UUID.fromString(communityID));
}
diff --git a/dspace-api/src/main/java/org/dspace/administer/CreateAdministrator.java b/dspace-api/src/main/java/org/dspace/administer/CreateAdministrator.java
index c1c80e5f52..7d603158ba 100644
--- a/dspace-api/src/main/java/org/dspace/administer/CreateAdministrator.java
+++ b/dspace-api/src/main/java/org/dspace/administer/CreateAdministrator.java
@@ -15,7 +15,6 @@ import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
-
import org.apache.commons.lang.StringUtils;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
@@ -34,20 +33,20 @@ import org.dspace.eperson.service.GroupService;
*
* Alternatively, it can be used to take the email, first name, last name and
* desired password as arguments thus:
- *
+ *
* CreateAdministrator -e [email] -f [first name] -l [last name] -p [password]
- *
- * This is particularly convenient for automated deploy scripts that require an
+ *
+ * This is particularly convenient for automated deploy scripts that require an
* initial administrator, for example, before deployment can be completed
- *
+ *
* @author Robert Tansley
* @author Richard Jones
- *
* @version $Revision$
*/
-public final class CreateAdministrator
-{
- /** DSpace Context object */
+public final class CreateAdministrator {
+ /**
+ * DSpace Context object
+ */
private final Context context;
protected EPersonService ePersonService;
@@ -56,160 +55,142 @@ public final class CreateAdministrator
/**
* For invoking via the command line. If called with no command line arguments,
* it will negotiate with the user for the administrator details
- *
+ *
* @param argv the command line arguments given
* @throws Exception if error
*/
public static void main(String[] argv)
- throws Exception
- {
+ throws Exception {
CommandLineParser parser = new PosixParser();
Options options = new Options();
-
+
CreateAdministrator ca = new CreateAdministrator();
-
+
options.addOption("e", "email", true, "administrator email address");
options.addOption("f", "first", true, "administrator first name");
options.addOption("l", "last", true, "administrator last name");
options.addOption("c", "language", true, "administrator language");
options.addOption("p", "password", true, "administrator password");
-
+
CommandLine line = parser.parse(options, argv);
-
+
if (line.hasOption("e") && line.hasOption("f") && line.hasOption("l") &&
- line.hasOption("c") && line.hasOption("p"))
- {
+ line.hasOption("c") && line.hasOption("p")) {
ca.createAdministrator(line.getOptionValue("e"),
- line.getOptionValue("f"), line.getOptionValue("l"),
- line.getOptionValue("c"), line.getOptionValue("p"));
- }
- else
- {
+ line.getOptionValue("f"), line.getOptionValue("l"),
+ line.getOptionValue("c"), line.getOptionValue("p"));
+ } else {
ca.negotiateAdministratorDetails();
}
}
-
- /**
+
+ /**
* constructor, which just creates and object with a ready context
- *
+ *
* @throws Exception if error
*/
protected CreateAdministrator()
- throws Exception
- {
+ throws Exception {
context = new Context();
groupService = EPersonServiceFactory.getInstance().getGroupService();
ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
}
-
+
/**
- * Method which will negotiate with the user via the command line to
+ * Method which will negotiate with the user via the command line to
* obtain the administrator's details
- *
+ *
* @throws Exception if error
*/
protected void negotiateAdministratorDetails()
- throws Exception
- {
+ throws Exception {
Console console = System.console();
-
+
System.out.println("Creating an initial administrator account");
-
+
boolean dataOK = false;
-
+
String email = null;
String firstName = null;
String lastName = null;
char[] password1 = null;
char[] password2 = null;
String language = I18nUtil.DEFAULTLOCALE.getLanguage();
-
- while (!dataOK)
- {
+
+ while (!dataOK) {
System.out.print("E-mail address: ");
System.out.flush();
-
+
email = console.readLine();
- if (!StringUtils.isBlank(email))
- {
+ if (!StringUtils.isBlank(email)) {
email = email.trim();
- }
- else
- {
+ } else {
System.out.println("Please provide an email address.");
continue;
}
-
+
System.out.print("First name: ");
System.out.flush();
-
+
firstName = console.readLine();
- if (firstName != null)
- {
+ if (firstName != null) {
firstName = firstName.trim();
}
-
+
System.out.print("Last name: ");
System.out.flush();
-
+
lastName = console.readLine();
- if (lastName != null)
- {
+ if (lastName != null) {
lastName = lastName.trim();
}
-
- if (ConfigurationManager.getProperty("webui.supported.locales") != null)
- {
- System.out.println("Select one of the following languages: " + ConfigurationManager.getProperty("webui.supported.locales"));
+
+ if (ConfigurationManager.getProperty("webui.supported.locales") != null) {
+ System.out.println("Select one of the following languages: " + ConfigurationManager
+ .getProperty("webui.supported.locales"));
System.out.print("Language: ");
System.out.flush();
-
+
language = console.readLine();
- if (language != null)
- {
+ if (language != null) {
language = language.trim();
language = I18nUtil.getSupportedLocale(new Locale(language)).getLanguage();
}
}
-
+
System.out.println("Password will not display on screen.");
System.out.print("Password: ");
System.out.flush();
password1 = console.readPassword();
-
+
System.out.print("Again to confirm: ");
System.out.flush();
-
+
password2 = console.readPassword();
//TODO real password validation
- if (password1.length > 1 && Arrays.equals(password1, password2))
- {
+ if (password1.length > 1 && Arrays.equals(password1, password2)) {
// password OK
System.out.print("Is the above data correct? (y or n): ");
System.out.flush();
-
+
String s = console.readLine();
- if (s != null)
- {
+ if (s != null) {
s = s.trim();
- if (s.toLowerCase().startsWith("y"))
- {
+ if (s.toLowerCase().startsWith("y")) {
dataOK = true;
}
}
- }
- else
- {
+ } else {
System.out.println("Passwords don't match");
}
}
-
+
// if we make it to here, we are ready to create an administrator
createAdministrator(email, firstName, lastName, language, String.valueOf(password1));
@@ -217,60 +198,56 @@ public final class CreateAdministrator
Arrays.fill(password1, ' ');
Arrays.fill(password2, ' ');
}
-
+
/**
* Create the administrator with the given details. If the user
* already exists then they are simply upped to administrator status
- *
+ *
* @param email the email for the user
* @param first user's first name
- * @param last user's last name
+ * @param last user's last name
* @param language preferred language
- * @param pw desired password
- *
+ * @param pw desired password
* @throws Exception if error
*/
protected void createAdministrator(String email, String first, String last,
- String language, String pw)
- throws Exception
- {
+ String language, String pw)
+ throws Exception {
// Of course we aren't an administrator yet so we need to
// circumvent authorisation
context.turnOffAuthorisationSystem();
-
+
// Find administrator group
Group admins = groupService.findByName(context, Group.ADMIN);
-
- if (admins == null)
- {
+
+ if (admins == null) {
throw new IllegalStateException("Error, no admin group (group 1) found");
}
-
+
// Create the administrator e-person
- EPerson eperson = ePersonService.findByEmail(context,email);
-
+ EPerson eperson = ePersonService.findByEmail(context, email);
+
// check if the email belongs to a registered user,
// if not create a new user with this email
- if (eperson == null)
- {
+ if (eperson == null) {
eperson = ePersonService.create(context);
eperson.setEmail(email);
eperson.setCanLogIn(true);
eperson.setRequireCertificate(false);
eperson.setSelfRegistered(false);
}
-
+
eperson.setLastName(context, last);
eperson.setFirstName(context, first);
eperson.setLanguage(context, language);
ePersonService.setPassword(eperson, pw);
ePersonService.update(context, eperson);
-
+
groupService.addMember(context, admins, eperson);
groupService.update(context, admins);
-
+
context.complete();
-
+
System.out.println("Administrator account created");
}
}
diff --git a/dspace-api/src/main/java/org/dspace/administer/MetadataExporter.java b/dspace-api/src/main/java/org/dspace/administer/MetadataExporter.java
index 57f1bccef1..27579a5b8e 100644
--- a/dspace-api/src/main/java/org/dspace/administer/MetadataExporter.java
+++ b/dspace-api/src/main/java/org/dspace/administer/MetadataExporter.java
@@ -7,7 +7,19 @@
*/
package org.dspace.administer;
-import org.apache.commons.cli.*;
+import java.io.BufferedWriter;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
import org.apache.xml.serialize.Method;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
@@ -19,69 +31,63 @@ import org.dspace.content.service.MetadataSchemaService;
import org.dspace.core.Context;
import org.xml.sax.SAXException;
-import java.io.BufferedWriter;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.sql.SQLException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
/**
* @author Graham Triggs
*
* This class creates an xml document as passed in the arguments and
* from the metadata schemas for the repository.
- *
+ *
* The form of the XML is as follows
* {@code
*
- *
- * dc
- * http://dublincore.org/documents/dcmi-terms/
- *
+ *
+ * dc
+ * http://dublincore.org/documents/dcmi-terms/
+ *
*
* }
*/
-public class MetadataExporter
-{
+public class MetadataExporter {
- protected static MetadataSchemaService metadataSchemaService = ContentServiceFactory.getInstance().getMetadataSchemaService();
- protected static MetadataFieldService metadataFieldService = ContentServiceFactory.getInstance().getMetadataFieldService();
+ protected static MetadataSchemaService metadataSchemaService = ContentServiceFactory.getInstance()
+ .getMetadataSchemaService();
+ protected static MetadataFieldService metadataFieldService = ContentServiceFactory.getInstance()
+ .getMetadataFieldService();
+
+ /**
+ * Default constructor
+ */
+ private MetadataExporter() { }
/**
* @param args commandline arguments
- * @throws ParseException if parser error
- * @throws SAXException if XML parse error
- * @throws IOException if IO error
- * @throws SQLException if database error
+ * @throws ParseException if parser error
+ * @throws SAXException if XML parse error
+ * @throws IOException if IO error
+ * @throws SQLException if database error
* @throws RegistryExportException if export error
*/
- public static void main(String[] args) throws ParseException, SQLException, IOException, SAXException, RegistryExportException
- {
+ public static void main(String[] args)
+ throws ParseException, SQLException, IOException, SAXException, RegistryExportException {
// create an options object and populate it
CommandLineParser parser = new PosixParser();
Options options = new Options();
- options.addOption("f", "file", true, "output xml file for registry");
+ options.addOption("f", "file", true, "output xml file for registry");
options.addOption("s", "schema", true, "the name of the schema to export");
CommandLine line = parser.parse(options, args);
-
- String file = null;
+
+ String file = null;
String schema = null;
- if (line.hasOption('f'))
- {
- file = line.getOptionValue('f');
- }
- else
- {
+ if (line.hasOption('f')) {
+ file = line.getOptionValue('f');
+ } else {
usage();
System.exit(0);
}
-
- if (line.hasOption('s'))
- {
+
+ if (line.hasOption('s')) {
schema = line.getOptionValue('s');
}
@@ -90,15 +96,16 @@ public class MetadataExporter
/**
* Save a registry to a filepath
- * @param file filepath
+ *
+ * @param file filepath
* @param schema schema definition to save
- * @throws SQLException if database error
- * @throws IOException if IO error
- * @throws SAXException if XML error
+ * @throws SQLException if database error
+ * @throws IOException if IO error
+ * @throws SAXException if XML error
* @throws RegistryExportException if export error
*/
- public static void saveRegistry(String file, String schema) throws SQLException, IOException, SAXException, RegistryExportException
- {
+ public static void saveRegistry(String file, String schema)
+ throws SQLException, IOException, SAXException, RegistryExportException {
// create a context
Context context = new Context();
context.turnOffAuthorisationSystem();
@@ -106,113 +113,102 @@ public class MetadataExporter
OutputFormat xmlFormat = new OutputFormat(Method.XML, "UTF-8", true);
xmlFormat.setLineWidth(120);
xmlFormat.setIndent(4);
-
+
XMLSerializer xmlSerializer = new XMLSerializer(new BufferedWriter(new FileWriter(file)), xmlFormat);
// XMLSerializer xmlSerializer = new XMLSerializer(System.out, xmlFormat);
xmlSerializer.startDocument();
xmlSerializer.startElement("dspace-dc-types", null);
-
+
// Save the schema definition(s)
saveSchema(context, xmlSerializer, schema);
List mdFields = null;
// If a single schema has been specified
- if (schema != null && !"".equals(schema))
- {
+ if (schema != null && !"".equals(schema)) {
// Get the id of that schema
MetadataSchema mdSchema = metadataSchemaService.find(context, schema);
- if (mdSchema == null)
- {
+ if (mdSchema == null) {
throw new RegistryExportException("no schema to export");
}
-
+
// Get the metadata fields only for the specified schema
mdFields = metadataFieldService.findAllInSchema(context, mdSchema);
- }
- else
- {
+ } else {
// Get the metadata fields for all the schemas
mdFields = metadataFieldService.findAll(context);
}
-
+
// Output the metadata fields
- for (MetadataField mdField : mdFields)
- {
+ for (MetadataField mdField : mdFields) {
saveType(context, xmlSerializer, mdField);
}
-
+
xmlSerializer.endElement("dspace-dc-types");
xmlSerializer.endDocument();
-
+
// abort the context, as we shouldn't have changed it!!
context.abort();
}
-
+
/**
* Serialize the schema registry. If the parameter 'schema' is null or empty, save all schemas
- * @param context DSpace Context
+ *
+ * @param context DSpace Context
* @param xmlSerializer XML serializer
- * @param schema schema (may be null to save all)
- * @throws SQLException if database error
- * @throws SAXException if XML error
+ * @param schema schema (may be null to save all)
+ * @throws SQLException if database error
+ * @throws SAXException if XML error
* @throws RegistryExportException if export error
*/
- public static void saveSchema(Context context, XMLSerializer xmlSerializer, String schema) throws SQLException, SAXException, RegistryExportException
- {
- if (schema != null && !"".equals(schema))
- {
+ public static void saveSchema(Context context, XMLSerializer xmlSerializer, String schema)
+ throws SQLException, SAXException, RegistryExportException {
+ if (schema != null && !"".equals(schema)) {
// Find a single named schema
MetadataSchema mdSchema = metadataSchemaService.find(context, schema);
-
+
saveSchema(xmlSerializer, mdSchema);
- }
- else
- {
+ } else {
// Find all schemas
List mdSchemas = metadataSchemaService.findAll(context);
-
- for (MetadataSchema mdSchema : mdSchemas)
- {
+
+ for (MetadataSchema mdSchema : mdSchemas) {
saveSchema(xmlSerializer, mdSchema);
}
}
}
-
+
/**
* Serialize a single schema (namespace) registry entry
- *
+ *
* @param xmlSerializer XML serializer
- * @param mdSchema DSpace metadata schema
- * @throws SAXException if XML error
+ * @param mdSchema DSpace metadata schema
+ * @throws SAXException if XML error
* @throws RegistryExportException if export error
*/
- private static void saveSchema(XMLSerializer xmlSerializer, MetadataSchema mdSchema) throws SAXException, RegistryExportException
- {
+ private static void saveSchema(XMLSerializer xmlSerializer, MetadataSchema mdSchema)
+ throws SAXException, RegistryExportException {
// If we haven't got a schema, it's an error
- if (mdSchema == null)
- {
+ if (mdSchema == null) {
throw new RegistryExportException("no schema to export");
}
-
- String name = mdSchema.getName();
+
+ String name = mdSchema.getName();
String namespace = mdSchema.getNamespace();
-
- if (name == null || "".equals(name))
- {
+
+ if (name == null || "".equals(name)) {
System.out.println("name is null, skipping");
return;
}
- if (namespace == null || "".equals(namespace))
- {
+ if (namespace == null || "".equals(namespace)) {
System.out.println("namespace is null, skipping");
return;
}
// Output the parent tag
xmlSerializer.startElement("dc-schema", null);
-
+
// Output the schema name
xmlSerializer.startElement("name", null);
xmlSerializer.characters(name.toCharArray(), 0, name.length());
@@ -225,26 +221,25 @@ public class MetadataExporter
xmlSerializer.endElement("dc-schema");
}
-
+
/**
* Serialize a single metadata field registry entry to xml
- *
- * @param context DSpace context
+ *
+ * @param context DSpace context
* @param xmlSerializer xml serializer
- * @param mdField DSpace metadata field
- * @throws SAXException if XML error
+ * @param mdField DSpace metadata field
+ * @throws SAXException if XML error
* @throws RegistryExportException if export error
- * @throws SQLException if database error
- * @throws IOException if IO error
+ * @throws SQLException if database error
+ * @throws IOException if IO error
*/
- private static void saveType(Context context, XMLSerializer xmlSerializer, MetadataField mdField) throws SAXException, RegistryExportException, SQLException, IOException
- {
+ private static void saveType(Context context, XMLSerializer xmlSerializer, MetadataField mdField)
+ throws SAXException, RegistryExportException, SQLException, IOException {
// If we haven't been given a field, it's an error
- if (mdField == null)
- {
+ if (mdField == null) {
throw new RegistryExportException("no field to export");
}
-
+
// Get the data from the metadata field
String schemaName = getSchemaName(context, mdField);
String element = mdField.getElement();
@@ -252,8 +247,7 @@ public class MetadataExporter
String scopeNote = mdField.getScopeNote();
// We must have a schema and element
- if (schemaName == null || element == null)
- {
+ if (schemaName == null || element == null) {
throw new RegistryExportException("incomplete field information");
}
@@ -271,73 +265,64 @@ public class MetadataExporter
xmlSerializer.endElement("element");
// Output the qualifier, if present
- if (qualifier != null)
- {
+ if (qualifier != null) {
xmlSerializer.startElement("qualifier", null);
xmlSerializer.characters(qualifier.toCharArray(), 0, qualifier.length());
xmlSerializer.endElement("qualifier");
- }
- else
- {
+ } else {
xmlSerializer.comment("unqualified");
}
-
+
// Output the scope note, if present
- if (scopeNote != null)
- {
+ if (scopeNote != null) {
xmlSerializer.startElement("scope_note", null);
xmlSerializer.characters(scopeNote.toCharArray(), 0, scopeNote.length());
xmlSerializer.endElement("scope_note");
- }
- else
- {
+ } else {
xmlSerializer.comment("no scope note");
}
-
+
xmlSerializer.endElement("dc-type");
}
-
+
static Map schemaMap = new HashMap();
+
/**
* Helper method to retrieve a schema name for the field.
* Caches the name after looking up the id.
+ *
* @param context DSpace Context
* @param mdField DSpace metadata field
* @return name of schema
- * @throws SQLException if database error
+ * @throws SQLException if database error
* @throws RegistryExportException if export error
*/
- private static String getSchemaName(Context context, MetadataField mdField) throws SQLException, RegistryExportException
- {
+ private static String getSchemaName(Context context, MetadataField mdField)
+ throws SQLException, RegistryExportException {
// Get name from cache
String name = schemaMap.get(mdField.getMetadataSchema().getID());
- if (name == null)
- {
+ if (name == null) {
// Name not retrieved before, so get the schema now
MetadataSchema mdSchema = metadataSchemaService.find(context, mdField.getMetadataSchema().getID());
- if (mdSchema != null)
- {
+ if (mdSchema != null) {
name = mdSchema.getName();
schemaMap.put(mdSchema.getID(), name);
- }
- else
- {
+ } else {
// Can't find the schema
throw new RegistryExportException("Can't get schema name for field");
}
}
return name;
}
-
+
/**
* Print the usage message to stdout
*/
- public static void usage()
- {
+ public static void usage() {
String usage = "Use this class with the following options:\n" +
- " -f : specify the output file for the schemas\n" +
- " -s : name of the schema to export\n";
+ " -f : specify the output file for the schemas\n" +
+ " -s : name of the schema to export\n";
System.out.println(usage);
}
}
diff --git a/dspace-api/src/main/java/org/dspace/administer/MetadataImporter.java b/dspace-api/src/main/java/org/dspace/administer/MetadataImporter.java
index 49e6661a55..ac547f1839 100644
--- a/dspace-api/src/main/java/org/dspace/administer/MetadataImporter.java
+++ b/dspace-api/src/main/java/org/dspace/administer/MetadataImporter.java
@@ -9,7 +9,6 @@ package org.dspace.administer;
import java.io.IOException;
import java.sql.SQLException;
-
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
@@ -18,9 +17,7 @@ import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
-
import org.apache.xpath.XPathAPI;
-
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.MetadataField;
import org.dspace.content.MetadataSchema;
@@ -31,79 +28,81 @@ import org.dspace.content.service.MetadataSchemaService;
import org.dspace.core.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-
import org.xml.sax.SAXException;
/**
* @author Richard Jones
*
* This class takes an xml document as passed in the arguments and
- * uses it to create metadata elements in the Metadata Registry if
+ * uses it to create metadata elements in the Metadata Registry if
* they do not already exist
- *
+ *
* The format of the XML file is as follows:
- *
+ *
* {@code
*
- *
- * icadmin
- * status
- * dateset
- * the workflow status of an item
- *
- *
- * [....]
- *
+ *
+ * icadmin
+ * status
+ * dateset
+ * the workflow status of an item
+ *
+ *
+ * [....]
+ *
*
* }
*/
-public class MetadataImporter
-{
- protected static MetadataSchemaService metadataSchemaService = ContentServiceFactory.getInstance().getMetadataSchemaService();
- protected static MetadataFieldService metadataFieldService = ContentServiceFactory.getInstance().getMetadataFieldService();
+public class MetadataImporter {
+ protected static MetadataSchemaService metadataSchemaService = ContentServiceFactory.getInstance()
+ .getMetadataSchemaService();
+ protected static MetadataFieldService metadataFieldService = ContentServiceFactory.getInstance()
+ .getMetadataFieldService();
- /** logging category */
+ /**
+ * logging category
+ */
private static final Logger log = LoggerFactory.getLogger(MetadataImporter.class);
+ /**
+ * Default constructor
+ */
+ private MetadataImporter() { }
+
/**
* main method for reading user input from the command line
*
* @param args the command line arguments given
- * @throws ParseException if parse error
- * @throws SQLException if database error
- * @throws IOException if IO error
- * @throws TransformerException if transformer error
+ * @throws ParseException if parse error
+ * @throws SQLException if database error
+ * @throws IOException if IO error
+ * @throws TransformerException if transformer error
* @throws ParserConfigurationException if config error
- * @throws AuthorizeException if authorization error
- * @throws SAXException if parser error
- * @throws NonUniqueMetadataException if duplicate metadata
- * @throws RegistryImportException if import fails
+ * @throws AuthorizeException if authorization error
+ * @throws SAXException if parser error
+ * @throws NonUniqueMetadataException if duplicate metadata
+ * @throws RegistryImportException if import fails
**/
public static void main(String[] args)
throws ParseException, SQLException, IOException, TransformerException,
- ParserConfigurationException, AuthorizeException, SAXException,
- NonUniqueMetadataException, RegistryImportException
- {
+ ParserConfigurationException, AuthorizeException, SAXException,
+ NonUniqueMetadataException, RegistryImportException {
boolean forceUpdate = false;
-
+
// create an options object and populate it
CommandLineParser parser = new PosixParser();
Options options = new Options();
options.addOption("f", "file", true, "source xml file for DC fields");
options.addOption("u", "update", false, "update an existing schema");
CommandLine line = parser.parse(options, args);
-
+
String file = null;
- if (line.hasOption('f'))
- {
+ if (line.hasOption('f')) {
file = line.getOptionValue('f');
- }
- else
- {
+ } else {
usage();
System.exit(0);
}
@@ -111,29 +110,27 @@ public class MetadataImporter
forceUpdate = line.hasOption('u');
loadRegistry(file, forceUpdate);
}
-
+
/**
* Load the data from the specified file path into the database
- *
- * @param file the file path containing the source data
- * @param forceUpdate whether to force update
- * @throws SQLException if database error
- * @throws IOException if IO error
- * @throws TransformerException if transformer error
+ *
+ * @param file the file path containing the source data
+ * @param forceUpdate whether to force update
+ * @throws SQLException if database error
+ * @throws IOException if IO error
+ * @throws TransformerException if transformer error
* @throws ParserConfigurationException if config error
- * @throws AuthorizeException if authorization error
- * @throws SAXException if parser error
- * @throws NonUniqueMetadataException if duplicate metadata
- * @throws RegistryImportException if import fails
+ * @throws AuthorizeException if authorization error
+ * @throws SAXException if parser error
+ * @throws NonUniqueMetadataException if duplicate metadata
+ * @throws RegistryImportException if import fails
*/
public static void loadRegistry(String file, boolean forceUpdate)
- throws SQLException, IOException, TransformerException, ParserConfigurationException,
- AuthorizeException, SAXException, NonUniqueMetadataException, RegistryImportException
- {
+ throws SQLException, IOException, TransformerException, ParserConfigurationException,
+ AuthorizeException, SAXException, NonUniqueMetadataException, RegistryImportException {
Context context = null;
- try
- {
+ try {
// create a context
context = new Context();
context.turnOffAuthorisationSystem();
@@ -145,8 +142,7 @@ public class MetadataImporter
NodeList schemaNodes = XPathAPI.selectNodeList(document, "/dspace-dc-types/dc-schema");
// Add each one as a new format to the registry
- for (int i = 0; i < schemaNodes.getLength(); i++)
- {
+ for (int i = 0; i < schemaNodes.getLength(); i++) {
Node n = schemaNodes.item(i);
loadSchema(context, n, forceUpdate);
}
@@ -155,110 +151,95 @@ public class MetadataImporter
NodeList typeNodes = XPathAPI.selectNodeList(document, "/dspace-dc-types/dc-type");
// Add each one as a new format to the registry
- for (int i = 0; i < typeNodes.getLength(); i++)
- {
+ for (int i = 0; i < typeNodes.getLength(); i++) {
Node n = typeNodes.item(i);
loadType(context, n);
}
context.restoreAuthSystemState();
context.complete();
- }
- finally
- {
- // Clean up our context, if it still exists & it was never completed
- if(context!=null && context.isValid())
+ } finally {
+ // Clean up our context, if it still exists & it was never completed
+ if (context != null && context.isValid()) {
context.abort();
+ }
}
}
-
+
/**
* Process a node in the metadata registry XML file. If the
* schema already exists, it will not be recreated
- *
- * @param context
- * DSpace context object
- * @param node
- * the node in the DOM tree
- * @throws SQLException if database error
- * @throws IOException if IO error
- * @throws TransformerException if transformer error
- * @throws AuthorizeException if authorization error
+ *
+ * @param context DSpace context object
+ * @param node the node in the DOM tree
+ * @throws SQLException if database error
+ * @throws IOException if IO error
+ * @throws TransformerException if transformer error
+ * @throws AuthorizeException if authorization error
* @throws NonUniqueMetadataException if duplicate metadata
- * @throws RegistryImportException if import fails
+ * @throws RegistryImportException if import fails
*/
private static void loadSchema(Context context, Node node, boolean updateExisting)
throws SQLException, IOException, TransformerException,
- AuthorizeException, NonUniqueMetadataException, RegistryImportException
- {
+ AuthorizeException, NonUniqueMetadataException, RegistryImportException {
// Get the values
String name = RegistryImporter.getElementData(node, "name");
String namespace = RegistryImporter.getElementData(node, "namespace");
-
- if (name == null || "".equals(name))
- {
+
+ if (name == null || "".equals(name)) {
throw new RegistryImportException("Name of schema must be supplied");
}
- if (namespace == null || "".equals(namespace))
- {
+ if (namespace == null || "".equals(namespace)) {
throw new RegistryImportException("Namespace of schema must be supplied");
}
// check to see if the schema already exists
MetadataSchema s = metadataSchemaService.find(context, name);
-
- if (s == null)
- {
+
+ if (s == null) {
// Schema does not exist - create
log.info("Registering Schema " + name + " (" + namespace + ")");
metadataSchemaService.create(context, name, namespace);
- }
- else
- {
+ } else {
// Schema exists - if it's the same namespace, allow the type imports to continue
- if (s.getNamespace().equals(namespace))
- {
+ if (s.getNamespace().equals(namespace)) {
// This schema already exists with this namespace, skipping it
return;
}
-
+
// It's a different namespace - have we been told to update?
- if (updateExisting)
- {
+ if (updateExisting) {
// Update the existing schema namespace and continue to type import
log.info("Updating Schema " + name + ": New namespace " + namespace);
s.setNamespace(namespace);
metadataSchemaService.update(context, s);
- }
- else
- {
- throw new RegistryImportException("Schema " + name + " already registered with different namespace " + namespace + ". Rerun with 'update' option enabled if you wish to update this schema.");
+ } else {
+ throw new RegistryImportException(
+ "Schema " + name + " already registered with different namespace " + namespace + ". Rerun with " +
+ "'update' option enabled if you wish to update this schema.");
}
}
-
+
}
-
+
/**
* Process a node in the metadata registry XML file. The node must
* be a "dc-type" node. If the type already exists, then it
* will not be reimported
- *
- * @param context
- * DSpace context object
- * @param node
- * the node in the DOM tree
- * @throws SQLException if database error
- * @throws IOException if IO error
- * @throws TransformerException if transformer error
- * @throws AuthorizeException if authorization error
+ *
+ * @param context DSpace context object
+ * @param node the node in the DOM tree
+ * @throws SQLException if database error
+ * @throws IOException if IO error
+ * @throws TransformerException if transformer error
+ * @throws AuthorizeException if authorization error
* @throws NonUniqueMetadataException if duplicate metadata
- * @throws RegistryImportException if import fails
+ * @throws RegistryImportException if import fails
*/
private static void loadType(Context context, Node node)
- throws SQLException, IOException, TransformerException,
- AuthorizeException, NonUniqueMetadataException, RegistryImportException
- {
+ throws SQLException, IOException, TransformerException,
+ AuthorizeException, NonUniqueMetadataException, RegistryImportException {
// Get the values
String schema = RegistryImporter.getElementData(node, "schema");
String element = RegistryImporter.getElementData(node, "element");
@@ -266,44 +247,41 @@ public class MetadataImporter
String scopeNote = RegistryImporter.getElementData(node, "scope_note");
// If the schema is not provided default to DC
- if (schema == null)
- {
+ if (schema == null) {
schema = MetadataSchema.DC_SCHEMA;
}
-
+
// Find the matching schema object
MetadataSchema schemaObj = metadataSchemaService.find(context, schema);
-
- if (schemaObj == null)
- {
+
+ if (schemaObj == null) {
throw new RegistryImportException("Schema '" + schema + "' is not registered and does not exist.");
}
-
+
MetadataField mf = metadataFieldService.findByElement(context, schemaObj, element, qualifier);
- if (mf != null)
- {
+ if (mf != null) {
// Metadata field already exists, skipping it
return;
}
-
+
// Actually create this metadata field as it doesn't yet exist
String fieldName = schema + "." + element + "." + qualifier;
- if(qualifier==null)
+ if (qualifier == null) {
fieldName = schema + "." + element;
+ }
log.info("Registering metadata field " + fieldName);
MetadataField field = metadataFieldService.create(context, schemaObj, element, qualifier, scopeNote);
metadataFieldService.update(context, field);
}
-
+
/**
* Print the usage message to stdout
*/
- public static void usage()
- {
+ public static void usage() {
String usage = "Use this class with the following option:\n" +
- " -f : specify which xml source file " +
- "contains the DC fields to import.\n";
+ " -f : specify which xml source file " +
+ "contains the DC fields to import.\n";
System.out.println(usage);
}
}
diff --git a/dspace-api/src/main/java/org/dspace/administer/RegistryExportException.java b/dspace-api/src/main/java/org/dspace/administer/RegistryExportException.java
index d40a11ec19..9df2d6caf7 100644
--- a/dspace-api/src/main/java/org/dspace/administer/RegistryExportException.java
+++ b/dspace-api/src/main/java/org/dspace/administer/RegistryExportException.java
@@ -12,45 +12,40 @@ package org.dspace.administer;
*
* An exception to report any problems with registry exports
*/
-public class RegistryExportException extends Exception
-{
+public class RegistryExportException extends Exception {
/**
* Create an empty authorize exception
*/
- public RegistryExportException()
- {
+ public RegistryExportException() {
super();
}
/**
* create an exception with only a message
- *
+ *
* @param message exception message
*/
- public RegistryExportException(String message)
- {
+ public RegistryExportException(String message) {
super(message);
}
-
+
/**
* create an exception with an inner exception and a message
- *
+ *
* @param message exception message
- * @param e reference to Throwable
+ * @param e reference to Throwable
*/
- public RegistryExportException(String message, Throwable e)
- {
+ public RegistryExportException(String message, Throwable e) {
super(message, e);
}
-
+
/**
* create an exception with an inner exception
- *
+ *
* @param e reference to Throwable
*/
- public RegistryExportException(Throwable e)
- {
+ public RegistryExportException(Throwable e) {
super(e);
}
-
+
}
diff --git a/dspace-api/src/main/java/org/dspace/administer/RegistryImportException.java b/dspace-api/src/main/java/org/dspace/administer/RegistryImportException.java
index 5a15631dfb..83f9c9fa88 100644
--- a/dspace-api/src/main/java/org/dspace/administer/RegistryImportException.java
+++ b/dspace-api/src/main/java/org/dspace/administer/RegistryImportException.java
@@ -12,45 +12,40 @@ package org.dspace.administer;
*
* An exception to report any problems with registry imports
*/
-public class RegistryImportException extends Exception
-{
+public class RegistryImportException extends Exception {
/**
* Create an empty authorize exception
*/
- public RegistryImportException()
- {
+ public RegistryImportException() {
super();
}
/**
* create an exception with only a message
- *
+ *
* @param message error message
*/
- public RegistryImportException(String message)
- {
+ public RegistryImportException(String message) {
super(message);
}
-
+
/**
* create an exception with an inner exception and a message
- *
- * @param message error message
- * @param e throwable
+ *
+ * @param message error message
+ * @param e throwable
*/
- public RegistryImportException(String message, Throwable e)
- {
- super(message, e);
+ public RegistryImportException(String message, Throwable e) {
+ super(message, e);
}
-
+
/**
* create an exception with an inner exception
- *
- * @param e throwable
+ *
+ * @param e throwable
*/
- public RegistryImportException(Throwable e)
- {
- super(e);
+ public RegistryImportException(Throwable e) {
+ super(e);
}
-
+
}
diff --git a/dspace-api/src/main/java/org/dspace/administer/RegistryImporter.java b/dspace-api/src/main/java/org/dspace/administer/RegistryImporter.java
index b8a7c2456a..5b5f70412a 100644
--- a/dspace-api/src/main/java/org/dspace/administer/RegistryImporter.java
+++ b/dspace-api/src/main/java/org/dspace/administer/RegistryImporter.java
@@ -9,18 +9,15 @@ package org.dspace.administer;
import java.io.File;
import java.io.IOException;
-
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.apache.xpath.XPathAPI;
-
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-
import org.xml.sax.SAXException;
/**
@@ -31,30 +28,32 @@ import org.xml.sax.SAXException;
* I am the author, really I ripped these methods off from other
* classes
*/
-public class RegistryImporter
-{
+public class RegistryImporter {
+
+ /**
+ * Default constructor
+ */
+ private RegistryImporter() { }
+
/**
* Load in the XML from file.
- *
- * @param filename
- * the filename to load from
- *
+ *
+ * @param filename the filename to load from
* @return the DOM representation of the XML file
- * @throws IOException if IO error
+ * @throws IOException if IO error
* @throws ParserConfigurationException if configuration parse error
- * @throws SAXException if XML parse error
+ * @throws SAXException if XML parse error
*/
- public static Document loadXML(String filename)
- throws IOException, ParserConfigurationException, SAXException
- {
+ public static Document loadXML(String filename)
+ throws IOException, ParserConfigurationException, SAXException {
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
- .newDocumentBuilder();
+ .newDocumentBuilder();
Document document = builder.parse(new File(filename));
-
+
return document;
}
-
+
/**
* Get the CDATA of a particular element. For example, if the XML document
* contains:
@@ -66,22 +65,18 @@ public class RegistryImporter
* return application/pdf
.
*
* Why this isn't a core part of the XML API I do not know...
- *
- * @param parentElement
- * the element, whose child element you want the CDATA from
- * @param childName
- * the name of the element you want the CDATA from
- * @throws TransformerException if error
+ *
+ * @param parentElement the element, whose child element you want the CDATA from
+ * @param childName the name of the element you want the CDATA from
* @return the CDATA as a String
+ * @throws TransformerException if error
*/
public static String getElementData(Node parentElement, String childName)
- throws TransformerException
- {
+ throws TransformerException {
// Grab the child node
Node childNode = XPathAPI.selectSingleNode(parentElement, childName);
- if (childNode == null)
- {
+ if (childNode == null) {
// No child node, so no values
return null;
}
@@ -89,8 +84,7 @@ public class RegistryImporter
// Get the #text
Node dataNode = childNode.getFirstChild();
- if (dataNode == null)
- {
+ if (dataNode == null) {
return null;
}
@@ -106,32 +100,28 @@ public class RegistryImporter
*
*
* <foo>
- * <bar>val1</bar>
- * <bar>val2</bar>
+ * <bar>val1</bar>
+ * <bar>val2</bar>
* </foo>
*
* passing this the foo
node and bar
will
* return val1
and val2
.
*
* Why this also isn't a core part of the XML API I do not know...
- *
- * @param parentElement
- * the element, whose child element you want the CDATA from
- * @param childName
- * the name of the element you want the CDATA from
- * @throws TransformerException if error
+ *
+ * @param parentElement the element, whose child element you want the CDATA from
+ * @param childName the name of the element you want the CDATA from
* @return the CDATA as a String
+ * @throws TransformerException if error
*/
public static String[] getRepeatedElementData(Node parentElement,
- String childName) throws TransformerException
- {
+ String childName) throws TransformerException {
// Grab the child node
NodeList childNodes = XPathAPI.selectNodeList(parentElement, childName);
String[] data = new String[childNodes.getLength()];
- for (int i = 0; i < childNodes.getLength(); i++)
- {
+ for (int i = 0; i < childNodes.getLength(); i++) {
// Get the #text node
Node dataNode = childNodes.item(i).getFirstChild();
diff --git a/dspace-api/src/main/java/org/dspace/administer/RegistryLoader.java b/dspace-api/src/main/java/org/dspace/administer/RegistryLoader.java
index 4ec485fb43..f1b8a0684c 100644
--- a/dspace-api/src/main/java/org/dspace/administer/RegistryLoader.java
+++ b/dspace-api/src/main/java/org/dspace/administer/RegistryLoader.java
@@ -12,7 +12,6 @@ import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
-
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@@ -40,32 +39,37 @@ import org.xml.sax.SAXException;
* RegistryLoader -bitstream bitstream-formats.xml
*
* RegistryLoader -dc dc-types.xml
- *
+ *
* @author Robert Tansley
* @version $Revision$
*/
-public class RegistryLoader
-{
- /** log4j category */
+public class RegistryLoader {
+ /**
+ * log4j category
+ */
private static Logger log = Logger.getLogger(RegistryLoader.class);
- protected static BitstreamFormatService bitstreamFormatService = ContentServiceFactory.getInstance().getBitstreamFormatService();
+ protected static BitstreamFormatService bitstreamFormatService = ContentServiceFactory.getInstance()
+ .getBitstreamFormatService();
+
+ /**
+ * Default constructor
+ */
+ private RegistryLoader() { }
/**
* For invoking via the command line
- *
+ *
* @param argv the command line arguments given
* @throws Exception if error
*/
- public static void main(String[] argv) throws Exception
- {
+ public static void main(String[] argv) throws Exception {
String usage = "Usage: " + RegistryLoader.class.getName()
- + " (-bitstream | -metadata) registry-file.xml";
+ + " (-bitstream | -metadata) registry-file.xml";
Context context = null;
- try
- {
+ try {
context = new Context();
// Can't update registries anonymously, so we need to turn off
@@ -73,17 +77,12 @@ public class RegistryLoader
context.turnOffAuthorisationSystem();
// Work out what we're loading
- if (argv[0].equalsIgnoreCase("-bitstream"))
- {
+ if (argv[0].equalsIgnoreCase("-bitstream")) {
RegistryLoader.loadBitstreamFormats(context, argv[1]);
- }
- else if (argv[0].equalsIgnoreCase("-metadata"))
- {
+ } else if (argv[0].equalsIgnoreCase("-metadata")) {
// Call MetadataImporter, as it handles Metadata schema updates
MetadataImporter.loadRegistry(argv[1], true);
- }
- else
- {
+ } else {
System.err.println(usage);
}
@@ -91,81 +90,69 @@ public class RegistryLoader
context.complete();
System.exit(0);
- }
- catch (ArrayIndexOutOfBoundsException ae)
- {
+ } catch (ArrayIndexOutOfBoundsException ae) {
System.err.println(usage);
System.exit(1);
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
log.fatal(LogManager.getHeader(context, "error_loading_registries",
- ""), e);
+ ""), e);
System.err.println("Error: \n - " + e.getMessage());
System.exit(1);
- }
- finally
- {
+ } finally {
// Clean up our context, if it still exists & it was never completed
- if(context!=null && context.isValid())
+ if (context != null && context.isValid()) {
context.abort();
+ }
}
}
/**
* Load Bitstream Format metadata
- *
- * @param context
- * DSpace context object
- * @param filename
- * the filename of the XML file to load
- * @throws SQLException if database error
- * @throws IOException if IO error
- * @throws TransformerException if transformer error
+ *
+ * @param context DSpace context object
+ * @param filename the filename of the XML file to load
+ * @throws SQLException if database error
+ * @throws IOException if IO error
+ * @throws TransformerException if transformer error
* @throws ParserConfigurationException if config error
- * @throws AuthorizeException if authorization error
- * @throws SAXException if parser error
+ * @throws AuthorizeException if authorization error
+ * @throws SAXException if parser error
*/
public static void loadBitstreamFormats(Context context, String filename)
- throws SQLException, IOException, ParserConfigurationException,
- SAXException, TransformerException, AuthorizeException
- {
+ throws SQLException, IOException, ParserConfigurationException,
+ SAXException, TransformerException, AuthorizeException {
Document document = loadXML(filename);
// Get the nodes corresponding to formats
NodeList typeNodes = XPathAPI.selectNodeList(document,
- "dspace-bitstream-types/bitstream-type");
+ "dspace-bitstream-types/bitstream-type");
// Add each one as a new format to the registry
- for (int i = 0; i < typeNodes.getLength(); i++)
- {
+ for (int i = 0; i < typeNodes.getLength(); i++) {
Node n = typeNodes.item(i);
loadFormat(context, n);
}
log.info(LogManager.getHeader(context, "load_bitstream_formats",
- "number_loaded=" + typeNodes.getLength()));
+ "number_loaded=" + typeNodes.getLength()));
}
/**
* Process a node in the bitstream format registry XML file. The node must
* be a "bitstream-type" node
- *
- * @param context
- * DSpace context object
- * @param node
- * the node in the DOM tree
- * @throws SQLException if database error
- * @throws IOException if IO error
+ *
+ * @param context DSpace context object
+ * @param node the node in the DOM tree
+ * @throws SQLException if database error
+ * @throws IOException if IO error
* @throws TransformerException if transformer error
- * @throws AuthorizeException if authorization error
+ * @throws AuthorizeException if authorization error
*/
private static void loadFormat(Context context, Node node)
- throws SQLException, IOException, TransformerException,
- AuthorizeException
- {
+ throws SQLException, IOException, TransformerException,
+ AuthorizeException {
// Get the values
String mimeType = getElementData(node, "mimetype");
String shortDesc = getElementData(node, "short_description");
@@ -181,16 +168,14 @@ public class RegistryLoader
// Check if this format already exists in our registry (by mime type)
BitstreamFormat exists = bitstreamFormatService.findByMIMEType(context, mimeType);
-
+
// If not found by mimeType, check by short description (since this must also be unique)
- if(exists==null)
- {
+ if (exists == null) {
exists = bitstreamFormatService.findByShortDescription(context, shortDesc);
}
-
+
// If it doesn't exist, create it..otherwise skip it.
- if(exists==null)
- {
+ if (exists == null) {
// Create the format object
BitstreamFormat format = bitstreamFormatService.create(context);
@@ -213,19 +198,17 @@ public class RegistryLoader
/**
* Load in the XML from file.
- *
- * @param filename
- * the filename to load from
- * @throws IOException if IO error
- * @throws ParserConfigurationException if config error
- * @throws SAXException if parser error
+ *
+ * @param filename the filename to load from
* @return the DOM representation of the XML file
+ * @throws IOException if IO error
+ * @throws ParserConfigurationException if config error
+ * @throws SAXException if parser error
*/
private static Document loadXML(String filename) throws IOException,
- ParserConfigurationException, SAXException
- {
+ ParserConfigurationException, SAXException {
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
- .newDocumentBuilder();
+ .newDocumentBuilder();
return builder.parse(new File(filename));
}
@@ -241,22 +224,18 @@ public class RegistryLoader
* return application/pdf
.
*
* Why this isn't a core part of the XML API I do not know...
- *
- * @param parentElement
- * the element, whose child element you want the CDATA from
- * @param childName
- * the name of the element you want the CDATA from
- * @throws TransformerException if transformer error
+ *
+ * @param parentElement the element, whose child element you want the CDATA from
+ * @param childName the name of the element you want the CDATA from
* @return the CDATA as a String
+ * @throws TransformerException if transformer error
*/
private static String getElementData(Node parentElement, String childName)
- throws TransformerException
- {
+ throws TransformerException {
// Grab the child node
Node childNode = XPathAPI.selectSingleNode(parentElement, childName);
- if (childNode == null)
- {
+ if (childNode == null) {
// No child node, so no values
return null;
}
@@ -264,8 +243,7 @@ public class RegistryLoader
// Get the #text
Node dataNode = childNode.getFirstChild();
- if (dataNode == null)
- {
+ if (dataNode == null) {
return null;
}
@@ -281,32 +259,28 @@ public class RegistryLoader
*
*
* <foo>
- * <bar>val1</bar>
- * <bar>val2</bar>
+ * <bar>val1</bar>
+ * <bar>val2</bar>
* </foo>
*
* passing this the foo
node and bar
will
* return val1
and val2
.
*
* Why this also isn't a core part of the XML API I do not know...
- *
- * @param parentElement
- * the element, whose child element you want the CDATA from
- * @param childName
- * the name of the element you want the CDATA from
- * @throws TransformerException if transformer error
+ *
+ * @param parentElement the element, whose child element you want the CDATA from
+ * @param childName the name of the element you want the CDATA from
* @return the CDATA as a String
+ * @throws TransformerException if transformer error
*/
private static String[] getRepeatedElementData(Node parentElement,
- String childName) throws TransformerException
- {
+ String childName) throws TransformerException {
// Grab the child node
NodeList childNodes = XPathAPI.selectNodeList(parentElement, childName);
String[] data = new String[childNodes.getLength()];
- for (int i = 0; i < childNodes.getLength(); i++)
- {
+ for (int i = 0; i < childNodes.getLength(); i++) {
// Get the #text node
Node dataNode = childNodes.item(i).getFirstChild();
diff --git a/dspace-api/src/main/java/org/dspace/administer/StructBuilder.java b/dspace-api/src/main/java/org/dspace/administer/StructBuilder.java
index c1f8f928fd..8c408b0e8a 100644
--- a/dspace-api/src/main/java/org/dspace/administer/StructBuilder.java
+++ b/dspace-api/src/main/java/org/dspace/administer/StructBuilder.java
@@ -14,7 +14,6 @@ import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
-
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@@ -44,116 +43,119 @@ import org.xml.sax.SAXException;
/**
* This class deals with importing community and collection structures from
* an XML file.
- *
+ *
* The XML file structure needs to be:
* {@code
*
- *
- * ....
- * ...
- *
- * ....
- *
- *
+ *
+ * ....
+ * ...
+ *
+ * ....
+ *
+ *
*
* }
* it can be arbitrarily deep, and supports all the metadata elements
* that make up the community and collection metadata. See the system
* documentation for more details
- *
- * @author Richard Jones
*
+ * @author Richard Jones
*/
-public class StructBuilder
-{
- /** the output xml document which will contain updated information about the
+public class StructBuilder {
+ /**
+ * the output xml document which will contain updated information about the
* imported structure
*/
private static org.jdom.Document xmlOutput = new org.jdom.Document(new Element("imported_structure"));
-
- /** a hashtable to hold metadata for the collection being worked on */
+
+ /**
+ * a hashtable to hold metadata for the collection being worked on
+ */
private static Map collectionMap = new HashMap();
-
- /** a hashtable to hold metadata for the community being worked on */
+
+ /**
+ * a hashtable to hold metadata for the community being worked on
+ */
private static Map communityMap = new HashMap();
protected static CommunityService communityService = ContentServiceFactory.getInstance().getCommunityService();
protected static CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService();
protected static EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
+ /**
+ * Default constructor
+ */
+ private StructBuilder() { }
+
/**
* Main method to be run from the command line to import a structure into
* DSpace
- *
+ *
* This is of the form:
- *
+ *
* {@code StructBuilder -f [xml source] -e [administrator email] -o [output file]}
- *
+ *
* The output file will contain exactly the same as the source xml document, but
* with the handle for each imported item added as an attribute.
*
* @param argv the command line arguments given
* @throws Exception if an error occurs
*/
- public static void main(String[] argv)
- throws Exception
- {
+ public static void main(String[] argv)
+ throws Exception {
CommandLineParser parser = new PosixParser();
Options options = new Options();
- options.addOption( "f", "file", true, "file");
- options.addOption( "e", "eperson", true, "eperson");
+ options.addOption("f", "file", true, "file");
+ options.addOption("e", "eperson", true, "eperson");
options.addOption("o", "output", true, "output");
-
- CommandLine line = parser.parse( options, argv );
-
+
+ CommandLine line = parser.parse(options, argv);
+
String file = null;
String eperson = null;
String output = null;
-
- if (line.hasOption('f'))
- {
+
+ if (line.hasOption('f')) {
file = line.getOptionValue('f');
}
-
- if (line.hasOption('e'))
- {
+
+ if (line.hasOption('e')) {
eperson = line.getOptionValue('e');
}
-
- if (line.hasOption('o'))
- {
+
+ if (line.hasOption('o')) {
output = line.getOptionValue('o');
}
-
- if (output == null || eperson == null || file == null)
- {
+
+ if (output == null || eperson == null || file == null) {
usage();
System.exit(0);
}
-
+
// create a context
Context context = new Context();
-
+
// set the context
context.setCurrentUser(ePersonService.findByEmail(context, eperson));
-
+
// load the XML
Document document = loadXML(file);
-
+
// run the preliminary validation, to be sure that the the XML document
// is properly structured
validate(document);
-
+
// load the mappings into the member variable hashmaps
communityMap.put("name", "name");
communityMap.put("description", "short_description");
communityMap.put("intro", "introductory_text");
communityMap.put("copyright", "copyright_text");
communityMap.put("sidebar", "side_bar_text");
-
+
collectionMap.put("name", "name");
collectionMap.put("description", "short_description");
collectionMap.put("intro", "introductory_text");
@@ -161,267 +163,232 @@ public class StructBuilder
collectionMap.put("sidebar", "side_bar_text");
collectionMap.put("license", "license");
collectionMap.put("provenance", "provenance_description");
-
+
// get the top level community list
NodeList first = XPathAPI.selectNodeList(document, "/import_structure/community");
-
+
// run the import starting with the top level communities
Element[] elements = handleCommunities(context, first, null);
-
+
// generate the output
Element root = xmlOutput.getRootElement();
- for (int i = 0; i < elements.length; i++)
- {
+ for (int i = 0; i < elements.length; i++) {
root.addContent(elements[i]);
}
-
+
// finally write the string into the output file
- try
- {
+ try {
BufferedWriter out = new BufferedWriter(new FileWriter(output));
out.write(new XMLOutputter().outputString(xmlOutput));
out.close();
- }
- catch (IOException e)
- {
+ } catch (IOException e) {
System.out.println("Unable to write to output file " + output);
System.exit(0);
}
-
+
context.complete();
}
-
+
/**
* Output the usage information
*/
- private static void usage()
- {
+ private static void usage() {
System.out.println("Usage: java StructBuilder -f -o -e ");
- System.out.println("Communities will be created from the top level, and a map of communities to handles will be returned in the output file");
+ System.out.println(
+ "Communities will be created from the top level, and a map of communities to handles will be returned in " +
+ "the output file");
return;
}
-
+
/**
* Validate the XML document. This method does not return, but if validation
* fails it generates an error and ceases execution
- *
- * @param document the XML document object
+ *
+ * @param document the XML document object
* @throws TransformerException if transformer error
- *
*/
private static void validate(org.w3c.dom.Document document)
- throws TransformerException
- {
+ throws TransformerException {
StringBuffer err = new StringBuffer();
boolean trip = false;
-
+
err.append("The following errors were encountered parsing the source XML\n");
err.append("No changes have been made to the DSpace instance\n\n");
-
+
NodeList first = XPathAPI.selectNodeList(document, "/import_structure/community");
- if (first.getLength() == 0)
- {
+ if (first.getLength() == 0) {
err.append("-There are no top level communities in the source document");
System.out.println(err.toString());
System.exit(0);
}
-
+
String errs = validateCommunities(first, 1);
- if (errs != null)
- {
+ if (errs != null) {
err.append(errs);
trip = true;
}
-
- if (trip)
- {
+
+ if (trip) {
System.out.println(err.toString());
System.exit(0);
}
}
-
+
/**
* Validate the communities section of the XML document. This returns a string
* containing any errors encountered, or null if there were no errors
- *
+ *
* @param communities the NodeList of communities to validate
- * @param level the level in the XML document that we are at, for the purposes
- * of error reporting
- *
+ * @param level the level in the XML document that we are at, for the purposes
+ * of error reporting
* @return the errors that need to be generated by the calling method, or null if
- * no errors.
+ * no errors.
*/
private static String validateCommunities(NodeList communities, int level)
- throws TransformerException
- {
+ throws TransformerException {
StringBuffer err = new StringBuffer();
boolean trip = false;
String errs = null;
-
- for (int i = 0; i < communities.getLength(); i++)
- {
+
+ for (int i = 0; i < communities.getLength(); i++) {
Node n = communities.item(i);
NodeList name = XPathAPI.selectNodeList(n, "name");
- if (name.getLength() != 1)
- {
+ if (name.getLength() != 1) {
String pos = Integer.toString(i + 1);
err.append("-The level " + level + " community in position " + pos);
err.append(" does not contain exactly one name field\n");
trip = true;
}
-
+
// validate sub communities
NodeList subCommunities = XPathAPI.selectNodeList(n, "community");
String comErrs = validateCommunities(subCommunities, level + 1);
- if (comErrs != null)
- {
+ if (comErrs != null) {
err.append(comErrs);
trip = true;
}
-
+
// validate collections
NodeList collections = XPathAPI.selectNodeList(n, "collection");
String colErrs = validateCollections(collections, level + 1);
- if (colErrs != null)
- {
+ if (colErrs != null) {
err.append(colErrs);
trip = true;
}
}
-
- if (trip)
- {
+
+ if (trip) {
errs = err.toString();
}
-
+
return errs;
}
-
+
/**
* validate the collection section of the XML document. This generates a
* string containing any errors encountered, or returns null if no errors
- *
+ *
* @param collections a NodeList of collections to validate
- * @param level the level in the XML document for the purposes of error reporting
- *
+ * @param level the level in the XML document for the purposes of error reporting
* @return the errors to be generated by the calling method, or null if none
*/
private static String validateCollections(NodeList collections, int level)
- throws TransformerException
- {
+ throws TransformerException {
StringBuffer err = new StringBuffer();
boolean trip = false;
String errs = null;
-
- for (int i = 0; i < collections.getLength(); i++)
- {
+
+ for (int i = 0; i < collections.getLength(); i++) {
Node n = collections.item(i);
NodeList name = XPathAPI.selectNodeList(n, "name");
- if (name.getLength() != 1)
- {
+ if (name.getLength() != 1) {
String pos = Integer.toString(i + 1);
err.append("-The level " + level + " collection in position " + pos);
err.append(" does not contain exactly one name field\n");
trip = true;
}
}
-
- if (trip)
- {
+
+ if (trip) {
errs = err.toString();
}
-
+
return errs;
}
-
+
/**
* Load in the XML from file.
- *
- * @param filename
- * the filename to load from
- *
+ *
+ * @param filename the filename to load from
* @return the DOM representation of the XML file
*/
- private static org.w3c.dom.Document loadXML(String filename)
- throws IOException, ParserConfigurationException, SAXException
- {
+ private static org.w3c.dom.Document loadXML(String filename)
+ throws IOException, ParserConfigurationException, SAXException {
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
- .newDocumentBuilder();
+ .newDocumentBuilder();
org.w3c.dom.Document document = builder.parse(new File(filename));
-
+
return document;
}
-
+
/**
* Return the String value of a Node
- *
+ *
* @param node the node from which we want to extract the string value
- *
* @return the string value of the node
*/
- public static String getStringValue(Node node)
- {
+ public static String getStringValue(Node node) {
String value = node.getNodeValue();
- if (node.hasChildNodes())
- {
+ if (node.hasChildNodes()) {
Node first = node.getFirstChild();
- if (first.getNodeType() == Node.TEXT_NODE)
- {
+ if (first.getNodeType() == Node.TEXT_NODE) {
return first.getNodeValue().trim();
}
}
return value;
}
-
+
/**
* Take a node list of communities and build the structure from them, delegating
* to the relevant methods in this class for sub-communities and collections
- *
- * @param context the context of the request
+ *
+ * @param context the context of the request
* @param communities a nodelist of communities to create along with their sub-structures
- * @param parent the parent community of the nodelist of communities to create
- *
- * @return an element array containing additional information regarding the
- * created communities (e.g. the handles they have been assigned)
+ * @param parent the parent community of the nodelist of communities to create
+ * @return an element array containing additional information regarding the
+ * created communities (e.g. the handles they have been assigned)
*/
private static Element[] handleCommunities(Context context, NodeList communities, Community parent)
- throws TransformerException, SQLException, Exception
- {
+ throws TransformerException, SQLException, Exception {
Element[] elements = new Element[communities.getLength()];
-
- for (int i = 0; i < communities.getLength(); i++)
- {
+
+ for (int i = 0; i < communities.getLength(); i++) {
Community community;
Element element = new Element("community");
-
+
// create the community or sub community
- if (parent != null)
- {
+ if (parent != null) {
community = communityService.create(parent, context);
- }
- else
- {
+ } else {
community = communityService.create(null, context);
}
-
+
// default the short description to be an empty string
communityService.setMetadata(context, community, "short_description", " ");
-
+
// now update the metadata
Node tn = communities.item(i);
- for (Map.Entry entry : communityMap.entrySet())
- {
+ for (Map.Entry entry : communityMap.entrySet()) {
NodeList nl = XPathAPI.selectNodeList(tn, entry.getKey());
- if (nl.getLength() == 1)
- {
+ if (nl.getLength() == 1) {
communityService.setMetadata(context, community, entry.getValue(), getStringValue(nl.item(0)));
}
}
-
+
// FIXME: at the moment, if the community already exists by name
// then this will throw a PSQLException on a duplicate key
// violation
@@ -432,7 +399,7 @@ public class StructBuilder
// to isolate the community that already exists without hitting
// the database directly.
communityService.update(context, community);
-
+
// build the element with the handle that identifies the new
// community
// along with all the information that we imported here
@@ -442,151 +409,134 @@ public class StructBuilder
// case
// we want to move it or make it switchable later
element.setAttribute("identifier", community.getHandle());
-
+
Element nameElement = new Element("name");
nameElement.setText(communityService.getMetadata(community, "name"));
element.addContent(nameElement);
-
- if (communityService.getMetadata(community, "short_description") != null)
- {
+
+ if (communityService.getMetadata(community, "short_description") != null) {
Element descriptionElement = new Element("description");
descriptionElement.setText(communityService.getMetadata(community, "short_description"));
element.addContent(descriptionElement);
}
-
- if (communityService.getMetadata(community, "introductory_text") != null)
- {
+
+ if (communityService.getMetadata(community, "introductory_text") != null) {
Element introElement = new Element("intro");
introElement.setText(communityService.getMetadata(community, "introductory_text"));
element.addContent(introElement);
}
-
- if (communityService.getMetadata(community, "copyright_text") != null)
- {
+
+ if (communityService.getMetadata(community, "copyright_text") != null) {
Element copyrightElement = new Element("copyright");
copyrightElement.setText(communityService.getMetadata(community, "copyright_text"));
element.addContent(copyrightElement);
}
-
- if (communityService.getMetadata(community, "side_bar_text") != null)
- {
+
+ if (communityService.getMetadata(community, "side_bar_text") != null) {
Element sidebarElement = new Element("sidebar");
sidebarElement.setText(communityService.getMetadata(community, "side_bar_text"));
element.addContent(sidebarElement);
}
-
+
// handle sub communities
NodeList subCommunities = XPathAPI.selectNodeList(tn, "community");
Element[] subCommunityElements = handleCommunities(context, subCommunities, community);
-
+
// handle collections
NodeList collections = XPathAPI.selectNodeList(tn, "collection");
Element[] collectionElements = handleCollections(context, collections, community);
-
+
int j;
- for (j = 0; j < subCommunityElements.length; j++)
- {
+ for (j = 0; j < subCommunityElements.length; j++) {
element.addContent(subCommunityElements[j]);
}
- for (j = 0; j < collectionElements.length; j++)
- {
+ for (j = 0; j < collectionElements.length; j++) {
element.addContent(collectionElements[j]);
}
-
+
elements[i] = element;
}
-
+
return elements;
}
-
+
/**
- * Take a node list of collections and create the structure from them
- *
- * @param context the context of the request
+ * Take a node list of collections and create the structure from them
+ *
+ * @param context the context of the request
* @param collections the node list of collections to be created
- * @param parent the parent community to whom the collections belong
- *
+ * @param parent the parent community to whom the collections belong
* @return an Element array containing additional information about the
- * created collections (e.g. the handle)
+ * created collections (e.g. the handle)
*/
private static Element[] handleCollections(Context context, NodeList collections, Community parent)
- throws TransformerException, SQLException, AuthorizeException, IOException, Exception
- {
+ throws TransformerException, SQLException, AuthorizeException, IOException, Exception {
Element[] elements = new Element[collections.getLength()];
-
- for (int i = 0; i < collections.getLength(); i++)
- {
+
+ for (int i = 0; i < collections.getLength(); i++) {
Element element = new Element("collection");
Collection collection = collectionService.create(context, parent);
-
+
// default the short description to the empty string
collectionService.setMetadata(context, collection, "short_description", " ");
-
+
// import the rest of the metadata
Node tn = collections.item(i);
- for (Map.Entry entry : collectionMap.entrySet())
- {
+ for (Map.Entry entry : collectionMap.entrySet()) {
NodeList nl = XPathAPI.selectNodeList(tn, entry.getKey());
- if (nl.getLength() == 1)
- {
+ if (nl.getLength() == 1) {
collectionService.setMetadata(context, collection, entry.getValue(), getStringValue(nl.item(0)));
}
}
collectionService.update(context, collection);
-
+
element.setAttribute("identifier", collection.getHandle());
-
+
Element nameElement = new Element("name");
nameElement.setText(collectionService.getMetadata(collection, "name"));
element.addContent(nameElement);
-
- if (collectionService.getMetadata(collection, "short_description") != null)
- {
+
+ if (collectionService.getMetadata(collection, "short_description") != null) {
Element descriptionElement = new Element("description");
descriptionElement.setText(collectionService.getMetadata(collection, "short_description"));
element.addContent(descriptionElement);
}
-
- if (collectionService.getMetadata(collection, "introductory_text") != null)
- {
+
+ if (collectionService.getMetadata(collection, "introductory_text") != null) {
Element introElement = new Element("intro");
introElement.setText(collectionService.getMetadata(collection, "introductory_text"));
element.addContent(introElement);
}
-
- if (collectionService.getMetadata(collection, "copyright_text") != null)
- {
+
+ if (collectionService.getMetadata(collection, "copyright_text") != null) {
Element copyrightElement = new Element("copyright");
copyrightElement.setText(collectionService.getMetadata(collection, "copyright_text"));
element.addContent(copyrightElement);
}
-
- if (collectionService.getMetadata(collection, "side_bar_text") != null)
- {
+
+ if (collectionService.getMetadata(collection, "side_bar_text") != null) {
Element sidebarElement = new Element("sidebar");
sidebarElement.setText(collectionService.getMetadata(collection, "side_bar_text"));
element.addContent(sidebarElement);
}
-
- if (collectionService.getMetadata(collection, "license") != null)
- {
+
+ if (collectionService.getMetadata(collection, "license") != null) {
Element sidebarElement = new Element("license");
sidebarElement.setText(collectionService.getMetadata(collection, "license"));
element.addContent(sidebarElement);
}
-
- if (collectionService.getMetadata(collection, "provenance_description") != null)
- {
+
+ if (collectionService.getMetadata(collection, "provenance_description") != null) {
Element sidebarElement = new Element("provenance");
sidebarElement.setText(collectionService.getMetadata(collection, "provenance_description"));
element.addContent(sidebarElement);
}
-
+
elements[i] = element;
}
-
+
return elements;
}
-
+
}
diff --git a/dspace-api/src/main/java/org/dspace/app/bulkedit/BulkEditChange.java b/dspace-api/src/main/java/org/dspace/app/bulkedit/BulkEditChange.java
index 28fb39b0f5..0dbb8bd7bb 100644
--- a/dspace-api/src/main/java/org/dspace/app/bulkedit/BulkEditChange.java
+++ b/dspace-api/src/main/java/org/dspace/app/bulkedit/BulkEditChange.java
@@ -7,67 +7,93 @@
*/
package org.dspace.app.bulkedit;
-import org.dspace.content.Item;
-import org.dspace.content.Collection;
-
import java.util.ArrayList;
import java.util.List;
+import org.dspace.content.Collection;
+import org.dspace.content.Item;
+
/**
* Utility class to store changes to item that may occur during a batch edit.
*
* @author Stuart Lewis
*/
-public class BulkEditChange
-{
- /** The item these changes relate to */
+public class BulkEditChange {
+ /**
+ * The item these changes relate to
+ */
private Item item;
- /** The List of hashtables with the new elements */
+ /**
+ * The List of hashtables with the new elements
+ */
private List adds;
- /** The List of hashtables with the removed elements */
+ /**
+ * The List of hashtables with the removed elements
+ */
private List removes;
- /** The List of hashtables with the unchanged elements */
+ /**
+ * The List of hashtables with the unchanged elements
+ */
private List constant;
- /** The List of the complete set of new values (constant + adds) */
+ /**
+ * The List of the complete set of new values (constant + adds)
+ */
private List complete;
- /** The list of old collections the item used to be mapped to */
+ /**
+ * The list of old collections the item used to be mapped to
+ */
private List oldMappedCollections;
- /** The list of new collections the item has been mapped into */
+ /**
+ * The list of new collections the item has been mapped into
+ */
private List newMappedCollections;
- /** The old owning collection */
+ /**
+ * The old owning collection
+ */
private Collection oldOwningCollection;
- /** The new owning collection */
+ /**
+ * The new owning collection
+ */
private Collection newOwningCollection;
- /** Is this a new item */
+ /**
+ * Is this a new item
+ */
private boolean newItem;
- /** Has this item been deleted? */
+ /**
+ * Has this item been deleted?
+ */
private boolean deleted;
- /** Has this item been withdrawn? */
+ /**
+ * Has this item been withdrawn?
+ */
private boolean withdrawn;
- /** Has this item been reinstated? */
+ /**
+ * Has this item been reinstated?
+ */
private boolean reinstated;
- /** Have any changes actually been made? */
+ /**
+ * Have any changes actually been made?
+ */
private boolean empty;
/**
- * Initialise a change holder for a new item
+ * Initialise a change holder for a new item
*/
- public BulkEditChange()
- {
+ public BulkEditChange() {
// Set the item to be null
item = null;
newItem = true;
@@ -89,8 +115,7 @@ public class BulkEditChange
*
* @param i The Item to store
*/
- public BulkEditChange(Item i)
- {
+ public BulkEditChange(Item i) {
// Store the item
item = i;
newItem = false;
@@ -110,8 +135,7 @@ public class BulkEditChange
*
* @param i The item
*/
- public void setItem(Item i)
- {
+ public void setItem(Item i) {
// Store the item
item = i;
}
@@ -121,8 +145,7 @@ public class BulkEditChange
*
* @param dcv The value to add
*/
- public void registerAdd(BulkEditMetadataValue dcv)
- {
+ public void registerAdd(BulkEditMetadataValue dcv) {
// Add the added value
adds.add(dcv);
complete.add(dcv);
@@ -134,8 +157,7 @@ public class BulkEditChange
*
* @param dcv The value to remove
*/
- public void registerRemove(BulkEditMetadataValue dcv)
- {
+ public void registerRemove(BulkEditMetadataValue dcv) {
// Add the removed value
removes.add(dcv);
empty = false;
@@ -146,8 +168,7 @@ public class BulkEditChange
*
* @param dcv The value to keep unchanged
*/
- public void registerConstant(BulkEditMetadataValue dcv)
- {
+ public void registerConstant(BulkEditMetadataValue dcv) {
// Add the removed value
constant.add(dcv);
complete.add(dcv);
@@ -158,8 +179,7 @@ public class BulkEditChange
*
* @param c The new mapped Collection
*/
- public void registerNewMappedCollection(Collection c)
- {
+ public void registerNewMappedCollection(Collection c) {
// Add the new owning Collection
newMappedCollections.add(c);
empty = false;
@@ -170,27 +190,22 @@ public class BulkEditChange
*
* @param c The old mapped Collection
*/
- public void registerOldMappedCollection(Collection c)
- {
+ public void registerOldMappedCollection(Collection c) {
// Add the old owning Collection (if it isn't there already, or is an old collection)
boolean found = false;
if ((this.getOldOwningCollection() != null) &&
- (this.getOldOwningCollection().getHandle().equals(c.getHandle())))
- {
+ (this.getOldOwningCollection().getHandle().equals(c.getHandle()))) {
found = true;
}
- for (Collection collection : oldMappedCollections)
- {
- if (collection.getHandle().equals(c.getHandle()))
- {
+ for (Collection collection : oldMappedCollections) {
+ if (collection.getHandle().equals(c.getHandle())) {
found = true;
}
}
- if (!found)
- {
+ if (!found) {
oldMappedCollections.add(c);
empty = false;
}
@@ -202,8 +217,7 @@ public class BulkEditChange
* @param oldC The old owning collection
* @param newC The new owning collection
*/
- public void changeOwningCollection(Collection oldC, Collection newC)
- {
+ public void changeOwningCollection(Collection oldC, Collection newC) {
// Store the old owning collection
oldOwningCollection = oldC;
@@ -217,8 +231,7 @@ public class BulkEditChange
*
* @param newC The new owning collection
*/
- public void setOwningCollection(Collection newC)
- {
+ public void setOwningCollection(Collection newC) {
// Store the new owning collection
newOwningCollection = newC;
//empty = false;
@@ -229,8 +242,7 @@ public class BulkEditChange
*
* @return The item
*/
- public Item getItem()
- {
+ public Item getItem() {
// Return the item
return item;
}
@@ -240,8 +252,7 @@ public class BulkEditChange
*
* @return the list of elements and their values that have been added.
*/
- public List getAdds()
- {
+ public List getAdds() {
// Return the array
return adds;
}
@@ -251,8 +262,7 @@ public class BulkEditChange
*
* @return the list of elements and their values that have been removed.
*/
- public List getRemoves()
- {
+ public List getRemoves() {
// Return the array
return removes;
}
@@ -262,8 +272,7 @@ public class BulkEditChange
*
* @return the list of unchanged values
*/
- public List getConstant()
- {
+ public List getConstant() {
// Return the array
return constant;
}
@@ -273,8 +282,7 @@ public class BulkEditChange
*
* @return the list of all values
*/
- public List getComplete()
- {
+ public List getComplete() {
// Return the array
return complete;
}
@@ -284,8 +292,7 @@ public class BulkEditChange
*
* @return the list of new mapped collections
*/
- public List getNewMappedCollections()
- {
+ public List getNewMappedCollections() {
// Return the array
return newMappedCollections;
}
@@ -295,8 +302,7 @@ public class BulkEditChange
*
* @return the list of old mapped collections
*/
- public List getOldMappedCollections()
- {
+ public List getOldMappedCollections() {
// Return the array
return oldMappedCollections;
}
@@ -306,8 +312,7 @@ public class BulkEditChange
*
* @return the old owning collection
*/
- public Collection getOldOwningCollection()
- {
+ public Collection getOldOwningCollection() {
// Return the old owning collection
return oldOwningCollection;
}
@@ -317,8 +322,7 @@ public class BulkEditChange
*
* @return the new owning collection
*/
- public Collection getNewOwningCollection()
- {
+ public Collection getNewOwningCollection() {
// Return the new owning collection
return newOwningCollection;
}
@@ -328,8 +332,7 @@ public class BulkEditChange
*
* @return Whether or not this is for a new item
*/
- public boolean isNewItem()
- {
+ public boolean isNewItem() {
// Return the new item status
return newItem;
}
@@ -339,8 +342,7 @@ public class BulkEditChange
*
* @return Whether or not this is for a deleted item
*/
- public boolean isDeleted()
- {
+ public boolean isDeleted() {
// Return the new item status
return deleted;
}
@@ -359,8 +361,7 @@ public class BulkEditChange
*
* @return Whether or not this is for a withdrawn item
*/
- public boolean isWithdrawn()
- {
+ public boolean isWithdrawn() {
// Return the new item status
return withdrawn;
}
@@ -379,8 +380,7 @@ public class BulkEditChange
*
* @return Whether or not this is for a reinstated item
*/
- public boolean isReinstated()
- {
+ public boolean isReinstated() {
// Return the new item status
return reinstated;
}
@@ -399,8 +399,7 @@ public class BulkEditChange
*
* @return Whether or not changes have been made
*/
- public boolean hasChanges()
- {
+ public boolean hasChanges() {
return !empty;
}
}
diff --git a/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSV.java b/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSV.java
index 51dbe5b402..e66c96782b 100644
--- a/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSV.java
+++ b/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSV.java
@@ -7,7 +7,26 @@
*/
package org.dspace.app.bulkedit;
-import org.apache.commons.lang3.StringUtils;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import org.dspace.authority.AuthorityValue;
import org.dspace.authority.factory.AuthorityServiceFactory;
import org.dspace.authority.service.AuthorityValueService;
@@ -16,19 +35,14 @@ import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.content.MetadataSchema;
import org.dspace.content.MetadataValue;
+import org.dspace.content.authority.Choices;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.MetadataFieldService;
import org.dspace.content.service.MetadataSchemaService;
-import org.dspace.content.authority.Choices;
import org.dspace.core.Context;
import org.dspace.services.factory.DSpaceServicesFactory;
-import java.util.*;
-import java.util.regex.Pattern;
-import java.util.regex.Matcher;
-import java.io.*;
-
/**
* Utility class to read and write CSV files
*
@@ -38,50 +52,74 @@ import java.io.*;
*
* This class has been made serializable, as it is stored in a Session.
* Is it wise to:
- * a) be putting this into a user's session?
- * b) holding an entire CSV upload in memory?
+ * a) be putting this into a user's session?
+ * b) holding an entire CSV upload in memory?
*
* @author Stuart Lewis
*/
-public class DSpaceCSV implements Serializable
-{
- /** The headings of the CSV file */
+public class DSpaceCSV implements Serializable {
+ /**
+ * The headings of the CSV file
+ */
protected List headings;
- /** An array list of CSV lines */
+ /**
+ * An array list of CSV lines
+ */
protected List lines;
- /** A counter of how many CSV lines this object holds */
+ /**
+ * A counter of how many CSV lines this object holds
+ */
protected int counter;
- /** The value separator (defaults to double pipe '||') */
+ /**
+ * The value separator (defaults to double pipe '||')
+ */
protected String valueSeparator;
- /** The value separator in an escaped form for using in regexes */
+ /**
+ * The value separator in an escaped form for using in regexes
+ */
protected String escapedValueSeparator;
- /** The field separator (defaults to comma) */
+ /**
+ * The field separator (defaults to comma)
+ */
protected String fieldSeparator;
- /** The field separator in an escaped form for using in regexes */
+ /**
+ * The field separator in an escaped form for using in regexes
+ */
protected String escapedFieldSeparator;
- /** The authority separator (defaults to double colon '::') */
+ /**
+ * The authority separator (defaults to double colon '::')
+ */
protected String authoritySeparator;
- /** The authority separator in an escaped form for using in regexes */
+ /**
+ * The authority separator in an escaped form for using in regexes
+ */
protected String escapedAuthoritySeparator;
protected transient final ItemService itemService = ContentServiceFactory.getInstance().getItemService();
- protected transient final MetadataSchemaService metadataSchemaService = ContentServiceFactory.getInstance().getMetadataSchemaService();
- protected transient final MetadataFieldService metadataFieldService = ContentServiceFactory.getInstance().getMetadataFieldService();
- protected transient final AuthorityValueService authorityValueService = AuthorityServiceFactory.getInstance().getAuthorityValueService();
+ protected transient final MetadataSchemaService metadataSchemaService =
+ ContentServiceFactory.getInstance().getMetadataSchemaService();
+ protected transient final MetadataFieldService metadataFieldService =
+ ContentServiceFactory.getInstance().getMetadataFieldService();
+ protected transient final AuthorityValueService authorityValueService =
+ AuthorityServiceFactory.getInstance().getAuthorityValueService();
- /** Whether to export all metadata such as handles and provenance information */
+ /**
+ * Whether to export all metadata such as handles and provenance information
+ */
protected boolean exportAll;
- /** A list of metadata elements to ignore */
+ /**
+ * A list of metadata elements to ignore
+ */
protected Map ignore;
@@ -90,8 +128,7 @@ public class DSpaceCSV implements Serializable
*
* @param exportAll Whether to export all metadata such as handles and provenance information
*/
- public DSpaceCSV(boolean exportAll)
- {
+ public DSpaceCSV(boolean exportAll) {
// Initialise the class
init();
@@ -104,48 +141,37 @@ public class DSpaceCSV implements Serializable
*
* @param f The file to read from
* @param c The DSpace Context
- *
* @throws Exception thrown if there is an error reading or processing the file
*/
- public DSpaceCSV(File f, Context c) throws Exception
- {
+ public DSpaceCSV(File f, Context c) throws Exception {
// Initialise the class
init();
// Open the CSV file
BufferedReader input = null;
- try
- {
- input = new BufferedReader(new InputStreamReader(new FileInputStream(f),"UTF-8"));
+ try {
+ input = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
// Read the heading line
String head = input.readLine();
String[] headingElements = head.split(escapedFieldSeparator);
int columnCounter = 0;
- for (String element : headingElements)
- {
+ for (String element : headingElements) {
columnCounter++;
// Remove surrounding quotes if there are any
- if ((element.startsWith("\"")) && (element.endsWith("\"")))
- {
+ if ((element.startsWith("\"")) && (element.endsWith("\""))) {
element = element.substring(1, element.length() - 1);
}
// Store the heading
- if ("collection".equals(element))
- {
+ if ("collection".equals(element)) {
// Store the heading
headings.add(element);
- }
- // Store the action
- else if ("action".equals(element))
- {
+ } else if ("action".equals(element)) { // Store the action
// Store the heading
headings.add(element);
- }
- else if (!"id".equals(element))
- {
+ } else if (!"id".equals(element)) {
String authorityPrefix = "";
AuthorityValue authorityValueType = authorityValueService.getAuthorityValueType(element);
if (authorityValueType != null) {
@@ -180,7 +206,8 @@ public class DSpaceCSV implements Serializable
}
// Check that the metadata element exists in the schema
- MetadataField foundField = metadataFieldService.findByElement(c, foundSchema, metadataElement, metadataQualifier);
+ MetadataField foundField = metadataFieldService
+ .findByElement(c, foundSchema, metadataElement, metadataQualifier);
if (foundField == null) {
throw new MetadataImportInvalidHeadingException(clean[0],
MetadataImportInvalidHeadingException.ELEMENT,
@@ -196,8 +223,7 @@ public class DSpaceCSV implements Serializable
StringBuilder lineBuilder = new StringBuilder();
String lineRead;
- while ((lineRead = input.readLine()) != null)
- {
+ while ((lineRead = input.readLine()) != null) {
if (lineBuilder.length() > 0) {
// Already have a previously read value - add this line
lineBuilder.append("\n").append(lineRead);
@@ -236,11 +262,8 @@ public class DSpaceCSV implements Serializable
addItem(lineRead);
}
}
- }
- finally
- {
- if (input != null)
- {
+ } finally {
+ if (input != null) {
input.close();
}
}
@@ -249,8 +272,7 @@ public class DSpaceCSV implements Serializable
/**
* Initialise this class with values from dspace.cfg
*/
- protected void init()
- {
+ protected void init() {
// Set the value separator
setValueSeparator();
@@ -273,13 +295,16 @@ public class DSpaceCSV implements Serializable
ignore = new HashMap<>();
// Specify default values
- String[] defaultValues = new String[]{"dc.date.accessioned, dc.date.available, " +
- "dc.date.updated, dc.description.provenance"};
- String[] toIgnoreArray = DSpaceServicesFactory.getInstance().getConfigurationService().getArrayProperty("bulkedit.ignore-on-export", defaultValues);
- for (String toIgnoreString : toIgnoreArray)
- {
- if (!"".equals(toIgnoreString.trim()))
- {
+ String[] defaultValues =
+ new String[] {
+ "dc.date.accessioned, dc.date.available, dc.date.updated, dc.description.provenance"
+ };
+ String[] toIgnoreArray =
+ DSpaceServicesFactory.getInstance()
+ .getConfigurationService()
+ .getArrayProperty("bulkedit.ignore-on-export", defaultValues);
+ for (String toIgnoreString : toIgnoreArray) {
+ if (!"".equals(toIgnoreString.trim())) {
ignore.put(toIgnoreString.trim(), toIgnoreString.trim());
}
}
@@ -307,16 +332,13 @@ public class DSpaceCSV implements Serializable
*
* If not set, defaults to double pipe '||'
*/
- private void setValueSeparator()
- {
+ private void setValueSeparator() {
// Get the value separator
- valueSeparator = DSpaceServicesFactory.getInstance().getConfigurationService().getProperty("bulkedit.valueseparator");
- if ((valueSeparator != null) && (!"".equals(valueSeparator.trim())))
- {
+ valueSeparator = DSpaceServicesFactory.getInstance().getConfigurationService()
+ .getProperty("bulkedit.valueseparator");
+ if ((valueSeparator != null) && (!"".equals(valueSeparator.trim()))) {
valueSeparator = valueSeparator.trim();
- }
- else
- {
+ } else {
valueSeparator = "||";
}
@@ -336,32 +358,22 @@ public class DSpaceCSV implements Serializable
* Special values are 'tab', 'hash' and 'semicolon' which will
* get substituted from the text to the value.
*/
- private void setFieldSeparator()
- {
+ private void setFieldSeparator() {
// Get the value separator
- fieldSeparator =DSpaceServicesFactory.getInstance().getConfigurationService().getProperty("bulkedit.fieldseparator");
- if ((fieldSeparator != null) && (!"".equals(fieldSeparator.trim())))
- {
+ fieldSeparator = DSpaceServicesFactory.getInstance().getConfigurationService()
+ .getProperty("bulkedit.fieldseparator");
+ if ((fieldSeparator != null) && (!"".equals(fieldSeparator.trim()))) {
fieldSeparator = fieldSeparator.trim();
- if ("tab".equals(fieldSeparator))
- {
+ if ("tab".equals(fieldSeparator)) {
fieldSeparator = "\t";
- }
- else if ("semicolon".equals(fieldSeparator))
- {
+ } else if ("semicolon".equals(fieldSeparator)) {
fieldSeparator = ";";
- }
- else if ("hash".equals(fieldSeparator))
- {
+ } else if ("hash".equals(fieldSeparator)) {
fieldSeparator = "#";
- }
- else
- {
+ } else {
fieldSeparator = fieldSeparator.trim();
}
- }
- else
- {
+ } else {
fieldSeparator = ",";
}
@@ -371,23 +383,20 @@ public class DSpaceCSV implements Serializable
escapedFieldSeparator = match.replaceAll("\\\\$1");
}
- /**
+ /**
* Set the authority separator for value with authority data.
*
* Is set in dspace.cfg as bulkedit.authorityseparator
*
* If not set, defaults to double colon '::'
*/
- private void setAuthoritySeparator()
- {
+ private void setAuthoritySeparator() {
// Get the value separator
- authoritySeparator = DSpaceServicesFactory.getInstance().getConfigurationService().getProperty("bulkedit.authorityseparator");
- if ((authoritySeparator != null) && (!"".equals(authoritySeparator.trim())))
- {
+ authoritySeparator = DSpaceServicesFactory.getInstance().getConfigurationService()
+ .getProperty("bulkedit.authorityseparator");
+ if ((authoritySeparator != null) && (!"".equals(authoritySeparator.trim()))) {
authoritySeparator = authoritySeparator.trim();
- }
- else
- {
+ } else {
authoritySeparator = "::";
}
@@ -401,11 +410,9 @@ public class DSpaceCSV implements Serializable
* Add a DSpace item to the CSV file
*
* @param i The DSpace item
- *
* @throws Exception if something goes wrong with adding the Item
*/
- public final void addItem(Item i) throws Exception
- {
+ public final void addItem(Item i) throws Exception {
// If the item does not have an "owningCollection" the the below "getHandle()" call will fail
// This should not happen but is here for safety.
if (i.getOwningCollection() == null) {
@@ -421,49 +428,42 @@ public class DSpaceCSV implements Serializable
// Add in any mapped collections
List collections = i.getCollections();
- for (Collection c : collections)
- {
+ for (Collection c : collections) {
// Only add if it is not the owning collection
- if (!c.getHandle().equals(owningCollectionHandle))
- {
+ if (!c.getHandle().equals(owningCollectionHandle)) {
line.add("collection", c.getHandle());
}
}
// Populate it
List md = itemService.getMetadata(i, Item.ANY, Item.ANY, Item.ANY, Item.ANY);
- for (MetadataValue value : md)
- {
+ for (MetadataValue value : md) {
MetadataField metadataField = value.getMetadataField();
MetadataSchema metadataSchema = metadataField.getMetadataSchema();
// Get the key (schema.element)
String key = metadataSchema.getName() + "." + metadataField.getElement();
// Add the qualifier if there is one (schema.element.qualifier)
- if (metadataField.getQualifier() != null)
- {
+ if (metadataField.getQualifier() != null) {
key = key + "." + metadataField.getQualifier();
}
// Add the language if there is one (schema.element.qualifier[langauge])
//if ((value.language != null) && (!"".equals(value.language)))
- if (value.getLanguage() != null)
- {
+ if (value.getLanguage() != null) {
key = key + "[" + value.getLanguage() + "]";
}
// Store the item
- if (exportAll || okToExport(metadataField))
- {
+ if (exportAll || okToExport(metadataField)) {
// Add authority and confidence if authority is not null
String mdValue = value.getValue();
- if (value.getAuthority() != null && !"".equals(value.getAuthority()))
- {
- mdValue += authoritySeparator + value.getAuthority() + authoritySeparator + (value.getConfidence() != -1 ? value.getConfidence() : Choices.CF_ACCEPTED);
+ if (value.getAuthority() != null && !"".equals(value.getAuthority())) {
+ mdValue += authoritySeparator + value.getAuthority() + authoritySeparator + (value
+ .getConfidence() != -1 ? value.getConfidence() : Choices.CF_ACCEPTED);
}
line.add(key, mdValue);
- if (!headings.contains(key))
- {
+ if (!headings.contains(key)) {
headings.add(key);
}
}
@@ -478,12 +478,10 @@ public class DSpaceCSV implements Serializable
* @param line The line of elements
* @throws Exception Thrown if an error occurs when adding the item
*/
- public final void addItem(String line) throws Exception
- {
+ public final void addItem(String line) throws Exception {
// Check to see if the last character is a field separator, which hides the last empty column
boolean last = false;
- if (line.endsWith(fieldSeparator))
- {
+ if (line.endsWith(fieldSeparator)) {
// Add a space to the end, then remove it later
last = true;
line += " ";
@@ -496,15 +494,12 @@ public class DSpaceCSV implements Serializable
// Merge parts with embedded separators
boolean alldone = false;
- while (!alldone)
- {
+ while (!alldone) {
boolean found = false;
int i = 0;
- for (String part : bits)
- {
+ for (String part : bits) {
int bitcounter = part.length() - part.replaceAll("\"", "").length();
- if ((part.startsWith("\"")) && ((!part.endsWith("\"")) || ((bitcounter & 1) == 1)))
- {
+ if ((part.startsWith("\"")) && ((!part.endsWith("\"")) || ((bitcounter & 1) == 1))) {
found = true;
String add = bits.get(i) + fieldSeparator + bits.get(i + 1);
bits.remove(i);
@@ -519,10 +514,8 @@ public class DSpaceCSV implements Serializable
// Deal with quotes around the elements
int i = 0;
- for (String part : bits)
- {
- if ((part.startsWith("\"")) && (part.endsWith("\"")))
- {
+ for (String part : bits) {
+ if ((part.startsWith("\"")) && (part.endsWith("\""))) {
part = part.substring(1, part.length() - 1);
bits.set(i, part);
}
@@ -531,10 +524,8 @@ public class DSpaceCSV implements Serializable
// Remove embedded quotes
i = 0;
- for (String part : bits)
- {
- if (part.contains("\"\""))
- {
+ for (String part : bits) {
+ if (part.contains("\"\"")) {
part = part.replaceAll("\"\"", "\"");
bits.set(i, part);
}
@@ -546,34 +537,25 @@ public class DSpaceCSV implements Serializable
DSpaceCSVLine csvLine;
// Is this an existing item, or a new item (where id = '+')
- if ("+".equals(id))
- {
+ if ("+".equals(id)) {
csvLine = new DSpaceCSVLine();
- }
- else
- {
- try
- {
+ } else {
+ try {
csvLine = new DSpaceCSVLine(UUID.fromString(id));
- }
- catch (NumberFormatException nfe)
- {
+ } catch (NumberFormatException nfe) {
System.err.println("Invalid item identifier: " + id);
System.err.println("Please check your CSV file for information. " +
- "Item id must be numeric, or a '+' to add a new item");
- throw(nfe);
+ "Item id must be numeric, or a '+' to add a new item");
+ throw (nfe);
}
}
// Add the rest of the parts
i = 0;
- for (String part : bits)
- {
- if (i > 0)
- {
+ for (String part : bits) {
+ if (i > 0) {
// Is this a last empty item?
- if ((last) && (i == headings.size()))
- {
+ if ((last) && (i == headings.size())) {
part = "";
}
@@ -585,10 +567,8 @@ public class DSpaceCSV implements Serializable
}
csvLine.add(headings.get(i - 1), null);
String[] elements = part.split(escapedValueSeparator);
- for (String element : elements)
- {
- if ((element != null) && (!"".equals(element)))
- {
+ for (String element : elements) {
+ if ((element != null) && (!"".equals(element))) {
csvLine.add(headings.get(i - 1), element);
}
}
@@ -604,8 +584,7 @@ public class DSpaceCSV implements Serializable
*
* @return The lines
*/
- public final List getCSVLines()
- {
+ public final List getCSVLines() {
// Return the lines
return lines;
}
@@ -615,22 +594,19 @@ public class DSpaceCSV implements Serializable
*
* @return the array of CSV formatted Strings
*/
- public final String[] getCSVLinesAsStringArray()
- {
+ public final String[] getCSVLinesAsStringArray() {
// Create the headings line
String[] csvLines = new String[counter + 1];
csvLines[0] = "id" + fieldSeparator + "collection";
List headingsCopy = new ArrayList<>(headings);
Collections.sort(headingsCopy);
- for (String value : headingsCopy)
- {
+ for (String value : headingsCopy) {
csvLines[0] = csvLines[0] + fieldSeparator + value;
}
Iterator i = lines.iterator();
int c = 1;
- while (i.hasNext())
- {
+ while (i.hasNext()) {
csvLines[c++] = i.next().toCSV(headingsCopy, fieldSeparator, valueSeparator);
}
@@ -641,15 +617,13 @@ public class DSpaceCSV implements Serializable
* Save the CSV file to the given filename
*
* @param filename The filename to save the CSV file to
- *
* @throws IOException Thrown if an error occurs when writing the file
*/
- public final void save(String filename) throws IOException
- {
+ public final void save(String filename) throws IOException {
// Save the file
BufferedWriter out = new BufferedWriter(
- new OutputStreamWriter(
- new FileOutputStream(filename), "UTF-8"));
+ new OutputStreamWriter(
+ new FileOutputStream(filename), "UTF-8"));
for (String csvLine : getCSVLinesAsStringArray()) {
out.write(csvLine + "\n");
}
@@ -666,12 +640,10 @@ public class DSpaceCSV implements Serializable
* @param md The Metadatum to examine
* @return Whether or not it is OK to export this element
*/
- protected boolean okToExport(MetadataField md)
- {
+ protected boolean okToExport(MetadataField md) {
// Now compare with the list to ignore
String key = md.getMetadataSchema().getName() + "." + md.getElement();
- if (md.getQualifier() != null)
- {
+ if (md.getQualifier() != null) {
key += "." + md.getQualifier();
}
if (ignore.get(key) != null) {
@@ -687,8 +659,7 @@ public class DSpaceCSV implements Serializable
*
* @return The headings
*/
- public List getHeadings()
- {
+ public List getHeadings() {
return headings;
}
@@ -698,13 +669,11 @@ public class DSpaceCSV implements Serializable
* @return The formatted String as a csv
*/
@Override
- public final String toString()
- {
+ public final String toString() {
// Return the csv as one long string
StringBuilder csvLines = new StringBuilder();
String[] lines = this.getCSVLinesAsStringArray();
- for (String line : lines)
- {
+ for (String line : lines) {
csvLines.append(line).append("\n");
}
return csvLines.toString();
diff --git a/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSVLine.java b/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSVLine.java
index bb5fdd4bb3..7fb09cfb4a 100644
--- a/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSVLine.java
+++ b/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSVLine.java
@@ -7,30 +7,41 @@
*/
package org.dspace.app.bulkedit;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.UUID;
+
import org.dspace.authority.AuthorityValue;
import org.dspace.authority.factory.AuthorityServiceFactory;
import org.dspace.authority.service.AuthorityValueService;
-import java.io.Serializable;
-import java.util.*;
-
/**
* Utility class to store a line from a CSV file
*
* @author Stuart Lewis
*/
-public class DSpaceCSVLine implements Serializable
-{
- /** The item id of the item represented by this line. -1 is for a new item */
+public class DSpaceCSVLine implements Serializable {
+ /**
+ * The item id of the item represented by this line. -1 is for a new item
+ */
private final UUID id;
- /** The elements in this line in a hashtable, keyed by the metadata type */
+ /**
+ * The elements in this line in a hashtable, keyed by the metadata type
+ */
private final Map items;
protected transient final AuthorityValueService authorityValueService
- = AuthorityServiceFactory.getInstance().getAuthorityValueService();
+ = AuthorityServiceFactory.getInstance().getAuthorityValueService();
- /** ensuring that the order-sensible columns of the csv are processed in the correct order */
+ /**
+ * ensuring that the order-sensible columns of the csv are processed in the correct order
+ */
private transient final Comparator super String> headerComparator = new Comparator() {
@Override
public int compare(String md1, String md2) {
@@ -41,8 +52,7 @@ public class DSpaceCSVLine implements Serializable
int compare;
if (source1 == null && source2 != null) {
compare = -1;
- }
- else if (source1 != null && source2 == null) {
+ } else if (source1 != null && source2 == null) {
compare = 1;
} else {
// the order of the rest does not matter
@@ -57,8 +67,7 @@ public class DSpaceCSVLine implements Serializable
*
* @param itemId The item ID of the line
*/
- public DSpaceCSVLine(UUID itemId)
- {
+ public DSpaceCSVLine(UUID itemId) {
// Store the ID + separator, and initialise the hashtable
this.id = itemId;
items = new TreeMap<>(headerComparator);
@@ -68,8 +77,7 @@ public class DSpaceCSVLine implements Serializable
/**
* Create a new CSV line for a new item
*/
- public DSpaceCSVLine()
- {
+ public DSpaceCSVLine() {
// Set the ID to be null, and initialise the hashtable
this.id = null;
this.items = new TreeMap<>(headerComparator);
@@ -80,8 +88,7 @@ public class DSpaceCSVLine implements Serializable
*
* @return The item ID
*/
- public UUID getID()
- {
+ public UUID getID() {
// Return the ID
return id;
}
@@ -89,20 +96,17 @@ public class DSpaceCSVLine implements Serializable
/**
* Add a new metadata value to this line
*
- * @param key The metadata key (e.g. dc.contributor.author)
+ * @param key The metadata key (e.g. dc.contributor.author)
* @param value The metadata value
*/
- public void add(String key, String value)
- {
+ public void add(String key, String value) {
// Create the array list if we need to
- if (items.get(key) == null)
- {
+ if (items.get(key) == null) {
items.put(key, new ArrayList());
}
// Store the item if it is not null
- if (value != null)
- {
+ if (value != null) {
items.get(key).add(value);
}
}
@@ -113,8 +117,7 @@ public class DSpaceCSVLine implements Serializable
* @param key The metadata key
* @return All the elements that match
*/
- public List get(String key)
- {
+ public List get(String key) {
// Return any relevant values
return items.get(key);
}
@@ -124,12 +127,11 @@ public class DSpaceCSVLine implements Serializable
*
* @return The action (may be blank, 'withdraw', 'reinstate' or 'delete')
*/
- public String getAction()
- {
+ public String getAction() {
if (items.containsKey("action")) {
ArrayList actions = items.get("action");
if (actions.size() > 0) {
- return ((String)actions.get(0)).trim();
+ return ((String) actions.get(0)).trim();
}
}
return "";
@@ -140,8 +142,7 @@ public class DSpaceCSVLine implements Serializable
*
* @return An enumeration of all the keys
*/
- public Set keys()
- {
+ public Set keys() {
// Return the keys
return items.keySet();
}
@@ -149,26 +150,23 @@ public class DSpaceCSVLine implements Serializable
/**
* Write this line out as a CSV formatted string, in the order given by the headings provided
*
- * @param headings The headings which define the order the elements must be presented in
+ * @param headings The headings which define the order the elements must be presented in
* @param fieldSeparator separator between metadata fields
* @param valueSeparator separator between metadata values (within a field)
* @return The CSV formatted String
*/
- protected String toCSV(List headings, String fieldSeparator, String valueSeparator)
- {
+ protected String toCSV(List headings, String fieldSeparator, String valueSeparator) {
StringBuilder bits = new StringBuilder();
// Add the id
bits.append("\"").append(id).append("\"").append(fieldSeparator);
- bits.append(valueToCSV(items.get("collection"),valueSeparator));
+ bits.append(valueToCSV(items.get("collection"), valueSeparator));
// Add the rest of the elements
- for (String heading : headings)
- {
+ for (String heading : headings) {
bits.append(fieldSeparator);
List values = items.get(heading);
- if (values != null && !"collection".equals(heading))
- {
+ if (values != null && !"collection".equals(heading)) {
bits.append(valueToCSV(values, valueSeparator));
}
}
@@ -179,33 +177,26 @@ public class DSpaceCSVLine implements Serializable
/**
* Internal method to create a CSV formatted String joining a given set of elements
*
- * @param values The values to create the string from
+ * @param values The values to create the string from
* @param valueSeparator value separator
* @return The line as a CSV formatted String
*/
- protected String valueToCSV(List values, String valueSeparator)
- {
+ protected String valueToCSV(List values, String valueSeparator) {
// Check there is some content
- if (values == null)
- {
+ if (values == null) {
return "";
}
// Get on with the work
String s;
- if (values.size() == 1)
- {
+ if (values.size() == 1) {
s = values.get(0);
- }
- else
- {
+ } else {
// Concatenate any fields together
StringBuilder str = new StringBuilder();
- for (String value : values)
- {
- if (str.length() > 0)
- {
+ for (String value : values) {
+ if (str.length() > 0) {
str.append(valueSeparator);
}
diff --git a/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataExport.java b/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataExport.java
index 9765511fb0..bc015ef5e0 100644
--- a/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataExport.java
+++ b/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataExport.java
@@ -7,36 +7,46 @@
*/
package org.dspace.app.bulkedit;
-import com.google.common.collect.Iterators;
-import org.apache.commons.cli.*;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
-import org.dspace.content.*;
+import com.google.common.collect.Iterators;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+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.dspace.content.Collection;
+import org.dspace.content.Community;
+import org.dspace.content.DSpaceObject;
+import org.dspace.content.Item;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.ItemService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.handle.factory.HandleServiceFactory;
-import java.util.ArrayList;
-import java.sql.SQLException;
-import java.util.Iterator;
-import java.util.List;
-
/**
* Metadata exporter to allow the batch export of metadata into a file
*
* @author Stuart Lewis
*/
-public class MetadataExport
-{
- /** The items to export */
+public class MetadataExport {
+ /**
+ * The items to export
+ */
protected Iterator- toExport;
protected ItemService itemService;
protected Context context;
- /** Whether to export all metadata, or just normally edited metadata */
+ /**
+ * Whether to export all metadata, or just normally edited metadata
+ */
protected boolean exportAll;
protected MetadataExport() {
@@ -46,12 +56,11 @@ public class MetadataExport
/**
* Set up a new metadata export
*
- * @param c The Context
- * @param toExport The ItemIterator of items to export
+ * @param c The Context
+ * @param toExport The ItemIterator of items to export
* @param exportAll whether to export all metadata or not (include handle, provenance etc)
*/
- public MetadataExport(Context c, Iterator
- toExport, boolean exportAll)
- {
+ public MetadataExport(Context c, Iterator
- toExport, boolean exportAll) {
itemService = ContentServiceFactory.getInstance().getItemService();
// Store the export settings
@@ -63,23 +72,19 @@ public class MetadataExport
/**
* Method to export a community (and sub-communities and collections)
*
- * @param c The Context
- * @param toExport The Community to export
+ * @param c The Context
+ * @param toExport The Community to export
* @param exportAll whether to export all metadata or not (include handle, provenance etc)
*/
- public MetadataExport(Context c, Community toExport, boolean exportAll)
- {
+ public MetadataExport(Context c, Community toExport, boolean exportAll) {
itemService = ContentServiceFactory.getInstance().getItemService();
- try
- {
+ try {
// Try to export the community
this.toExport = buildFromCommunity(c, toExport, 0);
this.exportAll = exportAll;
this.context = c;
- }
- catch (SQLException sqle)
- {
+ } catch (SQLException sqle) {
// Something went wrong...
System.err.println("Error running exporter:");
sqle.printStackTrace(System.err);
@@ -90,49 +95,43 @@ public class MetadataExport
/**
* Build an array list of item ids that are in a community (include sub-communities and collections)
*
- * @param context DSpace context
+ * @param context DSpace context
* @param community The community to build from
- * @param indent How many spaces to use when writing out the names of items added
+ * @param indent How many spaces to use when writing out the names of items added
* @return The list of item ids
* @throws SQLException if database error
*/
protected Iterator
- buildFromCommunity(Context context, Community community, int indent)
- throws SQLException
- {
+ throws SQLException {
// Add all the collections
List
collections = community.getCollections();
Iterator- result = null;
- for (Collection collection : collections)
- {
- for (int i = 0; i < indent; i++)
- {
+ for (Collection collection : collections) {
+ for (int i = 0; i < indent; i++) {
System.out.print(" ");
}
Iterator
- items = itemService.findByCollection(context, collection);
- result = addItemsToResult(result,items);
+ result = addItemsToResult(result, items);
}
// Add all the sub-communities
List
communities = community.getSubcommunities();
- for (Community subCommunity : communities)
- {
- for (int i = 0; i < indent; i++)
- {
+ for (Community subCommunity : communities) {
+ for (int i = 0; i < indent; i++) {
System.out.print(" ");
}
Iterator- items = buildFromCommunity(context, subCommunity, indent + 1);
- result = addItemsToResult(result,items);
+ result = addItemsToResult(result, items);
}
return result;
}
private Iterator
- addItemsToResult(Iterator
- result, Iterator
- items) {
- if(result == null)
- {
+ if (result == null) {
result = items;
- }else{
+ } else {
result = Iterators.concat(result, items);
}
@@ -144,17 +143,14 @@ public class MetadataExport
*
* @return the exported CSV lines
*/
- public DSpaceCSV export()
- {
- try
- {
+ public DSpaceCSV export() {
+ try {
Context.Mode originalMode = context.getCurrentMode();
context.setMode(Context.Mode.READ_ONLY);
// Process each item
DSpaceCSV csv = new DSpaceCSV(exportAll);
- while (toExport.hasNext())
- {
+ while (toExport.hasNext()) {
Item item = toExport.next();
csv.addItem(item);
context.uncacheEntity(item);
@@ -163,9 +159,7 @@ public class MetadataExport
context.setMode(originalMode);
// Return the results
return csv;
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
// Something went wrong...
System.err.println("Error exporting to CSV:");
e.printStackTrace();
@@ -176,11 +170,10 @@ public class MetadataExport
/**
* Print the help message
*
- * @param options The command line options the user gave
+ * @param options The command line options the user gave
* @param exitCode the system exit code to use
*/
- private static void printHelp(Options options, int exitCode)
- {
+ private static void printHelp(Options options, int exitCode) {
// print the help message
HelpFormatter myhelp = new HelpFormatter();
myhelp.printHelp("MetadataExport\n", options);
@@ -190,13 +183,12 @@ public class MetadataExport
}
/**
- * main method to run the metadata exporter
- *
- * @param argv the command line arguments given
- * @throws Exception if error occurs
- */
- public static void main(String[] argv) throws Exception
- {
+ * main method to run the metadata exporter
+ *
+ * @param argv the command line arguments given
+ * @throws Exception if error occurs
+ */
+ public static void main(String[] argv) throws Exception {
// Create an options object and populate it
CommandLineParser parser = new PosixParser();
@@ -204,30 +196,26 @@ public class MetadataExport
options.addOption("i", "id", true, "ID or handle of thing to export (item, collection, or community)");
options.addOption("f", "file", true, "destination where you want file written");
- options.addOption("a", "all", false, "include all metadata fields that are not normally changed (e.g. provenance)");
+ options.addOption("a", "all", false,
+ "include all metadata fields that are not normally changed (e.g. provenance)");
options.addOption("h", "help", false, "help");
CommandLine line = null;
- try
- {
+ try {
line = parser.parse(options, argv);
- }
- catch (ParseException pe)
- {
+ } catch (ParseException pe) {
System.err.println("Error with commands.");
printHelp(options, 1);
System.exit(0);
}
- if (line.hasOption('h'))
- {
+ if (line.hasOption('h')) {
printHelp(options, 0);
}
// Check a filename is given
- if (!line.hasOption('f'))
- {
+ if (!line.hasOption('f')) {
System.err.println("Required parameter -f missing!");
printHelp(options, 1);
}
@@ -247,42 +235,31 @@ public class MetadataExport
ContentServiceFactory contentServiceFactory = ContentServiceFactory.getInstance();
// Check we have an item OK
ItemService itemService = contentServiceFactory.getItemService();
- if (!line.hasOption('i'))
- {
+ if (!line.hasOption('i')) {
System.out.println("Exporting whole repository WARNING: May take some time!");
exporter = new MetadataExport(c, itemService.findAll(c), exportAll);
- }
- else
- {
+ } else {
String handle = line.getOptionValue('i');
DSpaceObject dso = HandleServiceFactory.getInstance().getHandleService().resolveToObject(c, handle);
- if (dso == null)
- {
+ if (dso == null) {
System.err.println("Item '" + handle + "' does not resolve to an item in your repository!");
printHelp(options, 1);
}
- if (dso.getType() == Constants.ITEM)
- {
+ if (dso.getType() == Constants.ITEM) {
System.out.println("Exporting item '" + dso.getName() + "' (" + handle + ")");
List
- item = new ArrayList<>();
item.add((Item) dso);
exporter = new MetadataExport(c, item.iterator(), exportAll);
- }
- else if (dso.getType() == Constants.COLLECTION)
- {
+ } else if (dso.getType() == Constants.COLLECTION) {
System.out.println("Exporting collection '" + dso.getName() + "' (" + handle + ")");
- Collection collection = (Collection)dso;
+ Collection collection = (Collection) dso;
toExport = itemService.findByCollection(c, collection);
exporter = new MetadataExport(c, toExport, exportAll);
- }
- else if (dso.getType() == Constants.COMMUNITY)
- {
+ } else if (dso.getType() == Constants.COMMUNITY) {
System.out.println("Exporting community '" + dso.getName() + "' (" + handle + ")");
- exporter = new MetadataExport(c, (Community)dso, exportAll);
- }
- else
- {
+ exporter = new MetadataExport(c, (Community) dso, exportAll);
+ } else {
System.err.println("Error identifying '" + handle + "'");
System.exit(1);
}
@@ -292,7 +269,7 @@ public class MetadataExport
DSpaceCSV csv = exporter.export();
// Save the files to the file
- csv.save(filename);
+ csv.save(filename);
// Finish off and tidy up
c.restoreAuthSystemState();
diff --git a/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImport.java b/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImport.java
index 997b1d1f9e..9f94506980 100644
--- a/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImport.java
+++ b/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImport.java
@@ -7,15 +7,35 @@
*/
package org.dspace.app.bulkedit;
-import org.dspace.authority.AuthorityValue;
-import org.apache.commons.cli.*;
-import org.apache.commons.lang.StringUtils;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+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.commons.lang.StringUtils;
import org.apache.log4j.Logger;
+import org.dspace.authority.AuthorityValue;
import org.dspace.authority.factory.AuthorityServiceFactory;
import org.dspace.authority.service.AuthorityValueService;
-import org.dspace.content.*;
+import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
+import org.dspace.content.DSpaceObject;
+import org.dspace.content.Item;
+import org.dspace.content.MetadataValue;
+import org.dspace.content.WorkspaceItem;
import org.dspace.content.authority.Choices;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CollectionService;
@@ -23,9 +43,8 @@ import org.dspace.content.service.InstallItemService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.WorkspaceItemService;
import org.dspace.core.ConfigurationManager;
-import org.dspace.core.Context;
import org.dspace.core.Constants;
-import org.dspace.authorize.AuthorizeException;
+import org.dspace.core.Context;
import org.dspace.core.LogManager;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.factory.EPersonServiceFactory;
@@ -35,40 +54,44 @@ import org.dspace.workflow.WorkflowItem;
import org.dspace.workflow.WorkflowService;
import org.dspace.workflow.factory.WorkflowServiceFactory;
-import java.util.*;
-import java.io.File;
-import java.io.InputStreamReader;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.sql.SQLException;
-
/**
* Metadata importer to allow the batch import of metadata from a file
*
* @author Stuart Lewis
*/
-public class MetadataImport
-{
- /** The Context */
+public class MetadataImport {
+ /**
+ * The Context
+ */
Context c;
- /** The DSpaceCSV object we're processing */
+ /**
+ * The DSpaceCSV object we're processing
+ */
DSpaceCSV csv;
- /** The lines to import */
+ /**
+ * The lines to import
+ */
List
toImport;
- /** The authority controlled fields */
+ /**
+ * The authority controlled fields
+ */
protected static Set authorityControlled;
- static
- {
+
+ static {
setAuthorizedMetadataFields();
}
- /** The prefix of the authority controlled field */
+ /**
+ * The prefix of the authority controlled field
+ */
protected static final String AC_PREFIX = "authority.controlled.";
- /** Logger */
+ /**
+ * Logger
+ */
protected static final Logger log = Logger.getLogger(MetadataImport.class);
protected final AuthorityValueService authorityValueService;
@@ -83,11 +106,10 @@ public class MetadataImport
* Create an instance of the metadata importer. Requires a context and an array of CSV lines
* to examine.
*
- * @param c The context
+ * @param c The context
* @param toImport An array of CSV lines to examine
*/
- public MetadataImport(Context c, DSpaceCSV toImport)
- {
+ public MetadataImport(Context c, DSpaceCSV toImport) {
// Store the import settings
this.c = c;
csv = toImport;
@@ -104,37 +126,32 @@ public class MetadataImport
* Run an import. The import can either be read-only to detect changes, or
* can write changes as it goes.
*
- * @param change Whether or not to write the changes to the database
- * @param useWorkflow Whether the workflows should be used when creating new items
+ * @param change Whether or not to write the changes to the database
+ * @param useWorkflow Whether the workflows should be used when creating new items
* @param workflowNotify If the workflows should be used, whether to send notifications or not
- * @param useTemplate Use collection template if create new item
+ * @param useTemplate Use collection template if create new item
* @return An array of BulkEditChange elements representing the items that have changed
- *
* @throws MetadataImportException if something goes wrong
*/
public List runImport(boolean change,
boolean useWorkflow,
boolean workflowNotify,
- boolean useTemplate) throws MetadataImportException
- {
+ boolean useTemplate) throws MetadataImportException {
// Store the changes
ArrayList changes = new ArrayList();
// Make the changes
- try
- {
+ try {
Context.Mode originalMode = c.getCurrentMode();
c.setMode(Context.Mode.BATCH_EDIT);
// Process each change
- for (DSpaceCSVLine line : toImport)
- {
+ for (DSpaceCSVLine line : toImport) {
// Get the DSpace item to compare with
UUID id = line.getID();
// Is there an action column?
- if (csv.hasActions() && (!"".equals(line.getAction())) && (id == null))
- {
+ if (csv.hasActions() && (!"".equals(line.getAction())) && (id == null)) {
throw new MetadataImportException("'action' not allowed for new items!");
}
@@ -143,12 +160,10 @@ public class MetadataImport
Item item = null;
// Is this a new item?
- if (id != null)
- {
+ if (id != null) {
// Get the item
item = itemService.find(c, id);
- if (item == null)
- {
+ if (item == null) {
throw new MetadataImportException("Unknown item ID " + id);
}
@@ -157,11 +172,9 @@ public class MetadataImport
// Has it moved collection?
List collections = line.get("collection");
- if (collections != null)
- {
+ if (collections != null) {
// Sanity check we're not orphaning it
- if (collections.size() == 0)
- {
+ if (collections.size() == 0) {
throw new MetadataImportException("Missing collection from item " + item.getHandle());
}
List actualCollections = item.getCollections();
@@ -169,21 +182,16 @@ public class MetadataImport
}
// Iterate through each metadata element in the csv line
- for (String md : line.keys())
- {
+ for (String md : line.keys()) {
// Get the values we already have
- if (!"id".equals(md))
- {
+ if (!"id".equals(md)) {
// Get the values from the CSV
String[] fromCSV = line.get(md).toArray(new String[line.get(md).size()]);
// Remove authority unless the md is not authority controlled
- if (!isAuthorityControlledField(md))
- {
- for (int i=0; i -1)
- {
+ if (pos > -1) {
fromCSV[i] = fromCSV[i].substring(0, pos);
}
}
@@ -194,94 +202,72 @@ public class MetadataImport
}
}
- if (csv.hasActions())
- {
+ if (csv.hasActions()) {
// Perform the action
String action = line.getAction();
- if ("".equals(action))
- {
+ if ("".equals(action)) {
// Do nothing
- }
- else if ("expunge".equals(action))
- {
+ } else if ("expunge".equals(action)) {
// Does the configuration allow deletes?
- if (!ConfigurationManager.getBooleanProperty("bulkedit", "allowexpunge", false))
- {
+ if (!ConfigurationManager.getBooleanProperty("bulkedit", "allowexpunge", false)) {
throw new MetadataImportException("'expunge' action denied by configuration");
}
// Remove the item
- if (change) {
- itemService.delete(c, item);
- }
-
+ if (change) {
+ itemService.delete(c, item);
+ }
+
whatHasChanged.setDeleted();
- }
- else if ("withdraw".equals(action))
- {
+ } else if ("withdraw".equals(action)) {
// Withdraw the item
- if (!item.isWithdrawn())
- {
- if (change)
- {
+ if (!item.isWithdrawn()) {
+ if (change) {
itemService.withdraw(c, item);
}
whatHasChanged.setWithdrawn();
}
- }
- else if ("reinstate".equals(action))
- {
+ } else if ("reinstate".equals(action)) {
// Reinstate the item
- if (item.isWithdrawn())
- {
- if (change)
- {
+ if (item.isWithdrawn()) {
+ if (change) {
itemService.reinstate(c, item);
}
whatHasChanged.setReinstated();
}
- }
- else {
+ } else {
// Unknown action!
throw new MetadataImportException("Unknown action: " + action);
}
}
// Only record if changes have been made
- if (whatHasChanged.hasChanges())
- {
+ if (whatHasChanged.hasChanges()) {
changes.add(whatHasChanged);
}
- }
- else
- {
+ } else {
// This is marked as a new item, so no need to compare
// First check a user is set, otherwise this can't happen
- if (c.getCurrentUser() == null)
- {
- throw new MetadataImportException("When adding new items, a user must be specified with the -e option");
+ if (c.getCurrentUser() == null) {
+ throw new MetadataImportException(
+ "When adding new items, a user must be specified with the -e option");
}
// Iterate through each metadata element in the csv line
BulkEditChange whatHasChanged = new BulkEditChange();
- for (String md : line.keys())
- {
+ for (String md : line.keys()) {
// Get the values we already have
- if (!"id".equals(md))
- {
+ if (!"id".equals(md)) {
// Get the values from the CSV
String[] fromCSV = line.get(md).toArray(new String[line.get(md).size()]);
// Remove authority unless the md is not authority controlled
- if (!isAuthorityControlledField(md))
- {
- for (int i=0; i -1)
- {
+ if (pos > -1) {
fromCSV[i] = fromCSV[i].substring(0, pos);
}
}
@@ -294,62 +280,55 @@ public class MetadataImport
// Check it has an owning collection
List collections = line.get("collection");
- if (collections == null)
- {
- throw new MetadataImportException("New items must have a 'collection' assigned in the form of a handle");
+ if (collections == null) {
+ throw new MetadataImportException(
+ "New items must have a 'collection' assigned in the form of a handle");
}
// Check collections are really collections
ArrayList check = new ArrayList();
Collection collection;
- for (String handle : collections)
- {
- try
- {
+ for (String handle : collections) {
+ try {
// Resolve the handle to the collection
collection = (Collection) handleService.resolveToObject(c, handle);
// Check it resolved OK
- if (collection == null)
- {
- throw new MetadataImportException("'" + handle + "' is not a Collection! You must specify a valid collection for new items");
+ if (collection == null) {
+ throw new MetadataImportException(
+ "'" + handle + "' is not a Collection! You must specify a valid collection for " +
+ "new items");
}
// Check for duplicate
- if (check.contains(collection))
- {
- throw new MetadataImportException("Duplicate collection assignment detected in new item! " + handle);
- }
- else
- {
+ if (check.contains(collection)) {
+ throw new MetadataImportException(
+ "Duplicate collection assignment detected in new item! " + handle);
+ } else {
check.add(collection);
}
- }
- catch (Exception ex)
- {
- throw new MetadataImportException("'" + handle + "' is not a Collection! You must specify a valid collection for new items", ex);
+ } catch (Exception ex) {
+ throw new MetadataImportException(
+ "'" + handle + "' is not a Collection! You must specify a valid collection for new " +
+ "items",
+ ex);
}
}
// Record the addition to collections
boolean first = true;
- for (String handle : collections)
- {
+ for (String handle : collections) {
Collection extra = (Collection) handleService.resolveToObject(c, handle);
- if (first)
- {
+ if (first) {
whatHasChanged.setOwningCollection(extra);
- }
- else
- {
+ } else {
whatHasChanged.registerNewMappedCollection(extra);
}
first = false;
}
// Create the new item?
- if (change)
- {
+ if (change) {
// Create the item
String collectionHandle = line.get("collection").get(0);
collection = (Collection) handleService.resolveToObject(c, collectionHandle);
@@ -357,37 +336,32 @@ public class MetadataImport
item = wsItem.getItem();
// Add the metadata to the item
- for (BulkEditMetadataValue dcv : whatHasChanged.getAdds())
- {
+ for (BulkEditMetadataValue dcv : whatHasChanged.getAdds()) {
itemService.addMetadata(c, item, dcv.getSchema(),
- dcv.getElement(),
- dcv.getQualifier(),
- dcv.getLanguage(),
- dcv.getValue(),
- dcv.getAuthority(),
- dcv.getConfidence());
+ dcv.getElement(),
+ dcv.getQualifier(),
+ dcv.getLanguage(),
+ dcv.getValue(),
+ dcv.getAuthority(),
+ dcv.getConfidence());
}
// Should the workflow be used?
- if(useWorkflow){
+ if (useWorkflow) {
WorkflowService workflowService = WorkflowServiceFactory.getInstance().getWorkflowService();
if (workflowNotify) {
wfItem = workflowService.start(c, wsItem);
} else {
wfItem = workflowService.startWithoutNotify(c, wsItem);
}
- }
- else
- {
+ } else {
// Install the item
installItemService.installItem(c, wsItem);
}
// Add to extra collections
- if (line.get("collection").size() > 0)
- {
- for (int i = 1; i < collections.size(); i++)
- {
+ if (line.get("collection").size() > 0) {
+ for (int i = 1; i < collections.size(); i++) {
String handle = collections.get(i);
Collection extra = (Collection) handleService.resolveToObject(c, handle);
collectionService.addItem(c, extra, item);
@@ -412,13 +386,9 @@ public class MetadataImport
}
c.setMode(originalMode);
- }
- catch (MetadataImportException mie)
- {
+ } catch (MetadataImportException mie) {
throw mie;
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
e.printStackTrace();
}
@@ -429,22 +399,21 @@ public class MetadataImport
/**
* Compare an item metadata with a line from CSV, and optionally update the item
*
- * @param item The current item metadata
+ * @param item The current item metadata
* @param fromCSV The metadata from the CSV file
- * @param change Whether or not to make the update
- * @param md The element to compare
+ * @param change Whether or not to make the update
+ * @param md The element to compare
* @param changes The changes object to populate
- * @param line line in CSV file
- * @throws SQLException if there is a problem accessing a Collection from the database, from its handle
+ * @param line line in CSV file
+ * @throws SQLException if there is a problem accessing a Collection from the database, from its handle
* @throws AuthorizeException if there is an authorization problem with permissions
*/
protected void compare(Item item, String[] fromCSV, boolean change,
- String md, BulkEditChange changes, DSpaceCSVLine line) throws SQLException, AuthorizeException
- {
+ String md, BulkEditChange changes, DSpaceCSVLine line)
+ throws SQLException, AuthorizeException {
// Log what metadata element we're looking at
String all = "";
- for (String part : fromCSV)
- {
+ for (String part : fromCSV) {
all += part + ",";
}
all = all.substring(0, all.length());
@@ -452,16 +421,14 @@ public class MetadataImport
"item_id=" + item.getID() + ",fromCSV=" + all));
// Don't compare collections or actions
- if (("collection".equals(md)) || ("action".equals(md)))
- {
+ if (("collection".equals(md)) || ("action".equals(md))) {
return;
}
// Make a String array of the current values stored in this element
// First, strip of language if it is there
String language = null;
- if (md.contains("["))
- {
+ if (md.contains("[")) {
String[] bits = md.split("\\[");
language = bits[1].substring(0, bits[1].length() - 1);
}
@@ -475,29 +442,26 @@ public class MetadataImport
String schema = bits[0];
String element = bits[1];
// If there is a language on the element, strip if off
- if (element.contains("["))
- {
+ if (element.contains("[")) {
element = element.substring(0, element.indexOf('['));
}
String qualifier = null;
- if (bits.length > 2)
- {
+ if (bits.length > 2) {
qualifier = bits[2];
// If there is a language, strip if off
- if (qualifier.contains("["))
- {
+ if (qualifier.contains("[")) {
qualifier = qualifier.substring(0, qualifier.indexOf('['));
}
}
log.debug(LogManager.getHeader(c, "metadata_import",
"item_id=" + item.getID() + ",fromCSV=" + all +
- ",looking_for_schema=" + schema +
- ",looking_for_element=" + element +
- ",looking_for_qualifier=" + qualifier +
- ",looking_for_language=" + language));
+ ",looking_for_schema=" + schema +
+ ",looking_for_element=" + element +
+ ",looking_for_qualifier=" + qualifier +
+ ",looking_for_language=" + language));
String[] dcvalues;
- if(fromAuthority==null) {
+ if (fromAuthority == null) {
List current = itemService.getMetadata(item, schema, element, qualifier, language);
dcvalues = new String[current.size()];
int i = 0;
@@ -506,14 +470,15 @@ public class MetadataImport
dcvalues[i] = dcv.getValue();
} else {
dcvalues[i] = dcv.getValue() + csv.getAuthoritySeparator() + dcv.getAuthority();
- dcvalues[i] += csv.getAuthoritySeparator() + (dcv.getConfidence() != -1 ? dcv.getConfidence() : Choices.CF_ACCEPTED);
+ dcvalues[i] += csv.getAuthoritySeparator() + (dcv.getConfidence() != -1 ? dcv
+ .getConfidence() : Choices.CF_ACCEPTED);
}
i++;
log.debug(LogManager.getHeader(c, "metadata_import",
- "item_id=" + item.getID() + ",fromCSV=" + all +
- ",found=" + dcv.getValue()));
+ "item_id=" + item.getID() + ",fromCSV=" + all +
+ ",found=" + dcv.getValue()));
}
- }else{
+ } else {
dcvalues = line.get(md).toArray(new String[line.get(md).size()]);
}
@@ -521,9 +486,11 @@ public class MetadataImport
// Compare from current->csv
for (int v = 0; v < fromCSV.length; v++) {
String value = fromCSV[v];
- BulkEditMetadataValue dcv = getBulkEditValueFromCSV(language, schema, element, qualifier, value, fromAuthority);
- if (fromAuthority!=null) {
- value = dcv.getValue() + csv.getAuthoritySeparator() + dcv.getAuthority() + csv.getAuthoritySeparator() + dcv.getConfidence();
+ BulkEditMetadataValue dcv = getBulkEditValueFromCSV(language, schema, element, qualifier, value,
+ fromAuthority);
+ if (fromAuthority != null) {
+ value = dcv.getValue() + csv.getAuthoritySeparator() + dcv.getAuthority() + csv
+ .getAuthoritySeparator() + dcv.getConfidence();
fromCSV[v] = value;
}
@@ -536,93 +503,79 @@ public class MetadataImport
}
// Compare from csv->current
- for (String value : dcvalues)
- {
+ for (String value : dcvalues) {
// Look to see if it should be removed
BulkEditMetadataValue dcv = new BulkEditMetadataValue();
dcv.setSchema(schema);
dcv.setElement(element);
dcv.setQualifier(qualifier);
dcv.setLanguage(language);
- if (value == null || !value.contains(csv.getAuthoritySeparator()))
+ if (value == null || !value.contains(csv.getAuthoritySeparator())) {
simplyCopyValue(value, dcv);
- else
- {
+ } else {
String[] parts = value.split(csv.getAuthoritySeparator());
dcv.setValue(parts[0]);
dcv.setAuthority(parts[1]);
dcv.setConfidence((parts.length > 2 ? Integer.valueOf(parts[2]) : Choices.CF_ACCEPTED));
}
- if ((value != null) && (!"".equals(value)) && (!contains(value, fromCSV)) && fromAuthority==null)
- // fromAuthority==null: with the current implementation metadata values from external authority sources can only be used to add metadata, not to change or remove them
- // because e.g. an author that is not in the column "ORCID:dc.contributor.author" could still be in the column "dc.contributor.author" so don't remove it
- {
+ // fromAuthority==null: with the current implementation metadata values from external authority sources
+ // can only be used to add metadata, not to change or remove them
+ // because e.g. an author that is not in the column "ORCID:dc.contributor.author" could still be in the
+ // column "dc.contributor.author" so don't remove it
+ if ((value != null) && (!"".equals(value)) && (!contains(value, fromCSV)) && fromAuthority == null) {
// Remove it
log.debug(LogManager.getHeader(c, "metadata_import",
"item_id=" + item.getID() + ",fromCSV=" + all +
- ",removing_schema=" + schema +
- ",removing_element=" + element +
- ",removing_qualifier=" + qualifier +
- ",removing_language=" + language));
+ ",removing_schema=" + schema +
+ ",removing_element=" + element +
+ ",removing_qualifier=" + qualifier +
+ ",removing_language=" + language));
changes.registerRemove(dcv);
}
}
// Update the item if it has changed
if ((change) &&
- ((changes.getAdds().size() > 0) || (changes.getRemoves().size() > 0)))
- {
+ ((changes.getAdds().size() > 0) || (changes.getRemoves().size() > 0))) {
// Get the complete list of what values should now be in that element
List list = changes.getComplete();
List values = new ArrayList();
List authorities = new ArrayList();
List confidences = new ArrayList();
- for (BulkEditMetadataValue value : list)
- {
- if ((qualifier == null) && (language == null))
- {
+ for (BulkEditMetadataValue value : list) {
+ if ((qualifier == null) && (language == null)) {
if ((schema.equals(value.getSchema())) &&
(element.equals(value.getElement())) &&
- (value.getQualifier() == null) &&
- (value.getLanguage() == null))
- {
+ (value.getQualifier() == null) &&
+ (value.getLanguage() == null)) {
values.add(value.getValue());
authorities.add(value.getAuthority());
confidences.add(value.getConfidence());
}
- }
- else if (qualifier == null)
- {
+ } else if (qualifier == null) {
if ((schema.equals(value.getSchema())) &&
(element.equals(value.getElement())) &&
(language.equals(value.getLanguage())) &&
- (value.getQualifier() == null))
- {
+ (value.getQualifier() == null)) {
values.add(value.getValue());
authorities.add(value.getAuthority());
confidences.add(value.getConfidence());
}
- }
- else if (language == null)
- {
+ } else if (language == null) {
if ((schema.equals(value.getSchema())) &&
(element.equals(value.getElement())) &&
(qualifier.equals(value.getQualifier())) &&
- (value.getLanguage() == null))
- {
+ (value.getLanguage() == null)) {
values.add(value.getValue());
authorities.add(value.getAuthority());
confidences.add(value.getConfidence());
}
- }
- else
- {
+ } else {
if ((schema.equals(value.getSchema())) &&
(element.equals(value.getElement())) &&
(qualifier.equals(value.getQualifier())) &&
- (language.equals(value.getLanguage())))
- {
+ (language.equals(value.getLanguage()))) {
values.add(value.getValue());
authorities.add(value.getAuthority());
confidences.add(value.getConfidence());
@@ -641,23 +594,22 @@ public class MetadataImport
* Compare changes between an items owning collection and mapped collections
* and what is in the CSV file
*
- * @param item The item in question
- * @param collections The collection handles from the CSV file
+ * @param item The item in question
+ * @param collections The collection handles from the CSV file
* @param actualCollections The Collections from the actual item
- * @param bechange The bulkedit change object for this item
- * @param change Whether or not to actuate a change
- *
- * @throws SQLException if there is a problem accessing a Collection from the database, from its handle
- * @throws AuthorizeException if there is an authorization problem with permissions
- * @throws IOException Can be thrown when moving items in communities
+ * @param bechange The bulkedit change object for this item
+ * @param change Whether or not to actuate a change
+ * @throws SQLException if there is a problem accessing a Collection from the database, from its handle
+ * @throws AuthorizeException if there is an authorization problem with permissions
+ * @throws IOException Can be thrown when moving items in communities
* @throws MetadataImportException If something goes wrong to be reported back to the user
*/
protected void compare(Item item,
- List collections,
- List actualCollections,
- BulkEditChange bechange,
- boolean change) throws SQLException, AuthorizeException, IOException, MetadataImportException
- {
+ List collections,
+ List actualCollections,
+ BulkEditChange bechange,
+ boolean change)
+ throws SQLException, AuthorizeException, IOException, MetadataImportException {
// First, check the owning collection (as opposed to mapped collections) is the same of changed
String oldOwner = item.getOwningCollection().getHandle();
String newOwner = collections.get(0);
@@ -665,32 +617,28 @@ public class MetadataImport
Collection newCollection = (Collection) handleService.resolveToObject(c, newOwner);
// Check it resolved OK
- if (newCollection == null)
- {
- throw new MetadataImportException("'" + newOwner + "' is not a Collection! You must specify a valid collection ID");
+ if (newCollection == null) {
+ throw new MetadataImportException(
+ "'" + newOwner + "' is not a Collection! You must specify a valid collection ID");
}
- if (!oldOwner.equals(newOwner))
- {
+ if (!oldOwner.equals(newOwner)) {
// Register the old and new owning collections
- bechange.changeOwningCollection(item.getOwningCollection(), (Collection) handleService.resolveToObject(c, newOwner));
+ bechange.changeOwningCollection(item.getOwningCollection(),
+ (Collection) handleService.resolveToObject(c, newOwner));
}
// Second, loop through the strings from the CSV of mapped collections
boolean first = true;
- for (String csvcollection : collections)
- {
+ for (String csvcollection : collections) {
// Ignore the first collection as this is the owning collection
- if (!first)
- {
+ if (!first) {
// Look for it in the actual list of Collections
boolean found = false;
- for (Collection collection : actualCollections)
- {
+ for (Collection collection : actualCollections) {
if (collection.getID() != item.getOwningCollection().getID()) {
// Is it there?
- if (csvcollection.equals(collection.getHandle()))
- {
+ if (csvcollection.equals(collection.getHandle())) {
found = true;
}
}
@@ -698,15 +646,13 @@ public class MetadataImport
// Was it found?
DSpaceObject dso = handleService.resolveToObject(c, csvcollection);
- if ((dso == null) || (dso.getType() != Constants.COLLECTION))
- {
+ if ((dso == null) || (dso.getType() != Constants.COLLECTION)) {
throw new MetadataImportException("Collection defined for item " + item.getID() +
- " (" + item.getHandle() + ") is not a collection");
+ " (" + item.getHandle() + ") is not a collection");
}
- if (!found)
- {
+ if (!found) {
// Register the new mapped collection
- Collection col = (Collection)dso;
+ Collection col = (Collection) dso;
bechange.registerNewMappedCollection(col);
}
}
@@ -714,23 +660,17 @@ public class MetadataImport
}
// Third, loop through the strings from the current item
- for (Collection collection : actualCollections)
- {
+ for (Collection collection : actualCollections) {
// Look for it in the actual list of Collections
boolean found = false;
first = true;
- for (String csvcollection : collections)
- {
+ for (String csvcollection : collections) {
// Don't check the owning collection
- if ((first) && (collection.getID().equals(item.getOwningCollection().getID())))
- {
+ if ((first) && (collection.getID().equals(item.getOwningCollection().getID()))) {
found = true;
- }
- else
- {
+ } else {
// Is it there?
- if (!first && collection.getHandle().equals(csvcollection))
- {
+ if (!first && collection.getHandle().equals(csvcollection)) {
found = true;
}
}
@@ -738,51 +678,42 @@ public class MetadataImport
}
// Was it found?
- if (!found)
- {
+ if (!found) {
// Record that it isn't there any more
bechange.registerOldMappedCollection(collection);
}
}
// Process the changes
- if (change)
- {
+ if (change) {
// Remove old mapped collections
- for (Collection collection : bechange.getOldMappedCollections())
- {
+ for (Collection collection : bechange.getOldMappedCollections()) {
collectionService.removeItem(c, collection, item);
}
// Add to new owned collection
- if (bechange.getNewOwningCollection() != null)
- {
+ if (bechange.getNewOwningCollection() != null) {
collectionService.addItem(c, bechange.getNewOwningCollection(), item);
item.setOwningCollection(bechange.getNewOwningCollection());
itemService.update(c, item);
}
// Remove from old owned collection (if still a member)
- if (bechange.getOldOwningCollection() != null)
- {
+ if (bechange.getOldOwningCollection() != null) {
boolean found = false;
- for (Collection c : item.getCollections())
- {
- if (c.getID().equals(bechange.getOldOwningCollection().getID()))
- {
+ for (Collection c : item.getCollections()) {
+ if (c.getID().equals(bechange.getOldOwningCollection().getID())) {
found = true;
}
}
- if (found)
- {
+ if (found) {
collectionService.removeItem(c, bechange.getOldOwningCollection(), item);
}
}
// Add to new mapped collections
- for (Collection collection : bechange.getNewMappedCollections())
- {
+ for (Collection collection : bechange.getNewMappedCollections()) {
collectionService.addItem(c, collection, item);
}
@@ -793,71 +724,66 @@ public class MetadataImport
* Add an item metadata with a line from CSV, and optionally update the item
*
* @param fromCSV The metadata from the CSV file
- * @param md The element to compare
+ * @param md The element to compare
* @param changes The changes object to populate
- *
- * @throws SQLException when an SQL error has occurred (querying DSpace)
+ * @throws SQLException when an SQL error has occurred (querying DSpace)
* @throws AuthorizeException If the user can't make the changes
*/
protected void add(String[] fromCSV, String md, BulkEditChange changes)
- throws SQLException, AuthorizeException
- {
+ throws SQLException, AuthorizeException {
// Don't add owning collection or action
- if (("collection".equals(md)) || ("action".equals(md)))
- {
+ if (("collection".equals(md)) || ("action".equals(md))) {
return;
}
// Make a String array of the values
// First, strip of language if it is there
String language = null;
- if (md.contains("["))
- {
+ if (md.contains("[")) {
String[] bits = md.split("\\[");
language = bits[1].substring(0, bits[1].length() - 1);
}
AuthorityValue fromAuthority = authorityValueService.getAuthorityValueType(md);
if (md.indexOf(':') > 0) {
- md = md.substring(md.indexOf(':')+1);
+ md = md.substring(md.indexOf(':') + 1);
}
String[] bits = md.split("\\.");
String schema = bits[0];
String element = bits[1];
// If there is a language on the element, strip if off
- if (element.contains("["))
- {
+ if (element.contains("[")) {
element = element.substring(0, element.indexOf('['));
}
String qualifier = null;
- if (bits.length > 2)
- {
+ if (bits.length > 2) {
qualifier = bits[2];
// If there is a language, strip if off
- if (qualifier.contains("["))
- {
+ if (qualifier.contains("[")) {
qualifier = qualifier.substring(0, qualifier.indexOf('['));
}
}
// Add all the values
- for (String value : fromCSV)
- {
- BulkEditMetadataValue dcv = getBulkEditValueFromCSV(language, schema, element, qualifier, value, fromAuthority);
- if(fromAuthority!=null){
- value = dcv.getValue() + csv.getAuthoritySeparator() + dcv.getAuthority() + csv.getAuthoritySeparator() + dcv.getConfidence();
+ for (String value : fromCSV) {
+ BulkEditMetadataValue dcv = getBulkEditValueFromCSV(language, schema, element, qualifier, value,
+ fromAuthority);
+ if (fromAuthority != null) {
+ value = dcv.getValue() + csv.getAuthoritySeparator() + dcv.getAuthority() + csv
+ .getAuthoritySeparator() + dcv.getConfidence();
}
// Add it
- if ((value != null) && (!"".equals(value)))
- {
+ if ((value != null) && (!"".equals(value))) {
changes.registerAdd(dcv);
}
}
}
- protected BulkEditMetadataValue getBulkEditValueFromCSV(String language, String schema, String element, String qualifier, String value, AuthorityValue fromAuthority) {
+ protected BulkEditMetadataValue getBulkEditValueFromCSV(String language, String schema, String element,
+ String qualifier, String value,
+ AuthorityValue fromAuthority) {
// Look to see if it should be removed
BulkEditMetadataValue dcv = new BulkEditMetadataValue();
dcv.setSchema(schema);
@@ -904,17 +830,14 @@ public class MetadataImport
/**
* Method to find if a String occurs in an array of Strings
*
- * @param needle The String to look for
+ * @param needle The String to look for
* @param haystack The array of Strings to search through
* @return Whether or not it is contained
*/
- protected boolean contains(String needle, String[] haystack)
- {
+ protected boolean contains(String needle, String[] haystack) {
// Look for the needle in the haystack
- for (String examine : haystack)
- {
- if (clean(examine).equals(clean(needle)))
- {
+ for (String examine : haystack) {
+ if (clean(examine).equals(clean(needle))) {
return true;
}
}
@@ -927,14 +850,12 @@ public class MetadataImport
* @param in The element to clean
* @return The cleaned up element
*/
- protected String clean(String in)
- {
+ protected String clean(String in) {
// Check for nulls
- if (in == null)
- {
+ if (in == null) {
return null;
}
-
+
// Remove newlines as different operating systems sometimes use different formats
return in.replaceAll("\r\n", "").replaceAll("\n", "").trim();
}
@@ -942,11 +863,10 @@ public class MetadataImport
/**
* Print the help message
*
- * @param options The command line options the user gave
+ * @param options The command line options the user gave
* @param exitCode the system exit code to use
*/
- private static void printHelp(Options options, int exitCode)
- {
+ private static void printHelp(Options options, int exitCode) {
// print the help message
HelpFormatter myhelp = new HelpFormatter();
myhelp.printHelp("MetatadataImport\n", options);
@@ -961,12 +881,10 @@ public class MetadataImport
* @param changed Whether or not the changes have been made
* @return The number of items that have changed
*/
- private static int displayChanges(List changes, boolean changed)
- {
+ private static int displayChanges(List changes, boolean changed) {
// Display the changes
int changeCounter = 0;
- for (BulkEditChange change : changes)
- {
+ for (BulkEditChange change : changes) {
// Get the changes
List adds = change.getAdds();
List removes = change.getRemoves();
@@ -975,27 +893,19 @@ public class MetadataImport
if ((adds.size() > 0) || (removes.size() > 0) ||
(newCollections.size() > 0) || (oldCollections.size() > 0) ||
(change.getNewOwningCollection() != null) || (change.getOldOwningCollection() != null) ||
- (change.isDeleted()) || (change.isWithdrawn()) || (change.isReinstated()))
- {
+ (change.isDeleted()) || (change.isWithdrawn()) || (change.isReinstated())) {
// Show the item
Item i = change.getItem();
System.out.println("-----------------------------------------------------------");
- if (!change.isNewItem())
- {
+ if (!change.isNewItem()) {
System.out.println("Changes for item: " + i.getID() + " (" + i.getHandle() + ")");
- }
- else
- {
+ } else {
System.out.print("New item: ");
- if (i != null)
- {
- if (i.getHandle() != null)
- {
+ if (i != null) {
+ if (i.getHandle() != null) {
System.out.print(i.getID() + " (" + i.getHandle() + ")");
- }
- else
- {
+ } else {
System.out.print(i.getID() + " (in workflow)");
}
}
@@ -1005,69 +915,48 @@ public class MetadataImport
}
// Show actions
- if (change.isDeleted())
- {
- if (changed)
- {
+ if (change.isDeleted()) {
+ if (changed) {
System.out.println(" - EXPUNGED!");
- }
- else
- {
+ } else {
System.out.println(" - EXPUNGE!");
}
}
- if (change.isWithdrawn())
- {
- if (changed)
- {
+ if (change.isWithdrawn()) {
+ if (changed) {
System.out.println(" - WITHDRAWN!");
- }
- else
- {
+ } else {
System.out.println(" - WITHDRAW!");
}
}
- if (change.isReinstated())
- {
- if (changed)
- {
+ if (change.isReinstated()) {
+ if (changed) {
System.out.println(" - REINSTATED!");
- }
- else
- {
+ } else {
System.out.println(" - REINSTATE!");
}
}
- if (change.getNewOwningCollection() != null)
- {
+ if (change.getNewOwningCollection() != null) {
Collection c = change.getNewOwningCollection();
- if (c != null)
- {
+ if (c != null) {
String cHandle = c.getHandle();
String cName = c.getName();
- if (!changed)
- {
+ if (!changed) {
System.out.print(" + New owning collection (" + cHandle + "): ");
- }
- else
- {
+ } else {
System.out.print(" + New owning collection (" + cHandle + "): ");
}
System.out.println(cName);
}
c = change.getOldOwningCollection();
- if (c != null)
- {
+ if (c != null) {
String cHandle = c.getHandle();
String cName = c.getName();
- if (!changed)
- {
+ if (!changed) {
System.out.print(" + Old owning collection (" + cHandle + "): ");
- }
- else
- {
+ } else {
System.out.print(" + Old owning collection (" + cHandle + "): ");
}
System.out.println(cName);
@@ -1075,60 +964,45 @@ public class MetadataImport
}
// Show new mapped collections
- for (Collection c : newCollections)
- {
+ for (Collection c : newCollections) {
String cHandle = c.getHandle();
String cName = c.getName();
- if (!changed)
- {
+ if (!changed) {
System.out.print(" + Map to collection (" + cHandle + "): ");
- }
- else
- {
+ } else {
System.out.print(" + Mapped to collection (" + cHandle + "): ");
}
System.out.println(cName);
}
// Show old mapped collections
- for (Collection c : oldCollections)
- {
+ for (Collection c : oldCollections) {
String cHandle = c.getHandle();
String cName = c.getName();
- if (!changed)
- {
+ if (!changed) {
System.out.print(" + Un-map from collection (" + cHandle + "): ");
- }
- else
- {
+ } else {
System.out.print(" + Un-mapped from collection (" + cHandle + "): ");
}
System.out.println(cName);
}
// Show additions
- for (BulkEditMetadataValue metadataValue : adds)
- {
+ for (BulkEditMetadataValue metadataValue : adds) {
String md = metadataValue.getSchema() + "." + metadataValue.getElement();
- if (metadataValue.getQualifier() != null)
- {
+ if (metadataValue.getQualifier() != null) {
md += "." + metadataValue.getQualifier();
}
- if (metadataValue.getLanguage() != null)
- {
+ if (metadataValue.getLanguage() != null) {
md += "[" + metadataValue.getLanguage() + "]";
}
- if (!changed)
- {
+ if (!changed) {
System.out.print(" + Add (" + md + "): ");
- }
- else
- {
+ } else {
System.out.print(" + Added (" + md + "): ");
}
System.out.print(metadataValue.getValue());
- if (isAuthorityControlledField(md))
- {
+ if (isAuthorityControlledField(md)) {
System.out.print(", authority = " + metadataValue.getAuthority());
System.out.print(", confidence = " + metadataValue.getConfidence());
}
@@ -1136,28 +1010,21 @@ public class MetadataImport
}
// Show removals
- for (BulkEditMetadataValue metadataValue : removes)
- {
+ for (BulkEditMetadataValue metadataValue : removes) {
String md = metadataValue.getSchema() + "." + metadataValue.getElement();
- if (metadataValue.getQualifier() != null)
- {
+ if (metadataValue.getQualifier() != null) {
md += "." + metadataValue.getQualifier();
}
- if (metadataValue.getLanguage() != null)
- {
+ if (metadataValue.getLanguage() != null) {
md += "[" + metadataValue.getLanguage() + "]";
}
- if (!changed)
- {
+ if (!changed) {
System.out.print(" - Remove (" + md + "): ");
- }
- else
- {
+ } else {
System.out.print(" - Removed (" + md + "): ");
}
System.out.print(metadataValue.getValue());
- if (isAuthorityControlledField(md))
- {
+ if (isAuthorityControlledField(md)) {
System.out.print(", authority = " + metadataValue.getAuthority());
System.out.print(", confidence = " + metadataValue.getConfidence());
}
@@ -1169,10 +1036,8 @@ public class MetadataImport
/**
* is the field is defined as authority controlled
- *
*/
- private static boolean isAuthorityControlledField(String md)
- {
+ private static boolean isAuthorityControlledField(String md) {
String mdf = StringUtils.substringAfter(md, ":");
mdf = StringUtils.substringBefore(mdf, "[");
return authorityControlled.contains(mdf);
@@ -1180,30 +1045,25 @@ public class MetadataImport
/**
* Set authority controlled fields
- *
*/
- private static void setAuthorizedMetadataFields()
- {
+ private static void setAuthorizedMetadataFields() {
authorityControlled = new HashSet();
Enumeration propertyNames = ConfigurationManager.getProperties().propertyNames();
- while(propertyNames.hasMoreElements())
- {
+ while (propertyNames.hasMoreElements()) {
String key = ((String) propertyNames.nextElement()).trim();
if (key.startsWith(AC_PREFIX)
- && ConfigurationManager.getBooleanProperty(key, false))
- {
- authorityControlled.add(key.substring(AC_PREFIX.length()));
+ && ConfigurationManager.getBooleanProperty(key, false)) {
+ authorityControlled.add(key.substring(AC_PREFIX.length()));
}
}
}
/**
- * main method to run the metadata exporter
- *
- * @param argv the command line arguments given
- */
- public static void main(String[] argv)
- {
+ * main method to run the metadata exporter
+ *
+ * @param argv the command line arguments given
+ */
+ public static void main(String[] argv) {
// Create an options object and populate it
CommandLineParser parser = new PosixParser();
@@ -1211,33 +1071,31 @@ public class MetadataImport
options.addOption("f", "file", true, "source file");
options.addOption("e", "email", true, "email address or user id of user (required if adding new items)");
- options.addOption("s", "silent", false, "silent operation - doesn't request confirmation of changes USE WITH CAUTION");
+ options.addOption("s", "silent", false,
+ "silent operation - doesn't request confirmation of changes USE WITH CAUTION");
options.addOption("w", "workflow", false, "workflow - when adding new items, use collection workflow");
- options.addOption("n", "notify", false, "notify - when adding new items using a workflow, send notification emails");
- options.addOption("t", "template", false, "template - when adding new items, use the collection template (if it exists)");
+ options.addOption("n", "notify", false,
+ "notify - when adding new items using a workflow, send notification emails");
+ options.addOption("t", "template", false,
+ "template - when adding new items, use the collection template (if it exists)");
options.addOption("h", "help", false, "help");
// Parse the command line arguments
CommandLine line;
- try
- {
+ try {
line = parser.parse(options, argv);
- }
- catch (ParseException pe)
- {
+ } catch (ParseException pe) {
System.err.println("Error parsing command line arguments: " + pe.getMessage());
System.exit(1);
return;
}
- if (line.hasOption('h'))
- {
+ if (line.hasOption('h')) {
printHelp(options, 0);
}
// Check a filename is given
- if (!line.hasOption('f'))
- {
+ if (!line.hasOption('f')) {
System.err.println("Required parameter -f missing!");
printHelp(options, 1);
}
@@ -1245,66 +1103,52 @@ public class MetadataImport
// Option to apply template to new items
boolean useTemplate = false;
- if (line.hasOption('t'))
- {
+ if (line.hasOption('t')) {
useTemplate = true;
}
// Options for workflows, and workflow notifications for new items
boolean useWorkflow = false;
- boolean workflowNotify = false;
- if (line.hasOption('w'))
- {
+ boolean workflowNotify = false;
+ if (line.hasOption('w')) {
useWorkflow = true;
- if (line.hasOption('n'))
- {
+ if (line.hasOption('n')) {
workflowNotify = true;
}
- }
- else if (line.hasOption('n'))
- {
+ } else if (line.hasOption('n')) {
System.err.println("Invalid option 'n': (notify) can only be specified with the 'w' (workflow) option.");
System.exit(1);
}
// Create a context
Context c;
- try
- {
+ try {
c = new Context();
c.turnOffAuthorisationSystem();
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
System.err.println("Unable to create a new DSpace Context: " + e.getMessage());
System.exit(1);
return;
}
// Find the EPerson, assign to context
- try
- {
- if (line.hasOption('e'))
- {
+ try {
+ if (line.hasOption('e')) {
EPerson eperson;
String e = line.getOptionValue('e');
- if (e.indexOf('@') != -1)
- {
+ if (e.indexOf('@') != -1) {
eperson = EPersonServiceFactory.getInstance().getEPersonService().findByEmail(c, e);
- } else
- {
+ } else {
eperson = EPersonServiceFactory.getInstance().getEPersonService().find(c, UUID.fromString(e));
}
- if (eperson == null)
- {
+ if (eperson == null) {
System.out.println("Error, eperson cannot be found: " + e);
System.exit(1);
}
c.setCurrentUser(eperson);
}
- } catch (Exception e)
- {
+ } catch (Exception e) {
System.err.println("Unable to find DSpace user: " + e.getMessage());
System.exit(1);
return;
@@ -1315,18 +1159,13 @@ public class MetadataImport
// Read lines from the CSV file
DSpaceCSV csv;
- try
- {
+ try {
csv = new DSpaceCSV(new File(filename), c);
- }
- catch (MetadataImportInvalidHeadingException miihe)
- {
+ } catch (MetadataImportInvalidHeadingException miihe) {
System.err.println(miihe.getMessage());
System.exit(1);
return;
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
System.err.println("Error reading file: " + e.getMessage());
System.exit(1);
return;
@@ -1336,15 +1175,11 @@ public class MetadataImport
MetadataImport importer = new MetadataImport(c, csv);
List changes;
- if (!line.hasOption('s'))
- {
+ if (!line.hasOption('s')) {
// See what has changed
- try
- {
+ try {
changes = importer.runImport(false, useWorkflow, workflowNotify, useTemplate);
- }
- catch (MetadataImportException mie)
- {
+ } catch (MetadataImportException mie) {
System.err.println("Error: " + mie.getMessage());
System.exit(1);
return;
@@ -1354,52 +1189,36 @@ public class MetadataImport
int changeCounter = displayChanges(changes, false);
// If there were changes, ask if we should execute them
- if (changeCounter > 0)
- {
- try
- {
+ if (changeCounter > 0) {
+ try {
// Ask the user if they want to make the changes
System.out.println("\n" + changeCounter + " item(s) will be changed\n");
System.out.print("Do you want to make these changes? [y/n] ");
String yn = (new BufferedReader(new InputStreamReader(System.in))).readLine();
- if ("y".equalsIgnoreCase(yn))
- {
+ if ("y".equalsIgnoreCase(yn)) {
change = true;
- }
- else
- {
+ } else {
System.out.println("No data has been changed.");
}
- }
- catch (IOException ioe)
- {
+ } catch (IOException ioe) {
System.err.println("Error: " + ioe.getMessage());
System.err.println("No changes have been made");
System.exit(1);
}
- }
- else
- {
+ } else {
System.out.println("There were no changes detected");
}
- }
- else
- {
+ } else {
change = true;
}
- try
- {
+ try {
// If required, make the change
- if (change)
- {
- try
- {
+ if (change) {
+ try {
// Make the changes
changes = importer.runImport(true, useWorkflow, workflowNotify, useTemplate);
- }
- catch (MetadataImportException mie)
- {
+ } catch (MetadataImportException mie) {
System.err.println("Error: " + mie.getMessage());
System.exit(1);
return;
@@ -1415,9 +1234,7 @@ public class MetadataImport
// Finsh off and tidy up
c.restoreAuthSystemState();
c.complete();
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
c.abort();
System.err.println("Error committing changes to database: " + e.getMessage());
System.err.println("Aborting most recent changes.");
diff --git a/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImportException.java b/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImportException.java
index da359a7e0b..3353d78122 100644
--- a/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImportException.java
+++ b/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImportException.java
@@ -12,26 +12,23 @@ package org.dspace.app.bulkedit;
*
* @author Stuart Lewis
*/
-public class MetadataImportException extends Exception
-{
+public class MetadataImportException extends Exception {
/**
* Instantiate a new MetadataImportException
*
* @param message the error message
*/
- public MetadataImportException(String message)
- {
- super(message);
+ public MetadataImportException(String message) {
+ super(message);
}
/**
* Instantiate a new MetadataImportException
*
- * @param message the error message
+ * @param message the error message
* @param exception the root cause
*/
- public MetadataImportException(String message, Exception exception)
- {
- super(message, exception);
+ public MetadataImportException(String message, Exception exception) {
+ super(message, exception);
}
-}
\ No newline at end of file
+}
diff --git a/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImportInvalidHeadingException.java b/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImportInvalidHeadingException.java
index 29e3155a22..6781517b97 100644
--- a/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImportInvalidHeadingException.java
+++ b/dspace-api/src/main/java/org/dspace/app/bulkedit/MetadataImportInvalidHeadingException.java
@@ -12,39 +12,51 @@ package org.dspace.app.bulkedit;
*
* @author Stuart Lewis
*/
-public class MetadataImportInvalidHeadingException extends Exception
-{
- /** The type of error (schema or element) */
+public class MetadataImportInvalidHeadingException extends Exception {
+ /**
+ * The type of error (schema or element)
+ */
private int type;
- /** The bad heading */
+ /**
+ * The bad heading
+ */
private String badHeading;
- /** The column number */
+ /**
+ * The column number
+ */
private int column;
- /** Error with the schema */
+ /**
+ * Error with the schema
+ */
public static final int SCHEMA = 0;
- /** Error with the element */
+ /**
+ * Error with the element
+ */
public static final int ELEMENT = 1;
- /** Error with a missing header */
+ /**
+ * Error with a missing header
+ */
public static final int MISSING = 98;
- /** Error with the whole entry */
+ /**
+ * Error with the whole entry
+ */
public static final int ENTRY = 99;
/**
* Instantiate a new MetadataImportInvalidHeadingException
*
- * @param message the error message
- * @param theType the type of the error
+ * @param message the error message
+ * @param theType the type of the error
* @param theColumn column number
*/
- public MetadataImportInvalidHeadingException(String message, int theType, int theColumn)
- {
+ public MetadataImportInvalidHeadingException(String message, int theType, int theColumn) {
super(message);
badHeading = message;
type = theType;
@@ -54,10 +66,9 @@ public class MetadataImportInvalidHeadingException extends Exception
/**
* Get the type of the exception
*
- * @return the type of the exception
+ * @return the type of the exception
*/
- public String getType()
- {
+ public String getType() {
return "" + type;
}
@@ -66,8 +77,7 @@ public class MetadataImportInvalidHeadingException extends Exception
*
* @return the invalid heading
*/
- public String getBadHeader()
- {
+ public String getBadHeader() {
return badHeading;
}
@@ -76,8 +86,7 @@ public class MetadataImportInvalidHeadingException extends Exception
*
* @return the invalid column number
*/
- public int getColumn()
- {
+ public int getColumn() {
return column;
}
@@ -87,19 +96,14 @@ public class MetadataImportInvalidHeadingException extends Exception
* @return The exception message
*/
@Override
- public String getMessage()
- {
- if (type == SCHEMA)
- {
+ public String getMessage() {
+ if (type == SCHEMA) {
return "Unknown metadata schema in column " + column + ": " + badHeading;
- } else if (type == ELEMENT)
- {
+ } else if (type == ELEMENT) {
return "Unknown metadata element in column " + column + ": " + badHeading;
- } else if (type == MISSING)
- {
+ } else if (type == MISSING) {
return "Row with missing header: column " + column;
- } else
- {
+ } else {
return "Bad metadata declaration in column" + column + ": " + badHeading;
}
}
diff --git a/dspace-api/src/main/java/org/dspace/app/checker/ChecksumChecker.java b/dspace-api/src/main/java/org/dspace/app/checker/ChecksumChecker.java
index 00ec210ad9..40cea2f786 100644
--- a/dspace-api/src/main/java/org/dspace/app/checker/ChecksumChecker.java
+++ b/dspace-api/src/main/java/org/dspace/app/checker/ChecksumChecker.java
@@ -9,7 +9,11 @@ package org.dspace.app.checker;
import java.io.FileNotFoundException;
import java.sql.SQLException;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
@@ -20,7 +24,15 @@ 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.checker.*;
+import org.dspace.checker.BitstreamDispatcher;
+import org.dspace.checker.CheckerCommand;
+import org.dspace.checker.HandleDispatcher;
+import org.dspace.checker.IteratorDispatcher;
+import org.dspace.checker.LimitedCountDispatcher;
+import org.dspace.checker.LimitedDurationDispatcher;
+import org.dspace.checker.ResultsLogger;
+import org.dspace.checker.ResultsPruner;
+import org.dspace.checker.SimpleDispatcher;
import org.dspace.content.Bitstream;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.BitstreamService;
@@ -28,15 +40,14 @@ import org.dspace.core.Context;
import org.dspace.core.Utils;
/**
- * Command line access to the checksum checker. Options are listed in the
+ * Command line access to the checksum checker. Options are listed in the
* documentation for the main method.
- *
+ *
* @author Jim Downing
* @author Grace Carpenter
* @author Nathan Sarr
*/
-public final class ChecksumChecker
-{
+public final class ChecksumChecker {
private static final Logger LOG = Logger.getLogger(ChecksumChecker.class);
private static final BitstreamService bitstreamService = ContentServiceFactory.getInstance().getBitstreamService();
@@ -44,33 +55,32 @@ public final class ChecksumChecker
/**
* Blanked off constructor, this class should be used as a command line
* tool.
- *
*/
- private ChecksumChecker()
- {
+ private ChecksumChecker() {
}
/**
* Command line access to the checksum package.
- *
+ *
*
- * -h
- * Print help on command line options
- * -l
- * loop through bitstreams once
- * -L
- * loop continuously through bitstreams
- * -d
- * specify duration of process run
- * -b
- * specify bitstream IDs
- * -a [handle_id]
- * check anything by handle
- * -e
- * Report only errors in the logs
- * -p
- * Don't prune results before running checker
+ * -h
+ * Print help on command line options
+ * -l
+ * loop through bitstreams once
+ * -L
+ * loop continuously through bitstreams
+ * -d
+ * specify duration of process run
+ * -b
+ * specify bitstream IDs
+ * -a [handle_id]
+ * check anything by handle
+ * -e
+ * Report only errors in the logs
+ * -p
+ * Don't prune results before running checker
*
+ *
* @param args the command line arguments given
* @throws SQLException if error
*/
@@ -84,7 +94,7 @@ public final class ChecksumChecker
options.addOption("l", "looping", false, "Loop once through bitstreams");
options.addOption("L", "continuous", false,
- "Loop continuously through bitstreams");
+ "Loop continuously through bitstreams");
options.addOption("h", "help", false, "Help");
options.addOption("d", "duration", true, "Checking duration");
options.addOption("c", "count", true, "Check count");
@@ -99,25 +109,21 @@ public final class ChecksumChecker
options.addOption("p", "prune", false, "Prune configuration file");
options.addOption(OptionBuilder
- .withArgName("prune")
- .hasOptionalArgs(1)
- .withDescription(
- "Prune old results (optionally using specified properties file for configuration)")
- .create('p'));
+ .withArgName("prune")
+ .hasOptionalArgs(1)
+ .withDescription(
+ "Prune old results (optionally using specified properties file for configuration)")
+ .create('p'));
- try
- {
+ try {
line = parser.parse(options, args);
- }
- catch (ParseException e)
- {
+ } catch (ParseException e) {
LOG.fatal(e);
System.exit(1);
}
// user asks for help
- if (line.hasOption('h'))
- {
+ if (line.hasOption('h')) {
printHelp(options);
}
Context context = null;
@@ -126,23 +132,19 @@ public final class ChecksumChecker
// Prune stage
- if (line.hasOption('p'))
- {
+ if (line.hasOption('p')) {
ResultsPruner rp = null;
- try
- {
+ try {
rp = (line.getOptionValue('p') != null) ? ResultsPruner
- .getPruner(context, line.getOptionValue('p')) : ResultsPruner
- .getDefaultPruner(context);
- }
- catch (FileNotFoundException e)
- {
+ .getPruner(context, line.getOptionValue('p')) : ResultsPruner
+ .getDefaultPruner(context);
+ } catch (FileNotFoundException e) {
LOG.error("File not found", e);
System.exit(1);
}
int count = rp.prune();
System.out.println("Pruned " + count
- + " old results from the database.");
+ + " old results from the database.");
}
Date processStart = Calendar.getInstance().getTime();
@@ -151,77 +153,55 @@ public final class ChecksumChecker
// process should loop infinitely through
// most_recent_checksum table
- if (line.hasOption('l'))
- {
+ if (line.hasOption('l')) {
dispatcher = new SimpleDispatcher(context, processStart, false);
- }
- else if (line.hasOption('L'))
- {
+ } else if (line.hasOption('L')) {
dispatcher = new SimpleDispatcher(context, processStart, true);
- }
- else if (line.hasOption('b'))
- {
+ } else if (line.hasOption('b')) {
// check only specified bitstream(s)
String[] ids = line.getOptionValues('b');
List bitstreams = new ArrayList<>(ids.length);
- for (int i = 0; i < ids.length; i++)
- {
- try
- {
+ for (int i = 0; i < ids.length; i++) {
+ try {
bitstreams.add(bitstreamService.find(context, UUID.fromString(ids[i])));
- }
- catch (NumberFormatException nfe)
- {
+ } catch (NumberFormatException nfe) {
System.err.println("The following argument: " + ids[i]
- + " is not an integer");
+ + " is not an integer");
System.exit(0);
}
}
dispatcher = new IteratorDispatcher(bitstreams.iterator());
- }
-
- else if (line.hasOption('a'))
- {
+ } else if (line.hasOption('a')) {
dispatcher = new HandleDispatcher(context, line.getOptionValue('a'));
- }
- else if (line.hasOption('d'))
- {
+ } else if (line.hasOption('d')) {
// run checker process for specified duration
- try
- {
+ try {
dispatcher = new LimitedDurationDispatcher(
- new SimpleDispatcher(context, processStart, true), new Date(
- System.currentTimeMillis()
- + Utils.parseDuration(line
- .getOptionValue('d'))));
- }
- catch (Exception e)
- {
+ new SimpleDispatcher(context, processStart, true), new Date(
+ System.currentTimeMillis()
+ + Utils.parseDuration(line
+ .getOptionValue('d'))));
+ } catch (Exception e) {
LOG.fatal("Couldn't parse " + line.getOptionValue('d')
- + " as a duration: ", e);
+ + " as a duration: ", e);
System.exit(0);
}
- }
- else if (line.hasOption('c'))
- {
+ } else if (line.hasOption('c')) {
int count = Integer.valueOf(line.getOptionValue('c'));
// run checker process for specified number of bitstreams
dispatcher = new LimitedCountDispatcher(new SimpleDispatcher(
- context, processStart, false), count);
- }
- else
- {
+ context, processStart, false), count);
+ } else {
dispatcher = new LimitedCountDispatcher(new SimpleDispatcher(
- context, processStart, false), 1);
+ context, processStart, false), 1);
}
ResultsLogger logger = new ResultsLogger(processStart);
CheckerCommand checker = new CheckerCommand(context);
// verbose reporting
- if (line.hasOption('v'))
- {
+ if (line.hasOption('v')) {
checker.setReportVerbose(true);
}
@@ -240,21 +220,20 @@ public final class ChecksumChecker
/**
* Print the help options for the user
- *
+ *
* @param options that are available for the user
*/
- private static void printHelp(Options options)
- {
+ private static void printHelp(Options options) {
HelpFormatter myhelp = new HelpFormatter();
myhelp.printHelp("Checksum Checker\n", options);
System.out.println("\nSpecify a duration for checker process, using s(seconds),"
- + "m(minutes), or h(hours): ChecksumChecker -d 30s"
- + " OR ChecksumChecker -d 30m"
- + " OR ChecksumChecker -d 2h");
+ + "m(minutes), or h(hours): ChecksumChecker -d 30s"
+ + " OR ChecksumChecker -d 30m"
+ + " OR ChecksumChecker -d 2h");
System.out.println("\nSpecify bitstream IDs: ChecksumChecker -b 13 15 17 20");
System.out.println("\nLoop once through all bitstreams: "
- + "ChecksumChecker -l");
+ + "ChecksumChecker -l");
System.out.println("\nLoop continuously through all bitstreams: ChecksumChecker -L");
System.out.println("\nCheck a defined number of bitstreams: ChecksumChecker -c 10");
System.out.println("\nReport all processing (verbose)(default reports only errors): ChecksumChecker -v");
diff --git a/dspace-api/src/main/java/org/dspace/app/configuration/APISpringLoader.java b/dspace-api/src/main/java/org/dspace/app/configuration/APISpringLoader.java
index f2d84a045c..ce5865742b 100644
--- a/dspace-api/src/main/java/org/dspace/app/configuration/APISpringLoader.java
+++ b/dspace-api/src/main/java/org/dspace/app/configuration/APISpringLoader.java
@@ -7,12 +7,12 @@
*/
package org.dspace.app.configuration;
-import org.dspace.kernel.config.SpringLoader;
-import org.dspace.services.ConfigurationService;
-
import java.io.File;
import java.net.MalformedURLException;
+import org.dspace.kernel.config.SpringLoader;
+import org.dspace.services.ConfigurationService;
+
/**
* @author Kevin Van de Velde (kevin at atmire dot com)
*/
@@ -32,7 +32,7 @@ public class APISpringLoader implements SpringLoader {
try {
- return new String[]{new File(filePath.toString()).toURI().toURL().toString() + XML_SUFFIX};
+ return new String[] {new File(filePath.toString()).toURI().toURL().toString() + XML_SUFFIX};
} catch (MalformedURLException e) {
return new String[0];
}
diff --git a/dspace-api/src/main/java/org/dspace/app/harvest/Harvest.java b/dspace-api/src/main/java/org/dspace/app/harvest/Harvest.java
index 09e43f160e..3b324f2763 100644
--- a/dspace-api/src/main/java/org/dspace/app/harvest/Harvest.java
+++ b/dspace-api/src/main/java/org/dspace/app/harvest/Harvest.java
@@ -7,7 +7,17 @@
*/
package org.dspace.app.harvest;
-import org.apache.commons.cli.*;
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.PosixParser;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.content.DSpaceObject;
@@ -27,27 +37,21 @@ import org.dspace.harvest.OAIHarvester;
import org.dspace.harvest.factory.HarvestServiceFactory;
import org.dspace.harvest.service.HarvestedCollectionService;
-import java.io.IOException;
-import java.sql.SQLException;
-import java.util.Iterator;
-import java.util.List;
-import java.util.UUID;
-
/**
- * Test class for harvested collections.
+ * Test class for harvested collections.
*
* @author Alexey Maslov
*/
-public class Harvest
-{
+public class Harvest {
private static Context context;
- private static final HarvestedCollectionService harvestedCollectionService = HarvestServiceFactory.getInstance().getHarvestedCollectionService();
+ private static final HarvestedCollectionService harvestedCollectionService =
+ HarvestServiceFactory.getInstance().getHarvestedCollectionService();
private static final EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
- private static final CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService();
+ private static final CollectionService collectionService =
+ ContentServiceFactory.getInstance().getCollectionService();
- public static void main(String[] argv) throws Exception
- {
+ public static void main(String[] argv) throws Exception {
// create an options object and populate it
CommandLineParser parser = new PosixParser();
@@ -61,47 +65,50 @@ public class Harvest
options.addOption("S", "start", false, "start the harvest loop");
options.addOption("R", "reset", false, "reset harvest status on all collections");
options.addOption("P", "purge", false, "purge all harvestable collections");
-
+
options.addOption("e", "eperson", true,
- "eperson");
+ "eperson");
options.addOption("c", "collection", true,
- "harvesting collection (handle or id)");
+ "harvesting collection (handle or id)");
options.addOption("t", "type", true,
- "type of harvesting (0 for none)");
+ "type of harvesting (0 for none)");
options.addOption("a", "address", true,
- "address of the OAI-PMH server");
+ "address of the OAI-PMH server");
options.addOption("i", "oai_set_id", true,
- "id of the PMH set representing the harvested collection");
+ "id of the PMH set representing the harvested collection");
options.addOption("m", "metadata_format", true,
- "the name of the desired metadata format for harvesting, resolved to namespace and crosswalk in dspace.cfg");
+ "the name of the desired metadata format for harvesting, resolved to namespace and " +
+ "crosswalk in dspace.cfg");
options.addOption("h", "help", false, "help");
CommandLine line = parser.parse(options, argv);
- String command = null;
+ String command = null;
String eperson = null;
String collection = null;
String oaiSource = null;
String oaiSetID = null;
String metadataKey = null;
int harvestType = 0;
-
- if (line.hasOption('h'))
- {
+
+ if (line.hasOption('h')) {
HelpFormatter myhelp = new HelpFormatter();
myhelp.printHelp("Harvest\n", options);
System.out.println("\nPING OAI server: Harvest -g -a oai_source -i oai_set_id");
- System.out.println("RUNONCE harvest with arbitrary options: Harvest -o -e eperson -c collection -t harvest_type -a oai_source -i oai_set_id -m metadata_format");
- System.out.println("SETUP a collection for harvesting: Harvest -s -c collection -t harvest_type -a oai_source -i oai_set_id -m metadata_format");
+ System.out.println(
+ "RUNONCE harvest with arbitrary options: Harvest -o -e eperson -c collection -t harvest_type -a " +
+ "oai_source -i oai_set_id -m metadata_format");
+ System.out.println(
+ "SETUP a collection for harvesting: Harvest -s -c collection -t harvest_type -a oai_source -i " +
+ "oai_set_id -m metadata_format");
System.out.println("RUN harvest once: Harvest -r -e eperson -c collection");
System.out.println("START harvest scheduler: Harvest -S");
System.out.println("RESET all harvest status: Harvest -R");
System.out.println("PURGE a collection of items and settings: Harvest -p -e eperson -c collection");
System.out.println("PURGE all harvestable collections: Harvest -P -e eperson");
-
-
+
System.exit(0);
}
@@ -131,7 +138,7 @@ public class Harvest
command = "purgeAll";
}
-
+
if (line.hasOption('e')) {
eperson = line.getOptionValue('e');
}
@@ -152,106 +159,87 @@ public class Harvest
if (line.hasOption('m')) {
metadataKey = line.getOptionValue('m');
}
-
+
// Instantiate our class
Harvest harvester = new Harvest();
harvester.context = new Context(Context.Mode.BATCH_EDIT);
-
-
+
+
// Check our options
- if (command == null)
- {
+ if (command == null) {
System.out
- .println("Error - no parameters specified (run with -h flag for details)");
+ .println("Error - no parameters specified (run with -h flag for details)");
System.exit(1);
- }
- // Run a single harvest cycle on a collection using saved settings.
- else if ("run".equals(command))
- {
- if (collection == null || eperson == null)
- {
+ } else if ("run".equals(command)) {
+ // Run a single harvest cycle on a collection using saved settings.
+ if (collection == null || eperson == null) {
System.out
- .println("Error - a target collection and eperson must be provided");
+ .println("Error - a target collection and eperson must be provided");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
-
+
harvester.runHarvest(collection, eperson);
- }
- // start the harvest loop
- else if ("start".equals(command))
- {
+ } else if ("start".equals(command)) {
+ // start the harvest loop
startHarvester();
- }
- // reset harvesting status
- else if ("reset".equals(command))
- {
+ } else if ("reset".equals(command)) {
+ // reset harvesting status
resetHarvesting();
- }
- // purge all collections that are set up for harvesting (obviously for testing purposes only)
- else if ("purgeAll".equals(command))
- {
- if (eperson == null)
- {
+ } else if ("purgeAll".equals(command)) {
+ // purge all collections that are set up for harvesting (obviously for testing purposes only)
+ if (eperson == null) {
System.out
- .println("Error - an eperson must be provided");
+ .println("Error - an eperson must be provided");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
List harvestedCollections = harvestedCollectionService.findAll(context);
- for (HarvestedCollection harvestedCollection : harvestedCollections)
- {
- System.out.println("Purging the following collections (deleting items and resetting harvest status): " + harvestedCollection.getCollection().getID().toString());
+ for (HarvestedCollection harvestedCollection : harvestedCollections) {
+ System.out.println(
+ "Purging the following collections (deleting items and resetting harvest status): " +
+ harvestedCollection
+ .getCollection().getID().toString());
harvester.purgeCollection(harvestedCollection.getCollection().getID().toString(), eperson);
}
context.complete();
- }
- // Delete all items in a collection. Useful for testing fresh harvests.
- else if ("purge".equals(command))
- {
- if (collection == null || eperson == null)
- {
+ } else if ("purge".equals(command)) {
+ // Delete all items in a collection. Useful for testing fresh harvests.
+ if (collection == null || eperson == null) {
System.out
- .println("Error - a target collection and eperson must be provided");
+ .println("Error - a target collection and eperson must be provided");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
-
+
harvester.purgeCollection(collection, eperson);
context.complete();
-
+
//TODO: implement this... remove all items and remember to unset "last-harvested" settings
- }
- // Configure a collection with the three main settings
- else if ("config".equals(command))
- {
- if (collection == null)
- {
+ } else if ("config".equals(command)) {
+ // Configure a collection with the three main settings
+ if (collection == null) {
System.out.println("Error - a target collection must be provided");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
- if (oaiSource == null || oaiSetID == null)
- {
+ if (oaiSource == null || oaiSetID == null) {
System.out.println("Error - both the OAI server address and OAI set id must be specified");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
- if (metadataKey == null)
- {
- System.out.println("Error - a metadata key (commonly the prefix) must be specified for this collection");
+ if (metadataKey == null) {
+ System.out
+ .println("Error - a metadata key (commonly the prefix) must be specified for this collection");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
-
+
harvester.configureCollection(collection, harvestType, oaiSource, oaiSetID, metadataKey);
- }
- else if ("ping".equals(command))
- {
- if (oaiSource == null || oaiSetID == null)
- {
+ } else if ("ping".equals(command)) {
+ if (oaiSource == null || oaiSetID == null) {
System.out.println("Error - both the OAI server address and OAI set id must be specified");
System.out.println(" (run with -h flag for details)");
System.exit(1);
@@ -260,10 +248,10 @@ public class Harvest
pingResponder(oaiSource, oaiSetID, metadataKey);
}
}
-
+
/*
* Resolve the ID into a collection and check to see if its harvesting options are set. If so, return
- * the collection, if not, bail out.
+ * the collection, if not, bail out.
*/
private Collection resolveCollection(String collectionID) {
@@ -272,47 +260,39 @@ public class Harvest
try {
// is the ID a handle?
- if (collectionID != null)
- {
- if (collectionID.indexOf('/') != -1)
- {
+ if (collectionID != null) {
+ if (collectionID.indexOf('/') != -1) {
// string has a / so it must be a handle - try and resolve it
dso = HandleServiceFactory.getInstance().getHandleService().resolveToObject(context, collectionID);
// resolved, now make sure it's a collection
- if (dso == null || dso.getType() != Constants.COLLECTION)
- {
+ if (dso == null || dso.getType() != Constants.COLLECTION) {
targetCollection = null;
- }
- else
- {
+ } else {
targetCollection = (Collection) dso;
}
- }
- // not a handle, try and treat it as an integer collection
- // database ID
- else
- {
- System.out.println("Looking up by id: " + collectionID + ", parsed as '" + Integer.parseInt(collectionID) + "', " + "in context: " + context);
+ } else {
+ // not a handle, try and treat it as an integer collection database ID
+ System.out.println("Looking up by id: " + collectionID + ", parsed as '" + Integer
+ .parseInt(collectionID) + "', " + "in context: " + context);
targetCollection = collectionService.find(context, UUID.fromString(collectionID));
}
}
// was the collection valid?
- if (targetCollection == null)
- {
+ if (targetCollection == null) {
System.out.println("Cannot resolve " + collectionID + " to collection");
System.exit(1);
}
- }
- catch (SQLException se) {
+ } catch (SQLException se) {
se.printStackTrace();
}
return targetCollection;
}
-
-
- private void configureCollection(String collectionID, int type, String oaiSource, String oaiSetId, String mdConfigId) {
+
+
+ private void configureCollection(String collectionID, int type, String oaiSource, String oaiSetId,
+ String mdConfigId) {
System.out.println("Running: configure collection");
Collection collection = resolveCollection(collectionID);
@@ -330,55 +310,52 @@ public class Harvest
harvestedCollectionService.update(context, hc);
context.restoreAuthSystemState();
context.complete();
- }
- catch (Exception e) {
+ } catch (Exception e) {
System.out.println("Changes could not be committed");
e.printStackTrace();
System.exit(1);
- }
- finally {
- if (context != null)
- {
+ } finally {
+ if (context != null) {
context.restoreAuthSystemState();
}
}
}
-
-
+
+
/**
* Purges a collection of all harvest-related data and settings. All items in the collection will be deleted.
- *
+ *
* @param collectionID
* @param email
*/
private void purgeCollection(String collectionID, String email) {
- System.out.println("Purging collection of all items and resetting last_harvested and harvest_message: " + collectionID);
+ System.out.println(
+ "Purging collection of all items and resetting last_harvested and harvest_message: " + collectionID);
Collection collection = resolveCollection(collectionID);
- try
- {
+ try {
EPerson eperson = ePersonService.findByEmail(context, email);
context.setCurrentUser(eperson);
context.turnOffAuthorisationSystem();
ItemService itemService = ContentServiceFactory.getInstance().getItemService();
Iterator- it = itemService.findByCollection(context, collection);
- int i=0;
+ int i = 0;
while (it.hasNext()) {
i++;
Item item = it.next();
System.out.println("Deleting: " + item.getHandle());
collectionService.removeItem(context, collection, item);
- context.uncacheEntity(item);// Dispatch events every 50 items
- if (i%50 == 0) {
- context.dispatchEvents();
- i=0;
- }
- }
-
- HarvestedCollection hc = harvestedCollectionService.find(context, collection);
- if (hc != null) {
- hc.setLastHarvested(null);
+ context.uncacheEntity(item);// Dispatch events every 50 items
+ if (i % 50 == 0) {
+ context.dispatchEvents();
+ i = 0;
+ }
+ }
+
+ HarvestedCollection hc = harvestedCollectionService.find(context, collection);
+ if (hc != null) {
+ hc.setLastHarvested(null);
hc.setHarvestMessage("");
hc.setHarvestStatus(HarvestedCollection.STATUS_READY);
hc.setHarvestStartTime(null);
@@ -386,20 +363,18 @@ public class Harvest
}
context.restoreAuthSystemState();
context.dispatchEvents();
- }
- catch (Exception e) {
+ } catch (Exception e) {
System.out.println("Changes could not be committed");
e.printStackTrace();
System.exit(1);
- }
- finally {
+ } finally {
context.restoreAuthSystemState();
}
}
-
-
+
+
/**
- * Run a single harvest cycle on the specified collection under the authorization of the supplied EPerson
+ * Run a single harvest cycle on the specified collection under the authorization of the supplied EPerson
*/
private void runHarvest(String collectionID, String email) {
System.out.println("Running: a harvest cycle on " + collectionID);
@@ -411,8 +386,7 @@ public class Harvest
HarvestedCollection hc = harvestedCollectionService.find(context, collection);
harvester = new OAIHarvester(context, collection, hc);
System.out.println("success. ");
- }
- catch (HarvestingException hex) {
+ } catch (HarvestingException hex) {
System.out.print("failed. ");
System.out.println(hex.getMessage());
throw new IllegalStateException("Unable to harvest", hex);
@@ -429,14 +403,11 @@ public class Harvest
context.setCurrentUser(eperson);
harvester.runHarvest();
context.complete();
- }
- catch (SQLException e) {
+ } catch (SQLException e) {
throw new IllegalStateException("Failed to run harvester", e);
- }
- catch (AuthorizeException e) {
+ } catch (AuthorizeException e) {
throw new IllegalStateException("Failed to run harvester", e);
- }
- catch (IOException e) {
+ } catch (IOException e) {
throw new IllegalStateException("Failed to run harvester", e);
}
@@ -444,24 +415,22 @@ public class Harvest
}
/**
- * Resets harvest_status and harvest_start_time flags for all collections that have a row in the harvested_collections table
+ * Resets harvest_status and harvest_start_time flags for all collections that have a row in the
+ * harvested_collections table
*/
private static void resetHarvesting() {
System.out.print("Resetting harvest status flag on all collections... ");
- try
- {
+ try {
List
harvestedCollections = harvestedCollectionService.findAll(context);
- for (HarvestedCollection harvestedCollection : harvestedCollections)
- {
+ for (HarvestedCollection harvestedCollection : harvestedCollections) {
//hc.setHarvestResult(null,"");
harvestedCollection.setHarvestStartTime(null);
harvestedCollection.setHarvestStatus(HarvestedCollection.STATUS_READY);
harvestedCollectionService.update(context, harvestedCollection);
}
System.out.println("success. ");
- }
- catch (Exception ex) {
+ } catch (Exception ex) {
System.out.println("failed. ");
ex.printStackTrace();
}
@@ -470,15 +439,12 @@ public class Harvest
/**
* Starts up the harvest scheduler. Terminating this process will stop the scheduler.
*/
- private static void startHarvester()
- {
- try
- {
+ private static void startHarvester() {
+ try {
System.out.print("Starting harvest loop... ");
HarvestServiceFactory.getInstance().getHarvestSchedulingService().startNewScheduler();
System.out.println("running. ");
- }
- catch (Exception ex) {
+ } catch (Exception ex) {
ex.printStackTrace();
}
}
@@ -486,34 +452,33 @@ public class Harvest
/**
* See if the responder is alive and working.
*
- * @param server address of the responder's host.
- * @param set name of an item set.
+ * @param server address of the responder's host.
+ * @param set name of an item set.
* @param metadataFormat local prefix name, or null for "dc".
*/
- private static void pingResponder(String server, String set, String metadataFormat)
- {
+ private static void pingResponder(String server, String set, String metadataFormat) {
List errors;
System.out.print("Testing basic PMH access: ");
errors = OAIHarvester.verifyOAIharvester(server, set,
- (null != metadataFormat) ? metadataFormat : "dc", false);
- if (errors.isEmpty())
+ (null != metadataFormat) ? metadataFormat : "dc", false);
+ if (errors.isEmpty()) {
System.out.println("OK");
- else
- {
- for (String error : errors)
+ } else {
+ for (String error : errors) {
System.err.println(error);
+ }
}
System.out.print("Testing ORE support: ");
errors = OAIHarvester.verifyOAIharvester(server, set,
- (null != metadataFormat) ? metadataFormat : "dc", true);
- if (errors.isEmpty())
+ (null != metadataFormat) ? metadataFormat : "dc", true);
+ if (errors.isEmpty()) {
System.out.println("OK");
- else
- {
- for (String error : errors)
+ } else {
+ for (String error : errors) {
System.err.println(error);
+ }
}
}
}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportCLITool.java b/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportCLITool.java
index 973f60346b..760a6fc9e5 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportCLITool.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportCLITool.java
@@ -7,7 +7,17 @@
*/
package org.dspace.app.itemexport;
-import org.apache.commons.cli.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.PosixParser;
import org.dspace.app.itemexport.factory.ItemExportServiceFactory;
import org.dspace.app.itemexport.service.ItemExportService;
import org.dspace.content.Collection;
@@ -20,8 +30,6 @@ import org.dspace.core.Context;
import org.dspace.handle.factory.HandleServiceFactory;
import org.dspace.handle.service.HandleService;
-import java.util.*;
-
/**
* Item exporter to create simple AIPs for DSpace content. Currently exports
* individual items, or entire collections. For instructions on use, see
@@ -45,17 +53,21 @@ import java.util.*;
*/
public class ItemExportCLITool {
- protected static ItemExportService itemExportService = ItemExportServiceFactory.getInstance().getItemExportService();
+ protected static ItemExportService itemExportService = ItemExportServiceFactory.getInstance()
+ .getItemExportService();
protected static HandleService handleService = HandleServiceFactory.getInstance().getHandleService();
protected static ItemService itemService = ContentServiceFactory.getInstance().getItemService();
protected static CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService();
+ /**
+ * Default constructor
+ */
+ private ItemExportCLITool() { }
/*
*
*/
- public static void main(String[] argv) throws Exception
- {
+ public static void main(String[] argv) throws Exception {
// create an options object and populate it
CommandLineParser parser = new PosixParser();
@@ -64,10 +76,11 @@ public class ItemExportCLITool {
options.addOption("t", "type", true, "type: COLLECTION or ITEM");
options.addOption("i", "id", true, "ID or handle of thing to export");
options.addOption("d", "dest", true,
- "destination where you want items to go");
- options.addOption("m", "migrate", false, "export for migration (remove handle and metadata that will be re-created in new system)");
+ "destination where you want items to go");
+ options.addOption("m", "migrate", false,
+ "export for migration (remove handle and metadata that will be re-created in new system)");
options.addOption("n", "number", true,
- "sequence number to begin exporting items with");
+ "sequence number to begin exporting items with");
options.addOption("z", "zip", true, "export as zip file (specify filename e.g. export.zip)");
options.addOption("h", "help", false, "help");
@@ -86,175 +99,140 @@ public class ItemExportCLITool {
Item myItem = null;
Collection mycollection = null;
- if (line.hasOption('h'))
- {
+ if (line.hasOption('h')) {
HelpFormatter myhelp = new HelpFormatter();
myhelp.printHelp("ItemExport\n", options);
System.out
- .println("\nfull collection: ItemExport -t COLLECTION -i ID -d dest -n number");
+ .println("\nfull collection: ItemExport -t COLLECTION -i ID -d dest -n number");
System.out
- .println("singleitem: ItemExport -t ITEM -i ID -d dest -n number");
+ .println("singleitem: ItemExport -t ITEM -i ID -d dest -n number");
System.exit(0);
}
- if (line.hasOption('t')) // type
- {
+ if (line.hasOption('t')) { // type
typeString = line.getOptionValue('t');
- if ("ITEM".equals(typeString))
- {
+ if ("ITEM".equals(typeString)) {
myType = Constants.ITEM;
- }
- else if ("COLLECTION".equals(typeString))
- {
+ } else if ("COLLECTION".equals(typeString)) {
myType = Constants.COLLECTION;
}
}
- if (line.hasOption('i')) // id
- {
+ if (line.hasOption('i')) { // id
myIDString = line.getOptionValue('i');
}
- if (line.hasOption('d')) // dest
- {
+ if (line.hasOption('d')) { // dest
destDirName = line.getOptionValue('d');
}
- if (line.hasOption('n')) // number
- {
+ if (line.hasOption('n')) { // number
seqStart = Integer.parseInt(line.getOptionValue('n'));
}
boolean migrate = false;
- if (line.hasOption('m')) // number
- {
+ if (line.hasOption('m')) { // number
migrate = true;
}
boolean zip = false;
String zipFileName = "";
- if (line.hasOption('z'))
- {
+ if (line.hasOption('z')) {
zip = true;
zipFileName = line.getOptionValue('z');
}
boolean excludeBitstreams = false;
- if (line.hasOption('x'))
- {
- excludeBitstreams = true;
+ if (line.hasOption('x')) {
+ excludeBitstreams = true;
}
// now validate the args
- if (myType == -1)
- {
+ if (myType == -1) {
System.out
- .println("type must be either COLLECTION or ITEM (-h for help)");
+ .println("type must be either COLLECTION or ITEM (-h for help)");
System.exit(1);
}
- if (destDirName == null)
- {
+ if (destDirName == null) {
System.out
- .println("destination directory must be set (-h for help)");
+ .println("destination directory must be set (-h for help)");
System.exit(1);
}
- if (seqStart == -1)
- {
+ if (seqStart == -1) {
System.out
- .println("sequence start number must be set (-h for help)");
+ .println("sequence start number must be set (-h for help)");
System.exit(1);
}
- if (myIDString == null)
- {
+ if (myIDString == null) {
System.out
- .println("ID must be set to either a database ID or a handle (-h for help)");
+ .println("ID must be set to either a database ID or a handle (-h for help)");
System.exit(1);
}
Context c = new Context(Context.Mode.READ_ONLY);
c.turnOffAuthorisationSystem();
- if (myType == Constants.ITEM)
- {
+ if (myType == Constants.ITEM) {
// first, is myIDString a handle?
- if (myIDString.indexOf('/') != -1)
- {
+ if (myIDString.indexOf('/') != -1) {
myItem = (Item) handleService.resolveToObject(c, myIDString);
- if ((myItem == null) || (myItem.getType() != Constants.ITEM))
- {
+ if ((myItem == null) || (myItem.getType() != Constants.ITEM)) {
myItem = null;
}
- }
- else
- {
+ } else {
myItem = itemService.find(c, UUID.fromString(myIDString));
}
- if (myItem == null)
- {
+ if (myItem == null) {
System.out
- .println("Error, item cannot be found: " + myIDString);
+ .println("Error, item cannot be found: " + myIDString);
}
- }
- else
- {
- if (myIDString.indexOf('/') != -1)
- {
+ } else {
+ if (myIDString.indexOf('/') != -1) {
// has a / must be a handle
mycollection = (Collection) handleService.resolveToObject(c,
- myIDString);
+ myIDString);
// ensure it's a collection
if ((mycollection == null)
- || (mycollection.getType() != Constants.COLLECTION))
- {
+ || (mycollection.getType() != Constants.COLLECTION)) {
mycollection = null;
}
- }
- else if (myIDString != null)
- {
+ } else if (myIDString != null) {
mycollection = collectionService.find(c, UUID.fromString(myIDString));
}
- if (mycollection == null)
- {
+ if (mycollection == null) {
System.out.println("Error, collection cannot be found: "
- + myIDString);
+ + myIDString);
System.exit(1);
}
}
- if (zip)
- {
+ if (zip) {
Iterator- items;
- if (myItem != null)
- {
+ if (myItem != null) {
List
- myItems = new ArrayList<>();
myItems.add(myItem);
items = myItems.iterator();
- }
- else
- {
+ } else {
System.out.println("Exporting from collection: " + myIDString);
items = itemService.findByCollection(c, mycollection);
}
itemExportService.exportAsZip(c, items, destDirName, zipFileName, seqStart, migrate, excludeBitstreams);
- }
- else
- {
- if (myItem != null)
- {
+ } else {
+ if (myItem != null) {
// it's only a single item
- itemExportService.exportItem(c, Collections.singletonList(myItem).iterator(), destDirName, seqStart, migrate, excludeBitstreams);
- }
- else
- {
+ itemExportService
+ .exportItem(c, Collections.singletonList(myItem).iterator(), destDirName, seqStart, migrate,
+ excludeBitstreams);
+ } else {
System.out.println("Exporting from collection: " + myIDString);
// it's a collection, so do a bunch of items
diff --git a/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportException.java b/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportException.java
index 4032abad6a..4b1def6dee 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportException.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportException.java
@@ -10,20 +10,17 @@ package org.dspace.app.itemexport;
/**
* An exception that can be thrown when error occur during item export
*/
-public class ItemExportException extends Exception
-{
+public class ItemExportException extends Exception {
public static final int EXPORT_TOO_LARGE = 0;
private int reason;
- public ItemExportException(int r, String message)
- {
+ public ItemExportException(int r, String message) {
super(message);
reason = r;
}
- public int getReason()
- {
+ public int getReason() {
return reason;
}
-}
\ No newline at end of file
+}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportServiceImpl.java b/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportServiceImpl.java
index 7a8bbcfa38..170aa0c620 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportServiceImpl.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemexport/ItemExportServiceImpl.java
@@ -7,28 +7,58 @@
*/
package org.dspace.app.itemexport;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.sql.SQLException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import java.util.UUID;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+import javax.mail.MessagingException;
+
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dspace.app.itemexport.service.ItemExportService;
-import org.dspace.content.*;
+import org.dspace.content.Bitstream;
+import org.dspace.content.Bundle;
import org.dspace.content.Collection;
+import org.dspace.content.Community;
+import org.dspace.content.DSpaceObject;
+import org.dspace.content.Item;
+import org.dspace.content.MetadataField;
+import org.dspace.content.MetadataSchema;
+import org.dspace.content.MetadataValue;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService;
-import org.dspace.core.*;
+import org.dspace.core.ConfigurationManager;
+import org.dspace.core.Constants;
+import org.dspace.core.Context;
+import org.dspace.core.Email;
+import org.dspace.core.I18nUtil;
+import org.dspace.core.LogManager;
+import org.dspace.core.Utils;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.service.EPersonService;
import org.dspace.handle.service.HandleService;
import org.springframework.beans.factory.annotation.Autowired;
-import javax.mail.MessagingException;
-import java.io.*;
-import java.sql.SQLException;
-import java.text.SimpleDateFormat;
-import java.util.*;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipOutputStream;
-
/**
* Item exporter to create simple AIPs for DSpace content. Currently exports
* individual items, or entire collections. For instructions on use, see
@@ -50,8 +80,7 @@ import java.util.zip.ZipOutputStream;
* @author David Little
* @author Jay Paz
*/
-public class ItemExportServiceImpl implements ItemExportService
-{
+public class ItemExportServiceImpl implements ItemExportService {
protected final int SUBDIR_LIMIT = 0;
@Autowired(required = true)
@@ -66,20 +95,20 @@ public class ItemExportServiceImpl implements ItemExportService
protected HandleService handleService;
- /** log4j logger */
+ /**
+ * log4j logger
+ */
private Logger log = Logger.getLogger(ItemExportServiceImpl.class);
- protected ItemExportServiceImpl()
- {
+ protected ItemExportServiceImpl() {
}
@Override
public void exportItem(Context c, Iterator
- i,
- String destDirName, int seqStart, boolean migrate,
- boolean excludeBitstreams) throws Exception
- {
+ String destDirName, int seqStart, boolean migrate,
+ boolean excludeBitstreams) throws Exception {
int mySequenceNumber = seqStart;
int counter = SUBDIR_LIMIT - 1;
int subDirSuffix = 0;
@@ -87,27 +116,22 @@ public class ItemExportServiceImpl implements ItemExportService
String subdir = "";
File dir;
- if (SUBDIR_LIMIT > 0)
- {
+ if (SUBDIR_LIMIT > 0) {
dir = new File(destDirName);
- if (!dir.isDirectory())
- {
+ if (!dir.isDirectory()) {
throw new IOException(destDirName + " is not a directory.");
}
}
System.out.println("Beginning export");
- while (i.hasNext())
- {
- if (SUBDIR_LIMIT > 0 && ++counter == SUBDIR_LIMIT)
- {
+ while (i.hasNext()) {
+ if (SUBDIR_LIMIT > 0 && ++counter == SUBDIR_LIMIT) {
subdir = Integer.valueOf(subDirSuffix++).toString();
fullPath = destDirName + File.separatorChar + subdir;
counter = 0;
- if (!new File(fullPath).mkdirs())
- {
+ if (!new File(fullPath).mkdirs()) {
throw new IOException("Error, can't make dir " + fullPath);
}
}
@@ -121,44 +145,35 @@ public class ItemExportServiceImpl implements ItemExportService
}
protected void exportItem(Context c, Item myItem, String destDirName,
- int seqStart, boolean migrate, boolean excludeBitstreams) throws Exception
- {
+ int seqStart, boolean migrate, boolean excludeBitstreams) throws Exception {
File destDir = new File(destDirName);
- if (destDir.exists())
- {
+ if (destDir.exists()) {
// now create a subdirectory
File itemDir = new File(destDir + "/" + seqStart);
-
- System.out.println("Exporting Item " + myItem.getID() +
- (myItem.getHandle() != null ? ", handle " + myItem.getHandle() : "") +
- " to " + itemDir);
- if (itemDir.exists())
- {
+ System.out.println("Exporting Item " + myItem.getID() +
+ (myItem.getHandle() != null ? ", handle " + myItem.getHandle() : "") +
+ " to " + itemDir);
+
+ if (itemDir.exists()) {
throw new Exception("Directory " + destDir + "/" + seqStart
- + " already exists!");
+ + " already exists!");
}
- if (itemDir.mkdir())
- {
+ if (itemDir.mkdir()) {
// make it this far, now start exporting
writeMetadata(c, myItem, itemDir, migrate);
writeBitstreams(c, myItem, itemDir, excludeBitstreams);
- if (!migrate)
- {
+ if (!migrate) {
writeHandle(c, myItem, itemDir);
}
- }
- else
- {
+ } else {
throw new Exception("Error, can't make dir " + itemDir);
}
- }
- else
- {
+ } else {
throw new Exception("Error, directory " + destDirName
- + " doesn't exist!");
+ + " doesn't exist!");
}
}
@@ -166,48 +181,42 @@ public class ItemExportServiceImpl implements ItemExportService
* Discover the different schemas in use and output a separate metadata XML
* file for each schema.
*
- * @param c DSpace context
- * @param i DSpace Item
+ * @param c DSpace context
+ * @param i DSpace Item
* @param destDir destination directory
* @param migrate Whether to use the migrate option or not
* @throws Exception if error
*/
protected void writeMetadata(Context c, Item i, File destDir, boolean migrate)
- throws Exception
- {
+ throws Exception {
Set
schemas = new HashSet();
List dcValues = itemService.getMetadata(i, Item.ANY, Item.ANY, Item.ANY, Item.ANY);
- for (MetadataValue metadataValue : dcValues)
- {
+ for (MetadataValue metadataValue : dcValues) {
schemas.add(metadataValue.getMetadataField().getMetadataSchema().getName());
}
// Save each of the schemas into it's own metadata file
- for (String schema : schemas)
- {
+ for (String schema : schemas) {
writeMetadata(c, schema, i, destDir, migrate);
}
}
/**
* output the item's dublin core into the item directory
- * @param c DSpace context
- * @param schema schema
- * @param i DSpace Item
+ *
+ * @param c DSpace context
+ * @param schema schema
+ * @param i DSpace Item
* @param destDir destination directory
* @param migrate Whether to use the migrate option or not
* @throws Exception if error
*/
protected void writeMetadata(Context c, String schema, Item i,
- File destDir, boolean migrate) throws Exception
- {
+ File destDir, boolean migrate) throws Exception {
String filename;
- if (schema.equals(MetadataSchema.DC_SCHEMA))
- {
+ if (schema.equals(MetadataSchema.DC_SCHEMA)) {
filename = "dublin_core.xml";
- }
- else
- {
+ } else {
filename = "metadata_" + schema + ".xml";
}
@@ -215,17 +224,16 @@ public class ItemExportServiceImpl implements ItemExportService
System.out.println("Attempting to create file " + outFile);
- if (outFile.createNewFile())
- {
+ if (outFile.createNewFile()) {
BufferedOutputStream out = new BufferedOutputStream(
- new FileOutputStream(outFile));
+ new FileOutputStream(outFile));
List dcorevalues = itemService.getMetadata(i, schema, Item.ANY, Item.ANY,
- Item.ANY);
+ Item.ANY);
// XML preamble
byte[] utf8 = "\n"
- .getBytes("UTF-8");
+ .getBytes("UTF-8");
out.write(utf8, 0, utf8.length);
String dcTag = "\n";
@@ -235,56 +243,48 @@ public class ItemExportServiceImpl implements ItemExportService
String dateIssued = null;
String dateAccessioned = null;
- for (MetadataValue dcv : dcorevalues)
- {
+ for (MetadataValue dcv : dcorevalues) {
MetadataField metadataField = dcv.getMetadataField();
String qualifier = metadataField.getQualifier();
- if (qualifier == null)
- {
+ if (qualifier == null) {
qualifier = "none";
}
String language = dcv.getLanguage();
- if (language != null)
- {
+ if (language != null) {
language = " language=\"" + language + "\"";
- }
- else
- {
+ } else {
language = "";
}
utf8 = (" "
- + Utils.addEntities(dcv.getValue()) + " \n")
- .getBytes("UTF-8");
+ + "qualifier=\"" + qualifier + "\""
+ + language + ">"
+ + Utils.addEntities(dcv.getValue()) + "\n")
+ .getBytes("UTF-8");
if ((!migrate) ||
(migrate && !(
- ("date".equals(metadataField.getElement()) && "issued".equals(qualifier)) ||
- ("date".equals(metadataField.getElement()) && "accessioned".equals(qualifier)) ||
- ("date".equals(metadataField.getElement()) && "available".equals(qualifier)) ||
- ("identifier".equals(metadataField.getElement()) && "uri".equals(qualifier) &&
- (dcv.getValue() != null && dcv.getValue().startsWith("http://hdl.handle.net/" +
- handleService.getPrefix() + "/"))) ||
- ("description".equals(metadataField.getElement()) && "provenance".equals(qualifier)) ||
- ("format".equals(metadataField.getElement()) && "extent".equals(qualifier)) ||
- ("format".equals(metadataField.getElement()) && "mimetype".equals(qualifier)))))
- {
+ ("date".equals(metadataField.getElement()) && "issued".equals(qualifier)) ||
+ ("date".equals(metadataField.getElement()) && "accessioned".equals(qualifier)) ||
+ ("date".equals(metadataField.getElement()) && "available".equals(qualifier)) ||
+ ("identifier".equals(metadataField.getElement()) && "uri".equals(qualifier) &&
+ (dcv.getValue() != null && dcv.getValue().startsWith(
+ handleService.getCanonicalPrefix() + handleService.getPrefix() + "/"))) ||
+ ("description".equals(metadataField.getElement()) && "provenance".equals(qualifier)) ||
+ ("format".equals(metadataField.getElement()) && "extent".equals(qualifier)) ||
+ ("format".equals(metadataField.getElement()) && "mimetype".equals(qualifier))))) {
out.write(utf8, 0, utf8.length);
}
// Store the date issued and accession to see if they are different
// because we need to keep date.issued if they are, when migrating
- if (("date".equals(metadataField.getElement()) && "issued".equals(qualifier)))
- {
+ if (("date".equals(metadataField.getElement()) && "issued".equals(qualifier))) {
dateIssued = dcv.getValue();
}
- if (("date".equals(metadataField.getElement()) && "accessioned".equals(qualifier)))
- {
+ if (("date".equals(metadataField.getElement()) && "accessioned".equals(qualifier))) {
dateAccessioned = dcv.getValue();
}
}
@@ -293,12 +293,11 @@ public class ItemExportServiceImpl implements ItemExportService
if ((migrate) &&
(dateIssued != null) &&
(dateAccessioned != null) &&
- (!dateIssued.equals(dateAccessioned)))
- {
+ (!dateIssued.equals(dateAccessioned))) {
utf8 = (" "
- + Utils.addEntities(dateIssued) + " \n")
- .getBytes("UTF-8");
+ + "qualifier=\"issued\">"
+ + Utils.addEntities(dateIssued) + "\n")
+ .getBytes("UTF-8");
out.write(utf8, 0, utf8.length);
}
@@ -306,44 +305,38 @@ public class ItemExportServiceImpl implements ItemExportService
out.write(utf8, 0, utf8.length);
out.close();
- }
- else
- {
+ } else {
throw new Exception("Cannot create dublin_core.xml in " + destDir);
}
}
/**
* create the file 'handle' which contains the handle assigned to the item
- * @param c DSpace Context
- * @param i DSpace Item
+ *
+ * @param c DSpace Context
+ * @param i DSpace Item
* @param destDir destination directory
* @throws Exception if error
*/
protected void writeHandle(Context c, Item i, File destDir)
- throws Exception
- {
- if (i.getHandle() == null)
- {
+ throws Exception {
+ if (i.getHandle() == null) {
return;
}
String filename = "handle";
File outFile = new File(destDir, filename);
- if (outFile.createNewFile())
- {
+ if (outFile.createNewFile()) {
PrintWriter out = new PrintWriter(new FileWriter(outFile));
out.println(i.getHandle());
// close the contents file
out.close();
- }
- else
- {
+ } else {
throw new Exception("Cannot create file " + filename + " in "
- + destDir);
+ + destDir);
}
}
@@ -353,24 +346,18 @@ public class ItemExportServiceImpl implements ItemExportService
* However, the export directory will contain actual copies of the content
* files being exported.
*
- * @param c
- * the DSpace context
- * @param i
- * the item being exported
- * @param destDir
- * the item's export directory
- * @param excludeBitstreams
- * whether to exclude bitstreams
+ * @param c the DSpace context
+ * @param i the item being exported
+ * @param destDir the item's export directory
+ * @param excludeBitstreams whether to exclude bitstreams
* @throws Exception if error
- * if there is any problem writing to the export directory
+ * if there is any problem writing to the export directory
*/
protected void writeBitstreams(Context c, Item i, File destDir,
- boolean excludeBitstreams) throws Exception
- {
+ boolean excludeBitstreams) throws Exception {
File outFile = new File(destDir, "contents");
- if (outFile.createNewFile())
- {
+ if (outFile.createNewFile()) {
PrintWriter out = new PrintWriter(new FileWriter(outFile));
List bundles = i.getBundles();
@@ -405,9 +392,9 @@ public class ItemExportServiceImpl implements ItemExportService
while (!excludeBitstreams && !isDone) {
if (myName.contains(File.separator)) {
String dirs = myName.substring(0, myName
- .lastIndexOf(File.separator));
+ .lastIndexOf(File.separator));
File fdirs = new File(destDir + File.separator
- + dirs);
+ + dirs);
if (!fdirs.exists() && !fdirs.mkdirs()) {
log.error("Unable to create destination directory");
}
@@ -416,7 +403,7 @@ public class ItemExportServiceImpl implements ItemExportService
File fout = new File(destDir, myName);
if (fout.createNewFile()) {
- InputStream is = bitstreamService.retrieve(c, bitstream);
+ InputStream is = bitstreamService.retrieve(c, bitstream);
FileOutputStream fos = new FileOutputStream(fout);
Utils.bufferedCopy(is, fos);
// close streams
@@ -433,50 +420,44 @@ public class ItemExportServiceImpl implements ItemExportService
myPrefix++;
}
}
-
+
// write the manifest file entry
if (bitstreamService.isRegisteredBitstream(bitstream)) {
out.println("-r -s " + bitstream.getStoreNumber()
- + " -f " + myName +
- "\tbundle:" + bundleName +
- primary + description);
+ + " -f " + myName +
+ "\tbundle:" + bundleName +
+ primary + description);
} else {
out.println(myName + "\tbundle:" + bundleName +
- primary + description);
+ primary + description);
}
-
+
}
}
// close the contents file
out.close();
- }
- else
- {
+ } else {
throw new Exception("Cannot create contents in " + destDir);
}
}
@Override
public void exportAsZip(Context context, Iterator- items,
- String destDirName, String zipFileName,
- int seqStart, boolean migrate,
- boolean excludeBitstreams) throws Exception
-
- {
+ String destDirName, String zipFileName,
+ int seqStart, boolean migrate,
+ boolean excludeBitstreams) throws Exception {
String workDir = getExportWorkDirectory() +
- System.getProperty("file.separator") +
- zipFileName;
+ System.getProperty("file.separator") +
+ zipFileName;
File wkDir = new File(workDir);
- if (!wkDir.exists() && !wkDir.mkdirs())
- {
+ if (!wkDir.exists() && !wkDir.mkdirs()) {
log.error("Unable to create working direcory");
}
File dnDir = new File(destDirName);
- if (!dnDir.exists() && !dnDir.mkdirs())
- {
+ if (!dnDir.exists() && !dnDir.mkdirs()) {
log.error("Unable to create destination directory");
}
@@ -489,28 +470,25 @@ public class ItemExportServiceImpl implements ItemExportService
@Override
public void createDownloadableExport(DSpaceObject dso,
- Context context, boolean migrate) throws Exception
- {
+ Context context, boolean migrate) throws Exception {
EPerson eperson = context.getCurrentUser();
ArrayList
list = new ArrayList(1);
list.add(dso);
processDownloadableExport(list, context, eperson == null ? null
- : eperson.getEmail(), migrate);
+ : eperson.getEmail(), migrate);
}
@Override
public void createDownloadableExport(List dsObjects,
- Context context, boolean migrate) throws Exception
- {
+ Context context, boolean migrate) throws Exception {
EPerson eperson = context.getCurrentUser();
processDownloadableExport(dsObjects, context, eperson == null ? null
- : eperson.getEmail(), migrate);
+ : eperson.getEmail(), migrate);
}
@Override
public void createDownloadableExport(DSpaceObject dso,
- Context context, String additionalEmail, boolean migrate) throws Exception
- {
+ Context context, String additionalEmail, boolean migrate) throws Exception {
ArrayList list = new ArrayList(1);
list.add(dso);
processDownloadableExport(list, context, additionalEmail, migrate);
@@ -518,8 +496,7 @@ public class ItemExportServiceImpl implements ItemExportService
@Override
public void createDownloadableExport(List dsObjects,
- Context context, String additionalEmail, boolean migrate) throws Exception
- {
+ Context context, String additionalEmail, boolean migrate) throws Exception {
processDownloadableExport(dsObjects, context, additionalEmail, migrate);
}
@@ -528,18 +505,15 @@ public class ItemExportServiceImpl implements ItemExportService
* Collection It then kicks off a new Thread to export the items, zip the
* export directory and send confirmation email
*
- * @param dsObjects
- * - List of dspace objects to process
- * @param context
- * - the dspace context
- * @param additionalEmail
- * - email address to cc in addition the the current user email
- * @param toMigrate Whether to use the migrate option or not
+ * @param dsObjects - List of dspace objects to process
+ * @param context - the dspace context
+ * @param additionalEmail - email address to cc in addition the the current user email
+ * @param toMigrate Whether to use the migrate option or not
* @throws Exception if error
*/
protected void processDownloadableExport(List dsObjects,
- Context context, final String additionalEmail, boolean toMigrate) throws Exception
- {
+ Context context, final String additionalEmail, boolean toMigrate)
+ throws Exception {
final EPerson eperson = context.getCurrentUser();
final boolean migrate = toMigrate;
@@ -553,103 +527,78 @@ public class ItemExportServiceImpl implements ItemExportService
// it will be checked against the config file entry
double size = 0;
final HashMap> itemsMap = new HashMap<>();
- for (DSpaceObject dso : dsObjects)
- {
- if (dso.getType() == Constants.COMMUNITY)
- {
+ for (DSpaceObject dso : dsObjects) {
+ if (dso.getType() == Constants.COMMUNITY) {
Community community = (Community) dso;
// get all the collections in the community
List collections = communityService.getAllCollections(context, community);
- for (Collection collection : collections)
- {
+ for (Collection collection : collections) {
ArrayList items = new ArrayList<>();
// get all the items in each collection
Iterator- iitems = itemService.findByCollection(context, collection);
- try
- {
- while (iitems.hasNext())
- {
+ try {
+ while (iitems.hasNext()) {
Item item = iitems.next();
// get all the bundles in the item
List
bundles = item.getBundles();
- for (Bundle bundle : bundles)
- {
+ for (Bundle bundle : bundles) {
// get all the bitstreams in each bundle
List bitstreams = bundle.getBitstreams();
- for (Bitstream bitstream : bitstreams)
- {
+ for (Bitstream bitstream : bitstreams) {
// add up the size
- size += bitstream.getSize();
+ size += bitstream.getSizeBytes();
}
}
items.add(item.getID());
}
- }
- finally
- {
- if (items.size() > 0)
- {
- itemsMap.put("collection_"+collection.getID(), items);
+ } finally {
+ if (items.size() > 0) {
+ itemsMap.put("collection_" + collection.getID(), items);
}
}
}
- }
- else if (dso.getType() == Constants.COLLECTION)
- {
+ } else if (dso.getType() == Constants.COLLECTION) {
Collection collection = (Collection) dso;
ArrayList items = new ArrayList<>();
// get all the items in the collection
Iterator- iitems = itemService.findByCollection(context, collection);
- try
- {
- while (iitems.hasNext())
- {
+ try {
+ while (iitems.hasNext()) {
Item item = iitems.next();
// get all thebundles in the item
List
bundles = item.getBundles();
- for (Bundle bundle : bundles)
- {
+ for (Bundle bundle : bundles) {
// get all the bitstreams in the bundle
List bitstreams = bundle.getBitstreams();
- for (Bitstream bitstream : bitstreams)
- {
+ for (Bitstream bitstream : bitstreams) {
// add up the size
- size += bitstream.getSize();
+ size += bitstream.getSizeBytes();
}
}
items.add(item.getID());
}
- }
- finally
- {
- if (items.size() > 0)
- {
- itemsMap.put("collection_"+collection.getID(), items);
+ } finally {
+ if (items.size() > 0) {
+ itemsMap.put("collection_" + collection.getID(), items);
}
}
- }
- else if (dso.getType() == Constants.ITEM)
- {
+ } else if (dso.getType() == Constants.ITEM) {
Item item = (Item) dso;
// get all the bundles in the item
List bundles = item.getBundles();
- for (Bundle bundle : bundles)
- {
+ for (Bundle bundle : bundles) {
// get all the bitstreams in the bundle
List bitstreams = bundle.getBitstreams();
- for (Bitstream bitstream : bitstreams)
- {
+ for (Bitstream bitstream : bitstreams) {
// add up the size
- size += bitstream.getSize();
+ size += bitstream.getSizeBytes();
}
}
ArrayList items = new ArrayList<>();
items.add(item.getID());
- itemsMap.put("item_"+item.getID(), items);
- }
- else
- {
+ itemsMap.put("item_" + item.getID(), items);
+ } else {
// nothing to do just ignore this type of DSpaceObject
}
}
@@ -657,58 +606,48 @@ public class ItemExportServiceImpl implements ItemExportService
// check the size of all the bitstreams against the configuration file
// entry if it exists
String megaBytes = ConfigurationManager
- .getProperty("org.dspace.app.itemexport.max.size");
- if (megaBytes != null)
- {
+ .getProperty("org.dspace.app.itemexport.max.size");
+ if (megaBytes != null) {
float maxSize = 0;
- try
- {
+ try {
maxSize = Float.parseFloat(megaBytes);
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
// ignore...configuration entry may not be present
}
- if (maxSize > 0 && maxSize < (size / 1048576.00))
- { // a megabyte
+ if (maxSize > 0 && maxSize < (size / 1048576.00)) { // a megabyte
throw new ItemExportException(ItemExportException.EXPORT_TOO_LARGE,
- "The overall size of this export is too large. Please contact your administrator for more information.");
+ "The overall size of this export is too large. Please contact your " +
+ "administrator for more information.");
}
}
// if we have any items to process then kick off anonymous thread
- if (itemsMap.size() > 0)
- {
- Thread go = new Thread()
- {
+ if (itemsMap.size() > 0) {
+ Thread go = new Thread() {
@Override
- public void run()
- {
+ public void run() {
Context context = null;
Iterator- iitems = null;
- try
- {
+ try {
// create a new dspace context
context = new Context();
// ignore auths
context.turnOffAuthorisationSystem();
String fileName = assembleFileName("item", eperson,
- new Date());
+ new Date());
String workParentDir = getExportWorkDirectory()
- + System.getProperty("file.separator")
- + fileName;
+ + System.getProperty("file.separator")
+ + fileName;
String downloadDir = getExportDownloadDirectory(eperson);
File dnDir = new File(downloadDir);
- if (!dnDir.exists() && !dnDir.mkdirs())
- {
+ if (!dnDir.exists() && !dnDir.mkdirs()) {
log.error("Unable to create download directory");
}
Iterator
iter = itemsMap.keySet().iterator();
- while(iter.hasNext())
- {
+ while (iter.hasNext()) {
String keyName = iter.next();
List uuids = itemsMap.get(keyName);
List- items = new ArrayList
- ();
@@ -717,13 +656,12 @@ public class ItemExportServiceImpl implements ItemExportService
}
iitems = items.iterator();
- String workDir = workParentDir
- + System.getProperty("file.separator")
- + keyName;
+ String workDir = workParentDir
+ + System.getProperty("file.separator")
+ + keyName;
File wkDir = new File(workDir);
- if (!wkDir.exists() && !wkDir.mkdirs())
- {
+ if (!wkDir.exists() && !wkDir.mkdirs()) {
log.error("Unable to create working directory");
}
@@ -734,34 +672,27 @@ public class ItemExportServiceImpl implements ItemExportService
// now zip up the export directory created above
zip(workParentDir, downloadDir
- + System.getProperty("file.separator")
- + fileName + ".zip");
+ + System.getProperty("file.separator")
+ + fileName + ".zip");
// email message letting user know the file is ready for
// download
emailSuccessMessage(context, eperson, fileName + ".zip");
// return to enforcing auths
context.restoreAuthSystemState();
- }
- catch (Exception e1)
- {
- try
- {
+ } catch (Exception e1) {
+ try {
emailErrorMessage(eperson, e1.getMessage());
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
// wont throw here
}
throw new IllegalStateException(e1);
- }
- finally
- {
+ } finally {
// Make sure the database connection gets closed in all conditions.
- try {
- context.complete();
- } catch (SQLException sqle) {
- context.abort();
- }
+ try {
+ context.complete();
+ } catch (SQLException sqle) {
+ context.abort();
+ }
}
}
@@ -769,9 +700,7 @@ public class ItemExportServiceImpl implements ItemExportService
go.isDaemon();
go.start();
- }
- else
- {
+ } else {
Locale supportedLocale = I18nUtil.getEPersonLocale(eperson);
emailErrorMessage(eperson, I18nUtil.getMessage("org.dspace.app.itemexport.no-result", supportedLocale));
}
@@ -779,8 +708,7 @@ public class ItemExportServiceImpl implements ItemExportService
@Override
public String assembleFileName(String type, EPerson eperson,
- Date date) throws Exception
- {
+ Date date) throws Exception {
// to format the date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MMM_dd");
String downloadDir = getExportDownloadDirectory(eperson);
@@ -788,13 +716,12 @@ public class ItemExportServiceImpl implements ItemExportService
int count = 1;
boolean exists = true;
String fileName = null;
- while (exists)
- {
+ while (exists) {
fileName = type + "_export_" + sdf.format(date) + "_" + count + "_"
- + eperson.getID();
+ + eperson.getID();
exists = new File(downloadDir
- + System.getProperty("file.separator") + fileName + ".zip")
- .exists();
+ + System.getProperty("file.separator") + fileName + ".zip")
+ .exists();
count++;
}
return fileName;
@@ -802,18 +729,15 @@ public class ItemExportServiceImpl implements ItemExportService
@Override
public String getExportDownloadDirectory(EPerson ePerson)
- throws Exception
- {
+ throws Exception {
String downloadDir = ConfigurationManager
- .getProperty("org.dspace.app.itemexport.download.dir");
- if (downloadDir == null)
- {
+ .getProperty("org.dspace.app.itemexport.download.dir");
+ if (downloadDir == null) {
throw new Exception(
- "A dspace.cfg entry for 'org.dspace.app.itemexport.download.dir' does not exist.");
+ "A dspace.cfg entry for 'org.dspace.app.itemexport.download.dir' does not exist.");
}
File result = new File(downloadDir + System.getProperty("file.separator") + ePerson.getID());
- if(!result.exists() && ePerson.getLegacyId()!=null)
- {
+ if (!result.exists() && ePerson.getLegacyId() != null) {
//Check for the old identifier
result = new File(downloadDir + System.getProperty("file.separator") + ePerson.getLegacyId());
}
@@ -822,51 +746,43 @@ public class ItemExportServiceImpl implements ItemExportService
}
@Override
- public String getExportWorkDirectory() throws Exception
- {
+ public String getExportWorkDirectory() throws Exception {
String exportDir = ConfigurationManager
- .getProperty("org.dspace.app.itemexport.work.dir");
- if (exportDir == null)
- {
+ .getProperty("org.dspace.app.itemexport.work.dir");
+ if (exportDir == null) {
throw new Exception(
- "A dspace.cfg entry for 'org.dspace.app.itemexport.work.dir' does not exist.");
+ "A dspace.cfg entry for 'org.dspace.app.itemexport.work.dir' does not exist.");
}
return exportDir;
}
@Override
public InputStream getExportDownloadInputStream(String fileName,
- EPerson eperson) throws Exception
- {
+ EPerson eperson) throws Exception {
File file = new File(getExportDownloadDirectory(eperson)
- + System.getProperty("file.separator") + fileName);
- if (file.exists())
- {
+ + System.getProperty("file.separator") + fileName);
+ if (file.exists()) {
return new FileInputStream(file);
- }
- else
- {
+ } else {
return null;
}
}
@Override
- public long getExportFileSize(Context context, String fileName) throws Exception
- {
+ public long getExportFileSize(Context context, String fileName) throws Exception {
String strID = fileName.substring(fileName.lastIndexOf('_') + 1,
- fileName.lastIndexOf('.'));
+ fileName.lastIndexOf('.'));
EPerson ePerson = getEPersonFromString(context, strID);
File file = new File(
- getExportDownloadDirectory(ePerson)
- + System.getProperty("file.separator") + fileName);
- if (!file.exists() || !file.isFile())
- {
+ getExportDownloadDirectory(ePerson)
+ + System.getProperty("file.separator") + fileName);
+ if (!file.exists() || !file.isFile()) {
throw new FileNotFoundException("The file "
- + getExportDownloadDirectory(ePerson)
- + System.getProperty("file.separator") + fileName
- + " does not exist.");
+ + getExportDownloadDirectory(ePerson)
+ + System.getProperty("file.separator") + fileName
+ + " does not exist.");
}
return file.length();
@@ -874,17 +790,18 @@ public class ItemExportServiceImpl implements ItemExportService
/**
* Attempt to find an EPerson based on string ID
+ *
* @param context DSpace context
- * @param strID string identifier
+ * @param strID string identifier
* @return EPerson object (if found)
* @throws SQLException if database error
*/
protected EPerson getEPersonFromString(Context context, String strID) throws SQLException {
EPerson eperson;
- try{
+ try {
UUID ePersonId = UUID.fromString(strID);
eperson = ePersonService.find(context, ePersonId);
- }catch (Exception e){
+ } catch (Exception e) {
eperson = ePersonService.findByLegacyId(context, Integer.parseInt(strID));
}
return eperson;
@@ -892,45 +809,37 @@ public class ItemExportServiceImpl implements ItemExportService
@Override
public long getExportFileLastModified(Context context, String fileName)
- throws Exception
- {
+ throws Exception {
String strID = fileName.substring(fileName.lastIndexOf('_') + 1,
- fileName.lastIndexOf('.'));
+ fileName.lastIndexOf('.'));
EPerson ePerson = getEPersonFromString(context, strID);
File file = new File(
- getExportDownloadDirectory(ePerson)
- + System.getProperty("file.separator") + fileName);
- if (!file.exists() || !file.isFile())
- {
+ getExportDownloadDirectory(ePerson)
+ + System.getProperty("file.separator") + fileName);
+ if (!file.exists() || !file.isFile()) {
throw new FileNotFoundException("The file "
- + getExportDownloadDirectory(ePerson)
- + System.getProperty("file.separator") + fileName
- + " does not exist.");
+ + getExportDownloadDirectory(ePerson)
+ + System.getProperty("file.separator") + fileName
+ + " does not exist.");
}
return file.lastModified();
}
@Override
- public boolean canDownload(Context context, String fileName)
- {
+ public boolean canDownload(Context context, String fileName) {
EPerson eperson = context.getCurrentUser();
- if (eperson == null)
- {
+ if (eperson == null) {
return false;
}
String strID = fileName.substring(fileName.lastIndexOf('_') + 1,
- fileName.lastIndexOf('.'));
- try
- {
- if (strID.equals(eperson.getID().toString()))
- {
+ fileName.lastIndexOf('.'));
+ try {
+ if (strID.equals(eperson.getID().toString())) {
return true;
}
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
return false;
}
return false;
@@ -938,26 +847,21 @@ public class ItemExportServiceImpl implements ItemExportService
@Override
public List
getExportsAvailable(EPerson eperson)
- throws Exception
- {
+ throws Exception {
File downloadDir = new File(getExportDownloadDirectory(eperson));
- if (!downloadDir.exists() || !downloadDir.isDirectory())
- {
+ if (!downloadDir.exists() || !downloadDir.isDirectory()) {
return null;
}
List fileNames = new ArrayList();
- for (String fileName : downloadDir.list())
- {
- if (fileName.contains("export") && fileName.endsWith(".zip"))
- {
+ for (String fileName : downloadDir.list()) {
+ if (fileName.contains("export") && fileName.endsWith(".zip")) {
fileNames.add(fileName);
}
}
- if (fileNames.size() > 0)
- {
+ if (fileNames.size() > 0) {
return fileNames;
}
@@ -965,23 +869,18 @@ public class ItemExportServiceImpl implements ItemExportService
}
@Override
- public void deleteOldExportArchives(EPerson eperson) throws Exception
- {
+ public void deleteOldExportArchives(EPerson eperson) throws Exception {
int hours = ConfigurationManager
- .getIntProperty("org.dspace.app.itemexport.life.span.hours");
+ .getIntProperty("org.dspace.app.itemexport.life.span.hours");
Calendar now = Calendar.getInstance();
now.setTime(new Date());
now.add(Calendar.HOUR, (-hours));
File downloadDir = new File(getExportDownloadDirectory(eperson));
- if (downloadDir.exists())
- {
+ if (downloadDir.exists()) {
File[] files = downloadDir.listFiles();
- for (File file : files)
- {
- if (file.lastModified() < now.getTimeInMillis())
- {
- if (!file.delete())
- {
+ for (File file : files) {
+ if (file.lastModified() < now.getTimeInMillis()) {
+ if (!file.delete()) {
log.error("Unable to delete export file");
}
}
@@ -991,37 +890,29 @@ public class ItemExportServiceImpl implements ItemExportService
}
@Override
- public void deleteOldExportArchives() throws Exception
- {
+ public void deleteOldExportArchives() throws Exception {
int hours = ConfigurationManager.getIntProperty("org.dspace.app.itemexport.life.span.hours");
Calendar now = Calendar.getInstance();
now.setTime(new Date());
now.add(Calendar.HOUR, (-hours));
File downloadDir = new File(ConfigurationManager.getProperty("org.dspace.app.itemexport.download.dir"));
- if (downloadDir.exists())
- {
+ if (downloadDir.exists()) {
// Get a list of all the sub-directories, potentially one for each ePerson.
File[] dirs = downloadDir.listFiles();
- for (File dir : dirs)
- {
+ for (File dir : dirs) {
// For each sub-directory delete any old files.
File[] files = dir.listFiles();
- for (File file : files)
- {
- if (file.lastModified() < now.getTimeInMillis())
- {
- if (!file.delete())
- {
+ for (File file : files) {
+ if (file.lastModified() < now.getTimeInMillis()) {
+ if (!file.delete()) {
log.error("Unable to delete old files");
}
}
}
// If the directory is now empty then we delete it too.
- if (dir.listFiles().length == 0)
- {
- if (!dir.delete())
- {
+ if (dir.listFiles().length == 0) {
+ if (!dir.delete()) {
log.error("Unable to delete directory");
}
}
@@ -1033,10 +924,8 @@ public class ItemExportServiceImpl implements ItemExportService
@Override
public void emailSuccessMessage(Context context, EPerson eperson,
- String fileName) throws MessagingException
- {
- try
- {
+ String fileName) throws MessagingException {
+ try {
Locale supportedLocale = I18nUtil.getEPersonLocale(eperson);
Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "export_success"));
email.addRecipient(eperson.getEmail());
@@ -1044,20 +933,16 @@ public class ItemExportServiceImpl implements ItemExportService
email.addArgument(ConfigurationManager.getProperty("org.dspace.app.itemexport.life.span.hours"));
email.send();
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
log.warn(LogManager.getHeader(context, "emailSuccessMessage", "cannot notify user of export"), e);
}
}
@Override
public void emailErrorMessage(EPerson eperson, String error)
- throws MessagingException
- {
+ throws MessagingException {
log.warn("An error occurred during item export, the user will be notified. " + error);
- try
- {
+ try {
Locale supportedLocale = I18nUtil.getEPersonLocale(eperson);
Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "export_error"));
email.addRecipient(eperson.getEmail());
@@ -1065,28 +950,22 @@ public class ItemExportServiceImpl implements ItemExportService
email.addArgument(ConfigurationManager.getProperty("dspace.url") + "/feedback");
email.send();
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
log.warn("error during item export error notification", e);
}
}
@Override
- public void zip(String strSource, String target) throws Exception
- {
+ public void zip(String strSource, String target) throws Exception {
ZipOutputStream cpZipOutputStream = null;
String tempFileName = target + "_tmp";
- try
- {
+ try {
File cpFile = new File(strSource);
- if (!cpFile.isFile() && !cpFile.isDirectory())
- {
+ if (!cpFile.isFile() && !cpFile.isDirectory()) {
return;
}
File targetFile = new File(tempFileName);
- if (!targetFile.createNewFile())
- {
+ if (!targetFile.createNewFile()) {
log.warn("Target file already exists: " + targetFile.getName());
}
@@ -1102,53 +981,42 @@ public class ItemExportServiceImpl implements ItemExportService
System.gc();
deleteDirectory(cpFile);
- if (!targetFile.renameTo(new File(target)))
- {
+ if (!targetFile.renameTo(new File(target))) {
log.error("Unable to rename file");
}
- }
- finally
- {
- if (cpZipOutputStream != null)
- {
+ } finally {
+ if (cpZipOutputStream != null) {
cpZipOutputStream.close();
}
}
}
/**
- *
- * @param cpFile file
- * @param strSource source location
- * @param strTarget target location
+ * @param cpFile file
+ * @param strSource source location
+ * @param strTarget target location
* @param cpZipOutputStream current zip outputstream
* @throws Exception if error
*/
protected void zipFiles(File cpFile, String strSource,
- String strTarget, ZipOutputStream cpZipOutputStream)
- throws Exception
- {
+ String strTarget, ZipOutputStream cpZipOutputStream)
+ throws Exception {
int byteCount;
final int DATA_BLOCK_SIZE = 2048;
FileInputStream cpFileInputStream = null;
- if (cpFile.isDirectory())
- {
+ if (cpFile.isDirectory()) {
File[] fList = cpFile.listFiles();
for (File aFList : fList) {
zipFiles(aFList, strSource, strTarget, cpZipOutputStream);
}
- }
- else
- {
- try
- {
- if (cpFile.getAbsolutePath().equalsIgnoreCase(strTarget))
- {
+ } else {
+ try {
+ if (cpFile.getAbsolutePath().equalsIgnoreCase(strTarget)) {
return;
}
String strAbsPath = cpFile.getPath();
String strZipEntryName = strAbsPath.substring(strSource
- .length() + 1, strAbsPath.length());
+ .length() + 1, strAbsPath.length());
// byte[] b = new byte[ (int)(cpFile.length()) ];
@@ -1159,17 +1027,13 @@ public class ItemExportServiceImpl implements ItemExportService
byte[] b = new byte[DATA_BLOCK_SIZE];
while ((byteCount = cpFileInputStream.read(b, 0,
- DATA_BLOCK_SIZE)) != -1)
- {
+ DATA_BLOCK_SIZE)) != -1) {
cpZipOutputStream.write(b, 0, byteCount);
}
// cpZipOutputStream.write(b, 0, (int)cpFile.length());
- }
- finally
- {
- if (cpFileInputStream != null)
- {
+ } finally {
+ if (cpFileInputStream != null) {
cpFileInputStream.close();
}
cpZipOutputStream.closeEntry();
@@ -1179,13 +1043,12 @@ public class ItemExportServiceImpl implements ItemExportService
/**
* Delete a directory
+ *
* @param path directory path
* @return true if successful, false otherwise
*/
- protected boolean deleteDirectory(File path)
- {
- if (path.exists())
- {
+ protected boolean deleteDirectory(File path) {
+ if (path.exists()) {
File[] files = path.listFiles();
for (File file : files) {
if (file.isDirectory()) {
diff --git a/dspace-api/src/main/java/org/dspace/app/itemexport/factory/ItemExportServiceFactory.java b/dspace-api/src/main/java/org/dspace/app/itemexport/factory/ItemExportServiceFactory.java
index c1b3b07d3d..a6020c5c9d 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemexport/factory/ItemExportServiceFactory.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemexport/factory/ItemExportServiceFactory.java
@@ -11,7 +11,8 @@ import org.dspace.app.itemexport.service.ItemExportService;
import org.dspace.services.factory.DSpaceServicesFactory;
/**
- * Abstract factory to get services for the itemexport package, use ItemExportServiceFactory.getInstance() to retrieve an implementation
+ * Abstract factory to get services for the itemexport package, use ItemExportServiceFactory.getInstance() to
+ * retrieve an implementation
*
* @author kevinvandevelde at atmire.com
*/
@@ -19,7 +20,8 @@ public abstract class ItemExportServiceFactory {
public abstract ItemExportService getItemExportService();
- public static ItemExportServiceFactory getInstance(){
- return DSpaceServicesFactory.getInstance().getServiceManager().getServiceByName("itemExportServiceFactory", ItemExportServiceFactory.class);
+ public static ItemExportServiceFactory getInstance() {
+ return DSpaceServicesFactory.getInstance().getServiceManager()
+ .getServiceByName("itemExportServiceFactory", ItemExportServiceFactory.class);
}
}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemexport/factory/ItemExportServiceFactoryImpl.java b/dspace-api/src/main/java/org/dspace/app/itemexport/factory/ItemExportServiceFactoryImpl.java
index 058accba4a..840d023cb9 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemexport/factory/ItemExportServiceFactoryImpl.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemexport/factory/ItemExportServiceFactoryImpl.java
@@ -11,7 +11,8 @@ import org.dspace.app.itemexport.service.ItemExportService;
import org.springframework.beans.factory.annotation.Autowired;
/**
- * Factory implementation to get services for the itemexport package, use ItemExportServiceFactory.getInstance() to retrieve an implementation
+ * Factory implementation to get services for the itemexport package, use ItemExportServiceFactory.getInstance() to
+ * retrieve an implementation
*
* @author kevinvandevelde at atmire.com
*/
diff --git a/dspace-api/src/main/java/org/dspace/app/itemexport/service/ItemExportService.java b/dspace-api/src/main/java/org/dspace/app/itemexport/service/ItemExportService.java
index e93a2f22b2..7dedc9950b 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemexport/service/ItemExportService.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemexport/service/ItemExportService.java
@@ -7,16 +7,16 @@
*/
package org.dspace.app.itemexport.service;
-import org.dspace.content.DSpaceObject;
-import org.dspace.content.Item;
-import org.dspace.core.Context;
-import org.dspace.eperson.EPerson;
-
-import javax.mail.MessagingException;
import java.io.InputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
+import javax.mail.MessagingException;
+
+import org.dspace.content.DSpaceObject;
+import org.dspace.content.Item;
+import org.dspace.core.Context;
+import org.dspace.eperson.EPerson;
/**
* Item exporter to create simple AIPs for DSpace content. Currently exports
@@ -47,122 +47,109 @@ public interface ItemExportService {
public static final String COMPRESSED_EXPORT_MIME_TYPE = "application/zip";
public void exportItem(Context c, Iterator- i,
- String destDirName, int seqStart, boolean migrate,
- boolean excludeBitstreams) throws Exception;
+ String destDirName, int seqStart, boolean migrate,
+ boolean excludeBitstreams) throws Exception;
/**
* Method to perform an export and save it as a zip file.
*
- * @param context The DSpace Context
- * @param items The items to export
- * @param destDirName The directory to save the export in
- * @param zipFileName The name to save the zip file as
- * @param seqStart The first number in the sequence
- * @param migrate Whether to use the migrate option or not
+ * @param context The DSpace Context
+ * @param items The items to export
+ * @param destDirName The directory to save the export in
+ * @param zipFileName The name to save the zip file as
+ * @param seqStart The first number in the sequence
+ * @param migrate Whether to use the migrate option or not
* @param excludeBitstreams Whether to exclude bitstreams or not
* @throws Exception if error
*/
public void exportAsZip(Context context, Iterator
- items,
- String destDirName, String zipFileName,
- int seqStart, boolean migrate,
- boolean excludeBitstreams) throws Exception;
+ String destDirName, String zipFileName,
+ int seqStart, boolean migrate,
+ boolean excludeBitstreams) throws Exception;
/**
* Convenience methot to create export a single Community, Collection, or
* Item
*
- * @param dso
- * - the dspace object to export
- * @param context
- * - the dspace context
+ * @param dso - the dspace object to export
+ * @param context - the dspace context
* @param migrate Whether to use the migrate option or not
* @throws Exception if error
*/
public void createDownloadableExport(DSpaceObject dso,
- Context context, boolean migrate) throws Exception;
+ Context context, boolean migrate) throws Exception;
/**
* Convenience method to export a List of dspace objects (Community,
* Collection or Item)
*
- * @param dsObjects
- * - List containing dspace objects
- * @param context
- * - the dspace context
- * @param migrate Whether to use the migrate option or not
+ * @param dsObjects - List containing dspace objects
+ * @param context - the dspace context
+ * @param migrate Whether to use the migrate option or not
* @throws Exception if error
*/
public void createDownloadableExport(List
dsObjects,
- Context context, boolean migrate) throws Exception;
+ Context context, boolean migrate) throws Exception;
/**
* Convenience methot to create export a single Community, Collection, or
* Item
*
- * @param dso
- * - the dspace object to export
- * @param context
- * - the dspace context
- * @param additionalEmail
- * - cc email to use
- * @param migrate Whether to use the migrate option or not
+ * @param dso - the dspace object to export
+ * @param context - the dspace context
+ * @param additionalEmail - cc email to use
+ * @param migrate Whether to use the migrate option or not
* @throws Exception if error
*/
public void createDownloadableExport(DSpaceObject dso,
- Context context, String additionalEmail, boolean migrate) throws Exception;
+ Context context, String additionalEmail, boolean migrate) throws Exception;
/**
* Convenience method to export a List of dspace objects (Community,
* Collection or Item)
*
- * @param dsObjects
- * - List containing dspace objects
- * @param context
- * - the dspace context
- * @param additionalEmail
- * - cc email to use
- * @param migrate Whether to use the migrate option or not
+ * @param dsObjects - List containing dspace objects
+ * @param context - the dspace context
+ * @param additionalEmail - cc email to use
+ * @param migrate Whether to use the migrate option or not
* @throws Exception if error
*/
public void createDownloadableExport(List dsObjects,
- Context context, String additionalEmail, boolean migrate) throws Exception;
+ Context context, String additionalEmail, boolean migrate) throws Exception;
/**
* Create a file name based on the date and eperson
*
- * @param type Type of object (as string)
- * @param eperson
- * - eperson who requested export and will be able to download it
- * @param date
- * - the date the export process was created
+ * @param type Type of object (as string)
+ * @param eperson - eperson who requested export and will be able to download it
+ * @param date - the date the export process was created
* @return String representing the file name in the form of
- * 'export_yyy_MMM_dd_count_epersonID'
+ * 'export_yyy_MMM_dd_count_epersonID'
* @throws Exception if error
*/
public String assembleFileName(String type, EPerson eperson,
- Date date) throws Exception;
+ Date date) throws Exception;
/**
* Use config file entry for org.dspace.app.itemexport.download.dir and id
* of the eperson to create a download directory name
*
- * @param ePerson
- * - the eperson who requested export archive
+ * @param ePerson - the eperson who requested export archive
* @return String representing a directory in the form of
- * org.dspace.app.itemexport.download.dir/epersonID
+ * org.dspace.app.itemexport.download.dir/epersonID
* @throws Exception if error
*/
public String getExportDownloadDirectory(EPerson ePerson)
- throws Exception;
+ throws Exception;
/**
* Returns config file entry for org.dspace.app.itemexport.work.dir
*
* @return String representing config file entry for
- * org.dspace.app.itemexport.work.dir
+ * org.dspace.app.itemexport.work.dir
* @throws Exception if error
*/
public String getExportWorkDirectory() throws Exception;
@@ -170,49 +157,43 @@ public interface ItemExportService {
/**
* Used to read the export archived. Inteded for download.
*
- * @param fileName
- * the name of the file to download
- * @param eperson
- * the eperson requesting the download
+ * @param fileName the name of the file to download
+ * @param eperson the eperson requesting the download
* @return an input stream of the file to be downloaded
* @throws Exception if error
*/
public InputStream getExportDownloadInputStream(String fileName,
- EPerson eperson) throws Exception;
+ EPerson eperson) throws Exception;
/**
* Get the file size of the export archive represented by the file name.
*
- * @param context DSpace context
- * @param fileName
- * name of the file to get the size.
- * @throws Exception if error
+ * @param context DSpace context
+ * @param fileName name of the file to get the size.
* @return size as long
+ * @throws Exception if error
*/
public long getExportFileSize(Context context, String fileName) throws Exception;
/**
* Get the last modified date of the export archive represented by the file name.
*
- * @param context DSpace context
- * @param fileName
- * name of the file to get the size.
+ * @param context DSpace context
+ * @param fileName name of the file to get the size.
* @return date as long
- * @see java.io.File#lastModified()
* @throws Exception if error
+ * @see java.io.File#lastModified()
*/
public long getExportFileLastModified(Context context, String fileName)
- throws Exception;
+ throws Exception;
/**
* The file name of the export archive contains the eperson id of the person
* who created it When requested for download this method can check if the
* person requesting it is the same one that created it
*
- * @param context
- * dspace context
- * @param fileName
- * the file name to check auths for
+ * @param context dspace context
+ * @param fileName the file name to check auths for
* @return true if it is the same person false otherwise
*/
public boolean canDownload(Context context, String fileName);
@@ -223,19 +204,18 @@ public interface ItemExportService {
*
* @param eperson EPerson object
* @return a list of file names representing export archives that have been
- * processed
+ * processed
* @throws Exception if error
*/
public List getExportsAvailable(EPerson eperson)
- throws Exception;
+ throws Exception;
/**
* A clean up method that is ran before a new export archive is created. It
* uses the config file entry 'org.dspace.app.itemexport.life.span.hours' to
* determine if the current exports are too old and need pruging
*
- * @param eperson
- * - the eperson to clean up
+ * @param eperson - the eperson to clean up
* @throws Exception if error
*/
public void deleteOldExportArchives(EPerson eperson) throws Exception;
@@ -256,17 +236,14 @@ public interface ItemExportService {
* communication with email instead. Send a success email once the export
* archive is complete and ready for download
*
- * @param context
- * - the current Context
- * @param eperson
- * - eperson to send the email to
- * @param fileName
- * - the file name to be downloaded. It is added to the url in
- * the email
+ * @param context - the current Context
+ * @param eperson - eperson to send the email to
+ * @param fileName - the file name to be downloaded. It is added to the url in
+ * the email
* @throws MessagingException if error
*/
public void emailSuccessMessage(Context context, EPerson eperson,
- String fileName) throws MessagingException;
+ String fileName) throws MessagingException;
/**
* Since the archive is created in a new thread we are unable to communicate
@@ -274,19 +251,18 @@ public interface ItemExportService {
* communication with email instead. Send an error email if the export
* archive fails
*
- * @param eperson
- * - EPerson to send the error message to
- * @param error
- * - the error message
+ * @param eperson - EPerson to send the error message to
+ * @param error - the error message
* @throws MessagingException if error
*/
public void emailErrorMessage(EPerson eperson, String error)
- throws MessagingException;
+ throws MessagingException;
/**
* Zip source to target
+ *
* @param strSource source file
- * @param target target file
+ * @param target target file
* @throws Exception if error
*/
public void zip(String strSource, String target) throws Exception;
diff --git a/dspace-api/src/main/java/org/dspace/app/itemimport/BTEBatchImportService.java b/dspace-api/src/main/java/org/dspace/app/itemimport/BTEBatchImportService.java
index df2a63eaa9..c8419a2fae 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemimport/BTEBatchImportService.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemimport/BTEBatchImportService.java
@@ -7,99 +7,100 @@
*/
package org.dspace.app.itemimport;
-import gr.ekt.bte.core.DataLoader;
-import gr.ekt.bte.core.TransformationEngine;
-import gr.ekt.bte.dataloader.FileDataLoader;
-
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-
+import gr.ekt.bte.core.DataLoader;
+import gr.ekt.bte.core.TransformationEngine;
+import gr.ekt.bte.dataloader.FileDataLoader;
/**
* This class acts as a Service in the procedure to batch import using the Biblio-Transformation-Engine
*/
-public class BTEBatchImportService
-{
+public class BTEBatchImportService {
- TransformationEngine transformationEngine;
+ TransformationEngine transformationEngine;
Map dataLoaders = new HashMap();
- Map outputMap = new HashMap();
-
+ Map outputMap = new HashMap();
+
/**
* Default constructor
*/
- public BTEBatchImportService()
- {
+ public BTEBatchImportService() {
super();
}
/**
* Setter method for dataLoaders parameter
+ *
* @param dataLoaders map of data loaders
*/
- public void setDataLoaders(Map dataLoaders)
- {
+ public void setDataLoaders(Map dataLoaders) {
this.dataLoaders = dataLoaders;
}
/**
* Get data loaders
+ *
* @return the map of DataLoaders
*/
- public Map getDataLoaders()
- {
+ public Map getDataLoaders() {
return dataLoaders;
}
/**
* Get output map
+ *
* @return the outputMapping
*/
- public Map getOutputMap() {
- return outputMap;
- }
+ public Map getOutputMap() {
+ return outputMap;
+ }
- /**
- * Setter method for the outputMapping
- * @param outputMap the output mapping
- */
- public void setOutputMap(Map outputMap) {
- this.outputMap = outputMap;
- }
+ /**
+ * Setter method for the outputMapping
+ *
+ * @param outputMap the output mapping
+ */
+ public void setOutputMap(Map outputMap) {
+ this.outputMap = outputMap;
+ }
- /**
- * Get transformation engine
- * @return transformation engine
- */
- public TransformationEngine getTransformationEngine() {
- return transformationEngine;
- }
+ /**
+ * Get transformation engine
+ *
+ * @return transformation engine
+ */
+ public TransformationEngine getTransformationEngine() {
+ return transformationEngine;
+ }
- /**
- * set transformation engine
- * @param transformationEngine transformation engine
- */
- public void setTransformationEngine(TransformationEngine transformationEngine) {
- this.transformationEngine = transformationEngine;
- }
-
- /**
- * Getter of file data loaders
- * @return List of file data loaders
- */
- public List getFileDataLoaders(){
- List result = new ArrayList();
-
- for (String key : dataLoaders.keySet()){
- DataLoader dl = dataLoaders.get(key);
- if (dl instanceof FileDataLoader){
- result.add(key);
- }
- }
- return result;
- }
-}
\ No newline at end of file
+ /**
+ * set transformation engine
+ *
+ * @param transformationEngine transformation engine
+ */
+ public void setTransformationEngine(TransformationEngine transformationEngine) {
+ this.transformationEngine = transformationEngine;
+ }
+
+ /**
+ * Getter of file data loaders
+ *
+ * @return List of file data loaders
+ */
+ public List getFileDataLoaders() {
+ List result = new ArrayList();
+
+ for (String key : dataLoaders.keySet()) {
+ DataLoader dl = dataLoaders.get(key);
+ if (dl instanceof FileDataLoader) {
+ result.add(key);
+ }
+ }
+ return result;
+ }
+}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemimport/BatchUpload.java b/dspace-api/src/main/java/org/dspace/app/itemimport/BatchUpload.java
index 58e40c6ab9..c574112159 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemimport/BatchUpload.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemimport/BatchUpload.java
@@ -20,198 +20,210 @@ import java.util.List;
/**
* @author kstamatis
- *
*/
public class BatchUpload {
- private Date date;
- private File dir;
- private boolean successful;
- private int itemsImported;
- private int totalItems = 0;
- private List handlesImported = new ArrayList();
- private String errorMsg = "";
- private String errorMsgHTML = "";
-
- /**
- * Initialize with directory
- * @param dirPath directory path
- */
- public BatchUpload(String dirPath) {
-
- this.initializeWithFile(new File(dirPath));
-
- }
+ private Date date;
+ private File dir;
+ private boolean successful;
+ private int itemsImported;
+ private int totalItems = 0;
+ private List handlesImported = new ArrayList();
+ private String errorMsg = "";
+ private String errorMsgHTML = "";
- /**
- * Initialize with directory
- * @param dir directory path
- */
- public BatchUpload(File dir) {
-
- this.initializeWithFile(dir);
-
- }
+ /**
+ * Initialize with directory
+ *
+ * @param dirPath directory path
+ */
+ public BatchUpload(String dirPath) {
- /**
- * Initialize with directory
- * @param dir directory path
- */
- private void initializeWithFile(File dir){
-
- this.dir = dir;
-
- String dirName = dir.getName();
- long timeMillis = Long.parseLong(dirName);
- Calendar calendar = new GregorianCalendar();
- calendar.setTimeInMillis(timeMillis);
- this.date = calendar.getTime();
-
- try {
- this.itemsImported = countLines(dir + File.separator + "mapfile");
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- for (File file : dir.listFiles()){
- if (file.isDirectory()){
- this.totalItems = file.list().length;
- }
- }
-
- this.successful = this.totalItems == this.itemsImported;
-
- //Parse possible error message
-
- File errorFile = new File(dir + File.separator + "error.txt");
- if (errorFile.exists()){
- try {
- readFile(dir + File.separator + "error.txt");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
-
- /**
- * Count lines in file
- * @param filename file name
- * @return lines in file
- * @throws IOException if IO error
- */
- private int countLines(String filename) throws IOException {
- LineNumberReader reader = new LineNumberReader(new FileReader(filename));
- int cnt = 0;
- String lineRead = "";
- while ((lineRead = reader.readLine()) != null) {
- String[] parts = lineRead.split(" ");
- if (parts.length > 1)
- handlesImported.add(parts[1].trim());
- else
- handlesImported.add(lineRead);
- }
+ this.initializeWithFile(new File(dirPath));
- cnt = reader.getLineNumber();
- reader.close();
- return cnt;
- }
-
- /**
- * Read a file
- * @param filename file name
- * @throws IOException if IO error
- */
- private void readFile(String filename) throws IOException {
- LineNumberReader reader = new LineNumberReader(new FileReader(filename));
- String lineRead = "";
- while ((lineRead = reader.readLine()) != null) {
- this.errorMsg += lineRead + "\n";
-
- if (lineRead.startsWith("\tat ")){
- this.errorMsgHTML += "" + lineRead + " ";
- }
- else if (lineRead.startsWith("Caused by")){
- this.errorMsgHTML += "" + lineRead + " ";
- }
- else {
- this.errorMsgHTML += lineRead + " ";
- }
- }
- reader.close();
- }
+ }
- /**
- * Get date
- * @return Date
- */
- public Date getDate() {
- return date;
- }
+ /**
+ * Initialize with directory
+ *
+ * @param dir directory path
+ */
+ public BatchUpload(File dir) {
- /**
- * Get path to directory
- * @return directory
- */
- public File getDir() {
- return dir;
- }
+ this.initializeWithFile(dir);
- /**
- * Whether successulf
- * @return true or false
- */
- public boolean isSuccessful() {
- return successful;
- }
+ }
- /**
- * Get items imported
- * @return number of items
- */
- public int getItemsImported() {
- return itemsImported;
- }
+ /**
+ * Initialize with directory
+ *
+ * @param dir directory path
+ */
+ private void initializeWithFile(File dir) {
- /**
- * Get total items
- * @return total
- */
- public int getTotalItems() {
- return totalItems;
- }
-
- /**
- * Get formatted date (DD/MM/YY)
- * @return date as string
- */
- public String getDateFormatted(){
- SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
-
- return df.format(date);
- }
+ this.dir = dir;
- /**
- * Get handles of imported files
- * @return list of handles
- */
- public List getHandlesImported() {
- return handlesImported;
- }
+ String dirName = dir.getName();
+ long timeMillis = Long.parseLong(dirName);
+ Calendar calendar = new GregorianCalendar();
+ calendar.setTimeInMillis(timeMillis);
+ this.date = calendar.getTime();
- /**
- * Get error message
- * @return error message
- */
- public String getErrorMsg() {
- return errorMsg;
- }
+ try {
+ this.itemsImported = countLines(dir + File.separator + "mapfile");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
- /**
- * Get error message as HTML
- * @return error message string as HTML
- */
- public String getErrorMsgHTML() {
- return errorMsgHTML;
- }
+ for (File file : dir.listFiles()) {
+ if (file.isDirectory()) {
+ this.totalItems = file.list().length;
+ }
+ }
+
+ this.successful = this.totalItems == this.itemsImported;
+
+ //Parse possible error message
+
+ File errorFile = new File(dir + File.separator + "error.txt");
+ if (errorFile.exists()) {
+ try {
+ readFile(dir + File.separator + "error.txt");
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * Count lines in file
+ *
+ * @param filename file name
+ * @return lines in file
+ * @throws IOException if IO error
+ */
+ private int countLines(String filename) throws IOException {
+ LineNumberReader reader = new LineNumberReader(new FileReader(filename));
+ int cnt = 0;
+ String lineRead = "";
+ while ((lineRead = reader.readLine()) != null) {
+ String[] parts = lineRead.split(" ");
+ if (parts.length > 1) {
+ handlesImported.add(parts[1].trim());
+ } else {
+ handlesImported.add(lineRead);
+ }
+ }
+
+ cnt = reader.getLineNumber();
+ reader.close();
+ return cnt;
+ }
+
+ /**
+ * Read a file
+ *
+ * @param filename file name
+ * @throws IOException if IO error
+ */
+ private void readFile(String filename) throws IOException {
+ LineNumberReader reader = new LineNumberReader(new FileReader(filename));
+ String lineRead = "";
+ while ((lineRead = reader.readLine()) != null) {
+ this.errorMsg += lineRead + "\n";
+
+ if (lineRead.startsWith("\tat ")) {
+ this.errorMsgHTML += "" + lineRead + " ";
+ } else if (lineRead.startsWith("Caused by")) {
+ this.errorMsgHTML += "" + lineRead + " ";
+ } else {
+ this.errorMsgHTML += lineRead + " ";
+ }
+ }
+ reader.close();
+ }
+
+ /**
+ * Get date
+ *
+ * @return Date
+ */
+ public Date getDate() {
+ return date;
+ }
+
+ /**
+ * Get path to directory
+ *
+ * @return directory
+ */
+ public File getDir() {
+ return dir;
+ }
+
+ /**
+ * Whether successulf
+ *
+ * @return true or false
+ */
+ public boolean isSuccessful() {
+ return successful;
+ }
+
+ /**
+ * Get items imported
+ *
+ * @return number of items
+ */
+ public int getItemsImported() {
+ return itemsImported;
+ }
+
+ /**
+ * Get total items
+ *
+ * @return total
+ */
+ public int getTotalItems() {
+ return totalItems;
+ }
+
+ /**
+ * Get formatted date (DD/MM/YY)
+ *
+ * @return date as string
+ */
+ public String getDateFormatted() {
+ SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
+
+ return df.format(date);
+ }
+
+ /**
+ * Get handles of imported files
+ *
+ * @return list of handles
+ */
+ public List getHandlesImported() {
+ return handlesImported;
+ }
+
+ /**
+ * Get error message
+ *
+ * @return error message
+ */
+ public String getErrorMsg() {
+ return errorMsg;
+ }
+
+ /**
+ * Get error message as HTML
+ *
+ * @return error message string as HTML
+ */
+ public String getErrorMsgHTML() {
+ return errorMsgHTML;
+ }
}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImportCLITool.java b/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImportCLITool.java
index d4339e11a2..28ae816607 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImportCLITool.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImportCLITool.java
@@ -7,7 +7,17 @@
*/
package org.dspace.app.itemimport;
-import org.apache.commons.cli.*;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.PosixParser;
import org.dspace.app.itemimport.factory.ItemImportServiceFactory;
import org.dspace.app.itemimport.service.ItemImportService;
import org.dspace.content.Collection;
@@ -21,12 +31,6 @@ import org.dspace.eperson.service.EPersonService;
import org.dspace.handle.factory.HandleServiceFactory;
import org.dspace.handle.service.HandleService;
-import java.io.File;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.UUID;
-
/**
* Import items into DSpace. The conventional use is upload files by copying
* them. DSpace writes the item's bitstreams into its assetstore. Metadata is
@@ -47,12 +51,17 @@ public class ItemImportCLITool {
private static boolean template = false;
- private static final CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService();
+ private static final CollectionService collectionService = ContentServiceFactory.getInstance()
+ .getCollectionService();
private static final EPersonService epersonService = EPersonServiceFactory.getInstance().getEPersonService();
private static final HandleService handleService = HandleServiceFactory.getInstance().getHandleService();
- public static void main(String[] argv) throws Exception
- {
+ /**
+ * Default constructor
+ */
+ private ItemImportCLITool() { }
+
+ public static void main(String[] argv) throws Exception {
Date startTime = new Date();
int status = 0;
@@ -66,24 +75,24 @@ public class ItemImportCLITool {
options.addOption("b", "add-bte", false, "add items to DSpace via Biblio-Transformation-Engine (BTE)");
options.addOption("r", "replace", false, "replace items in mapfile");
options.addOption("d", "delete", false,
- "delete items listed in mapfile");
+ "delete items listed in mapfile");
options.addOption("i", "inputtype", true, "input type in case of BTE import");
options.addOption("s", "source", true, "source of items (directory)");
options.addOption("z", "zip", true, "name of zip file");
options.addOption("c", "collection", true,
- "destination collection(s) Handle or database ID");
+ "destination collection(s) Handle or database ID");
options.addOption("m", "mapfile", true, "mapfile items in mapfile");
options.addOption("e", "eperson", true,
- "email of eperson doing importing");
+ "email of eperson doing importing");
options.addOption("w", "workflow", false,
- "send submission through collection's workflow");
+ "send submission through collection's workflow");
options.addOption("n", "notify", false,
- "if sending submissions through the workflow, send notification emails");
+ "if sending submissions through the workflow, send notification emails");
options.addOption("t", "test", false,
- "test run - do not actually import items");
+ "test run - do not actually import items");
options.addOption("p", "template", false, "apply template");
options.addOption("R", "resume", false,
- "resume a failed import (add only)");
+ "resume a failed import (add only)");
options.addOption("q", "quiet", false, "don't display metadata");
options.addOption("h", "help", false, "help");
@@ -106,15 +115,19 @@ public class ItemImportCLITool {
HelpFormatter myhelp = new HelpFormatter();
myhelp.printHelp("ItemImport\n", options);
System.out
- .println("\nadding items: ItemImport -a -e eperson -c collection -s sourcedir -m mapfile");
+ .println("\nadding items: ItemImport -a -e eperson -c collection -s sourcedir -m mapfile");
System.out
- .println("\nadding items from zip file: ItemImport -a -e eperson -c collection -s sourcedir -z filename.zip -m mapfile");
+ .println(
+ "\nadding items from zip file: ItemImport -a -e eperson -c collection -s sourcedir -z " +
+ "filename.zip -m mapfile");
System.out
- .println("replacing items: ItemImport -r -e eperson -c collection -s sourcedir -m mapfile");
+ .println("replacing items: ItemImport -r -e eperson -c collection -s sourcedir -m mapfile");
System.out
- .println("deleting items: ItemImport -d -e eperson -m mapfile");
+ .println("deleting items: ItemImport -d -e eperson -m mapfile");
System.out
- .println("If multiple collections are specified, the first collection will be the one that owns the item.");
+ .println(
+ "If multiple collections are specified, the first collection will be the one that owns the " +
+ "item.");
System.exit(0);
}
@@ -155,30 +168,26 @@ public class ItemImportCLITool {
template = true;
}
- if (line.hasOption('s')) // source
- {
+ if (line.hasOption('s')) { // source
sourcedir = line.getOptionValue('s');
}
- if (line.hasOption('m')) // mapfile
- {
+ if (line.hasOption('m')) { // mapfile
mapfile = line.getOptionValue('m');
}
- if (line.hasOption('e')) // eperson
- {
+ if (line.hasOption('e')) { // eperson
eperson = line.getOptionValue('e');
}
- if (line.hasOption('c')) // collections
- {
+ if (line.hasOption('c')) { // collections
collections = line.getOptionValues('c');
}
if (line.hasOption('R')) {
isResume = true;
System.out
- .println("**Resume import** - attempting to import items not already imported");
+ .println("**Resume import** - attempting to import items not already imported");
}
if (line.hasOption('q')) {
@@ -198,26 +207,26 @@ public class ItemImportCLITool {
// must have a command set
if (command == null) {
System.out
- .println("Error - must run with either add, replace, or remove (run with -h flag for details)");
+ .println("Error - must run with either add, replace, or remove (run with -h flag for details)");
System.exit(1);
} else if ("add".equals(command) || "replace".equals(command)) {
if (sourcedir == null) {
System.out
- .println("Error - a source directory containing items must be set");
+ .println("Error - a source directory containing items must be set");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
if (mapfile == null) {
System.out
- .println("Error - a map file to hold importing results must be specified");
+ .println("Error - a map file to hold importing results must be specified");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
if (eperson == null) {
System.out
- .println("Error - an eperson to do the importing must be specified");
+ .println("Error - an eperson to do the importing must be specified");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
@@ -227,18 +236,19 @@ public class ItemImportCLITool {
commandLineCollections = false;
}
} else if ("add-bte".equals(command)) {
- //Source dir can be null, the user can specify the parameters for his loader in the Spring XML configuration file
+ //Source dir can be null, the user can specify the parameters for his loader in the Spring XML
+ // configuration file
if (mapfile == null) {
System.out
- .println("Error - a map file to hold importing results must be specified");
+ .println("Error - a map file to hold importing results must be specified");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
if (eperson == null) {
System.out
- .println("Error - an eperson to do the importing must be specified");
+ .println("Error - an eperson to do the importing must be specified");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
@@ -250,14 +260,16 @@ public class ItemImportCLITool {
if (bteInputType == null) {
System.out
- .println("Error - an input type (tsv, csv, ris, endnote, bibtex or any other type you have specified in BTE Spring XML configuration file) must be specified");
+ .println(
+ "Error - an input type (tsv, csv, ris, endnote, bibtex or any other type you have " +
+ "specified in BTE Spring XML configuration file) must be specified");
System.out.println(" (run with -h flag for details)");
System.exit(1);
}
} else if ("delete".equals(command)) {
if (eperson == null) {
System.out
- .println("Error - an eperson to do the importing must be specified");
+ .println("Error - an eperson to do the importing must be specified");
System.exit(1);
}
@@ -270,7 +282,7 @@ public class ItemImportCLITool {
// can only resume for adds
if (isResume && !"add".equals(command) && !"add-bte".equals(command)) {
System.out
- .println("Error - resume option only works with the --add or the --add-bte commands");
+ .println("Error - resume option only works with the --add or the --add-bte commands");
System.exit(1);
}
@@ -280,9 +292,9 @@ public class ItemImportCLITool {
if (!isResume && "add".equals(command) && myFile.exists()) {
System.out.println("Error - the mapfile " + mapfile
- + " already exists.");
+ + " already exists.");
System.out
- .println("Either delete it or use --resume if attempting to resume an aborted import.");
+ .println("Either delete it or use --resume if attempting to resume an aborted import.");
System.exit(1);
}
@@ -330,24 +342,22 @@ public class ItemImportCLITool {
// string has a / so it must be a handle - try and resolve
// it
mycollections.add((Collection) handleService
- .resolveToObject(c, collections[i]));
+ .resolveToObject(c, collections[i]));
// resolved, now make sure it's a collection
if ((mycollections.get(i) == null)
- || (mycollections.get(i).getType() != Constants.COLLECTION)) {
+ || (mycollections.get(i).getType() != Constants.COLLECTION)) {
mycollections.set(i, null);
}
- }
- // not a handle, try and treat it as an integer collection
- // database ID
- else if (collections[i] != null) {
+ } else if (collections[i] != null) {
+ // not a handle, try and treat it as an integer collection database ID
mycollections.set(i, collectionService.find(c, UUID.fromString(collections[i])));
}
// was the collection valid?
if (mycollections.get(i) == null) {
throw new IllegalArgumentException("Cannot resolve "
- + collections[i] + " to collection");
+ + collections[i] + " to collection");
}
// print progress info
@@ -358,7 +368,7 @@ public class ItemImportCLITool {
}
System.out.println(owningPrefix + " Collection: "
- + mycollections.get(i).getName());
+ + mycollections.get(i).getName());
}
} // end of validating collections
@@ -394,11 +404,13 @@ public class ItemImportCLITool {
try {
if (zip) {
System.gc();
- System.out.println("Deleting temporary zip directory: " + myloader.getTempWorkDirFile().getAbsolutePath());
+ System.out.println(
+ "Deleting temporary zip directory: " + myloader.getTempWorkDirFile().getAbsolutePath());
myloader.cleanupZipTemp();
}
} catch (Exception ex) {
- System.out.println("Unable to delete temporary zip archive location: " + myloader.getTempWorkDirFile().getAbsolutePath());
+ System.out.println("Unable to delete temporary zip archive location: " + myloader.getTempWorkDirFile()
+ .getAbsolutePath());
}
@@ -409,7 +421,9 @@ public class ItemImportCLITool {
Date endTime = new Date();
System.out.println("Started: " + startTime.getTime());
System.out.println("Ended: " + endTime.getTime());
- System.out.println("Elapsed time: " + ((endTime.getTime() - startTime.getTime()) / 1000) + " secs (" + (endTime.getTime() - startTime.getTime()) + " msecs)");
+ System.out.println(
+ "Elapsed time: " + ((endTime.getTime() - startTime.getTime()) / 1000) + " secs (" + (endTime
+ .getTime() - startTime.getTime()) + " msecs)");
}
System.exit(status);
diff --git a/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImportServiceImpl.java b/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImportServiceImpl.java
index 786c3cd632..829594f448 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImportServiceImpl.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemimport/ItemImportServiceImpl.java
@@ -7,6 +7,44 @@
*/
package org.dspace.app.itemimport;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.sql.SQLException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.TreeMap;
+import java.util.UUID;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import javax.mail.MessagingException;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+
import gr.ekt.bte.core.DataLoader;
import gr.ekt.bte.core.TransformationEngine;
import gr.ekt.bte.core.TransformationResult;
@@ -14,7 +52,7 @@ import gr.ekt.bte.core.TransformationSpec;
import gr.ekt.bte.dataloader.FileDataLoader;
import gr.ekt.bteio.generators.DSpaceOutputGenerator;
import gr.ekt.bteio.loaders.OAIPMHDataLoader;
-import org.apache.commons.collections.ComparatorUtils;
+import org.apache.commons.collections4.ComparatorUtils;
import org.apache.commons.io.FileDeleteStrategy;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.RandomStringUtils;
@@ -28,10 +66,30 @@ import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.authorize.service.ResourcePolicyService;
-import org.dspace.content.*;
+import org.dspace.content.Bitstream;
+import org.dspace.content.BitstreamFormat;
+import org.dspace.content.Bundle;
import org.dspace.content.Collection;
-import org.dspace.content.service.*;
-import org.dspace.core.*;
+import org.dspace.content.DSpaceObject;
+import org.dspace.content.Item;
+import org.dspace.content.MetadataField;
+import org.dspace.content.MetadataSchema;
+import org.dspace.content.WorkspaceItem;
+import org.dspace.content.service.BitstreamFormatService;
+import org.dspace.content.service.BitstreamService;
+import org.dspace.content.service.BundleService;
+import org.dspace.content.service.CollectionService;
+import org.dspace.content.service.InstallItemService;
+import org.dspace.content.service.ItemService;
+import org.dspace.content.service.MetadataFieldService;
+import org.dspace.content.service.MetadataSchemaService;
+import org.dspace.content.service.WorkspaceItemService;
+import org.dspace.core.ConfigurationManager;
+import org.dspace.core.Constants;
+import org.dspace.core.Context;
+import org.dspace.core.Email;
+import org.dspace.core.I18nUtil;
+import org.dspace.core.LogManager;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group;
import org.dspace.eperson.service.EPersonService;
@@ -48,19 +106,6 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
-import javax.mail.MessagingException;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.TransformerException;
-import java.io.*;
-import java.net.URL;
-import java.sql.SQLException;
-import java.text.SimpleDateFormat;
-import java.util.*;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
-
/**
* Import items into DSpace. The conventional use is upload files by copying
@@ -78,8 +123,7 @@ import java.util.zip.ZipFile;
* Modified by David Little, UCSD Libraries 12/21/04 to
* allow the registration of files (bitstreams) into DSpace.
*/
-public class ItemImportServiceImpl implements ItemImportService, InitializingBean
-{
+public class ItemImportServiceImpl implements ItemImportService, InitializingBean {
private final Logger log = Logger.getLogger(ItemImportServiceImpl.class);
@Autowired(required = true)
@@ -125,7 +169,7 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
public void afterPropertiesSet() throws Exception {
//Ensure tempWorkDir exists
File tempWorkDirFile = new File(tempWorkDir);
- if (!tempWorkDirFile.exists()){
+ if (!tempWorkDirFile.exists()) {
boolean success = tempWorkDirFile.mkdir();
if (success) {
log.info("Created org.dspace.app.batchitemimport.work.dir of: " + tempWorkDir);
@@ -139,112 +183,116 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
protected FilenameFilter metadataFileFilter = new LocalSchemaFilenameFilter();
// File listing filter to check for folders
- protected FilenameFilter directoryFilter = new FilenameFilter()
- {
+ protected FilenameFilter directoryFilter = new FilenameFilter() {
@Override
- public boolean accept(File dir, String n)
- {
+ public boolean accept(File dir, String n) {
File item = new File(dir.getAbsolutePath() + File.separatorChar + n);
return item.isDirectory();
}
};
-
- protected ItemImportServiceImpl(){
+
+ protected ItemImportServiceImpl() {
//Protected consumer to ensure that we use spring to create a bean, NEVER make this public
}
-
-
/**
* In this method, the BTE is instantiated. THe workflow generates the DSpace files
* necessary for the upload, and the default item import method is called
- * @param c The contect
+ *
+ * @param c The contect
* @param mycollections The collections the items are inserted to
- * @param sourceDir The filepath to the file to read data from
- * @param mapFile The filepath to mapfile to be generated
- * @param template whether to use collection template item as starting point
- * @param inputType The type of the input data (bibtex, csv, etc.)
- * @param workingDir The path to create temporary files (for command line or UI based)
+ * @param sourceDir The filepath to the file to read data from
+ * @param mapFile The filepath to mapfile to be generated
+ * @param template whether to use collection template item as starting point
+ * @param inputType The type of the input data (bibtex, csv, etc.)
+ * @param workingDir The path to create temporary files (for command line or UI based)
* @throws Exception if error occurs
*/
@Override
public void addBTEItems(Context c, List mycollections,
- String sourceDir, String mapFile, boolean template, String inputType, String workingDir) throws Exception
- {
- //Determine the folder where BTE will output the results
- String outputFolder = null;
- if (workingDir == null){ //This indicates a command line import, create a random path
- File importDir = new File(ConfigurationManager.getProperty("org.dspace.app.batchitemimport.work.dir"));
- if (!importDir.exists()){
- boolean success = importDir.mkdir();
- if (!success) {
- log.info("Cannot create batch import directory!");
- throw new Exception("Cannot create batch import directory!");
- }
+ String sourceDir, String mapFile, boolean template, String inputType, String workingDir)
+ throws Exception {
+ //Determine the folder where BTE will output the results
+ String outputFolder = null;
+ if (workingDir == null) { //This indicates a command line import, create a random path
+ File importDir = new File(ConfigurationManager.getProperty("org.dspace.app.batchitemimport.work.dir"));
+ if (!importDir.exists()) {
+ boolean success = importDir.mkdir();
+ if (!success) {
+ log.info("Cannot create batch import directory!");
+ throw new Exception("Cannot create batch import directory!");
+ }
}
//Get a random folder in case two admins batch import data at the same time
- outputFolder = importDir + File.separator + generateRandomFilename(true);
- }
- else { //This indicates a UI import, working dir is preconfigured
- outputFolder = workingDir;
- }
+ outputFolder = importDir + File.separator + generateRandomFilename(true);
+ } else { //This indicates a UI import, working dir is preconfigured
+ outputFolder = workingDir;
+ }
- BTEBatchImportService dls = new DSpace().getSingletonService(BTEBatchImportService.class);
+ BTEBatchImportService dls = new DSpace().getSingletonService(BTEBatchImportService.class);
DataLoader dataLoader = dls.getDataLoaders().get(inputType);
Map outputMap = dls.getOutputMap();
TransformationEngine te = dls.getTransformationEngine();
- if (dataLoader==null){
- System.out.println("ERROR: The key used in -i parameter must match a valid DataLoader in the BTE Spring XML configuration file!");
+ if (dataLoader == null) {
+ System.out.println(
+ "ERROR: The key used in -i parameter must match a valid DataLoader in the BTE Spring XML " +
+ "configuration file!");
return;
}
- if (outputMap==null){
- System.out.println("ERROR: The key used in -i parameter must match a valid outputMapping in the BTE Spring XML configuration file!");
+ if (outputMap == null) {
+ System.out.println(
+ "ERROR: The key used in -i parameter must match a valid outputMapping in the BTE Spring XML " +
+ "configuration file!");
return;
}
- if (dataLoader instanceof FileDataLoader){
+ if (dataLoader instanceof FileDataLoader) {
FileDataLoader fdl = (FileDataLoader) dataLoader;
if (!StringUtils.isBlank(sourceDir)) {
- System.out.println("INFO: Dataloader will load data from the file specified in the command prompt (and not from the Spring XML configuration file)");
+ System.out.println(
+ "INFO: Dataloader will load data from the file specified in the command prompt (and not from the " +
+ "Spring XML configuration file)");
fdl.setFilename(sourceDir);
}
- }
- else if (dataLoader instanceof OAIPMHDataLoader){
+ } else if (dataLoader instanceof OAIPMHDataLoader) {
OAIPMHDataLoader fdl = (OAIPMHDataLoader) dataLoader;
System.out.println(sourceDir);
- if (!StringUtils.isBlank(sourceDir)){
- System.out.println("INFO: Dataloader will load data from the address specified in the command prompt (and not from the Spring XML configuration file)");
+ if (!StringUtils.isBlank(sourceDir)) {
+ System.out.println(
+ "INFO: Dataloader will load data from the address specified in the command prompt (and not from " +
+ "the Spring XML configuration file)");
fdl.setServerAddress(sourceDir);
}
}
- if (dataLoader!=null){
- System.out.println("INFO: Dataloader " + dataLoader.toString()+" will be used for the import!");
+ if (dataLoader != null) {
+ System.out.println("INFO: Dataloader " + dataLoader.toString() + " will be used for the import!");
- te.setDataLoader(dataLoader);
+ te.setDataLoader(dataLoader);
- DSpaceOutputGenerator outputGenerator = new DSpaceOutputGenerator(outputMap);
- outputGenerator.setOutputDirectory(outputFolder);
+ DSpaceOutputGenerator outputGenerator = new DSpaceOutputGenerator(outputMap);
+ outputGenerator.setOutputDirectory(outputFolder);
- te.setOutputGenerator(outputGenerator);
+ te.setOutputGenerator(outputGenerator);
- try {
- TransformationResult res = te.transform(new TransformationSpec());
- List output = res.getOutput();
- outputGenerator.writeOutput(output);
- } catch (Exception e) {
- System.err.println("Exception");
- e.printStackTrace();
- throw e;
- }
- addItems(c, mycollections, outputFolder, mapFile, template);
+ try {
+ TransformationResult res = te.transform(new TransformationSpec());
+ List output = res.getOutput();
+ outputGenerator.writeOutput(output);
+ } catch (Exception e) {
+ System.err.println("Exception");
+ e.printStackTrace();
+ throw e;
+ }
+ addItems(c, mycollections, outputFolder, mapFile, template);
}
}
@Override
- public void addItemsAtomic(Context c, List mycollections, String sourceDir, String mapFile, boolean template) throws Exception {
+ public void addItemsAtomic(Context c, List mycollections, String sourceDir, String mapFile,
+ boolean template) throws Exception {
try {
addItems(c, mycollections, sourceDir, mapFile, template);
} catch (Exception addException) {
@@ -257,8 +305,7 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
@Override
public void addItems(Context c, List mycollections,
- String sourceDir, String mapFile, boolean template) throws Exception
- {
+ String sourceDir, String mapFile, boolean template) throws Exception {
// create the mapfile
File outFile = null;
PrintWriter mapOut = null;
@@ -272,17 +319,14 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
log.debug("Generating mapfile: " + mapFile);
boolean directoryFileCollections = false;
- if (mycollections == null)
- {
+ if (mycollections == null) {
directoryFileCollections = true;
}
- if (!isTest)
- {
+ if (!isTest) {
// get the directory names of items to skip (will be in keys of
// hash)
- if (isResume)
- {
+ if (isResume) {
skipItems = readMapFile(mapFile);
}
@@ -290,8 +334,7 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
outFile = new File(mapFile);
mapOut = new PrintWriter(new FileWriter(outFile, isResume));
- if (mapOut == null)
- {
+ if (mapOut == null) {
throw new Exception("can't open mapfile: " + mapFile);
}
}
@@ -299,8 +342,7 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
// open and process the source directory
File d = new java.io.File(sourceDir);
- if (d == null || !d.isDirectory())
- {
+ if (d == null || !d.isDirectory()) {
throw new Exception("Error, cannot open source directory " + sourceDir);
}
@@ -308,42 +350,36 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
Arrays.sort(dircontents, ComparatorUtils.naturalComparator());
- for (int i = 0; i < dircontents.length; i++)
- {
- if (skipItems.containsKey(dircontents[i]))
- {
- System.out.println("Skipping import of " + dircontents[i]);
- }
- else
- {
- List clist;
- if (directoryFileCollections) {
- String path = sourceDir + File.separatorChar + dircontents[i];
- try {
- List cols = processCollectionFile(c, path, "collections");
- if (cols == null) {
- System.out.println("No collections specified for item " + dircontents[i] + ". Skipping.");
+ for (int i = 0; i < dircontents.length; i++) {
+ if (skipItems.containsKey(dircontents[i])) {
+ System.out.println("Skipping import of " + dircontents[i]);
+ } else {
+ List clist;
+ if (directoryFileCollections) {
+ String path = sourceDir + File.separatorChar + dircontents[i];
+ try {
+ List cols = processCollectionFile(c, path, "collections");
+ if (cols == null) {
+ System.out
+ .println("No collections specified for item " + dircontents[i] + ". Skipping.");
+ continue;
+ }
+ clist = cols;
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage() + " Skipping.");
continue;
}
- clist = cols;
- }
- catch (IllegalArgumentException e)
- {
- System.out.println(e.getMessage() + " Skipping." );
- continue;
+ } else {
+ clist = mycollections;
}
+ Item item = addItem(c, clist, sourceDir, dircontents[i], mapOut, template);
+ c.uncacheEntity(item);
+ System.out.println(i + " " + dircontents[i]);
}
- else
- {
- clist = mycollections;
- }
- Item item =addItem(c, clist, sourceDir, dircontents[i], mapOut, template);c.uncacheEntity(item);
- System.out.println(i + " " + dircontents[i]);
}
- }
} finally {
- if(mapOut!=null) {
+ if (mapOut != null) {
mapOut.flush();
mapOut.close();
}
@@ -352,15 +388,13 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
@Override
public void replaceItems(Context c, List mycollections,
- String sourceDir, String mapFile, boolean template) throws Exception
- {
+ String sourceDir, String mapFile, boolean template) throws Exception {
// verify the source directory
File d = new java.io.File(sourceDir);
- if (d == null || !d.isDirectory())
- {
+ if (d == null || !d.isDirectory()) {
throw new Exception("Error, cannot open source directory "
- + sourceDir);
+ + sourceDir);
}
// read in HashMap first, to get list of handles & source dirs
@@ -368,23 +402,19 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
// for each handle, re-import the item, discard the new handle
// and re-assign the old handle
- for (Map.Entry mapEntry : myHash.entrySet())
- {
+ for (Map.Entry mapEntry : myHash.entrySet()) {
// get the old handle
String newItemName = mapEntry.getKey();
String oldHandle = mapEntry.getValue();
Item oldItem = null;
- if (oldHandle.indexOf('/') != -1)
- {
+ if (oldHandle.indexOf('/') != -1) {
System.out.println("\tReplacing: " + oldHandle);
// add new item, locate old one
oldItem = (Item) handleService.resolveToObject(c, oldHandle);
- }
- else
- {
+ } else {
oldItem = itemService.findByIdOrLegacyId(c, oldHandle);
}
@@ -401,8 +431,7 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
File handleFile = new File(sourceDir + File.separatorChar + newItemName + File.separatorChar + "handle");
PrintWriter handleOut = new PrintWriter(new FileWriter(handleFile, true));
- if (handleOut == null)
- {
+ if (handleOut == null) {
throw new Exception("can't open handle file: " + handleFile.getCanonicalPath());
}
@@ -417,8 +446,7 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
}
@Override
- public void deleteItems(Context c, String mapFile) throws Exception
- {
+ public void deleteItems(Context c, String mapFile) throws Exception {
System.out.println("Deleting items listed in mapfile: " + mapFile);
// read in the mapfile
@@ -427,18 +455,14 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
// now delete everything that appeared in the mapFile
Iterator i = myhash.keySet().iterator();
- while (i.hasNext())
- {
+ while (i.hasNext()) {
String itemID = myhash.get(i.next());
- if (itemID.indexOf('/') != -1)
- {
+ if (itemID.indexOf('/') != -1) {
String myhandle = itemID;
System.out.println("Deleting item " + myhandle);
deleteItem(c, myhandle);
- }
- else
- {
+ } else {
// it's an ID
Item myitem = itemService.findByIdOrLegacyId(c, itemID);
System.out.println("Deleting item " + itemID);
@@ -450,18 +474,18 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
/**
* item? try and add it to the archive.
- * @param c current Context
+ *
+ * @param c current Context
* @param mycollections - add item to these Collections.
- * @param path - directory containing the item directories.
- * @param itemname handle - non-null means we have a pre-defined handle already
- * @param mapOut - mapfile we're writing
- * @param template whether to use collection template item as starting point
+ * @param path - directory containing the item directories.
+ * @param itemname handle - non-null means we have a pre-defined handle already
+ * @param mapOut - mapfile we're writing
+ * @param template whether to use collection template item as starting point
* @return Item
* @throws Exception if error occurs
*/
protected Item addItem(Context c, List mycollections, String path,
- String itemname, PrintWriter mapOut, boolean template) throws Exception
- {
+ String itemname, PrintWriter mapOut, boolean template) throws Exception {
String mapOutputString = null;
System.out.println("Adding item from directory " + itemname);
@@ -472,28 +496,25 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
WorkspaceItem wi = null;
WorkflowItem wfi = null;
- if (!isTest)
- {
+ if (!isTest) {
wi = workspaceItemService.create(c, mycollections.iterator().next(), template);
myitem = wi.getItem();
}
// now fill out dublin core for item
loadMetadata(c, myitem, path + File.separatorChar + itemname
- + File.separatorChar);
+ + File.separatorChar);
// and the bitstreams from the contents file
// process contents file, add bistreams and bundles, return any
// non-standard permissions
List options = processContentsFile(c, myitem, path
- + File.separatorChar + itemname, "contents");
+ + File.separatorChar + itemname, "contents");
- if (useWorkflow)
- {
+ if (useWorkflow) {
// don't process handle file
// start up a workflow
- if (!isTest)
- {
+ if (!isTest) {
// Should we send a workflow alert email or not?
if (useWorkflowSendEmail) {
wfi = workflowService.start(c, wi);
@@ -504,16 +525,13 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
// send ID to the mapfile
mapOutputString = itemname + " " + myitem.getID();
}
- }
- else
- {
+ } else {
// only process handle file if not using workflow system
String myhandle = processHandleFile(c, myitem, path
- + File.separatorChar + itemname, "handle");
+ + File.separatorChar + itemname, "handle");
// put item in system
- if (!isTest)
- {
+ if (!isTest) {
try {
installItemService.installItem(c, wi, myhandle);
} catch (Exception e) {
@@ -529,28 +547,23 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
}
// set permissions if specified in contents file
- if (options.size() > 0)
- {
+ if (options.size() > 0) {
System.out.println("Processing options");
processOptions(c, myitem, options);
}
}
// now add to multiple collections if requested
- if (mycollections.size() > 1)
- {
- for (int i = 1; i < mycollections.size(); i++)
- {
- if (!isTest)
- {
+ if (mycollections.size() > 1) {
+ for (int i = 1; i < mycollections.size(); i++) {
+ if (!isTest) {
collectionService.addItem(c, mycollections.get(i), myitem);
}
}
}
// made it this far, everything is fine, commit transaction
- if (mapOut != null)
- {
+ if (mapOut != null) {
mapOut.println(mapOutputString);
}
@@ -562,10 +575,8 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
}
// remove, given the actual item
- protected void deleteItem(Context c, Item myitem) throws Exception
- {
- if (!isTest)
- {
+ protected void deleteItem(Context c, Item myitem) throws Exception {
+ if (!isTest) {
ArrayList removeList = new ArrayList<>();
List collections = myitem.getCollections();
@@ -582,18 +593,14 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
}
// remove, given a handle
- protected void deleteItem(Context c, String myhandle) throws Exception
- {
+ protected void deleteItem(Context c, String myhandle) throws Exception {
// bit of a hack - to remove an item, you must remove it
// from all collections it's a part of, then it will be removed
Item myitem = (Item) handleService.resolveToObject(c, myhandle);
- if (myitem == null)
- {
+ if (myitem == null) {
System.out.println("Error - cannot locate item - already deleted?");
- }
- else
- {
+ } else {
deleteItem(c, myitem);
c.uncacheEntity(myitem);
}
@@ -603,50 +610,38 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
// utility methods
////////////////////////////////////
// read in the map file and generate a hashmap of (file,handle) pairs
- protected Map readMapFile(String filename) throws Exception
- {
+ protected Map readMapFile(String filename) throws Exception {
Map myHash = new HashMap<>();
BufferedReader is = null;
- try
- {
+ try {
is = new BufferedReader(new FileReader(filename));
String line;
- while ((line = is.readLine()) != null)
- {
+ while ((line = is.readLine()) != null) {
String myFile;
String myHandle;
// a line should be archive filenamehandle
StringTokenizer st = new StringTokenizer(line);
- if (st.hasMoreTokens())
- {
+ if (st.hasMoreTokens()) {
myFile = st.nextToken();
- }
- else
- {
+ } else {
throw new Exception("Bad mapfile line:\n" + line);
}
- if (st.hasMoreTokens())
- {
+ if (st.hasMoreTokens()) {
myHandle = st.nextToken();
- }
- else
- {
+ } else {
throw new Exception("Bad mapfile line:\n" + line);
}
myHash.put(myFile, myHandle);
}
- }
- finally
- {
- if (is != null)
- {
+ } finally {
+ if (is != null) {
is.close();
}
}
@@ -656,25 +651,22 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
// Load all metadata schemas into the item.
protected void loadMetadata(Context c, Item myitem, String path)
- throws SQLException, IOException, ParserConfigurationException,
- SAXException, TransformerException, AuthorizeException
- {
+ throws SQLException, IOException, ParserConfigurationException,
+ SAXException, TransformerException, AuthorizeException {
// Load the dublin core metadata
loadDublinCore(c, myitem, path + "dublin_core.xml");
// Load any additional metadata schemas
File folder = new File(path);
File file[] = folder.listFiles(metadataFileFilter);
- for (int i = 0; i < file.length; i++)
- {
+ for (int i = 0; i < file.length; i++) {
loadDublinCore(c, myitem, file[i].getAbsolutePath());
}
}
protected void loadDublinCore(Context c, Item myitem, String filename)
- throws SQLException, IOException, ParserConfigurationException,
- SAXException, TransformerException, AuthorizeException
- {
+ throws SQLException, IOException, ParserConfigurationException,
+ SAXException, TransformerException, AuthorizeException {
Document document = loadXML(filename);
// Get the schema, for backward compatibility we will default to the
@@ -683,44 +675,36 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
String schema;
NodeList metadata = XPathAPI.selectNodeList(document, "/dublin_core");
Node schemaAttr = metadata.item(0).getAttributes().getNamedItem(
- "schema");
- if (schemaAttr == null)
- {
+ "schema");
+ if (schemaAttr == null) {
schema = MetadataSchema.DC_SCHEMA;
- }
- else
- {
+ } else {
schema = schemaAttr.getNodeValue();
}
// Get the nodes corresponding to formats
NodeList dcNodes = XPathAPI.selectNodeList(document,
- "/dublin_core/dcvalue");
+ "/dublin_core/dcvalue");
- if (!isQuiet)
- {
+ if (!isQuiet) {
System.out.println("\tLoading dublin core from " + filename);
}
// Add each one as a new format to the registry
- for (int i = 0; i < dcNodes.getLength(); i++)
- {
+ for (int i = 0; i < dcNodes.getLength(); i++) {
Node n = dcNodes.item(i);
addDCValue(c, myitem, schema, n);
}
}
- protected void addDCValue(Context c, Item i, String schema, Node n) throws TransformerException, SQLException, AuthorizeException
- {
+ protected void addDCValue(Context c, Item i, String schema, Node n)
+ throws TransformerException, SQLException, AuthorizeException {
String value = getStringValue(n); //n.getNodeValue();
// compensate for empty value getting read as "null", which won't display
- if (value == null)
- {
+ if (value == null) {
value = "";
- }
- else
- {
- value = value.trim();
+ } else {
+ value = value.trim();
}
// //getElementData(n, "element");
String element = getAttributeValue(n, "element");
@@ -728,43 +712,37 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
// //getElementData(n,
// "qualifier");
String language = getAttributeValue(n, "language");
- if (language != null)
- {
+ if (language != null) {
language = language.trim();
}
- if (!isQuiet)
- {
+ if (!isQuiet) {
System.out.println("\tSchema: " + schema + " Element: " + element + " Qualifier: " + qualifier
- + " Value: " + value);
+ + " Value: " + value);
}
- if ("none".equals(qualifier) || "".equals(qualifier))
- {
+ if ("none".equals(qualifier) || "".equals(qualifier)) {
qualifier = null;
}
// only add metadata if it is no test and there is an actual value
- if (!isTest && !value.equals(""))
- {
+ if (!isTest && !value.equals("")) {
itemService.addMetadata(c, i, schema, element, qualifier, language, value);
- }
- else
- {
+ } else {
// If we're just test the import, let's check that the actual metadata field exists.
- MetadataSchema foundSchema = metadataSchemaService.find(c,schema);
+ MetadataSchema foundSchema = metadataSchemaService.find(c, schema);
- if (foundSchema == null)
- {
- System.out.println("ERROR: schema '"+schema+"' was not found in the registry.");
- return;
- }
+ if (foundSchema == null) {
+ System.out.println("ERROR: schema '" + schema + "' was not found in the registry.");
+ return;
+ }
- MetadataField foundField = metadataFieldService.findByElement(c, foundSchema, element, qualifier);
+ MetadataField foundField = metadataFieldService.findByElement(c, foundSchema, element, qualifier);
- if (foundField == null)
- {
- System.out.println("ERROR: Metadata field: '"+schema+"."+element+"."+qualifier+"' was not found in the registry.");
- return;
+ if (foundField == null) {
+ System.out.println(
+ "ERROR: Metadata field: '" + schema + "." + element + "." + qualifier + "' was not found in the " +
+ "registry.");
+ return;
}
}
}
@@ -775,61 +753,49 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
* which the item should be inserted. If it does not exist or it
* is empty return null.
*
- * @param c The context
- * @param path The path to the data directory for this item
+ * @param c The context
+ * @param path The path to the data directory for this item
* @param filename The collections file filename. Should be "collections"
* @return A list of collections in which to insert the item or null
- * @throws IOException if IO error
+ * @throws IOException if IO error
* @throws SQLException if database error
*/
- protected List processCollectionFile(Context c, String path, String filename) throws IOException, SQLException
- {
+ protected List processCollectionFile(Context c, String path, String filename)
+ throws IOException, SQLException {
File file = new File(path + File.separatorChar + filename);
ArrayList collections = new ArrayList<>();
List result = null;
System.out.println("Processing collections file: " + filename);
- if(file.exists())
- {
+ if (file.exists()) {
BufferedReader br = null;
- try
- {
+ try {
br = new BufferedReader(new FileReader(file));
String line = null;
- while ((line = br.readLine()) != null)
- {
+ while ((line = br.readLine()) != null) {
DSpaceObject obj = null;
- if (line.indexOf('/') != -1)
- {
+ if (line.indexOf('/') != -1) {
obj = handleService.resolveToObject(c, line);
- if (obj == null || obj.getType() != Constants.COLLECTION)
- {
+ if (obj == null || obj.getType() != Constants.COLLECTION) {
obj = null;
}
- }
- else
- {
+ } else {
obj = collectionService.find(c, UUID.fromString(line));
}
if (obj == null) {
throw new IllegalArgumentException("Cannot resolve " + line + " to a collection.");
}
- collections.add((Collection)obj);
+ collections.add((Collection) obj);
}
result = collections;
- }
- catch (FileNotFoundException e)
- {
+ } catch (FileNotFoundException e) {
System.out.println("No collections file found.");
- }
- finally
- {
- if (br != null)
- {
+ } finally {
+ if (br != null) {
try {
br.close();
} catch (IOException e) {
@@ -844,23 +810,21 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
/**
* Read in the handle file contents or return null if empty or doesn't exist
- * @param c DSpace context
- * @param i DSpace item
- * @param path path to handle file
+ *
+ * @param c DSpace context
+ * @param i DSpace item
+ * @param path path to handle file
* @param filename name of file
* @return handle file contents or null if doesn't exist
*/
- protected String processHandleFile(Context c, Item i, String path, String filename)
- {
+ protected String processHandleFile(Context c, Item i, String path, String filename) {
File file = new File(path + File.separatorChar + filename);
String result = null;
System.out.println("Processing handle file: " + filename);
- if (file.exists())
- {
+ if (file.exists()) {
BufferedReader is = null;
- try
- {
+ try {
is = new BufferedReader(new FileReader(file));
// result gets contents of file, or null
@@ -868,34 +832,22 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
System.out.println("read handle: '" + result + "'");
- }
- catch (FileNotFoundException e)
- {
+ } catch (FileNotFoundException e) {
// probably no handle file, just return null
System.out.println("It appears there is no handle file -- generating one");
- }
- catch (IOException e)
- {
+ } catch (IOException e) {
// probably no handle file, just return null
System.out.println("It appears there is no handle file -- generating one");
- }
- finally
- {
- if (is != null)
- {
- try
- {
+ } finally {
+ if (is != null) {
+ try {
is.close();
- }
- catch (IOException e1)
- {
+ } catch (IOException e1) {
System.err.println("Non-critical problem releasing resources.");
}
}
}
- }
- else
- {
+ } else {
// probably no handle file, just return null
System.out.println("It appears there is no handle file -- generating one");
}
@@ -907,92 +859,73 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
* Given a contents file and an item, stuffing it with bitstreams from the
* contents file Returns a List of Strings with lines from the contents
* file that request non-default bitstream permission
- * @param c DSpace Context
- * @param i DSpace item
- * @param path path as string
+ *
+ * @param c DSpace Context
+ * @param i DSpace item
+ * @param path path as string
* @param filename file name
* @return List of Strings
- * @throws SQLException if database error
- * @throws IOException if IO error
+ * @throws SQLException if database error
+ * @throws IOException if IO error
* @throws AuthorizeException if authorization error
*/
protected List processContentsFile(Context c, Item i, String path,
- String filename) throws SQLException, IOException,
- AuthorizeException
- {
+ String filename) throws SQLException, IOException,
+ AuthorizeException {
File contentsFile = new File(path + File.separatorChar + filename);
String line = "";
List options = new ArrayList<>();
System.out.println("\tProcessing contents file: " + contentsFile);
- if (contentsFile.exists())
- {
+ if (contentsFile.exists()) {
BufferedReader is = null;
- try
- {
+ try {
is = new BufferedReader(new FileReader(contentsFile));
- while ((line = is.readLine()) != null)
- {
- if ("".equals(line.trim()))
- {
+ while ((line = is.readLine()) != null) {
+ if ("".equals(line.trim())) {
continue;
}
- // 1) registered into dspace (leading -r)
+ // 1) registered into dspace (leading -r)
// 2) imported conventionally into dspace (no -r)
- if (line.trim().startsWith("-r "))
- {
+ if (line.trim().startsWith("-r ")) {
// line should be one of these two:
// -r -s n -f filepath
// -r -s n -f filepath\tbundle:bundlename
// where
- // n is the assetstore number
- // filepath is the path of the file to be registered
- // bundlename is an optional bundle name
+ // n is the assetstore number
+ // filepath is the path of the file to be registered
+ // bundlename is an optional bundle name
String sRegistrationLine = line.trim();
int iAssetstore = -1;
String sFilePath = null;
String sBundle = null;
StringTokenizer tokenizer = new StringTokenizer(sRegistrationLine);
- while (tokenizer.hasMoreTokens())
- {
+ while (tokenizer.hasMoreTokens()) {
String sToken = tokenizer.nextToken();
- if ("-r".equals(sToken))
- {
+ if ("-r".equals(sToken)) {
continue;
- }
- else if ("-s".equals(sToken) && tokenizer.hasMoreTokens())
- {
- try
- {
+ } else if ("-s".equals(sToken) && tokenizer.hasMoreTokens()) {
+ try {
iAssetstore =
Integer.parseInt(tokenizer.nextToken());
- }
- catch (NumberFormatException e)
- {
+ } catch (NumberFormatException e) {
// ignore - iAssetstore remains -1
}
- }
- else if ("-f".equals(sToken) && tokenizer.hasMoreTokens())
- {
+ } else if ("-f".equals(sToken) && tokenizer.hasMoreTokens()) {
sFilePath = tokenizer.nextToken();
- }
- else if (sToken.startsWith("bundle:"))
- {
+ } else if (sToken.startsWith("bundle:")) {
sBundle = sToken.substring(7);
- }
- else
- {
+ } else {
// unrecognized token - should be no problem
}
} // while
- if (iAssetstore == -1 || sFilePath == null)
- {
+ if (iAssetstore == -1 || sFilePath == null) {
System.out.println("\tERROR: invalid contents file line");
System.out.println("\t\tSkipping line: "
- + sRegistrationLine);
+ + sRegistrationLine);
continue;
}
@@ -1001,40 +934,34 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
String descriptionMarker = "\tdescription:";
int dMarkerIndex = line.indexOf(descriptionMarker);
int dEndIndex = 0;
- if (dMarkerIndex > 0)
- {
- dEndIndex = line.indexOf("\t", dMarkerIndex + 1);
- if (dEndIndex == -1)
- {
- dEndIndex = line.length();
- }
- descriptionExists = true;
+ if (dMarkerIndex > 0) {
+ dEndIndex = line.indexOf("\t", dMarkerIndex + 1);
+ if (dEndIndex == -1) {
+ dEndIndex = line.length();
+ }
+ descriptionExists = true;
}
String sDescription = "";
- if (descriptionExists)
- {
- sDescription = line.substring(dMarkerIndex, dEndIndex);
- sDescription = sDescription.replaceFirst("description:", "");
+ if (descriptionExists) {
+ sDescription = line.substring(dMarkerIndex, dEndIndex);
+ sDescription = sDescription.replaceFirst("description:", "");
}
registerBitstream(c, i, iAssetstore, sFilePath, sBundle, sDescription);
System.out.println("\tRegistering Bitstream: " + sFilePath
- + "\tAssetstore: " + iAssetstore
- + "\tBundle: " + sBundle
- + "\tDescription: " + sDescription);
- continue; // process next line in contents file
+ + "\tAssetstore: " + iAssetstore
+ + "\tBundle: " + sBundle
+ + "\tDescription: " + sDescription);
+ continue; // process next line in contents file
}
int bitstreamEndIndex = line.indexOf('\t');
- if (bitstreamEndIndex == -1)
- {
+ if (bitstreamEndIndex == -1) {
// no extra info
processContentFileEntry(c, i, path, line, null, false);
System.out.println("\tBitstream: " + line);
- }
- else
- {
+ } else {
String bitstreamName = line.substring(0, bitstreamEndIndex);
@@ -1046,11 +973,9 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
String bundleMarker = "\tbundle:";
int bMarkerIndex = line.indexOf(bundleMarker);
int bEndIndex = 0;
- if (bMarkerIndex > 0)
- {
+ if (bMarkerIndex > 0) {
bEndIndex = line.indexOf("\t", bMarkerIndex + 1);
- if (bEndIndex == -1)
- {
+ if (bEndIndex == -1) {
bEndIndex = line.length();
}
bundleExists = true;
@@ -1060,11 +985,9 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
String permissionsMarker = "\tpermissions:";
int pMarkerIndex = line.indexOf(permissionsMarker);
int pEndIndex = 0;
- if (pMarkerIndex > 0)
- {
+ if (pMarkerIndex > 0) {
pEndIndex = line.indexOf("\t", pMarkerIndex + 1);
- if (pEndIndex == -1)
- {
+ if (pEndIndex == -1) {
pEndIndex = line.length();
}
permissionsExist = true;
@@ -1074,11 +997,9 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
String descriptionMarker = "\tdescription:";
int dMarkerIndex = line.indexOf(descriptionMarker);
int dEndIndex = 0;
- if (dMarkerIndex > 0)
- {
+ if (dMarkerIndex > 0) {
dEndIndex = line.indexOf("\t", dMarkerIndex + 1);
- if (dEndIndex == -1)
- {
+ if (dEndIndex == -1) {
dEndIndex = line.length();
}
descriptionExists = true;
@@ -1088,65 +1009,52 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
String primaryBitstreamMarker = "\tprimary:true";
boolean primary = false;
String primaryStr = "";
- if (line.contains(primaryBitstreamMarker))
- {
+ if (line.contains(primaryBitstreamMarker)) {
primary = true;
primaryStr = "\t **Setting as primary bitstream**";
}
- if (bundleExists)
- {
+ if (bundleExists) {
String bundleName = line.substring(bMarkerIndex
- + bundleMarker.length(), bEndIndex).trim();
+ + bundleMarker.length(), bEndIndex).trim();
processContentFileEntry(c, i, path, bitstreamName, bundleName, primary);
System.out.println("\tBitstream: " + bitstreamName +
- "\tBundle: " + bundleName +
- primaryStr);
- }
- else
- {
+ "\tBundle: " + bundleName +
+ primaryStr);
+ } else {
processContentFileEntry(c, i, path, bitstreamName, null, primary);
System.out.println("\tBitstream: " + bitstreamName + primaryStr);
}
- if (permissionsExist || descriptionExists)
- {
+ if (permissionsExist || descriptionExists) {
String extraInfo = bitstreamName;
- if (permissionsExist)
- {
+ if (permissionsExist) {
extraInfo = extraInfo
- + line.substring(pMarkerIndex, pEndIndex);
+ + line.substring(pMarkerIndex, pEndIndex);
}
- if (descriptionExists)
- {
+ if (descriptionExists) {
extraInfo = extraInfo
- + line.substring(dMarkerIndex, dEndIndex);
+ + line.substring(dMarkerIndex, dEndIndex);
}
options.add(extraInfo);
}
}
}
- }
- finally
- {
- if (is != null)
- {
+ } finally {
+ if (is != null) {
is.close();
}
}
- }
- else
- {
+ } else {
File dir = new File(path);
String[] dirListing = dir.list();
- for (String fileName : dirListing)
- {
- if (!"dublin_core.xml".equals(fileName) && !fileName.equals("handle") && !metadataFileFilter.accept(dir, fileName))
- {
+ for (String fileName : dirListing) {
+ if (!"dublin_core.xml".equals(fileName) && !fileName.equals("handle") && !metadataFileFilter
+ .accept(dir, fileName)) {
throw new FileNotFoundException("No contents file found");
}
}
@@ -1159,56 +1067,48 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
/**
* each entry represents a bitstream....
- * @param c DSpace Context
- * @param i Dspace Item
- * @param path path to file
- * @param fileName file name
+ *
+ * @param c DSpace Context
+ * @param i Dspace Item
+ * @param path path to file
+ * @param fileName file name
* @param bundleName bundle name
- * @param primary if primary bitstream
- * @throws SQLException if database error
- * @throws IOException if IO error
+ * @param primary if primary bitstream
+ * @throws SQLException if database error
+ * @throws IOException if IO error
* @throws AuthorizeException if authorization error
*/
protected void processContentFileEntry(Context c, Item i, String path,
- String fileName, String bundleName, boolean primary) throws SQLException,
- IOException, AuthorizeException
- {
+ String fileName, String bundleName, boolean primary) throws SQLException,
+ IOException, AuthorizeException {
String fullpath = path + File.separatorChar + fileName;
// get an input stream
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
- fullpath));
+ fullpath));
Bitstream bs = null;
String newBundleName = bundleName;
- if (bundleName == null)
- {
+ if (bundleName == null) {
// is it license.txt?
- if ("license.txt".equals(fileName))
- {
+ if ("license.txt".equals(fileName)) {
newBundleName = "LICENSE";
- }
- else
- {
+ } else {
// call it ORIGINAL
newBundleName = "ORIGINAL";
}
}
- if (!isTest)
- {
+ if (!isTest) {
// find the bundle
List bundles = itemService.getBundles(i, newBundleName);
Bundle targetBundle = null;
- if (bundles.size() < 1)
- {
+ if (bundles.size() < 1) {
// not found, create a new one
targetBundle = bundleService.create(c, i, newBundleName);
- }
- else
- {
+ } else {
// put bitstreams into first bundle
targetBundle = bundles.iterator().next();
}
@@ -1225,8 +1125,7 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
bitstreamService.setFormat(c, bs, bf);
// Is this a the primary bitstream?
- if (primary)
- {
+ if (primary) {
targetBundle.setPrimaryBitstreamID(bs);
bundleService.update(c, targetBundle);
}
@@ -1240,100 +1139,89 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
/**
* Register the bitstream file into DSpace
*
- * @param c DSpace Context
- * @param i DSpace Item
- * @param assetstore assetstore number
+ * @param c DSpace Context
+ * @param i DSpace Item
+ * @param assetstore assetstore number
* @param bitstreamPath the full filepath expressed in the contents file
- * @param bundleName bundle name
- * @param description bitstream description
- * @throws SQLException if database error
- * @throws IOException if IO error
+ * @param bundleName bundle name
+ * @param description bitstream description
+ * @throws SQLException if database error
+ * @throws IOException if IO error
* @throws AuthorizeException if authorization error
*/
protected void registerBitstream(Context c, Item i, int assetstore,
- String bitstreamPath, String bundleName, String description )
- throws SQLException, IOException, AuthorizeException
- {
+ String bitstreamPath, String bundleName, String description)
+ throws SQLException, IOException, AuthorizeException {
// TODO validate assetstore number
// TODO make sure the bitstream is there
Bitstream bs = null;
String newBundleName = bundleName;
- if (StringUtils.isBlank(bundleName))
- {
+ if (StringUtils.isBlank(bundleName)) {
// is it license.txt?
- if (bitstreamPath.endsWith("license.txt"))
- {
+ if (bitstreamPath.endsWith("license.txt")) {
newBundleName = "LICENSE";
- }
- else
- {
+ } else {
// call it ORIGINAL
newBundleName = "ORIGINAL";
}
}
- if(!isTest)
- {
- // find the bundle
- List bundles = itemService.getBundles(i, newBundleName);
- Bundle targetBundle = null;
+ if (!isTest) {
+ // find the bundle
+ List bundles = itemService.getBundles(i, newBundleName);
+ Bundle targetBundle = null;
- if( bundles.size() < 1 )
- {
- // not found, create a new one
- targetBundle = bundleService.create(c, i, newBundleName);
- }
- else
- {
- // put bitstreams into first bundle
- targetBundle = bundles.iterator().next();
- }
+ if (bundles.size() < 1) {
+ // not found, create a new one
+ targetBundle = bundleService.create(c, i, newBundleName);
+ } else {
+ // put bitstreams into first bundle
+ targetBundle = bundles.iterator().next();
+ }
- // now add the bitstream
- bs = bitstreamService.register(c, targetBundle, assetstore, bitstreamPath);
+ // now add the bitstream
+ bs = bitstreamService.register(c, targetBundle, assetstore, bitstreamPath);
- // set the name to just the filename
- int iLastSlash = bitstreamPath.lastIndexOf('/');
- bs.setName(c, bitstreamPath.substring(iLastSlash + 1));
+ // set the name to just the filename
+ int iLastSlash = bitstreamPath.lastIndexOf('/');
+ bs.setName(c, bitstreamPath.substring(iLastSlash + 1));
- // Identify the format
- // FIXME - guessing format guesses license.txt incorrectly as a text file format!
- BitstreamFormat bf = bitstreamFormatService.guessFormat(c, bs);
+ // Identify the format
+ // FIXME - guessing format guesses license.txt incorrectly as a text file format!
+ BitstreamFormat bf = bitstreamFormatService.guessFormat(c, bs);
bitstreamService.setFormat(c, bs, bf);
- bs.setDescription(c, description);
+ bs.setDescription(c, description);
bitstreamService.update(c, bs);
}
}
/**
- *
* Process the Options to apply to the Item. The options are tab delimited
*
* Options:
- * {@code
- * 48217870-MIT.pdf permissions: -r 'MIT Users' description: Full printable version (MIT only)
- * permissions:[r|w]-['group name']
- * description: 'the description of the file'
- * }
- * where:
- * {@code
- * [r|w] (meaning: read|write)
- * ['MIT Users'] (the group name)
- * }
- * @param c DSpace Context
- * @param myItem DSpace Item
+ * {@code
+ * 48217870-MIT.pdf permissions: -r 'MIT Users' description: Full printable version (MIT only)
+ * permissions:[r|w]-['group name']
+ * description: 'the description of the file'
+ * }
+ * where:
+ * {@code
+ * [r|w] (meaning: read|write)
+ * ['MIT Users'] (the group name)
+ * }
+ *
+ * @param c DSpace Context
+ * @param myItem DSpace Item
* @param options List of option strings
- * @throws SQLException if database error
+ * @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
protected void processOptions(Context c, Item myItem, List options)
- throws SQLException, AuthorizeException
- {
- for (String line : options)
- {
+ throws SQLException, AuthorizeException {
+ for (String line : options) {
System.out.println("\tprocessing " + line);
boolean permissionsExist = false;
@@ -1342,11 +1230,9 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
String permissionsMarker = "\tpermissions:";
int pMarkerIndex = line.indexOf(permissionsMarker);
int pEndIndex = 0;
- if (pMarkerIndex > 0)
- {
+ if (pMarkerIndex > 0) {
pEndIndex = line.indexOf("\t", pMarkerIndex + 1);
- if (pEndIndex == -1)
- {
+ if (pEndIndex == -1) {
pEndIndex = line.length();
}
permissionsExist = true;
@@ -1355,11 +1241,9 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
String descriptionMarker = "\tdescription:";
int dMarkerIndex = line.indexOf(descriptionMarker);
int dEndIndex = 0;
- if (dMarkerIndex > 0)
- {
+ if (dMarkerIndex > 0) {
dEndIndex = line.indexOf("\t", dMarkerIndex + 1);
- if (dEndIndex == -1)
- {
+ if (dEndIndex == -1) {
dEndIndex = line.length();
}
descriptionExists = true;
@@ -1371,10 +1255,9 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
int actionID = -1;
String groupName = "";
Group myGroup = null;
- if (permissionsExist)
- {
+ if (permissionsExist) {
String thisPermission = line.substring(pMarkerIndex
- + permissionsMarker.length(), pEndIndex);
+ + permissionsMarker.length(), pEndIndex);
// get permission type ("read" or "write")
int pTypeIndex = thisPermission.indexOf('-');
@@ -1385,92 +1268,71 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
// if not in single quotes, assume everything after type flag is
// group name
- if (groupIndex == -1)
- {
+ if (groupIndex == -1) {
groupIndex = thisPermission.indexOf(' ', pTypeIndex);
groupEndIndex = thisPermission.length();
}
groupName = thisPermission.substring(groupIndex + 1,
- groupEndIndex);
+ groupEndIndex);
- if (thisPermission.toLowerCase().charAt(pTypeIndex + 1) == 'r')
- {
+ if (thisPermission.toLowerCase().charAt(pTypeIndex + 1) == 'r') {
actionID = Constants.READ;
- }
- else if (thisPermission.toLowerCase().charAt(pTypeIndex + 1) == 'w')
- {
+ } else if (thisPermission.toLowerCase().charAt(pTypeIndex + 1) == 'w') {
actionID = Constants.WRITE;
}
- try
- {
+ try {
myGroup = groupService.findByName(c, groupName);
- }
- catch (SQLException sqle)
- {
+ } catch (SQLException sqle) {
System.out.println("SQL Exception finding group name: "
- + groupName);
+ + groupName);
// do nothing, will check for null group later
}
}
String thisDescription = "";
- if (descriptionExists)
- {
+ if (descriptionExists) {
thisDescription = line.substring(
- dMarkerIndex + descriptionMarker.length(), dEndIndex)
- .trim();
+ dMarkerIndex + descriptionMarker.length(), dEndIndex)
+ .trim();
}
Bitstream bs = null;
boolean notfound = true;
- if (!isTest)
- {
+ if (!isTest) {
// find bitstream
List bitstreams = itemService.getNonInternalBitstreams(c, myItem);
- for (int j = 0; j < bitstreams.size() && notfound; j++)
- {
- if (bitstreams.get(j).getName().equals(bitstreamName))
- {
+ for (int j = 0; j < bitstreams.size() && notfound; j++) {
+ if (bitstreams.get(j).getName().equals(bitstreamName)) {
bs = bitstreams.get(j);
notfound = false;
}
}
}
- if (notfound && !isTest)
- {
+ if (notfound && !isTest) {
// this should never happen
System.out.println("\tdefault permissions set for "
- + bitstreamName);
- }
- else if (!isTest)
- {
- if (permissionsExist)
- {
- if (myGroup == null)
- {
+ + bitstreamName);
+ } else if (!isTest) {
+ if (permissionsExist) {
+ if (myGroup == null) {
System.out.println("\t" + groupName
- + " not found, permissions set to default");
- }
- else if (actionID == -1)
- {
+ + " not found, permissions set to default");
+ } else if (actionID == -1) {
System.out
- .println("\tinvalid permissions flag, permissions set to default");
- }
- else
- {
+ .println("\tinvalid permissions flag, permissions set to default");
+ } else {
System.out.println("\tSetting special permissions for "
- + bitstreamName);
+ + bitstreamName);
setPermission(c, myGroup, actionID, bs);
}
}
- if (descriptionExists)
- {
+ if (descriptionExists) {
System.out.println("\tSetting description for "
- + bitstreamName);
+ + bitstreamName);
bs.setDescription(c, thisDescription);
bitstreamService.update(c, bs);
}
@@ -1481,19 +1343,17 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
/**
* Set the Permission on a Bitstream.
*
- * @param c DSpace Context
- * @param g Dspace Group
+ * @param c DSpace Context
+ * @param g Dspace Group
* @param actionID action identifier
- * @param bs Bitstream
- * @see org.dspace.core.Constants
- * @throws SQLException if database error
+ * @param bs Bitstream
+ * @throws SQLException if database error
* @throws AuthorizeException if authorization error
+ * @see org.dspace.core.Constants
*/
protected void setPermission(Context c, Group g, int actionID, Bitstream bs)
- throws SQLException, AuthorizeException
- {
- if (!isTest)
- {
+ throws SQLException, AuthorizeException {
+ if (!isTest) {
// remove the default policy
authorizeService.removeAllPolicies(c, bs);
@@ -1505,15 +1365,10 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
rp.setGroup(g);
resourcePolicyService.update(c, rp);
- }
- else
- {
- if (actionID == Constants.READ)
- {
+ } else {
+ if (actionID == Constants.READ) {
System.out.println("\t\tpermissions: READ for " + g.getName());
- }
- else if (actionID == Constants.WRITE)
- {
+ } else if (actionID == Constants.WRITE) {
System.out.println("\t\tpermissions: WRITE for " + g.getName());
}
}
@@ -1521,22 +1376,21 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
}
// XML utility methods
+
/**
* Lookup an attribute from a DOM node.
- * @param n node
+ *
+ * @param n node
* @param name attribute name
* @return attribute value
*/
- private String getAttributeValue(Node n, String name)
- {
+ private String getAttributeValue(Node n, String name) {
NamedNodeMap nm = n.getAttributes();
- for (int i = 0; i < nm.getLength(); i++)
- {
+ for (int i = 0; i < nm.getLength(); i++) {
Node node = nm.item(i);
- if (name.equals(node.getNodeName()))
- {
+ if (name.equals(node.getNodeName())) {
return node.getNodeValue();
}
}
@@ -1547,19 +1401,17 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
/**
* Return the String value of a Node.
+ *
* @param node node
* @return string value
*/
- protected String getStringValue(Node node)
- {
+ protected String getStringValue(Node node) {
String value = node.getNodeValue();
- if (node.hasChildNodes())
- {
+ if (node.hasChildNodes()) {
Node first = node.getFirstChild();
- if (first.getNodeType() == Node.TEXT_NODE)
- {
+ if (first.getNodeType() == Node.TEXT_NODE) {
return first.getNodeValue();
}
}
@@ -1570,43 +1422,34 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
/**
* Load in the XML from file.
*
- * @param filename
- * the filename to load from
- *
+ * @param filename the filename to load from
* @return the DOM representation of the XML file
- * @throws IOException if IO error
+ * @throws IOException if IO error
* @throws ParserConfigurationException if config error
- * @throws SAXException if XML error
+ * @throws SAXException if XML error
*/
protected Document loadXML(String filename) throws IOException,
- ParserConfigurationException, SAXException
- {
+ ParserConfigurationException, SAXException {
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
- .newDocumentBuilder();
+ .newDocumentBuilder();
return builder.parse(new File(filename));
}
/**
* Delete a directory and its child files and directories
+ *
* @param path The directory to delete
* @return Whether the deletion was successful or not
*/
- protected boolean deleteDirectory(File path)
- {
- if (path.exists())
- {
+ protected boolean deleteDirectory(File path) {
+ if (path.exists()) {
File[] files = path.listFiles();
- for (int i = 0; i < files.length; i++)
- {
- if (files[i].isDirectory())
- {
+ for (int i = 0; i < files.length; i++) {
+ if (files[i].isDirectory()) {
deleteDirectory(files[i]);
- }
- else
- {
- if (!files[i].delete())
- {
+ } else {
+ if (!files[i].delete()) {
log.error("Unable to delete file: " + files[i].getName());
}
}
@@ -1619,37 +1462,35 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
@Override
public String unzip(File zipfile) throws IOException {
- return unzip(zipfile, null);
+ return unzip(zipfile, null);
}
-
+
@Override
public String unzip(File zipfile, String destDir) throws IOException {
// 2
// does the zip file exist and can we write to the temp directory
- if (!zipfile.canRead())
- {
+ if (!zipfile.canRead()) {
log.error("Zip file '" + zipfile.getAbsolutePath() + "' does not exist, or is not readable.");
}
String destinationDir = destDir;
- if (destinationDir == null){
- destinationDir = tempWorkDir;
+ if (destinationDir == null) {
+ destinationDir = tempWorkDir;
}
File tempdir = new File(destinationDir);
- if (!tempdir.isDirectory())
- {
+ if (!tempdir.isDirectory()) {
log.error("'" + ConfigurationManager.getProperty("org.dspace.app.itemexport.work.dir") +
- "' as defined by the key 'org.dspace.app.itemexport.work.dir' in dspace.cfg " +
- "is not a valid directory");
+ "' as defined by the key 'org.dspace.app.itemexport.work.dir' in dspace.cfg " +
+ "is not a valid directory");
}
- if (!tempdir.exists() && !tempdir.mkdirs())
- {
+ if (!tempdir.exists() && !tempdir.mkdirs()) {
log.error("Unable to create temporary directory: " + tempdir.getAbsolutePath());
}
String sourcedir = destinationDir + System.getProperty("file.separator") + zipfile.getName();
- String zipDir = destinationDir + System.getProperty("file.separator") + zipfile.getName() + System.getProperty("file.separator");
+ String zipDir = destinationDir + System.getProperty("file.separator") + zipfile.getName() + System
+ .getProperty("file.separator");
// 3
@@ -1657,32 +1498,24 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
ZipFile zf = new ZipFile(zipfile);
ZipEntry entry;
Enumeration extends ZipEntry> entries = zf.entries();
- while (entries.hasMoreElements())
- {
+ while (entries.hasMoreElements()) {
entry = entries.nextElement();
- if (entry.isDirectory())
- {
- if (!new File(zipDir + entry.getName()).mkdirs())
- {
+ if (entry.isDirectory()) {
+ if (!new File(zipDir + entry.getName()).mkdirs()) {
log.error("Unable to create contents directory: " + zipDir + entry.getName());
}
- }
- else
- {
+ } else {
System.out.println("Extracting file: " + entry.getName());
log.info("Extracting file: " + entry.getName());
int index = entry.getName().lastIndexOf('/');
- if (index == -1)
- {
+ if (index == -1) {
// Was it created on Windows instead?
index = entry.getName().lastIndexOf('\\');
}
- if (index > 0)
- {
+ if (index > 0) {
File dir = new File(zipDir + entry.getName().substring(0, index));
- if (!dir.exists() && !dir.mkdirs())
- {
+ if (!dir.exists() && !dir.mkdirs()) {
log.error("Unable to create directory: " + dir.getAbsolutePath());
}
@@ -1695,8 +1528,8 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
//regex supports either windows or *nix file paths
String[] entryChunks = entry.getName().split("/|\\\\");
- if(entryChunks.length > 2) {
- if(StringUtils.equals(sourceDirForZip, sourcedir)) {
+ if (entryChunks.length > 2) {
+ if (StringUtils.equals(sourceDirForZip, sourcedir)) {
sourceDirForZip = sourcedir + "/" + entryChunks[0];
}
}
@@ -1707,9 +1540,8 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
int len;
InputStream in = zf.getInputStream(entry);
BufferedOutputStream out = new BufferedOutputStream(
- new FileOutputStream(zipDir + entry.getName()));
- while((len = in.read(buffer)) >= 0)
- {
+ new FileOutputStream(zipDir + entry.getName()));
+ while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
in.close();
@@ -1719,8 +1551,8 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
//Close zip file
zf.close();
-
- if(!StringUtils.equals(sourceDirForZip, sourcedir)) {
+
+ if (!StringUtils.equals(sourceDirForZip, sourcedir)) {
sourcedir = sourceDirForZip;
System.out.println("Set sourceDir using path inside of Zip: " + sourcedir);
log.info("Set sourceDir using path inside of Zip: " + sourcedir);
@@ -1734,67 +1566,67 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
File zipfile = new File(sourcedir + File.separator + zipfilename);
return unzip(zipfile);
}
-
+
/**
* Generate a random filename based on current time
+ *
* @param hidden set to add . as a prefix to make the file hidden
* @return the filename
*/
- protected String generateRandomFilename(boolean hidden)
- {
- String filename = String.format("%s", RandomStringUtils.randomAlphanumeric(8));
+ protected String generateRandomFilename(boolean hidden) {
+ String filename = String.format("%s", RandomStringUtils.randomAlphanumeric(8));
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmm");
String datePart = sdf.format(new Date());
- filename = datePart+"_"+filename;
+ filename = datePart + "_" + filename;
return filename;
}
/**
- *
- * Given a local file or public URL to a zip file that has the Simple Archive Format, this method imports the contents to DSpace
- * @param filepath The filepath to local file or the public URL of the zip file
+ * Given a local file or public URL to a zip file that has the Simple Archive Format, this method imports the
+ * contents to DSpace
+ *
+ * @param filepath The filepath to local file or the public URL of the zip file
* @param owningCollection The owning collection the items will belong to
* @param otherCollections The collections the created items will be inserted to, apart from the owning one
- * @param resumeDir In case of a resume request, the directory that containsthe old mapfile and data
- * @param inputType The input type of the data (bibtex, csv, etc.), in case of local file
- * @param context The context
- * @param template whether to use template item
+ * @param resumeDir In case of a resume request, the directory that containsthe old mapfile and data
+ * @param inputType The input type of the data (bibtex, csv, etc.), in case of local file
+ * @param context The context
+ * @param template whether to use template item
* @throws Exception if error
*/
@Override
- public void processUIImport(String filepath, Collection owningCollection, String[] otherCollections, String resumeDir, String inputType, Context context, final boolean template) throws Exception
- {
- final EPerson oldEPerson = context.getCurrentUser();
- final String[] theOtherCollections = otherCollections;
- final Collection theOwningCollection = owningCollection;
- final String theFilePath = filepath;
- final String theInputType = inputType;
- final String theResumeDir = resumeDir;
+ public void processUIImport(String filepath, Collection owningCollection, String[] otherCollections,
+ String resumeDir, String inputType, Context context, final boolean template)
+ throws Exception {
+ final EPerson oldEPerson = context.getCurrentUser();
+ final String[] theOtherCollections = otherCollections;
+ final Collection theOwningCollection = owningCollection;
+ final String theFilePath = filepath;
+ final String theInputType = inputType;
+ final String theResumeDir = resumeDir;
final boolean useTemplateItem = template;
-
- Thread go = new Thread()
- {
- @Override
- public void run()
- {
- Context context = null;
- String importDir = null;
- EPerson eperson = null;
-
- try {
-
- // create a new dspace context
- context = new Context();
- eperson = ePersonService.find(context, oldEPerson.getID());
- context.setCurrentUser(eperson);
- context.turnOffAuthorisationSystem();
-
- boolean isResume = theResumeDir!=null;
-
- List collectionList = new ArrayList<>();
- if (theOtherCollections != null) {
+ Thread go = new Thread() {
+ @Override
+ public void run() {
+ Context context = null;
+
+ String importDir = null;
+ EPerson eperson = null;
+
+ try {
+
+ // create a new dspace context
+ context = new Context();
+ eperson = ePersonService.find(context, oldEPerson.getID());
+ context.setCurrentUser(eperson);
+ context.turnOffAuthorisationSystem();
+
+ boolean isResume = theResumeDir != null;
+
+ List collectionList = new ArrayList<>();
+ if (theOtherCollections != null) {
for (String colID : theOtherCollections) {
UUID colId = UUID.fromString(colID);
if (!theOwningCollection.getID().equals(colId)) {
@@ -1806,164 +1638,160 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
}
}
- importDir = ConfigurationManager.getProperty("org.dspace.app.batchitemimport.work.dir") + File.separator + "batchuploads" + File.separator + context.getCurrentUser().getID() + File.separator + (isResume?theResumeDir:(new GregorianCalendar()).getTimeInMillis());
- File importDirFile = new File(importDir);
- if (!importDirFile.exists()){
- boolean success = importDirFile.mkdirs();
- if (!success) {
- log.info("Cannot create batch import directory!");
- throw new Exception("Cannot create batch import directory!");
- }
- }
-
- String dataPath = null;
- String dataDir = null;
-
- if (theInputType.equals("saf")){ //In case of Simple Archive Format import (from remote url)
- dataPath = importDirFile + File.separator + "data.zip";
- dataDir = importDirFile + File.separator + "data_unzipped2" + File.separator;
- }
- else if (theInputType.equals("safupload")){ //In case of Simple Archive Format import (from upload file)
- FileUtils.copyFileToDirectory(new File(theFilePath), importDirFile);
- dataPath = importDirFile + File.separator + (new File(theFilePath)).getName();
- dataDir = importDirFile + File.separator + "data_unzipped2" + File.separator;
- }
- else { // For all other imports
- dataPath = importDirFile + File.separator + (new File(theFilePath)).getName();
- dataDir = importDirFile + File.separator + "data" + File.separator;
- }
-
- //Clear these files, if a resume
- if (isResume){
- if (!theInputType.equals("safupload")) {
- (new File(dataPath)).delete();
- }
- (new File(importDirFile + File.separator + "error.txt")).delete();
- FileDeleteStrategy.FORCE.delete(new File(dataDir));
- FileDeleteStrategy.FORCE.delete(new File(importDirFile + File.separator + "data_unzipped" + File.separator));
- }
+ importDir = ConfigurationManager.getProperty(
+ "org.dspace.app.batchitemimport.work.dir") + File.separator + "batchuploads" + File.separator
+ + context
+ .getCurrentUser()
+ .getID() + File.separator + (isResume ? theResumeDir : (new GregorianCalendar())
+ .getTimeInMillis());
+ File importDirFile = new File(importDir);
+ if (!importDirFile.exists()) {
+ boolean success = importDirFile.mkdirs();
+ if (!success) {
+ log.info("Cannot create batch import directory!");
+ throw new Exception("Cannot create batch import directory!");
+ }
+ }
- //In case of Simple Archive Format import we need an extra effort to download the zip file and unzip it
- String sourcePath = null;
- if (theInputType.equals("saf")){
- OutputStream os = new FileOutputStream(dataPath);
+ String dataPath = null;
+ String dataDir = null;
- byte[] b = new byte[2048];
- int length;
+ if (theInputType.equals("saf")) { //In case of Simple Archive Format import (from remote url)
+ dataPath = importDirFile + File.separator + "data.zip";
+ dataDir = importDirFile + File.separator + "data_unzipped2" + File.separator;
+ } else if (theInputType
+ .equals("safupload")) { //In case of Simple Archive Format import (from upload file)
+ FileUtils.copyFileToDirectory(new File(theFilePath), importDirFile);
+ dataPath = importDirFile + File.separator + (new File(theFilePath)).getName();
+ dataDir = importDirFile + File.separator + "data_unzipped2" + File.separator;
+ } else { // For all other imports
+ dataPath = importDirFile + File.separator + (new File(theFilePath)).getName();
+ dataDir = importDirFile + File.separator + "data" + File.separator;
+ }
- InputStream is = new URL(theFilePath).openStream();
- while ((length = is.read(b)) != -1) {
- os.write(b, 0, length);
- }
+ //Clear these files, if a resume
+ if (isResume) {
+ if (!theInputType.equals("safupload")) {
+ (new File(dataPath)).delete();
+ }
+ (new File(importDirFile + File.separator + "error.txt")).delete();
+ FileDeleteStrategy.FORCE.delete(new File(dataDir));
+ FileDeleteStrategy.FORCE
+ .delete(new File(importDirFile + File.separator + "data_unzipped" + File.separator));
+ }
- is.close();
- os.close();
+ //In case of Simple Archive Format import we need an extra effort to download the zip file and
+ // unzip it
+ String sourcePath = null;
+ if (theInputType.equals("saf")) {
+ OutputStream os = new FileOutputStream(dataPath);
- sourcePath = unzip(new File(dataPath), dataDir);
-
- //Move files to the required folder
- FileUtils.moveDirectory(new File(sourcePath), new File(importDirFile + File.separator + "data_unzipped" + File.separator));
- FileDeleteStrategy.FORCE.delete(new File(dataDir));
- dataDir = importDirFile + File.separator + "data_unzipped" + File.separator;
- }
- else if (theInputType.equals("safupload")){
- sourcePath = unzip(new File(dataPath), dataDir);
- //Move files to the required folder
- FileUtils.moveDirectory(new File(sourcePath), new File(importDirFile + File.separator + "data_unzipped" + File.separator));
- FileDeleteStrategy.FORCE.delete(new File(dataDir));
- dataDir = importDirFile + File.separator + "data_unzipped" + File.separator;
- }
-
- //Create mapfile path
- String mapFilePath = importDirFile + File.separator + "mapfile";
-
- List finalCollections = null;
- if (theOwningCollection != null){
+ byte[] b = new byte[2048];
+ int length;
+
+ InputStream is = new URL(theFilePath).openStream();
+ while ((length = is.read(b)) != -1) {
+ os.write(b, 0, length);
+ }
+
+ is.close();
+ os.close();
+
+ sourcePath = unzip(new File(dataPath), dataDir);
+
+ //Move files to the required folder
+ FileUtils.moveDirectory(new File(sourcePath), new File(
+ importDirFile + File.separator + "data_unzipped" + File.separator));
+ FileDeleteStrategy.FORCE.delete(new File(dataDir));
+ dataDir = importDirFile + File.separator + "data_unzipped" + File.separator;
+ } else if (theInputType.equals("safupload")) {
+ sourcePath = unzip(new File(dataPath), dataDir);
+ //Move files to the required folder
+ FileUtils.moveDirectory(new File(sourcePath), new File(
+ importDirFile + File.separator + "data_unzipped" + File.separator));
+ FileDeleteStrategy.FORCE.delete(new File(dataDir));
+ dataDir = importDirFile + File.separator + "data_unzipped" + File.separator;
+ }
+
+ //Create mapfile path
+ String mapFilePath = importDirFile + File.separator + "mapfile";
+
+ List finalCollections = null;
+ if (theOwningCollection != null) {
finalCollections = new ArrayList<>();
- finalCollections.add(theOwningCollection);
+ finalCollections.add(theOwningCollection);
finalCollections.addAll(collectionList);
- }
-
- setResume(isResume);
-
- if (theInputType.equals("saf") || theInputType.equals("safupload")){ //In case of Simple Archive Format import
- addItems(context, finalCollections, dataDir, mapFilePath, template);
- }
- else { // For all other imports (via BTE)
- addBTEItems(context, finalCollections, theFilePath, mapFilePath, useTemplateItem, theInputType, dataDir);
- }
-
- // email message letting user know the file is ready for
+ }
+
+ setResume(isResume);
+
+ if (theInputType.equals("saf") || theInputType
+ .equals("safupload")) { //In case of Simple Archive Format import
+ addItems(context, finalCollections, dataDir, mapFilePath, template);
+ } else { // For all other imports (via BTE)
+ addBTEItems(context, finalCollections, theFilePath, mapFilePath, useTemplateItem, theInputType,
+ dataDir);
+ }
+
+ // email message letting user know the file is ready for
// download
emailSuccessMessage(context, eperson, mapFilePath);
-
- context.complete();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ context.complete();
+
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
String exceptionString = ExceptionUtils.getStackTrace(e);
- try
- {
- File importDirFile = new File(importDir+File.separator+"error.txt");
- PrintWriter errorWriter = new PrintWriter(importDirFile);
- errorWriter.print(exceptionString);
- errorWriter.close();
-
+ try {
+ File importDirFile = new File(importDir + File.separator + "error.txt");
+ PrintWriter errorWriter = new PrintWriter(importDirFile);
+ errorWriter.print(exceptionString);
+ errorWriter.close();
+
emailErrorMessage(eperson, exceptionString);
throw new Exception(e.getMessage());
- }
- catch (Exception e2)
- {
+ } catch (Exception e2) {
// wont throw here
}
- }
-
- finally
- {
+ } finally {
// Make sure the database connection gets closed in all conditions.
- try {
- context.complete();
- } catch (SQLException sqle) {
- context.abort();
- }
+ try {
+ context.complete();
+ } catch (SQLException sqle) {
+ context.abort();
+ }
}
- }
+ }
- };
+ };
- go.isDaemon();
- go.start();
-
- }
+ go.isDaemon();
+ go.start();
+
+ }
@Override
public void emailSuccessMessage(Context context, EPerson eperson,
- String fileName) throws MessagingException
- {
- try
- {
+ String fileName) throws MessagingException {
+ try {
Locale supportedLocale = I18nUtil.getEPersonLocale(eperson);
Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "bte_batch_import_success"));
email.addRecipient(eperson.getEmail());
email.addArgument(fileName);
email.send();
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
log.warn(LogManager.getHeader(context, "emailSuccessMessage", "cannot notify user of import"), e);
}
}
@Override
public void emailErrorMessage(EPerson eperson, String error)
- throws MessagingException
- {
+ throws MessagingException {
log.warn("An error occurred during item import, the user will be notified. " + error);
- try
- {
+ try {
Locale supportedLocale = I18nUtil.getEPersonLocale(eperson);
Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "bte_batch_import_error"));
email.addRecipient(eperson.getEmail());
@@ -1971,38 +1799,32 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
email.addArgument(ConfigurationManager.getProperty("dspace.url") + "/feedback");
email.send();
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
log.warn("error during item import error notification", e);
}
}
-
+
@Override
public List getImportsAvailable(EPerson eperson)
- throws Exception
- {
+ throws Exception {
File uploadDir = new File(getImportUploadableDirectory(eperson));
- if (!uploadDir.exists() || !uploadDir.isDirectory())
- {
+ if (!uploadDir.exists() || !uploadDir.isDirectory()) {
return null;
}
Map fileNames = new TreeMap<>();
- for (String fileName : uploadDir.list())
- {
+ for (String fileName : uploadDir.list()) {
File file = new File(uploadDir + File.separator + fileName);
- if (file.isDirectory()){
-
- BatchUpload upload = new BatchUpload(file);
-
- fileNames.put(upload.getDir().getName(), upload);
+ if (file.isDirectory()) {
+
+ BatchUpload upload = new BatchUpload(file);
+
+ fileNames.put(upload.getDir().getName(), upload);
}
}
- if (fileNames.size() > 0)
- {
+ if (fileNames.size() > 0) {
return new ArrayList<>(fileNames.values());
}
@@ -2011,19 +1833,16 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
@Override
public String getImportUploadableDirectory(EPerson ePerson)
- throws Exception
- {
+ throws Exception {
String uploadDir = ConfigurationManager.getProperty("org.dspace.app.batchitemimport.work.dir");
- if (uploadDir == null)
- {
+ if (uploadDir == null) {
throw new Exception(
- "A dspace.cfg entry for 'org.dspace.app.batchitemimport.work.dir' does not exist.");
+ "A dspace.cfg entry for 'org.dspace.app.batchitemimport.work.dir' does not exist.");
}
String uploadDirBasePath = uploadDir + File.separator + "batchuploads" + File.separator;
//Check for backwards compatibility with the old identifier
File uploadDirectory = new File(uploadDirBasePath + ePerson.getLegacyId());
- if(!uploadDirectory.exists())
- {
+ if (!uploadDirectory.exists()) {
uploadDirectory = new File(uploadDirBasePath + ePerson.getID());
}
@@ -2032,16 +1851,15 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
}
@Override
- public void deleteBatchUpload(Context c, String uploadId) throws Exception
- {
- String uploadDir = null;
- String mapFilePath = null;
+ public void deleteBatchUpload(Context c, String uploadId) throws Exception {
+ String uploadDir = null;
+ String mapFilePath = null;
- uploadDir = getImportUploadableDirectory(c.getCurrentUser()) + File.separator + uploadId;
- mapFilePath = uploadDir + File.separator + "mapfile";
-
- this.deleteItems(c, mapFilePath);
- FileDeleteStrategy.FORCE.delete(new File(uploadDir));
+ uploadDir = getImportUploadableDirectory(c.getCurrentUser()) + File.separator + uploadId;
+ mapFilePath = uploadDir + File.separator + "mapfile";
+
+ this.deleteItems(c, mapFilePath);
+ FileDeleteStrategy.FORCE.delete(new File(uploadDir));
}
@Override
@@ -2051,20 +1869,20 @@ public class ItemImportServiceImpl implements ItemImportService, InitializingBea
@Override
public File getTempWorkDirFile()
- throws IOException
- {
+ throws IOException {
File tempDirFile = new File(getTempWorkDir());
- if(!tempDirFile.exists()) {
+ if (!tempDirFile.exists()) {
boolean success = tempDirFile.mkdirs();
- if (!success)
- {
+ if (!success) {
throw new IOException("Work directory "
- + tempDirFile.getAbsolutePath()
- + " could not be created.");
+ + tempDirFile.getAbsolutePath()
+ + " could not be created.");
+ } else {
+ log.debug("Created directory " + tempDirFile.getAbsolutePath());
}
- else log.debug("Created directory " + tempDirFile.getAbsolutePath());
+ } else {
+ log.debug("Work directory exists: " + tempDirFile.getAbsolutePath());
}
- else log.debug("Work directory exists: " + tempDirFile.getAbsolutePath());
return tempDirFile;
}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemimport/factory/ItemImportServiceFactory.java b/dspace-api/src/main/java/org/dspace/app/itemimport/factory/ItemImportServiceFactory.java
index 6a66e8f0a5..486a6b993e 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemimport/factory/ItemImportServiceFactory.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemimport/factory/ItemImportServiceFactory.java
@@ -11,7 +11,8 @@ import org.dspace.app.itemimport.service.ItemImportService;
import org.dspace.services.factory.DSpaceServicesFactory;
/**
- * Abstract factory to get services for the itemimport package, use ItemImportService.getInstance() to retrieve an implementation
+ * Abstract factory to get services for the itemimport package, use ItemImportService.getInstance() to retrieve an
+ * implementation
*
* @author kevinvandevelde at atmire.com
*/
@@ -19,7 +20,8 @@ public abstract class ItemImportServiceFactory {
public abstract ItemImportService getItemImportService();
- public static ItemImportServiceFactory getInstance(){
- return DSpaceServicesFactory.getInstance().getServiceManager().getServiceByName("itemImportServiceFactory", ItemImportServiceFactory.class);
+ public static ItemImportServiceFactory getInstance() {
+ return DSpaceServicesFactory.getInstance().getServiceManager()
+ .getServiceByName("itemImportServiceFactory", ItemImportServiceFactory.class);
}
}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemimport/factory/ItemImportServiceFactoryImpl.java b/dspace-api/src/main/java/org/dspace/app/itemimport/factory/ItemImportServiceFactoryImpl.java
index b1be4c4255..c344c0b2ad 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemimport/factory/ItemImportServiceFactoryImpl.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemimport/factory/ItemImportServiceFactoryImpl.java
@@ -11,7 +11,8 @@ import org.dspace.app.itemimport.service.ItemImportService;
import org.springframework.beans.factory.annotation.Autowired;
/**
- * Factory implementation to get services for the itemimport package, use ItemImportService.getInstance() to retrieve an implementation
+ * Factory implementation to get services for the itemimport package, use ItemImportService.getInstance() to retrieve
+ * an implementation
*
* @author kevinvandevelde at atmire.com
*/
diff --git a/dspace-api/src/main/java/org/dspace/app/itemimport/service/ItemImportService.java b/dspace-api/src/main/java/org/dspace/app/itemimport/service/ItemImportService.java
index 966c06f36a..296f6a1e9c 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemimport/service/ItemImportService.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemimport/service/ItemImportService.java
@@ -7,16 +7,16 @@
*/
package org.dspace.app.itemimport.service;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import javax.mail.MessagingException;
+
import org.dspace.app.itemimport.BatchUpload;
import org.dspace.content.Collection;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
-import javax.mail.MessagingException;
-import java.io.File;
-import java.io.IOException;
-import java.util.List;
-
/**
* Import items into DSpace. The conventional use is upload files by copying
* them. DSpace writes the item's bitstreams into its assetstore. Metadata is
@@ -37,30 +37,32 @@ public interface ItemImportService {
/**
- *
- * @param c DSpace Context
+ * @param c DSpace Context
* @param mycollections List of Collections
- * @param sourceDir source location
- * @param mapFile map file
- * @param template whether to use template item
- * @throws Exception if error
+ * @param sourceDir source location
+ * @param mapFile map file
+ * @param template whether to use template item
+ * @throws Exception if error
*/
- public void addItemsAtomic(Context c, List mycollections, String sourceDir, String mapFile, boolean template) throws Exception;
+ public void addItemsAtomic(Context c, List mycollections, String sourceDir, String mapFile,
+ boolean template) throws Exception;
/**
* Add items
- * @param c DSpace Context
+ *
+ * @param c DSpace Context
* @param mycollections List of Collections
- * @param sourceDir source location
- * @param mapFile map file
- * @param template whether to use template item
+ * @param sourceDir source location
+ * @param mapFile map file
+ * @param template whether to use template item
* @throws Exception if error
*/
public void addItems(Context c, List mycollections,
- String sourceDir, String mapFile, boolean template) throws Exception;
+ String sourceDir, String mapFile, boolean template) throws Exception;
/**
* Unzip a file
+ *
* @param zipfile file
* @return unzip location
* @throws IOException if error
@@ -69,6 +71,7 @@ public interface ItemImportService {
/**
* Unzip a file to a destination
+ *
* @param zipfile file
* @param destDir destination directory
* @return unzip location
@@ -78,7 +81,8 @@ public interface ItemImportService {
/**
* Unzip a file in a specific source directory
- * @param sourcedir source directory
+ *
+ * @param sourcedir source directory
* @param zipfilename file name
* @return unzip location
* @throws IOException if error
@@ -86,18 +90,19 @@ public interface ItemImportService {
public String unzip(String sourcedir, String zipfilename) throws IOException;
/**
- *
* Given a public URL to a zip file that has the Simple Archive Format, this method imports the contents to DSpace
- * @param url The public URL of the zip file
+ *
+ * @param url The public URL of the zip file
* @param owningCollection The owning collection the items will belong to
- * @param collections The collections the created items will be inserted to, apart from the owning one
- * @param resumeDir In case of a resume request, the directory that containsthe old mapfile and data
- * @param inputType The input type of the data (bibtex, csv, etc.), in case of local file
- * @param context The context
- * @param template whether to use template item
+ * @param collections The collections the created items will be inserted to, apart from the owning one
+ * @param resumeDir In case of a resume request, the directory that containsthe old mapfile and data
+ * @param inputType The input type of the data (bibtex, csv, etc.), in case of local file
+ * @param context The context
+ * @param template whether to use template item
* @throws Exception if error
*/
- public void processUIImport(String url, Collection owningCollection, String[] collections, String resumeDir, String inputType, Context context, boolean template) throws Exception;
+ public void processUIImport(String url, Collection owningCollection, String[] collections, String resumeDir,
+ String inputType, Context context, boolean template) throws Exception;
/**
* Since the BTE batch import is done in a new thread we are unable to communicate
@@ -105,16 +110,13 @@ public interface ItemImportService {
* communication with email instead. Send a success email once the batch
* import is complete
*
- * @param context
- * - the current Context
- * @param eperson
- * - eperson to send the email to
- * @param fileName
- * - the filepath to the mapfile created by the batch import
+ * @param context - the current Context
+ * @param eperson - eperson to send the email to
+ * @param fileName - the filepath to the mapfile created by the batch import
* @throws MessagingException if error
*/
public void emailSuccessMessage(Context context, EPerson eperson,
- String fileName) throws MessagingException;
+ String fileName) throws MessagingException;
/**
* Since the BTE batch import is done in a new thread we are unable to communicate
@@ -122,37 +124,38 @@ public interface ItemImportService {
* communication with email instead. Send an error email if the batch
* import fails
*
- * @param eperson
- * - EPerson to send the error message to
- * @param error
- * - the error message
+ * @param eperson - EPerson to send the error message to
+ * @param error - the error message
* @throws MessagingException if error
*/
public void emailErrorMessage(EPerson eperson, String error)
- throws MessagingException;
+ throws MessagingException;
/**
* Get imports available for a person
+ *
* @param eperson EPerson object
* @return List of batch uploads
* @throws Exception if error
*/
public List getImportsAvailable(EPerson eperson)
- throws Exception;
+ throws Exception;
/**
* Get import upload directory
+ *
* @param ePerson EPerson object
* @return directory
- * @throws Exception if error
+ * @throws Exception if error
*/
public String getImportUploadableDirectory(EPerson ePerson)
- throws Exception;
+ throws Exception;
/**
* Delete a batch by ID
- * @param c DSpace Context
+ *
+ * @param c DSpace Context
* @param uploadId identifier
* @throws Exception if error
*/
@@ -160,18 +163,21 @@ public interface ItemImportService {
/**
* Replace items
- * @param c DSpace Context
+ *
+ * @param c DSpace Context
* @param mycollections List of Collections
- * @param sourcedir source directory
- * @param mapfile map file
- * @param template whether to use template item
+ * @param sourcedir source directory
+ * @param mapfile map file
+ * @param template whether to use template item
* @throws Exception if error
*/
- public void replaceItems(Context c, List mycollections, String sourcedir, String mapfile, boolean template) throws Exception;
+ public void replaceItems(Context c, List mycollections, String sourcedir, String mapfile,
+ boolean template) throws Exception;
/**
* Delete items via mapfile
- * @param c DSpace Context
+ *
+ * @param c DSpace Context
* @param mapfile map file
* @throws Exception if error
*/
@@ -179,25 +185,29 @@ public interface ItemImportService {
/**
* Add items
- * @param c DSpace Context
+ *
+ * @param c DSpace Context
* @param mycollections List of Collections
- * @param sourcedir source directory
- * @param mapfile map file
- * @param template whether to use template item
- * @param bteInputType The input type of the data (bibtex, csv, etc.), in case of local file
- * @param workingDir working directory
+ * @param sourcedir source directory
+ * @param mapfile map file
+ * @param template whether to use template item
+ * @param bteInputType The input type of the data (bibtex, csv, etc.), in case of local file
+ * @param workingDir working directory
* @throws Exception if error
*/
- public void addBTEItems(Context c, List mycollections, String sourcedir, String mapfile, boolean template, String bteInputType, String workingDir) throws Exception;
+ public void addBTEItems(Context c, List mycollections, String sourcedir, String mapfile,
+ boolean template, String bteInputType, String workingDir) throws Exception;
/**
* Get temporary work directory
+ *
* @return directory as string
*/
public String getTempWorkDir();
/**
* Get temporary work directory (as File)
+ *
* @return directory as File
* @throws java.io.IOException if the directory cannot be created.
*/
@@ -210,18 +220,21 @@ public interface ItemImportService {
/**
* Set test flag
- * @param isTest true or false
+ *
+ * @param isTest true or false
*/
public void setTest(boolean isTest);
/**
* Set resume flag
- * @param isResume true or false
+ *
+ * @param isResume true or false
*/
public void setResume(boolean isResume);
/**
* Set use workflow
+ *
* @param useWorkflow whether to enable workflow
*/
public void setUseWorkflow(boolean useWorkflow);
@@ -233,6 +246,7 @@ public interface ItemImportService {
/**
* Set quiet flag
+ *
* @param isQuiet true or false
*/
public void setQuiet(boolean isQuiet);
diff --git a/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingAvailabilityBitstreamStrategy.java b/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingAvailabilityBitstreamStrategy.java
index 45c63660bd..cd08ad032c 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingAvailabilityBitstreamStrategy.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingAvailabilityBitstreamStrategy.java
@@ -23,75 +23,71 @@ import org.springframework.beans.factory.annotation.Autowired;
/**
* This is an item marking Strategy class that tries to mark an item availability
* based on the existence of bitstreams within the ORIGINAL bundle.
- *
+ *
* @author Kostas Stamatis
- *
*/
public class ItemMarkingAvailabilityBitstreamStrategy implements ItemMarkingExtractor {
- private String availableImageName;
- private String nonAvailableImageName;
+ private String availableImageName;
+ private String nonAvailableImageName;
@Autowired(required = true)
protected ItemService itemService;
-
- public ItemMarkingAvailabilityBitstreamStrategy() {
-
- }
- @Override
- public ItemMarkingInfo getItemMarkingInfo(Context context, Item item)
- throws SQLException {
-
- List bundles = itemService.getBundles(item, "ORIGINAL");
- if (bundles.size() == 0){
- ItemMarkingInfo markInfo = new ItemMarkingInfo();
- markInfo.setImageName(nonAvailableImageName);
-
- return markInfo;
- }
- else {
- Bundle originalBundle = bundles.iterator().next();
- if (originalBundle.getBitstreams().size() == 0){
- ItemMarkingInfo markInfo = new ItemMarkingInfo();
- markInfo.setImageName(nonAvailableImageName);
-
- return markInfo;
- }
- else {
+ public ItemMarkingAvailabilityBitstreamStrategy() {
+
+ }
+
+ @Override
+ public ItemMarkingInfo getItemMarkingInfo(Context context, Item item)
+ throws SQLException {
+
+ List bundles = itemService.getBundles(item, "ORIGINAL");
+ if (bundles.size() == 0) {
+ ItemMarkingInfo markInfo = new ItemMarkingInfo();
+ markInfo.setImageName(nonAvailableImageName);
+
+ return markInfo;
+ } else {
+ Bundle originalBundle = bundles.iterator().next();
+ if (originalBundle.getBitstreams().size() == 0) {
+ ItemMarkingInfo markInfo = new ItemMarkingInfo();
+ markInfo.setImageName(nonAvailableImageName);
+
+ return markInfo;
+ } else {
Bitstream bitstream = originalBundle.getBitstreams().get(0);
ItemMarkingInfo signInfo = new ItemMarkingInfo();
signInfo.setImageName(availableImageName);
signInfo.setTooltip(bitstream.getName());
-
-
-
- String bsLink = "";
+
+
+ String bsLink = "";
bsLink = bsLink + "bitstream/"
- + item.getHandle() + "/"
- + bitstream.getSequenceID() + "/";
-
+ + item.getHandle() + "/"
+ + bitstream.getSequenceID() + "/";
+
try {
- bsLink = bsLink + Util.encodeBitstreamName(bitstream.getName(), Constants.DEFAULT_ENCODING);
- } catch (UnsupportedEncodingException e) {
-
- e.printStackTrace();
- }
-
- signInfo.setLink(bsLink);
-
- return signInfo;
- }
- }
- }
+ bsLink = bsLink + Util.encodeBitstreamName(bitstream.getName(), Constants.DEFAULT_ENCODING);
+ } catch (UnsupportedEncodingException e) {
- public void setAvailableImageName(String availableImageName) {
- this.availableImageName = availableImageName;
- }
+ e.printStackTrace();
+ }
- public void setNonAvailableImageName(String nonAvailableImageName) {
- this.nonAvailableImageName = nonAvailableImageName;
- }
+ signInfo.setLink(bsLink);
+
+ return signInfo;
+ }
+ }
+ }
+
+ public void setAvailableImageName(String availableImageName) {
+ this.availableImageName = availableImageName;
+ }
+
+ public void setNonAvailableImageName(String nonAvailableImageName) {
+ this.nonAvailableImageName = nonAvailableImageName;
+ }
}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingCollectionStrategy.java b/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingCollectionStrategy.java
index deaee2a748..5fcf045c87 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingCollectionStrategy.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingCollectionStrategy.java
@@ -18,33 +18,32 @@ import org.dspace.core.Context;
/**
* This is an item marking Strategy class that tries to mark an item
* based on the collection the items belong to
- *
+ *
* @author Kostas Stamatis
- *
*/
public class ItemMarkingCollectionStrategy implements ItemMarkingExtractor {
- Map mapping = new HashMap();
-
- public ItemMarkingCollectionStrategy() {
- }
+ Map mapping = new HashMap();
- @Override
- public ItemMarkingInfo getItemMarkingInfo(Context context, Item item)
- throws SQLException {
-
- if (mapping!=null){
- for (Collection collection : item.getCollections()){
- if (mapping.containsKey(collection.getHandle())){
- return mapping.get(collection.getHandle());
- }
- }
- }
-
- return null;
- }
+ public ItemMarkingCollectionStrategy() {
+ }
- public void setMapping(Map mapping) {
- this.mapping = mapping;
- }
+ @Override
+ public ItemMarkingInfo getItemMarkingInfo(Context context, Item item)
+ throws SQLException {
+
+ if (mapping != null) {
+ for (Collection collection : item.getCollections()) {
+ if (mapping.containsKey(collection.getHandle())) {
+ return mapping.get(collection.getHandle());
+ }
+ }
+ }
+
+ return null;
+ }
+
+ public void setMapping(Map mapping) {
+ this.mapping = mapping;
+ }
}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingExtractor.java b/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingExtractor.java
index 3a036e81a5..ecbd952541 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingExtractor.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingExtractor.java
@@ -14,11 +14,10 @@ import org.dspace.core.Context;
/**
* Interface to abstract the strategy for item signing
- *
+ *
* @author Kostas Stamatis
- *
*/
public interface ItemMarkingExtractor {
- public ItemMarkingInfo getItemMarkingInfo(Context context, Item item)
- throws SQLException;
+ public ItemMarkingInfo getItemMarkingInfo(Context context, Item item)
+ throws SQLException;
}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingInfo.java b/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingInfo.java
index 3ca671f7ce..297df6a939 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingInfo.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingInfo.java
@@ -9,49 +9,48 @@ package org.dspace.app.itemmarking;
/**
* Simple DTO to transfer data about the marking info for an item
- *
+ *
* @author Kostas Stamatis
- *
*/
public class ItemMarkingInfo {
- private String imageName;
- private String classInfo;
- private String tooltip;
- private String link;
+ private String imageName;
+ private String classInfo;
+ private String tooltip;
+ private String link;
- public ItemMarkingInfo() {
- super();
- }
+ public ItemMarkingInfo() {
+ super();
+ }
- public String getImageName() {
- return imageName;
- }
+ public String getImageName() {
+ return imageName;
+ }
- public void setImageName(String imageName) {
- this.imageName = imageName;
- }
+ public void setImageName(String imageName) {
+ this.imageName = imageName;
+ }
- public String getTooltip() {
- return tooltip;
- }
+ public String getTooltip() {
+ return tooltip;
+ }
- public void setTooltip(String tooltip) {
- this.tooltip = tooltip;
- }
-
- public String getLink() {
- return link;
- }
+ public void setTooltip(String tooltip) {
+ this.tooltip = tooltip;
+ }
- public void setLink(String link) {
- this.link = link;
- }
-
- public String getClassInfo() {
- return classInfo;
- }
+ public String getLink() {
+ return link;
+ }
- public void setClassInfo(String classInfo) {
- this.classInfo = classInfo;
- }
+ public void setLink(String link) {
+ this.link = link;
+ }
+
+ public String getClassInfo() {
+ return classInfo;
+ }
+
+ public void setClassInfo(String classInfo) {
+ this.classInfo = classInfo;
+ }
}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingMetadataStrategy.java b/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingMetadataStrategy.java
index 6e637ee10c..3573e0c2bc 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingMetadataStrategy.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemmarking/ItemMarkingMetadataStrategy.java
@@ -22,46 +22,43 @@ import org.springframework.beans.factory.annotation.Autowired;
* This is an item marking Strategy class that tries to mark an item
* based on the existence of a specific value within the values of a specific
* metadata field
- *
+ *
* @author Kostas Stamatis
- *
*/
public class ItemMarkingMetadataStrategy implements ItemMarkingExtractor {
@Autowired(required = true)
protected ItemService itemService;
- private String metadataField;
- Map mapping = new HashMap();
-
- public ItemMarkingMetadataStrategy() {
- }
+ private String metadataField;
+ Map mapping = new HashMap();
- @Override
- public ItemMarkingInfo getItemMarkingInfo(Context context, Item item)
- throws SQLException {
-
- if (metadataField != null && mapping!=null)
- {
- List vals = itemService.getMetadataByMetadataString(item, metadataField);
- if (vals.size() > 0)
- {
- for (MetadataValue value : vals){
- String type = value.getValue();
- if (mapping.containsKey(type)){
- return mapping.get(type);
- }
- }
- }
- }
- return null;
- }
+ public ItemMarkingMetadataStrategy() {
+ }
- public void setMetadataField(String metadataField) {
- this.metadataField = metadataField;
- }
+ @Override
+ public ItemMarkingInfo getItemMarkingInfo(Context context, Item item)
+ throws SQLException {
- public void setMapping(Map mapping) {
- this.mapping = mapping;
- }
+ if (metadataField != null && mapping != null) {
+ List vals = itemService.getMetadataByMetadataString(item, metadataField);
+ if (vals.size() > 0) {
+ for (MetadataValue value : vals) {
+ String type = value.getValue();
+ if (mapping.containsKey(type)) {
+ return mapping.get(type);
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ public void setMetadataField(String metadataField) {
+ this.metadataField = metadataField;
+ }
+
+ public void setMapping(Map mapping) {
+ this.mapping = mapping;
+ }
}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemupdate/ActionManager.java b/dspace-api/src/main/java/org/dspace/app/itemupdate/ActionManager.java
index 6a5579a958..a79566d69b 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemupdate/ActionManager.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemupdate/ActionManager.java
@@ -12,80 +12,70 @@ import java.util.LinkedHashMap;
import java.util.Map;
/**
- * Container for UpdateActions
- * Order of actions is very import for correct processing. This implementation
- * supports an iterator that returns the actions in the order in which they are
- * put in. Adding the same action a second time has no effect on this order.
- *
- *
+ * Container for UpdateActions
+ * Order of actions is very import for correct processing. This implementation
+ * supports an iterator that returns the actions in the order in which they are
+ * put in. Adding the same action a second time has no effect on this order.
*/
public class ActionManager implements Iterable {
-
- protected Map, UpdateAction> registry
- = new LinkedHashMap, UpdateAction>();
-
- /**
- * Get update action
- * @param actionClass UpdateAction class
- * @return instantiation of UpdateAction class
- * @throws InstantiationException if instantiation error
- * @throws IllegalAccessException if illegal access error
- */
- public UpdateAction getUpdateAction(Class extends UpdateAction> actionClass)
- throws InstantiationException, IllegalAccessException
- {
- UpdateAction action = registry.get(actionClass);
-
- if (action == null)
- {
- action = actionClass.newInstance();
- registry.put(actionClass, action);
- }
-
- return action;
- }
-
- /**
- *
- * @return whether any actions have been registered with this manager
- */
- public boolean hasActions()
- {
- return !registry.isEmpty();
- }
-
- /**
- * This implementation guarantees the iterator order is the same as the order
- * in which updateActions have been added
- *
- * @return iterator for UpdateActions
- */
- @Override
- public Iterator iterator()
- {
- return new Iterator()
- {
- private Iterator> itr = registry.keySet().iterator();
-
- @Override
- public boolean hasNext()
- {
- return itr.hasNext();
- }
-
- @Override
- public UpdateAction next()
- {
- return registry.get(itr.next());
- }
-
- //not supported
- @Override
- public void remove()
- {
- throw new UnsupportedOperationException();
- }
- };
-
- }
+
+ protected Map, UpdateAction> registry
+ = new LinkedHashMap, UpdateAction>();
+
+ /**
+ * Get update action
+ *
+ * @param actionClass UpdateAction class
+ * @return instantiation of UpdateAction class
+ * @throws InstantiationException if instantiation error
+ * @throws IllegalAccessException if illegal access error
+ */
+ public UpdateAction getUpdateAction(Class extends UpdateAction> actionClass)
+ throws InstantiationException, IllegalAccessException {
+ UpdateAction action = registry.get(actionClass);
+
+ if (action == null) {
+ action = actionClass.newInstance();
+ registry.put(actionClass, action);
+ }
+
+ return action;
+ }
+
+ /**
+ * @return whether any actions have been registered with this manager
+ */
+ public boolean hasActions() {
+ return !registry.isEmpty();
+ }
+
+ /**
+ * This implementation guarantees the iterator order is the same as the order
+ * in which updateActions have been added
+ *
+ * @return iterator for UpdateActions
+ */
+ @Override
+ public Iterator iterator() {
+ return new Iterator() {
+ private Iterator> itr = registry.keySet().iterator();
+
+ @Override
+ public boolean hasNext() {
+ return itr.hasNext();
+ }
+
+ @Override
+ public UpdateAction next() {
+ return registry.get(itr.next());
+ }
+
+ //not supported
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+
+ }
}
diff --git a/dspace-api/src/main/java/org/dspace/app/itemupdate/AddBitstreamsAction.java b/dspace-api/src/main/java/org/dspace/app/itemupdate/AddBitstreamsAction.java
index d78c73693b..e9693fb3d1 100644
--- a/dspace-api/src/main/java/org/dspace/app/itemupdate/AddBitstreamsAction.java
+++ b/dspace-api/src/main/java/org/dspace/app/itemupdate/AddBitstreamsAction.java
@@ -19,7 +19,11 @@ import java.util.List;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.factory.AuthorizeServiceFactory;
import org.dspace.authorize.service.AuthorizeService;
-import org.dspace.content.*;
+import org.dspace.content.Bitstream;
+import org.dspace.content.BitstreamFormat;
+import org.dspace.content.Bundle;
+import org.dspace.content.DCDate;
+import org.dspace.content.Item;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.BitstreamFormatService;
import org.dspace.content.service.InstallItemService;
@@ -29,201 +33,176 @@ import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.eperson.service.GroupService;
/**
- * Action to add bitstreams listed in item contents file to the item in DSpace
- *
- *
+ * Action to add bitstreams listed in item contents file to the item in DSpace
*/
public class AddBitstreamsAction extends UpdateBitstreamsAction {
protected AuthorizeService authorizeService = AuthorizeServiceFactory.getInstance().getAuthorizeService();
- protected BitstreamFormatService bitstreamFormatService = ContentServiceFactory.getInstance().getBitstreamFormatService();
+ protected BitstreamFormatService bitstreamFormatService = ContentServiceFactory.getInstance()
+ .getBitstreamFormatService();
protected GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
protected InstallItemService installItemService = ContentServiceFactory.getInstance().getInstallItemService();
- public AddBitstreamsAction()
- {
- //empty
- }
-
- /**
- * Adds bitstreams from the archive as listed in the contents file.
- *
- * @param context DSpace Context
- * @param itarch Item Archive
- * @param isTest test flag
- * @param suppressUndo undo flag
- * @throws IOException if IO error
- * @throws IllegalArgumentException if arg exception
- * @throws SQLException if database error
- * @throws AuthorizeException if authorization error
- * @throws ParseException if parse error
- */
- @Override
- public void execute(Context context, ItemArchive itarch, boolean isTest,
- boolean suppressUndo) throws IllegalArgumentException,
- ParseException, IOException, AuthorizeException, SQLException
- {
- Item item = itarch.getItem();
- File dir = itarch.getDirectory();
-
- List contents = MetadataUtilities.readContentsFile(new File(dir, ItemUpdate.CONTENTS_FILE));
-
- if (contents.isEmpty())
- {
- ItemUpdate.pr("Contents is empty - no bitstreams to add");
- return;
- }
-
- ItemUpdate.pr("Contents bitstream count: " + contents.size());
-
- String[] files = dir.list(ItemUpdate.fileFilter);
- List fileList = new ArrayList();
- for (String filename : files)
- {
- fileList.add(filename);
- ItemUpdate.pr("file: " + filename);
- }
-
- for (ContentsEntry ce : contents)
- {
- //validate match to existing file in archive
- if (!fileList.contains(ce.filename))
- {
- throw new IllegalArgumentException("File listed in contents is missing: " + ce.filename);
- }
- }
- int bitstream_bundles_updated = 0;
-
- //now okay to add
- for (ContentsEntry ce : contents)
- {
- String targetBundleName = addBitstream(context, itarch, item, dir, ce, suppressUndo, isTest);
- if (!targetBundleName.equals("")
- && !targetBundleName.equals("THUMBNAIL")
- && !targetBundleName.equals("TEXT"))
- {
- bitstream_bundles_updated++;
- }
- }
+ public AddBitstreamsAction() {
+ //empty
+ }
+
+ /**
+ * Adds bitstreams from the archive as listed in the contents file.
+ *
+ * @param context DSpace Context
+ * @param itarch Item Archive
+ * @param isTest test flag
+ * @param suppressUndo undo flag
+ * @throws IOException if IO error
+ * @throws IllegalArgumentException if arg exception
+ * @throws SQLException if database error
+ * @throws AuthorizeException if authorization error
+ * @throws ParseException if parse error
+ */
+ @Override
+ public void execute(Context context, ItemArchive itarch, boolean isTest,
+ boolean suppressUndo) throws IllegalArgumentException,
+ ParseException, IOException, AuthorizeException, SQLException {
+ Item item = itarch.getItem();
+ File dir = itarch.getDirectory();
+
+ List contents = MetadataUtilities.readContentsFile(new File(dir, ItemUpdate.CONTENTS_FILE));
+
+ if (contents.isEmpty()) {
+ ItemUpdate.pr("Contents is empty - no bitstreams to add");
+ return;
+ }
+
+ ItemUpdate.pr("Contents bitstream count: " + contents.size());
+
+ String[] files = dir.list(ItemUpdate.fileFilter);
+ List