Skip to content

Commit

Permalink
[DS-3439] Copy collection template item specified metadata during Swo…
Browse files Browse the repository at this point in the history
…rd v2 METS deposit ingestion.
  • Loading branch information
marsaoua committed Dec 11, 2023
1 parent 344d79b commit 7ead4ae
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 47 deletions.
50 changes: 50 additions & 0 deletions dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -276,6 +277,55 @@ public Item createTemplateItem(Context context, Collection collection) throws SQ
}
}

@Override
public void populateWithTemplateItemMetadata(Context context, Collection collection, boolean template, Item item)
throws SQLException {

Item templateItem = collection.getTemplateItem();

Optional<MetadataValue> colEntityType = getDSpaceEntityType(collection);
Optional<MetadataValue> templateItemEntityType = getDSpaceEntityType(templateItem);

if (template && colEntityType.isPresent() && templateItemEntityType.isPresent() &&
!StringUtils.equals(colEntityType.get().getValue(), templateItemEntityType.get().getValue())) {
throw new IllegalStateException("The template item has entity type : (" +
templateItemEntityType.get().getValue() + ") different than collection entity type : " +
colEntityType.get().getValue());
}

if (template && colEntityType.isPresent() && templateItemEntityType.isEmpty()) {
MetadataValue original = colEntityType.get();
MetadataField metadataField = original.getMetadataField();
MetadataSchema metadataSchema = metadataField.getMetadataSchema();
// NOTE: dspace.entity.type = <blank> does not make sense
// the collection entity type is by default blank when a collection is first created
if (StringUtils.isNotBlank(original.getValue())) {
addMetadata(context, item, metadataSchema.getName(), metadataField.getElement(),
metadataField.getQualifier(), original.getLanguage(), original.getValue());
}
}

if (template && (templateItem != null)) {
List<MetadataValue> md = getMetadata(templateItem, Item.ANY, Item.ANY, Item.ANY, Item.ANY);

for (MetadataValue aMd : md) {
MetadataField metadataField = aMd.getMetadataField();
MetadataSchema metadataSchema = metadataField.getMetadataSchema();
addMetadata(context, item, metadataSchema.getName(), metadataField.getElement(),
metadataField.getQualifier(), aMd.getLanguage(), aMd.getValue());
}
}
}

private Optional<MetadataValue> getDSpaceEntityType(DSpaceObject dSpaceObject) {
return Objects.nonNull(dSpaceObject) ? dSpaceObject.getMetadata()
.stream()
.filter(x -> x.getMetadataField().toString('.')
.equalsIgnoreCase("dspace.entity.type"))
.findFirst()
: Optional.empty();
}

@Override
public Iterator<Item> findAll(Context context) throws SQLException {
return itemDAO.findAll(context, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.dspace.app.util.DCInputsReaderException;
import org.dspace.app.util.Util;
Expand Down Expand Up @@ -134,41 +131,7 @@ public WorkspaceItem create(Context context, Collection collection, UUID uuid, b
.addPolicy(context, item, Constants.DELETE, item.getSubmitter(), ResourcePolicy.TYPE_SUBMISSION);

// Copy template if appropriate
Item templateItem = collection.getTemplateItem();

Optional<MetadataValue> colEntityType = getDSpaceEntityType(collection);
Optional<MetadataValue> templateItemEntityType = getDSpaceEntityType(templateItem);

if (template && colEntityType.isPresent() && templateItemEntityType.isPresent() &&
!StringUtils.equals(colEntityType.get().getValue(), templateItemEntityType.get().getValue())) {
throw new IllegalStateException("The template item has entity type : (" +
templateItemEntityType.get().getValue() + ") different than collection entity type : " +
colEntityType.get().getValue());
}

if (template && colEntityType.isPresent() && templateItemEntityType.isEmpty()) {
MetadataValue original = colEntityType.get();
MetadataField metadataField = original.getMetadataField();
MetadataSchema metadataSchema = metadataField.getMetadataSchema();
// NOTE: dspace.entity.type = <blank> does not make sense
// the collection entity type is by default blank when a collection is first created
if (StringUtils.isNotBlank(original.getValue())) {
itemService.addMetadata(context, item, metadataSchema.getName(), metadataField.getElement(),
metadataField.getQualifier(), original.getLanguage(), original.getValue());
}
}

if (template && (templateItem != null)) {
List<MetadataValue> md = itemService.getMetadata(templateItem, Item.ANY, Item.ANY, Item.ANY, Item.ANY);

for (MetadataValue aMd : md) {
MetadataField metadataField = aMd.getMetadataField();
MetadataSchema metadataSchema = metadataField.getMetadataSchema();
itemService.addMetadata(context, item, metadataSchema.getName(), metadataField.getElement(),
metadataField.getQualifier(), aMd.getLanguage(),
aMd.getValue());
}
}
itemService.populateWithTemplateItemMetadata(context, collection, template, item);

itemService.update(context, item);

Expand Down Expand Up @@ -204,15 +167,6 @@ public WorkspaceItem create(Context context, Collection collection, UUID uuid, b
return workspaceItem;
}

private Optional<MetadataValue> getDSpaceEntityType(DSpaceObject dSpaceObject) {
return Objects.nonNull(dSpaceObject) ? dSpaceObject.getMetadata()
.stream()
.filter(x -> x.getMetadataField().toString('.')
.equalsIgnoreCase("dspace.entity.type"))
.findFirst()
: Optional.empty();
}

@Override
public WorkspaceItem create(Context c, WorkflowItem workflowItem) throws SQLException, AuthorizeException {
WorkspaceItem workspaceItem = workspaceItemDAO.create(c, new WorkspaceItem());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,9 @@ protected DSpaceObject replaceObject(Context context, DSpaceObject dso,
owningCollection = inProgressSubmission.getCollection();
}

itemService.populateWithTemplateItemMetadata(context, owningCollection, params.useCollectionTemplate(),
item);

addLicense(context, item, license, owningCollection
, params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ public interface ItemService
*/
public Item createTemplateItem(Context context, Collection collection) throws SQLException, AuthorizeException;

/**
* Populate the given item with all template item specified metadata.
*
* @param context DSpace context object
* @param collection Collection (parent)
* @param template if <code>true</code>, the item inherits all collection's template item metadata
* @param item item to populate with template item specified metadata
* @throws SQLException if database error
*/
public void populateWithTemplateItemMetadata (Context context, Collection collection, boolean template, Item item)
throws SQLException;

/**
* Get all the items in the archive. Only items with the "in archive" flag
* set are included. The order of the list is indeterminate.
Expand Down

0 comments on commit 7ead4ae

Please sign in to comment.