Skip to content

Commit

Permalink
Merge pull request DSpace#9229 from tuub/DS-3439
Browse files Browse the repository at this point in the history
[DS-3439] Copy collection template item specified metadata during …
  • Loading branch information
tdonohue authored Apr 29, 2024
2 parents c847e8e + 7ead4ae commit d63b815
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 @@ -279,6 +280,55 @@ public Item createTemplateItem(Context context, Collection collection) throws SQ
return collection.getTemplateItem();
}

@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 @@ -141,41 +138,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 @@ -213,15 +176,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
*/
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 d63b815

Please sign in to comment.