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 ec28694
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 46 deletions.
52 changes: 52 additions & 0 deletions dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java
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,57 @@ public Item createTemplateItem(Context context, Collection collection) throws SQ
}
}

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

Item templateItem = collection.getTemplateItem();

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

if (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 (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 (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();
log.error(metadataSchema.getName() + metadataField.getElement() + metadataField.getQualifier());
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
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,40 +131,8 @@ 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());
}
if (template) {
itemService.populateWithTemplateItemMetadata(context, collection, item);
}

itemService.update(context, item);
Expand Down Expand Up @@ -204,15 +169,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
Expand Up @@ -636,6 +636,10 @@ protected DSpaceObject replaceObject(Context context, DSpaceObject dso,
owningCollection = inProgressSubmission.getCollection();
}

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

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

Expand Down
Expand Up @@ -83,6 +83,17 @@ 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 item item to populate with template item specified metadata
* @throws SQLException if database error
*/
public void populateWithTemplateItemMetadata (Context context, Collection collection, 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 ec28694

Please sign in to comment.