Prevent showing duplicate Tags

This commit is contained in:
George Kalampokis 2020-03-17 18:03:43 +02:00
parent f48e4b5275
commit d6c8803c9f
3 changed files with 27 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import eu.eudat.elastic.repository.Repository;
import eu.eudat.logic.proxy.config.exceptions.HugeResultSet;
import eu.eudat.logic.proxy.config.exceptions.NoURLFound;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.utilities.helpers.StreamDistinctBy;
import eu.eudat.models.data.helpers.responses.ResponseItem;
import eu.eudat.types.ApiMessageCode;
import org.springframework.beans.factory.annotation.Autowired;
@ -47,7 +48,7 @@ public class TagController extends BaseController {
//ExternalUrlCriteria externalUrlCriteria = new ExternalUrlCriteria(query);
/*List<Map<String, String>> remoteRepos = this.getApiContext().getOperationsContext().getRemoteFetcher().getTags(externalUrlCriteria, type);
TagExternalSourcesModel researchersExternalSourcesModel = new TagExternalSourcesModel().fromExternalItem(remoteRepos);*/
List<Tag> tags = this.getApiContext().getOperationsContext().getDatasetRepository().query(new DatasetCriteria()).stream().map(Dataset::getTags).flatMap(Collection::stream).collect(Collectors.toList());
List<Tag> tags = this.getApiContext().getOperationsContext().getDatasetRepository().query(new DatasetCriteria()).stream().map(Dataset::getTags).flatMap(Collection::stream).filter(StreamDistinctBy.distinctByKey(Tag::getId)).collect(Collectors.toList());
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<Tag>>().payload(tags).status(ApiMessageCode.NO_MESSAGE));
}

View File

@ -15,6 +15,7 @@ import eu.eudat.data.query.items.table.datasetprofile.DatasetProfileTableRequest
import eu.eudat.elastic.criteria.DatasetCriteria;
import eu.eudat.elastic.entities.Collaborator;
import eu.eudat.elastic.entities.Organization;
import eu.eudat.elastic.entities.Tag;
import eu.eudat.elastic.repository.DatasetRepository;
import eu.eudat.exceptions.security.UnauthorisedException;
import eu.eudat.logic.builders.BuilderFactory;
@ -588,7 +589,15 @@ public class DatasetManager {
// if (datasetWizardModel.getTags() != null && !datasetWizardModel.getTags().isEmpty()) {
eu.eudat.elastic.entities.Dataset dataset = new eu.eudat.elastic.entities.Dataset();
dataset.setId(datasetWizardModel.getId().toString());
dataset.setTags(datasetWizardModel.getTags());
DatasetCriteria criteria = new DatasetCriteria();
criteria.setTags(datasetWizardModel.getTags());
List<Tag> tags = apiContext.getOperationsContext().getDatasetRepository().query(criteria).stream().map(eu.eudat.elastic.entities.Dataset::getTags).flatMap(Collection::stream).collect(Collectors.toList());
if (tags.isEmpty()) {
datasetWizardModel.getTags().forEach(tag -> tag.setId(UUID.randomUUID().toString()));
dataset.setTags(datasetWizardModel.getTags());
} else {
dataset.setTags(tags);
}
dataset.setLabel(datasetWizardModel.getLabel());
dataset.setDescription(datasetWizardModel.getDescription());
dataset.setTemplate(datasetWizardModel.getProfile());

View File

@ -0,0 +1,15 @@
package eu.eudat.logic.utilities.helpers;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
public class StreamDistinctBy {
public static <T> Predicate<T> distinctByKey(
Function<? super T, ?> keyExtractor) {
Map<Object, Boolean> found = new ConcurrentHashMap<>();
return t -> found.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}