diff --git a/dspace-api/src/main/java/org/dspace/app/batch/ItemImportOA.java b/dspace-api/src/main/java/org/dspace/app/batch/ItemImportOA.java index f74146ab91d..189805074b7 100644 --- a/dspace-api/src/main/java/org/dspace/app/batch/ItemImportOA.java +++ b/dspace-api/src/main/java/org/dspace/app/batch/ItemImportOA.java @@ -19,6 +19,7 @@ import java.util.Calendar; import java.util.Date; import java.util.List; +import java.util.Objects; import java.util.StringTokenizer; import java.util.UUID; import java.util.stream.Collectors; @@ -547,11 +548,14 @@ private UUID addItem(Context c, Collection[] mycollections, ImpRecord impRecord, Item myitem = null; WorkspaceItem wsi = null; c.setCurrentUser(myEPerson); - - wsi = workspaceItemService.create(c, mycollections[0], false); + if (Objects.nonNull(impRecord.getUUID())) { + wsi = workspaceItemService.create(c, mycollections[0], impRecord.getUUID(),false); + } else { + wsi = workspaceItemService.create(c, mycollections[0], false); + } myitem = wsi.getItem(); - if (StringUtils.isNotEmpty(handle)) { + if (StringUtils.isNotEmpty(impRecord.getHandle())) { identifierService.register(c, myitem, handle); } diff --git a/dspace-api/src/main/java/org/dspace/batch/ImpRecord.java b/dspace-api/src/main/java/org/dspace/batch/ImpRecord.java index b295db26730..dd1e93be6b8 100644 --- a/dspace-api/src/main/java/org/dspace/batch/ImpRecord.java +++ b/dspace-api/src/main/java/org/dspace/batch/ImpRecord.java @@ -66,6 +66,9 @@ public class ImpRecord { @Column(name = "handle", length = 64) private String handle; + @Column(name = "uuid", unique = true) + protected java.util.UUID uuid; + @Column(name = "imp_sourceref", length = 256) private String impSourceref; @@ -129,6 +132,14 @@ public String getHandle() { return handle; } + public UUID getUUID() { + return uuid; + } + + public void setUUID(UUID uuid) { + this.uuid = uuid; + } + public void setHandle(String handle) { this.handle = handle; } diff --git a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.6_2024.02.20__add_imp_record_uuid.sql b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.6_2024.02.20__add_imp_record_uuid.sql new file mode 100644 index 00000000000..04cd1631350 --- /dev/null +++ b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.6_2024.02.20__add_imp_record_uuid.sql @@ -0,0 +1,13 @@ +-- +-- 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/ +-- + +----------------------------------------------------------------------------------- +-- Alter table imp_record with additional uuid for predefined uuid to set +----------------------------------------------------------------------------------- + +ALTER TABLE imp_record ADD COLUMN if NOT EXISTS uuid UUID; diff --git a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.6_2024.02.20__add_imp_record_uuid.sql b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.6_2024.02.20__add_imp_record_uuid.sql new file mode 100644 index 00000000000..04cd1631350 --- /dev/null +++ b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.6_2024.02.20__add_imp_record_uuid.sql @@ -0,0 +1,13 @@ +-- +-- 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/ +-- + +----------------------------------------------------------------------------------- +-- Alter table imp_record with additional uuid for predefined uuid to set +----------------------------------------------------------------------------------- + +ALTER TABLE imp_record ADD COLUMN if NOT EXISTS uuid UUID; diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/batch/ImportBatchIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/batch/ImportBatchIT.java index 95306b49b93..75e895d0835 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/batch/ImportBatchIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/batch/ImportBatchIT.java @@ -77,6 +77,7 @@ import org.dspace.eperson.service.GroupService; import org.dspace.services.ConfigurationService; import org.dspace.services.factory.DSpaceServicesFactory; +import org.dspace.util.UUIDUtils; import org.dspace.xmlworkflow.factory.XmlWorkflowServiceFactory; import org.dspace.xmlworkflow.storedcomponents.XmlWorkflowItem; import org.dspace.xmlworkflow.storedcomponents.service.XmlWorkflowItemService; @@ -207,6 +208,47 @@ public void createNewWorkspaceItem() throws IOException { } } + @Test + public void createNewWorkspaceItemWithUUID() throws IOException { + try { + // create imp_record records + int impRecordKey = 1; + UUID predefineduuid = UUIDUtils.fromString("0525aa6d-9eeb-4f05-96cc-4bb8c3cb6785"); + ImpRecord impRecord = createImpRecordWithPredefinedUUID(context, impRecordKey, + ImpRecordService.SEND_BACK_TO_WORKSPACE_STATUS, + ImpRecordService.INSERT_OR_UPDATE_OPERATION, admin, collection, predefineduuid); + + // create imp_metadatavalue records + createImpMetadatavalue(context, impRecord, MetadataSchemaEnum.DC.getName(), "title", + null, null, "Sample Item"); + + // Create a new item + String argv[] = new String[] { "-E", admin.getEmail() }; + + ItemImportMainOA.main(argv); + + List wis = workspaceItemService.findAll(context); + + WorkspaceItem wi = wis.get(0); + Item item = wi.getItem(); + + List metadata = item.getMetadata(); + // one metadata is explicit the other is the cris.sourceid + assertEquals("Only two metadata found", 2, metadata.size()); + + String defLanguage = configurationService.getProperty("default.language"); + metadata = itemService.getMetadata(item, MetadataSchemaEnum.DC.getName(), "title", null, defLanguage); + assertEquals("Only one metadata is assigned to the item", 1, metadata.size()); + assertEquals("Is the new metadata value the right one?", metadata.get(0).getValue(), "Sample Item"); + assertEquals("Is the predefined uuid assigned correctly?", UUIDUtils.toString(item.getID()), + UUIDUtils.toString(predefineduuid)); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } finally { + context.restoreAuthSystemState(); + } + } + /*** * Remove an item. * @@ -1210,6 +1252,24 @@ private ImpRecord createImpRecord(Context context, int impRecordKey, Character s return impRecordService.create(context, impRecord); } + private ImpRecord createImpRecordWithPredefinedUUID(Context context, int impRecordKey, Character status, + String operation, EPerson eperson, Collection collection, UUID predefined) + throws SQLException { + // create imp_record records + String sourceRecordId = "" + impRecordKey; + ImpRecord impRecord = new ImpRecord(); + impRecord.setImpId(impSeq++); + impRecordService.setImpCollection(impRecord, collection); + impRecordService.setImpEperson(impRecord, eperson); + impRecord.setImpRecordId(sourceRecordId); + impRecord.setImpSourceref(SOURCE_REF); + impRecord.setUUID(predefined); + impRecordService.setStatus(impRecord, status); + impRecordService.setOperation(impRecord, operation); + + return impRecordService.create(context, impRecord); + } + /*** * Create a Metadata of ImpRecord * diff --git a/dspace/etc/migration/entity_migration.ktr b/dspace/etc/migration/entity_migration.ktr index 60cbc816068..3a7f9f77eca 100644 --- a/dspace/etc/migration/entity_migration.ktr +++ b/dspace/etc/migration/entity_migration.ktr @@ -2880,6 +2880,9 @@ WHERE property_def.id = properties.typo_id entity_type_var + + uuid + @@ -4395,7 +4398,7 @@ and parent_id = ? dspace - SELECT id, crisid, sourceref, row_number() OVER () as row_number + SELECT id, crisid, uuid, sourceref, row_number() OVER () as row_number FROM "public".${ENTITY_TABLE} WHERE ${ENTITY_FILTER} @@ -6307,6 +6310,10 @@ ORDER BY parent_id ASC; status status + + uuid + uuid +