w2p-80200 Retain UUIDs of DSpace objects when using packager

This commit is contained in:
Nathan Buckingham
2021-07-12 12:25:34 -04:00
parent d04b8d3881
commit 72fab65012
16 changed files with 295 additions and 34 deletions

View File

@@ -0,0 +1,33 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.content;
import java.io.Serializable;
import java.util.UUID;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.UUIDGenerator;
/**
* Allows DSpaceObjects to provide a pre-determined UUID
*
* @author April Herron
*/
public class CheckExistingUUIDGenerator extends UUIDGenerator {
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) {
if (object instanceof DSpaceObject) {
UUID uuid = ((DSpaceObject) object).getPredefinedUUID();
if (uuid != null) {
return uuid;
}
}
return super.generate(session, object);
}
}

View File

@@ -14,6 +14,7 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.annotation.Nonnull;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
@@ -103,6 +104,9 @@ public class Collection extends DSpaceObject implements DSpaceObjectLegacySuppor
protected Collection() {
}
protected Collection(UUID uuid) {
this.predefinedUUID = uuid;
}
@Override
public String getName() {

View File

@@ -130,11 +130,22 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> i
@Override
public Collection create(Context context, Community community, String handle)
throws SQLException, AuthorizeException {
return create(context, community, handle, null);
}
@Override
public Collection create(Context context, Community community,
String handle, UUID uuid) throws SQLException, AuthorizeException {
if (community == null) {
throw new IllegalArgumentException("Community cannot be null when creating a new collection.");
}
Collection newCollection = collectionDAO.create(context, new Collection());
Collection newCollection;
if (uuid != null) {
newCollection = collectionDAO.create(context, new Collection(uuid));
} else {
newCollection = collectionDAO.create(context, new Collection());
}
//Add our newly created collection to our community, authorization checks occur in THIS method
communityService.addCollection(context, community, newCollection);
@@ -148,7 +159,8 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> i
authorizeService
.createResourcePolicy(context, newCollection, anonymousGroup, null, Constants.DEFAULT_ITEM_READ, null);
authorizeService
.createResourcePolicy(context, newCollection, anonymousGroup, null, Constants.DEFAULT_BITSTREAM_READ, null);
.createResourcePolicy(context, newCollection, anonymousGroup, null,
Constants.DEFAULT_BITSTREAM_READ, null);
collectionDAO.save(context, newCollection);

View File

@@ -11,6 +11,7 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
@@ -96,6 +97,10 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport
}
protected Community(UUID uuid) {
this.predefinedUUID = uuid;
}
void addSubCommunity(Community subCommunity) {
subCommunities.add(subCommunity);
setModified();

View File

@@ -86,13 +86,24 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl<Community> imp
@Override
public Community create(Community parent, Context context, String handle) throws SQLException, AuthorizeException {
return create(parent, context, handle, null);
}
@Override
public Community create(Community parent, Context context, String handle,
UUID uuid) throws SQLException, AuthorizeException {
if (!(authorizeService.isAdmin(context) ||
(parent != null && authorizeService.authorizeActionBoolean(context, parent, Constants.ADD)))) {
throw new AuthorizeException(
"Only administrators can create communities");
}
Community newCommunity = communityDAO.create(context, new Community());
Community newCommunity;
if (uuid != null) {
newCommunity = communityDAO.create(context, new Community(uuid));
} else {
newCommunity = communityDAO.create(context, new Community());
}
if (parent != null) {
parent.addSubCommunity(newCommunity);
@@ -387,13 +398,25 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl<Community> imp
return createSubcommunity(context, parentCommunity, null);
}
@Override
public Community createSubcommunity(Context context, Community parentCommunity, String handle)
throws SQLException, AuthorizeException {
return createSubcommunity(context, parentCommunity, handle, null);
}
@Override
public Community createSubcommunity(Context context, Community parentCommunity, String handle,
UUID uuid) throws SQLException, AuthorizeException {
// Check authorisation
authorizeService.authorizeAction(context, parentCommunity, Constants.ADD);
Community c = create(parentCommunity, context, handle);
Community c;
if (uuid != null) {
c = create(parentCommunity, context, handle, uuid);
} else {
c = create(parentCommunity, context, handle);
}
addSubcommunity(context, parentCommunity, c);
return c;

View File

@@ -38,8 +38,8 @@ import org.hibernate.annotations.GenericGenerator;
@Table(name = "dspaceobject")
public abstract class DSpaceObject implements Serializable, ReloadableEntity<java.util.UUID> {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@GeneratedValue(generator = "check-existing-uuid")
@GenericGenerator(name = "check-existing-uuid", strategy = "org.dspace.content.CheckExistingUUIDGenerator")
@Column(name = "uuid", unique = true, nullable = false, insertable = true, updatable = false)
protected java.util.UUID id;
@@ -76,6 +76,12 @@ public abstract class DSpaceObject implements Serializable, ReloadableEntity<jav
@Transient
private boolean modified = false;
@Transient
protected UUID predefinedUUID;
public UUID getPredefinedUUID() {
return predefinedUUID;
}
protected DSpaceObject() {
}

View File

@@ -13,6 +13,7 @@ import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -131,6 +132,10 @@ public class Item extends DSpaceObject implements DSpaceObjectLegacySupport {
}
protected Item(UUID uuid) {
this.predefinedUUID = uuid;
}
/**
* Find out if the item is part of the main archive
*

View File

@@ -190,6 +190,23 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl<Item> implements It
return item;
}
@Override
public Item create(Context context, WorkspaceItem workspaceItem,
UUID uuid) throws SQLException, AuthorizeException {
if (workspaceItem.getItem() != null) {
throw new IllegalArgumentException(
"Attempting to create an item for a workspace item that already contains an item");
}
Item item = createItem(context, uuid);
workspaceItem.setItem(item);
log.info(LogManager.getHeader(context, "create_item", "item_id="
+ item.getID()));
return item;
}
@Override
public Item createTemplateItem(Context context, Collection collection) throws SQLException, AuthorizeException {
if (collection == null || collection.getTemplateItem() != null) {
@@ -418,6 +435,30 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl<Item> implements It
return bitstreamList;
}
protected Item createItem(Context context, UUID uuid) throws SQLException, AuthorizeException {
Item item;
if (uuid != null) {
item = itemDAO.create(context, new Item(uuid));
} else {
item = itemDAO.create(context, new Item());
}
// set discoverable to true (default)
item.setDiscoverable(true);
// Call update to give the item a last modified date. OK this isn't
// amazingly efficient but creates don't happen that often.
context.turnOffAuthorisationSystem();
update(context, item);
context.restoreAuthSystemState();
context.addEvent(new Event(Event.CREATE, Constants.ITEM, item.getID(),
null, getIdentifiers(context, item)));
log.info(LogManager.getHeader(context, "create_item", "item_id=" + item.getID()));
return item;
}
protected Item createItem(Context context) throws SQLException, AuthorizeException {
Item item = itemDAO.create(context, new Item());
// set discoverable to true (default)

View File

@@ -12,6 +12,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.logging.log4j.Logger;
import org.dspace.app.util.DCInputsReaderException;
@@ -81,6 +82,12 @@ public class WorkspaceItemServiceImpl implements WorkspaceItemService {
@Override
public WorkspaceItem create(Context context, Collection collection, boolean template)
throws AuthorizeException, SQLException {
return create(context, collection, null, template);
}
@Override
public WorkspaceItem create(Context context, Collection collection, UUID uuid, boolean template)
throws AuthorizeException, SQLException {
// Check the user has permission to ADD to the collection
authorizeService.authorizeAction(context, collection, Constants.ADD);
@@ -89,7 +96,12 @@ public class WorkspaceItemServiceImpl implements WorkspaceItemService {
// Create an item
Item item = itemService.create(context, workspaceItem);
Item item;
if (uuid != null) {
item = itemService.create(context, workspaceItem, uuid);
} else {
item = itemService.create(context, workspaceItem);
}
item.setSubmitter(context.getCurrentUser());
// Now create the policies for the submitter to modify item and contents

View File

@@ -777,9 +777,6 @@ public abstract class AbstractMETSDisseminator
Mets mets = new Mets();
String identifier = "DB-ID-" + dso.getID();
if (dso.getHandle() != null) {
identifier = dso.getHandle().replace('/', '-');
}
// this ID should be globally unique (format: DSpace_[objType]_[handle with slash replaced with a dash])
mets.setID("DSpace_" + Constants.typeText[dso.getType()] + "_" + identifier);

View File

@@ -16,6 +16,7 @@ import java.net.URLConnection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -409,6 +410,7 @@ public abstract class AbstractMETSIngester extends AbstractPackageIngester {
// get handle from manifest
handle = getObjectHandle(manifest);
}
UUID uuid = getObjectID(manifest);
// -- Step 2 --
// Create our DSpace Object based on info parsed from manifest, and
@@ -416,7 +418,7 @@ public abstract class AbstractMETSIngester extends AbstractPackageIngester {
DSpaceObject dso;
try {
dso = PackageUtils.createDSpaceObject(context, parent,
type, handle, params);
type, handle, uuid, params);
} catch (SQLException sqle) {
throw new PackageValidationException("Exception while ingesting "
+ pkgFile.getPath(), sqle);
@@ -727,7 +729,6 @@ public abstract class AbstractMETSIngester extends AbstractPackageIngester {
// retrieve path/name of file in manifest
String path = METSManifest.getFileName(mfile);
// extract the file input stream from package (or retrieve
// externally, if it is an externally referenced file)
InputStream fileStream = getFileInputStream(pkgFile, params, path);
@@ -1506,4 +1507,22 @@ public abstract class AbstractMETSIngester extends AbstractPackageIngester {
*/
public abstract String getConfigurationName();
public UUID getObjectID(METSManifest manifest)
throws PackageValidationException {
Element mets = manifest.getMets();
String idStr = mets.getAttributeValue("ID");
if (idStr == null || idStr.length() == 0) {
throw new PackageValidationException("Manifest is missing the required mets@ID attribute.");
}
if (idStr.contains("DB-ID-")) {
idStr = idStr.substring(idStr.lastIndexOf("DB-ID-") + 6, idStr.length());
}
try {
return UUID.fromString(idStr);
} catch (IllegalArgumentException ignored) {
//do nothing
}
return null;
}
}

View File

@@ -17,6 +17,7 @@ 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;
@@ -447,6 +448,7 @@ public class PackageUtils {
* @param parent Parent Object
* @param type Type of new Object
* @param handle Handle of new Object (may be null)
* @param uuid
* @param params Properties-style list of options (interpreted by each packager).
* @return newly created DSpace Object (or null)
* @throws AuthorizeException if authorization error
@@ -454,29 +456,55 @@ public class PackageUtils {
* @throws IOException if IO error
*/
public static DSpaceObject createDSpaceObject(Context context, DSpaceObject parent, int type, String handle,
PackageParameters params)
UUID uuid, PackageParameters params)
throws AuthorizeException, SQLException, IOException {
DSpaceObject dso = null;
switch (type) {
case Constants.COLLECTION:
Collection collection = collectionService.find(context, uuid);
if (collection != null) {
dso = collectionService.create(context, (Community) parent, handle);
} else {
dso = collectionService.create(context, (Community) parent, handle, uuid);
}
return dso;
case Constants.COMMUNITY:
// top-level community?
if (parent == null || parent.getType() == Constants.SITE) {
Community community = communityService.find(context, uuid);
if (community != null) {
dso = communityService.create(null, context, handle);
} else {
dso = communityService.create(null, context, handle, uuid);
}
} else {
Community community = communityService.find(context, uuid);
if (community != null) {
dso = communityService.createSubcommunity(context, ((Community) parent), handle);
} else {
dso = communityService.createSubcommunity(context, ((Community) parent), handle, uuid);
}
}
return dso;
case Constants.ITEM:
//Initialize a WorkspaceItem
//(Note: Handle is not set until item is finished)
WorkspaceItem wsi = workspaceItemService
.create(context, (Collection) parent, params.useCollectionTemplate());
Item item = itemService.find(context, uuid);
if (item != null) {
return item;
}
WorkspaceItem wsi = null;
if (!params.replaceModeEnabled()) {
wsi = workspaceItemService.create(context, (Collection)parent, params.useCollectionTemplate());
} else {
wsi = workspaceItemService.create(context, (Collection)parent,
uuid, params.useCollectionTemplate());
}
// Please note that we are returning an Item which is *NOT* yet in the Archive,
// and doesn't yet have a handle assigned.

View File

@@ -12,6 +12,7 @@ import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;
@@ -60,6 +61,21 @@ public interface CollectionService
public Collection create(Context context, Community community, String handle) throws SQLException,
AuthorizeException;
/**
* Create a new collection with the supplied handle and ID.
* Once created the collection is added to the given community
*
* @param context DSpace context object
* @param community DSpace Community (parent)
* @param handle the pre-determined Handle to assign to the new collection
* @param uuid the pre-determined UUID to assign to the new collection
* @return the newly created collection
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
public Collection create(Context context, Community community, String handle, UUID uuid) throws SQLException,
AuthorizeException;
/**
* Get all collections in the system. These are alphabetically sorted by
* collection name.

View File

@@ -11,6 +11,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;
@@ -53,6 +54,20 @@ public interface CommunityService extends DSpaceObjectService<Community>, DSpace
public Community create(Community parent, Context context, String handle)
throws SQLException, AuthorizeException;
/**
* Create a new top-level community, with a new ID.
*
* @param parent parent community
* @param context DSpace context object
* @param handle the pre-determined Handle to assign to the new community
* @param uuid the pre-determined uuid to assign to the new community
* @return the newly created community
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
public Community create(Community parent, Context context,
String handle, UUID uuid) throws SQLException, AuthorizeException;
/**
* Get a list of all communities in the system. These are alphabetically
@@ -202,6 +217,20 @@ public interface CommunityService extends DSpaceObjectService<Community>, DSpace
public Community createSubcommunity(Context context, Community parentCommunity, String handle)
throws SQLException, AuthorizeException;
/**
* Create a new sub-community within this community.
*
* @param context context
* @param handle the pre-determined Handle to assign to the new community
* @param parentCommunity parent community
* @param uuid the pre-determined UUID to assign to the new community
* @return the new community
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
public Community createSubcommunity(Context context, Community parentCommunity, String handle, UUID uuid)
throws SQLException, AuthorizeException;
/**
* Add an existing community as a subcommunity to the community
*

View File

@@ -55,6 +55,20 @@ public interface ItemService
*/
public Item create(Context context, WorkspaceItem workspaceItem) throws SQLException, AuthorizeException;
/**
* Create a new item, with a provided ID. This method is not public,
* since items need to be created as workspace items. Authorisation is the
* responsibility of the caller.
*
* @param context DSpace context object
* @param workspaceItem in progress workspace item
* @param uuid the pre-determined UUID to assign to the new item
* @return the newly created item
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
public Item create(Context context, WorkspaceItem workspaceItem, UUID uuid) throws SQLException, AuthorizeException;
/**
* Create an empty template item for this collection. If one already exists,
* no action is taken. Caution: Make sure you call <code>update</code> on

View File

@@ -11,6 +11,7 @@ import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
@@ -55,6 +56,22 @@ public interface WorkspaceItemService extends InProgressSubmissionService<Worksp
public WorkspaceItem create(Context context, Collection collection, boolean template)
throws AuthorizeException, SQLException;
/**
* Create a new workspace item, with a new ID. An Item is also created. The
* submitter is the current user in the context.
*
* @param context DSpace context object
* @param collection Collection being submitted to
* @param uuid the preferred uuid of the new item (used if restoring an item and retaining old uuid)
* @param template if <code>true</code>, the workspace item starts as a copy
* of the collection's template item
* @return the newly created workspace item
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
public WorkspaceItem create(Context context, Collection collection, UUID uuid, boolean template)
throws AuthorizeException, SQLException;
public WorkspaceItem create(Context c, WorkflowItem wfi) throws SQLException, AuthorizeException;