Created
August 22, 2016 03:34
-
-
Save aschweer/9650dc6482d406727f9147739b900d7a to your computer and use it in GitHub Desktop.
Retrospectively assign DOIs to DSpace items (DSpace 5.x)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nz.co.lernz.db.curation; | |
import org.apache.commons.lang.StringUtils; | |
import org.apache.log4j.Logger; | |
import org.dspace.authorize.AuthorizeException; | |
import org.dspace.content.DSpaceObject; | |
import org.dspace.content.Item; | |
import org.dspace.curate.AbstractCurationTask; | |
import org.dspace.curate.Curator; | |
import org.dspace.curate.Distributive; | |
import org.dspace.curate.Mutative; | |
import org.dspace.identifier.DOI; | |
import org.dspace.identifier.Identifier; | |
import org.dspace.identifier.IdentifierException; | |
import org.dspace.identifier.IdentifierService; | |
import org.dspace.utils.DSpace; | |
import java.io.IOException; | |
import java.sql.SQLException; | |
/** | |
* @author Andrea Schweer [email protected] | |
* for the University of Waikato's Institutional Research Repositories | |
*/ | |
@Mutative | |
@Distributive | |
public class RetrospectivelyRegisterIdentifier extends AbstractCurationTask { | |
private static final Logger log = Logger.getLogger(RetrospectivelyRegisterIdentifier.class); | |
private int processedItems; | |
private int changedItems; | |
private boolean hasError; | |
private IdentifierService identifierService; | |
private Class<? extends Identifier> identifierClass; | |
@Override | |
public int perform(DSpaceObject dso) throws IOException { | |
processedItems = 0; | |
changedItems = 0; | |
hasError = false; | |
identifierService = new DSpace().getSingletonService(IdentifierService.class); | |
if (identifierService == null) { | |
String message = "Couldn't obtain DOI provider, aborting"; | |
report(message); | |
setResult(message); | |
return Curator.CURATE_ERROR; | |
} | |
String identifierClassProp = taskProperty("identifier.class"); | |
identifierClass = null; | |
if (StringUtils.isNotBlank(identifierClassProp)) { | |
try { | |
identifierClass = (Class<? extends Identifier>) Class.forName(identifierClassProp); | |
} catch (ClassNotFoundException e) { | |
String message = "Specified identifier class (" + identifierClassProp + ") not found: " + e.getMessage(); | |
log.error(message); | |
report(message); | |
setResult(message); | |
return Curator.CURATE_FAIL; | |
} catch (ClassCastException e) { | |
String message = "Specified identifier class (" + identifierClassProp + ") is invalid (not a subclass of org.dspace.identifier.Identifier): " + e.getMessage(); | |
log.error(message); | |
report(message); | |
setResult(message); | |
return Curator.CURATE_FAIL; | |
} | |
} | |
if (identifierClass == null) { | |
log.info("No identifier class specified, defaulting to DOI"); | |
identifierClass = DOI.class; | |
} | |
distribute(dso); | |
if (hasError) { | |
String message = "Encountered error, check logs / report for details"; | |
report(message); | |
setResult(message); | |
return Curator.CURATE_ERROR; | |
} | |
if (processedItems == 0) { | |
String message = "No items processed"; | |
report(message); | |
setResult(message); | |
return Curator.CURATE_SKIP; | |
} | |
if (changedItems > 0) { | |
String message = "Processed " + processedItems + " items, changed " + changedItems; | |
report(message); | |
setResult(message); | |
return Curator.CURATE_SUCCESS; | |
} else { | |
String message = "Processed " + processedItems + " items but changed none"; | |
report(message); | |
setResult(message); | |
return Curator.CURATE_FAIL; | |
} | |
} | |
@Override | |
protected void performItem(Item item) throws SQLException, IOException { | |
processedItems++; | |
String existingIdentifier = null; | |
try { | |
existingIdentifier = identifierService.lookup(Curator.curationContext(), item, identifierClass); | |
} catch (Exception e) { | |
// ignore any exceptions here | |
log.warn("Ignoring exception during identifier lookup: " + e.getMessage(), e); | |
} | |
if (StringUtils.isBlank(existingIdentifier)) { | |
log.info("No existing identifier found for item id=" + item.getID() + ", trying to register for one"); | |
try { | |
identifierService.register(Curator.curationContext(), item); | |
log.info("Registered identifier for item id=" + item.getID()); | |
changedItems++; | |
} catch (IdentifierException | AuthorizeException e) { | |
String message = "Problem retrospectively registering DOI for item " + item.getID() + ": " + e.getMessage(); | |
log.error(message, e); | |
report(message); | |
hasError = true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Originally written for the LERNZ freshwater quality database. Use at own risk.