Skip to content

Commit

Permalink
Merge remote-tracking branch '4science-bitbucket/dspace-cris-7' into …
Browse files Browse the repository at this point in the history
…DSC-621
  • Loading branch information
LucaGiamminonni committed Sep 30, 2022
2 parents 5765ba6 + f6f59b1 commit 64dbb73
Show file tree
Hide file tree
Showing 2 changed files with 469 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@
*/
package org.dspace.app.rest.repository;

import static org.apache.commons.lang3.ArrayUtils.nullToEmpty;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Spliterators;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -32,11 +42,14 @@
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.BundleService;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.dspace.core.exception.SQLRuntimeException;
import org.dspace.handle.service.HandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -72,6 +85,9 @@ public class BitstreamRestRepository extends DSpaceObjectRestRepository<Bitstrea
@Autowired
private HandleService handleService;

@Autowired
private ItemService itemService;

@Autowired
public BitstreamRestRepository(BitstreamService dsoService) {
super(dsoService);
Expand Down Expand Up @@ -186,6 +202,37 @@ public BitstreamRest findByItemHandle(@Parameter(value = "handle", required = tr
}
}

/**
* Find the bitstream for the provided uuid of an item, the name of bundle, the metadata field
* and the value of this metadata.
*
* @param uuid The uuid of the item
* @param name The bundle name
* @param metadata The filter metadata field
* @param value The filter metadata value
*
* @return a Page of BitstreamRest instance matching the user query
*/
@SearchRestMethod(name = "byItemId")
public Page<BitstreamRest> findByItemId(@Parameter(value = "uuid", required = true) UUID uuid,
@Parameter(value = "name", required = true) String bundleName,
@Parameter(value = "filterMetadata") String[] filterMetadataFields,
@Parameter(value = "filterMetadataValue") String[] filterMetadataValues,
Pageable pageable) {

Map<String, String> filterMetadata = composeFilterMetadata(filterMetadataFields, filterMetadataValues);

Item item = findItemById(uuid)
.orElseThrow(() -> new UnprocessableEntityException("No item found with the given UUID"));

List<Bitstream> bitstreams = getItemBitstreams(item)
.filter(bitstream -> isContainedInBundleNamed(bitstream, bundleName))
.filter(bitstream -> hasAllMetadataValues(bitstream, filterMetadata))
.collect(Collectors.toList());

return converter.toRestPage(bitstreams, pageable, utils.obtainProjection());
}

private Bitstream getFirstMatchedBitstream(Item item, Integer sequence, String filename) {
List<Bundle> bundles = item.getBundles();
List<Bitstream> bitstreams = new LinkedList<>();
Expand Down Expand Up @@ -248,4 +295,74 @@ public BundleRest performBitstreamMove(Context context, Bitstream bitstream, Bun

return converter.toRest(targetBundle, utils.obtainProjection());
}

private Optional<Item> findItemById(UUID uuid) {
try {
return Optional.ofNullable(itemService.find(obtainContext(), uuid));
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
}

private Map<String, String> composeFilterMetadata(String[] fields, String[] values) {

if (filterMetadataDoNotHaveSameCardinality(fields, values)) {
throw new IllegalArgumentException("The request must include a filterMetadata " +
"and a filterMetadataValue parameters with the same cardinality");
}

Map<String, String> filterMetadata = new HashMap<String, String>();

for (int i = 0; i < nullToEmpty(fields).length; i++) {
filterMetadata.put(fields[i], values[i]);
}

return filterMetadata;

}

private boolean filterMetadataDoNotHaveSameCardinality(String[] fields, String[] values) {
return nullToEmpty(fields).length != nullToEmpty(values).length;
}

private Stream<Bitstream> getItemBitstreams(Item item) {
try {
Iterator<Bitstream> bitstreamIterator = bs.getItemBitstreams(obtainContext(), item);
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(bitstreamIterator, 0), false);
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
}

private boolean isContainedInBundleNamed(Bitstream bitstream, String name) {
try {
return bitstream.getBundles().stream()
.anyMatch(bundle -> bundle.getName().equals(name));
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
}

private boolean hasAllMetadataValues(Bitstream bitstream, Map<String, String> filterMetadata) {
return filterMetadata.keySet().stream()
.allMatch(metadataField -> hasMetadataValue(bitstream, metadataField, filterMetadata.get(metadataField)));
}

private boolean hasMetadataValue(Bitstream bitstream, String metadataField, String value) {
return bitstream.getMetadata().stream()
.filter(metadataValue -> metadataValue.getMetadataField().toString('.').equals(metadataField))
.anyMatch(metadataValue -> matchesMetadataValue(metadataValue, value));
}

private boolean matchesMetadataValue(MetadataValue metadataValue, String value) {

if (value.startsWith("(") && value.endsWith(")")) {
value = value.substring(1, value.length() - 1);
return metadataValue.getValue().matches(value);
} else {
return metadataValue.getValue().equals(value);
}

}

}
Loading

0 comments on commit 64dbb73

Please sign in to comment.