Skip to content

Commit ce32d49

Browse files
UFAL/Fix handle duplicities during the import #1297 (#1207)
* Issue 1297 fix: don't create additional item handle on item import * code improvement, fixed tests * added test * resolve Copilot comments
1 parent 8089447 commit ce32d49

5 files changed

Lines changed: 53 additions & 22 deletions

File tree

dspace-api/src/main/java/org/dspace/app/itemimport/ItemImportServiceImpl.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public void addItems(Context c, List<Collection> mycollections,
348348

349349
/**
350350
* Add relationships from a 'relationships' manifest file.
351-
*
351+
*
352352
* @param c Context
353353
* @param sourceDir The parent import source directory
354354
* @throws Exception
@@ -452,7 +452,7 @@ protected void addRelationship(Context c, Item item, Item relationItem, String r
452452

453453
/**
454454
* Get the item's entity type from meta.
455-
*
455+
*
456456
* @param item
457457
* @return
458458
*/
@@ -462,7 +462,7 @@ protected String getEntityType(Item item) {
462462

463463
/**
464464
* Read the relationship manifest file.
465-
*
465+
*
466466
* Each line in the file contains a relationship type id and an item
467467
* identifier in the following format:
468468
*
@@ -548,7 +548,7 @@ protected Map<String, List<String>> processRelationshipFile(String path, String
548548
* The import item map will be checked first to see if the identifier refers to an item folder
549549
* that was just imported. Next it will try to find the item by handle or UUID, or by a unique
550550
* meta value.
551-
*
551+
*
552552
* @param c Context
553553
* @param itemIdentifier The identifier string found in the import manifest (handle, uuid, or import subfolder)
554554
* @return Item if found, or null.
@@ -606,7 +606,7 @@ protected Item resolveItem(Context c, String itemIdentifier)
606606

607607
/**
608608
* Lookup an item by a (unique) meta value.
609-
*
609+
*
610610
* @param c current DSpace session.
611611
* @param metaKey name of the metadata field to match.
612612
* @param metaValue value to be matched.
@@ -786,8 +786,18 @@ protected Item addItem(Context c, List<Collection> mycollections, String path,
786786
WorkspaceItem wi = null;
787787
WorkflowItem wfi = null;
788788

789+
String myhandle = null;
790+
789791
if (!isTest) {
790-
wi = workspaceItemService.create(c, mycollections.iterator().next(), template);
792+
if (!useWorkflow) {
793+
// only process handle file if not using workflow system
794+
myhandle = processHandleFile(path + File.separatorChar + itemname, "handle");
795+
}
796+
797+
// in case the handle exists in import file, we need to avoid new handle registration
798+
// (it's almost the same as we'd create a new version of an existing item)
799+
boolean isNewVersion = (myhandle != null);
800+
wi = workspaceItemService.create(c, mycollections.iterator().next(), template, isNewVersion);
791801
myitem = wi.getItem();
792802
}
793803

@@ -823,9 +833,6 @@ protected Item addItem(Context c, List<Collection> mycollections, String path,
823833
mapOutputString = itemname + " " + myitem.getID();
824834
}
825835
} else {
826-
// only process handle file if not using workflow system
827-
String myhandle = processHandleFile(c, myitem, itemPathDir, "handle");
828-
829836
// put item in system
830837
if (!isTest) {
831838
try {
@@ -1148,13 +1155,11 @@ protected List<Collection> processCollectionFile(Context c, String path, String
11481155
/**
11491156
* Read in the handle file contents or return null if empty or doesn't exist
11501157
*
1151-
* @param c DSpace context
1152-
* @param i DSpace item
11531158
* @param path path to handle file
11541159
* @param filename name of file
11551160
* @return handle file contents or null if doesn't exist
11561161
*/
1157-
protected String processHandleFile(Context c, Item i, String path, String filename) {
1162+
protected String processHandleFile(String path, String filename) {
11581163
File file = new File(path + File.separatorChar + filename);
11591164
String result = null;
11601165

dspace-api/src/main/java/org/dspace/content/InstallItemServiceImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,10 @@ public Item installItem(Context c, InProgressSubmission is,
8989
AuthorizeException {
9090
Item item = is.getItem();
9191
Collection collection = is.getCollection();
92-
93-
// Get map of filters to use for identifier types.
94-
Map<Class<? extends Identifier>, Filter> filters = FilterUtils.getIdentifierFilters(false);
9592
try {
9693
if (suppliedHandle == null) {
94+
// Get map of filters to use for identifier types.
95+
Map<Class<? extends Identifier>, Filter> filters = FilterUtils.getIdentifierFilters(false);
9796
// Register with the filters we've set up
9897
identifierService.register(c, item, filters);
9998
} else {

dspace-api/src/main/java/org/dspace/content/service/WorkspaceItemService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public interface WorkspaceItemService extends InProgressSubmissionService<Worksp
5656
public WorkspaceItem create(Context context, Collection collection, boolean template)
5757
throws AuthorizeException, SQLException;
5858

59-
6059
/**
6160
* Create a new workspace item, with a new ID. An Item is also created. The
6261
* submitter is the current user in the context.

dspace-api/src/test/java/org/dspace/app/itemimport/ItemImportCLIIT.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.dspace.content.factory.ContentServiceFactory;
3333
import org.dspace.content.service.ItemService;
3434
import org.dspace.content.service.RelationshipService;
35+
import org.dspace.identifier.factory.IdentifierServiceFactory;
36+
import org.dspace.identifier.service.IdentifierService;
3537
import org.dspace.services.ConfigurationService;
3638
import org.dspace.services.factory.DSpaceServicesFactory;
3739
import org.flywaydb.core.internal.util.ExceptionUtils;
@@ -55,6 +57,7 @@ public class ItemImportCLIIT extends AbstractIntegrationTestWithDatabase {
5557
private ItemService itemService = ContentServiceFactory.getInstance().getItemService();
5658
private RelationshipService relationshipService = ContentServiceFactory.getInstance().getRelationshipService();
5759
private ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
60+
private IdentifierService identifierService = IdentifierServiceFactory.getInstance().getIdentifierService();
5861
private Collection collection;
5962
private Path tempDir;
6063
private Path workDir;
@@ -114,6 +117,26 @@ public void importItemBySafWithMetadataOnly() throws Exception {
114117
checkMetadata();
115118
}
116119

120+
@Test
121+
public void importItemBySafWithExistingHandle() throws Exception {
122+
// create simple SAF
123+
Path safDir = Files.createDirectory(Path.of(tempDir.toString() + "/test"));
124+
Path itemDir = Files.createDirectory(Path.of(safDir.toString() + "/item_000"));
125+
Files.copy(getClass().getResourceAsStream("dublin_core.xml"),
126+
Path.of(itemDir + "/dublin_core.xml"));
127+
Files.copy(getClass().getResourceAsStream("handle"),
128+
Path.of(itemDir + "/handle"));
129+
130+
String[] args = new String[] { "import", "-a", "-e", admin.getEmail(), "-c", collection.getID().toString(),
131+
"-s", safDir.toString(), "-m", tempDir.toString() + "/mapfile.out" };
132+
perfomImportScript(args);
133+
134+
Item item = findItemByTitle(publicationTitle);
135+
assertEquals("123456789/3900021-03", item.getHandle());
136+
checkMetadata();
137+
identifierService.delete(context, item, item.getHandle());
138+
}
139+
117140
@Test
118141
public void importItemBySafWithBitstreams() throws Exception {
119142
// create simple SAF
@@ -549,7 +572,8 @@ public void deleteItemByMapFile() throws Exception {
549572
* @throws Exception
550573
*/
551574
private void checkMetadata() throws Exception {
552-
Item item = itemService.findByMetadataField(context, "dc", "title", null, publicationTitle).next();
575+
Item item = findItemByTitle(publicationTitle);
576+
assertEquals(1, item.getHandles().size());
553577
assertEquals(item.getName(), publicationTitle);
554578
assertEquals(itemService.getMetadata(item, "dc.date.issued"), "1990");
555579
assertEquals(itemService.getMetadata(item, "dc.title.alternative"), "J'aime les Printemps");
@@ -560,7 +584,7 @@ private void checkMetadata() throws Exception {
560584
* @throws Exception
561585
*/
562586
private void checkMetadataWithAnotherSchema() throws Exception {
563-
Item item = itemService.findByMetadataField(context, "dc", "title", null, publicationTitle).next();
587+
Item item = findItemByTitle(publicationTitle);
564588
assertEquals(item.getName(), publicationTitle);
565589
assertEquals(itemService.getMetadata(item, "dcterms.title"), publicationTitle);
566590
}
@@ -570,8 +594,7 @@ private void checkMetadataWithAnotherSchema() throws Exception {
570594
* @throws Exception
571595
*/
572596
private void checkBitstream() throws Exception {
573-
Bitstream bitstream = itemService.findByMetadataField(context, "dc", "title", null, publicationTitle).next()
574-
.getBundles("ORIGINAL").get(0).getBitstreams().get(0);
597+
Bitstream bitstream = findItemByTitle(publicationTitle).getBundles("ORIGINAL").get(0).getBitstreams().get(0);
575598
assertEquals(bitstream.getName(), "file1.txt");
576599
}
577600

@@ -589,8 +612,8 @@ private void checkItemDeletion() throws Exception {
589612
* @throws Exception
590613
*/
591614
private void checkRelationship() throws Exception {
592-
Item item = itemService.findByMetadataField(context, "dc", "title", null, publicationTitle).next();
593-
Item author = itemService.findByMetadataField(context, "dc", "title", null, personTitle).next();
615+
Item item = findItemByTitle(publicationTitle);
616+
Item author = findItemByTitle(personTitle);
594617
List<Relationship> relationships = relationshipService.findByItem(context, item);
595618
assertEquals(1, relationships.size());
596619
assertEquals(author.getID(), relationships.get(0).getRightItem().getID());
@@ -601,4 +624,8 @@ private void perfomImportScript(String[] args)
601624
throws Exception {
602625
runDSpaceScript(args);
603626
}
627+
628+
private Item findItemByTitle(String title) throws Exception {
629+
return itemService.findByMetadataField(context, "dc", "title", null, title).next();
630+
}
604631
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
123456789/3900021-03

0 commit comments

Comments
 (0)