Skip to content

Commit

Permalink
MET-6002 Readjusted code for depublication reason. Created new API ca…
Browse files Browse the repository at this point in the history
…ll to get all possible depublication reasons
  • Loading branch information
JoanaCMS committed Jul 9, 2024
1 parent d081d0f commit e4047ff
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.europeana.metis.core.rest.controller;

import com.fasterxml.jackson.annotation.JsonProperty;
import eu.europeana.metis.authentication.rest.client.AuthenticationClient;
import eu.europeana.metis.authentication.user.MetisUserView;
import eu.europeana.metis.core.exceptions.NoDatasetFoundException;
Expand All @@ -16,9 +17,11 @@
import eu.europeana.metis.utils.RestEndpoints;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -230,10 +233,33 @@ public WorkflowExecution addDepublishWorkflowInQueueOfWorkflowExecutions(
datasetDepublish, priority, recordIdsInSeparateLines);
}

/**
* API to return all possible values of depublication reasons
* @return All possible values of depublication reasons
*/
@GetMapping(value = RestEndpoints.DEPUBLISH_REASONS, produces = {
MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
@ResponseStatus(HttpStatus.OK)
public List<String> getAllPossibleDepublicationReasons(){
return Collections.emptyList();
public List<DepublicationReasonView> getAllPossibleDepublicationReasons(){
return Arrays.stream(DepublicationReason.values()).filter(value -> value != DepublicationReason.UNKNOWN)
.map(DepublicationReasonView::new).collect(Collectors.toList());
}

private static class DepublicationReasonView {

@JsonProperty("name")
private final String name;
@JsonProperty("valueAsString")
private final String valueAsString;

/**
* Instantiates a new DepublicationReason view.
*
* @param depublicationReason the depublication reason
*/
DepublicationReasonView(DepublicationReason depublicationReason) {
this.name = depublicationReason.name();
this.valueAsString = depublicationReason.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -253,7 +249,7 @@ public List<DepublishRecordIdView> getDepublishRecordIds(String datasetId, int p
* @throws BadContentException In case the records would violate the maximum number of depublished records that each dataset can
* have.
*/
public List<DepublishRecordId> getAllDepublishRecordIdsWithStatus(String datasetId,
public Set<String> getAllDepublishRecordIdsWithStatus(String datasetId,
DepublishRecordIdSortField sortField, SortDirection sortDirection,
DepublicationStatus depublicationStatus) throws BadContentException {
return getAllDepublishRecordIdsWithStatus(datasetId, sortField, sortDirection,
Expand All @@ -276,7 +272,7 @@ public List<DepublishRecordId> getAllDepublishRecordIdsWithStatus(String dataset
* @throws BadContentException In case the records would violate the maximum number of depublished records that each dataset can
* have.
*/
public List<DepublishRecordId> getAllDepublishRecordIdsWithStatus(String datasetId,
public Set<String> getAllDepublishRecordIdsWithStatus(String datasetId,
DepublishRecordIdSortField sortField, SortDirection sortDirection,
DepublicationStatus depublicationStatus, Set<String> recordIds) throws BadContentException {
// Check list size: if this is too large we can throw exception regardless of what's in the database.
Expand All @@ -301,7 +297,7 @@ public List<DepublishRecordId> getAllDepublishRecordIdsWithStatus(String dataset
final List<DepublishRecordId> result = getListOfQueryRetryable(query, findOptions);

// Convert result to right object.
return result.stream().filter(distinctByRecordId(DepublishRecordId::getRecordId)).collect(Collectors.toList());
return result.stream().map(DepublishRecordId::getRecordId).collect(Collectors.toSet());
}

private Query<DepublishRecordId> prepareQueryForDepublishRecordIds(String datasetId,
Expand Down Expand Up @@ -410,10 +406,4 @@ long deleteRecords(Query<DepublishRecordId> query) {
() -> query.delete(new DeleteOptions().multi(true)).getDeletedCount());
}

private static <T> Predicate<T> distinctByRecordId(Function<? super T, ?> keyExtractor) {

Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void validateDepublishPlugin(String datasetId,
// If record depublication requested, check if there are pending record ids in the db
if (depublishPluginMetadata.isPresent() && !depublishPluginMetadata.get()
.isDatasetDepublish()) {
final List<DepublishRecordId> pendingDepublicationIds = depublishRecordIdDao
final Set<String> pendingDepublicationIds = depublishRecordIdDao
.getAllDepublishRecordIdsWithStatus(datasetId,
DepublishRecordIdSortField.DEPUBLICATION_STATE, SortDirection.ASCENDING,
DepublicationStatus.PENDING_DEPUBLICATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,14 @@ private void reinstateDepublishRecordIdsStatus(IndexToPublishPlugin indexPlugin,
final boolean isIncremental = indexPlugin.getPluginMetadata().isIncrementalIndexing();
if (isIncremental) {
// get all currently de-published records IDs from the database and create their full versions
final List<DepublishRecordId> depublishedRecordIds = depublishRecordIdDao.getAllDepublishRecordIdsWithStatus(
final Set<String> depublishedRecordIds = depublishRecordIdDao.getAllDepublishRecordIdsWithStatus(
datasetId, DepublishRecordIdSortField.DEPUBLICATION_STATE, SortDirection.ASCENDING,
DepublicationStatus.DEPUBLISHED);
DepublicationReason depublicationReason = depublishedRecordIds.getFirst().getDepublicationReason();
final Map<String, String> depublishedRecordIdsByFullId = depublishedRecordIds.stream()
.collect(Collectors.toMap(
record -> RecordIdUtils.composeFullRecordId(
datasetId, record.getRecordId()),
DepublishRecordId::getRecordId));
id -> RecordIdUtils.composeFullRecordId(
datasetId, id),
Function.identity()));

// Check which have been published by the index action - use full record IDs for eCloud.
if (!CollectionUtils.isEmpty(depublishedRecordIdsByFullId)) {
Expand All @@ -126,8 +125,7 @@ record -> RecordIdUtils.composeFullRecordId(
if (!CollectionUtils.isEmpty(publishedRecordIds)) {
depublishRecordIdDao.markRecordIdsWithDepublicationStatus(datasetId,
publishedRecordIds.stream().map(depublishedRecordIdsByFullId::get)
.collect(Collectors.toSet()), DepublicationStatus.PENDING_DEPUBLICATION, null,
depublicationReason);
.collect(Collectors.toSet()), DepublicationStatus.PENDING_DEPUBLICATION, null, null);
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,13 @@ private void setupDepublishPluginMetadata(Dataset dataset, DepublishPluginMetada
pendingDepublicationIds = depublishRecordIdDao
.getAllDepublishRecordIdsWithStatus(dataset.getDatasetId(),
DepublishRecordIdSortField.DEPUBLICATION_STATE, SortDirection.ASCENDING,
DepublicationStatus.PENDING_DEPUBLICATION)
.stream().map(DepublishRecordId::getRecordId).collect(Collectors.toSet());
DepublicationStatus.PENDING_DEPUBLICATION);
} else {
//Match provided record ids that are marked as PENDING_DEPUBLICATION in the database
pendingDepublicationIds = depublishRecordIdDao
.getAllDepublishRecordIdsWithStatus(dataset.getDatasetId(),
DepublishRecordIdSortField.DEPUBLICATION_STATE, SortDirection.ASCENDING,
DepublicationStatus.PENDING_DEPUBLICATION, recordIdsToDepublish)
.stream().map(DepublishRecordId::getRecordId).collect(Collectors.toSet());
DepublicationStatus.PENDING_DEPUBLICATION, recordIdsToDepublish);
}
pluginMetadata.setRecordIdsToDepublish(pendingDepublicationIds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,14 @@ void getAllDepublishRecordIdsWithStatusTest() throws BadContentException {
.addRecords(setTest, datasetId, DepublicationStatus.DEPUBLISHED, Instant.now(), DepublicationReason.UNKNOWN);
Set<String> result = depublishRecordIdDao.getAllDepublishRecordIdsWithStatus(datasetId,
DepublishRecordIdSortField.DEPUBLICATION_STATE, SortDirection.ASCENDING,
DepublicationStatus.DEPUBLISHED, setTest)
.stream().map(DepublishRecordId::getRecordId).collect(Collectors.toSet());
DepublicationStatus.DEPUBLISHED, setTest);

assertEquals(1, result.size());

//Check also when requesting without recordIds set parameter
result = depublishRecordIdDao.getAllDepublishRecordIdsWithStatus(datasetId,
DepublishRecordIdSortField.DEPUBLICATION_STATE, SortDirection.ASCENDING,
DepublicationStatus.DEPUBLISHED)
.stream().map(DepublishRecordId::getRecordId).collect(Collectors.toSet());
DepublicationStatus.DEPUBLISHED);

assertEquals(1, result.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void testValidateWorkflowPlugins_testWorkflowComposition() throws GenericMetisEx
when(depublishRecordIdDao
.getAllDepublishRecordIdsWithStatus(workflowDepublish.getDatasetId(),
DepublishRecordIdSortField.DEPUBLICATION_STATE, SortDirection.ASCENDING,
DepublicationStatus.PENDING_DEPUBLICATION)).thenReturn(Collections.emptyList());
DepublicationStatus.PENDING_DEPUBLICATION)).thenReturn(Collections.emptySet());
assertThrows(BadContentException.class, () -> validationUtils
.validateWorkflowPlugins(workflowDepublish, null));

Expand Down

0 comments on commit e4047ff

Please sign in to comment.