Skip to content

Commit

Permalink
Merge pull request #462 from SSHOC/feature/SSHOC-432-optimisation-of-…
Browse files Browse the repository at this point in the history
…workflow-retrieval

Feature/sshoc 432 optimisation of workflow retrieval
  • Loading branch information
KlausIllmayer authored Sep 16, 2024
2 parents b863ab2 + 8a71c3b commit a4bdbde
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

Expand All @@ -14,4 +15,6 @@ interface MediaDataRepository extends JpaRepository<MediaData, UUID> {
@Query("select m from MediaData m where m.linkCount = 0 and m.touchTimestamp < :retentionTimestamp")
Stream<MediaData> streamStaleMedia(@Param("retentionTimestamp") LocalDateTime retentionTimestamp);

@Query("SELECT m FROM MediaData m WHERE m.id IN :listOfMediaIds")
List<MediaData> finaAllByIds(List<UUID> listOfMediaIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import jakarta.persistence.EntityNotFoundException;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;


@Service
Expand Down Expand Up @@ -96,6 +98,11 @@ public MediaDetails getMediaDetails(UUID mediaId) {
return toMediaDetails(mediaData);
}

public List<MediaDetails> getMediaDetails(List<UUID> listOfMediaIds) {
List<MediaData> listOfMediaData = mediaDataRepository.finaAllByIds(listOfMediaIds);
return listOfMediaData.stream().map(this::toMediaDetails).collect(Collectors.toList());
}

public MediaDetails saveCompleteMedia(Resource mediaFile, Optional<MediaType> mimeType) {

UUID mediaId = resolveNewMediaId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public interface ItemRelatedItemRepository extends JpaRepository<ItemRelatedItem

int countAllByObjectId(long subjectId);

@Query("SELECT i.subject.id, count(i.subject) FROM ItemRelatedItem i WHERE i.subject.id in :ids group by i.subject.id")
List<Long[]> countAllBySubject(List<Long> ids);

@Query("SELECT i.object.id, count(i.object) FROM ItemRelatedItem i WHERE i.object.id in :ids group by i.object.id")
List<Long[]> countAllByObject(List<Long> ids);

List<ItemRelatedItem> findAllByObjectId(long objectId);

boolean existsByRelation(ItemRelation itemRelation);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package eu.sshopencloud.marketplace.repositories.sources;

import eu.sshopencloud.marketplace.model.sources.Source;
import eu.sshopencloud.marketplace.repositories.sources.projection.DetailedSourceView;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -12,6 +11,7 @@

@Repository
public interface SourceRepository extends JpaRepository<Source, Long> {
String PERSISTENT_ID_COLUMN_NAME = "persistent_id";

Source findByDomain(String domain);

Expand Down Expand Up @@ -82,4 +82,13 @@ public interface SourceRepository extends JpaRepository<Source, Long> {
" WHERE i.status = 'APPROVED' and i.persistent_id = :persistentId", nativeQuery = true
)
List<Map<String, Object>> findDetailedSourcesOfItem(@Param("persistentId") String persistentId);

@Query(value =
"SELECT s.id, s.domain, s.label, s.last_harvested_date, s.url, s.url_template, i.source_item_id, i.persistent_id "+
" FROM sources s " +
" INNER JOIN items i" +
" ON i.source_id = s.id"+
" WHERE i.status = 'APPROVED' and i.persistent_id IN :persistentIds", nativeQuery = true
)
List<Map<String, Object>> findDetailedSourcesOfItems(@Param("persistentIds") List<String> persistentIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface VocabularyRepository extends JpaRepository<Vocabulary, String> {

Page<VocabularyBasicView> findAllBasicBy(Pageable pageable);

@Query(value = "SELECT v, ptv.propertyType FROM Vocabulary v, PropertyTypeVocabulary ptv where v = ptv.vocabulary and ptv.propertyType.code in :collect")
List<Object[]> findAllByPropertyTypeCode(List<String> collect);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import eu.sshopencloud.marketplace.services.search.IndexItemService;
import eu.sshopencloud.marketplace.services.sources.SourceService;
import eu.sshopencloud.marketplace.services.vocabularies.PropertyTypeService;
import eu.sshopencloud.marketplace.services.vocabularies.VocabularyService;
import eu.sshopencloud.marketplace.validators.datasets.DatasetFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -46,11 +47,12 @@ public DatasetService(DatasetRepository datasetRepository, DatasetFactory datase
ItemVisibilityService itemVisibilityService, ItemUpgradeRegistry<Dataset> itemUpgradeRegistry,
DraftItemRepository draftItemRepository, ItemRelatedItemService itemRelatedItemService,
PropertyTypeService propertyTypeService, IndexItemService indexItemService, UserService userService,
MediaStorageService mediaStorageService, SourceService sourceService, ApplicationEventPublisher eventPublisher) {
MediaStorageService mediaStorageService, SourceService sourceService, ApplicationEventPublisher eventPublisher,
VocabularyService vocabularyService) {

super(itemRepository, versionedItemRepository, itemVisibilityService, itemUpgradeRegistry, draftItemRepository,
itemRelatedItemService, propertyTypeService, indexItemService, userService, mediaStorageService,
sourceService, eventPublisher);
sourceService, eventPublisher, vocabularyService);

this.datasetRepository = datasetRepository;
this.datasetFactory = datasetFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import eu.sshopencloud.marketplace.dto.items.*;
import eu.sshopencloud.marketplace.dto.sources.SourceDto;
import eu.sshopencloud.marketplace.dto.vocabularies.PropertyDto;
import eu.sshopencloud.marketplace.dto.vocabularies.PropertyTypeDto;
import eu.sshopencloud.marketplace.dto.workflows.WorkflowDto;
import eu.sshopencloud.marketplace.mappers.items.ItemExtBasicConverter;
import eu.sshopencloud.marketplace.mappers.vocabularies.VocabularyBasicMapper;
import eu.sshopencloud.marketplace.model.auth.User;
import eu.sshopencloud.marketplace.model.items.*;
import eu.sshopencloud.marketplace.model.vocabularies.Vocabulary;
import eu.sshopencloud.marketplace.repositories.items.DraftItemRepository;
import eu.sshopencloud.marketplace.repositories.items.ItemRepository;
import eu.sshopencloud.marketplace.repositories.items.VersionedItemRepository;
Expand All @@ -24,6 +27,7 @@
import eu.sshopencloud.marketplace.services.search.IndexItemService;
import eu.sshopencloud.marketplace.services.sources.SourceService;
import eu.sshopencloud.marketplace.services.vocabularies.PropertyTypeService;
import eu.sshopencloud.marketplace.services.vocabularies.VocabularyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -32,10 +36,8 @@
import org.springframework.security.access.AccessDeniedException;

import jakarta.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
Expand All @@ -56,14 +58,15 @@ abstract class ItemCrudService<I extends Item, D extends ItemDto, P extends Pagi
private final SourceService sourceService;

private final ApplicationEventPublisher eventPublisher;
private final VocabularyService vocabularyService;


public ItemCrudService(ItemRepository itemRepository, VersionedItemRepository versionedItemRepository,
ItemVisibilityService itemVisibilityService, ItemUpgradeRegistry<I> itemUpgradeRegistry,
DraftItemRepository draftItemRepository, ItemRelatedItemService itemRelatedItemService,
PropertyTypeService propertyTypeService, IndexItemService indexItemService, UserService userService,
MediaStorageService mediaStorageService, SourceService sourceService,
ApplicationEventPublisher eventPublisher) {
ApplicationEventPublisher eventPublisher, VocabularyService vocabularyService) {

super(versionedItemRepository, itemVisibilityService);

Expand All @@ -82,6 +85,7 @@ public ItemCrudService(ItemRepository itemRepository, VersionedItemRepository ve
this.sourceService = sourceService;

this.eventPublisher = eventPublisher;
this.vocabularyService = vocabularyService;
}


Expand Down Expand Up @@ -629,38 +633,55 @@ private void completeDto(D dto, Item item) {
private void complete(ItemDto dto, Item item) {
completeDtoProperties(dto);

List<UUID> mediaIds = item.getMedia().stream().map(ItemMedia::getMediaId).collect(Collectors.toList());

Optional.ofNullable(item.getThumbnail()).ifPresent(thumbnail -> mediaIds.add(thumbnail.getMediaId()));

List<MediaDetails> listOfMediaWithThumbnail = mediaStorageService.getMediaDetails(mediaIds);

Map<UUID, List<MediaDetails>> groupedById = listOfMediaWithThumbnail.stream().collect(Collectors.groupingBy(MediaDetails::getMediaId));

for (int i = 0; i < item.getMedia().size(); ++i) {
ItemMedia media = item.getMedia().get(i);
MediaDetails mediaDetails = mediaStorageService.getMediaDetails(media.getMediaId());
dto.getMedia().get(i).setInfo(mediaDetails);
getMediaDetails(groupedById, media).ifPresent(dto.getMedia().get(i)::setInfo);
}

ItemMedia thumbnail = item.getThumbnail();
if (thumbnail != null) {
MediaDetails mediaDetails = mediaStorageService.getMediaDetails(thumbnail.getMediaId());
dto.getThumbnail().setInfo(mediaDetails);
getMediaDetails(groupedById, thumbnail).ifPresent(dto.getThumbnail()::setInfo);
}
}

private static Optional<MediaDetails> getMediaDetails(Map<UUID, List<MediaDetails>> groupedById, ItemMedia media) {
if (groupedById.get(media.getMediaId()) != null && !groupedById.get(media.getMediaId()).isEmpty()) {
return Optional.of(groupedById.get(media.getMediaId()).get(0));
}
return Optional.empty();
}


private void completeDtoProperties(ItemDto dto) {
User currentUser = LoggedInUserHolder.getLoggedInUser();
Map<String, List<Vocabulary>> allowedVocabularies = vocabularyService.getAllowedVocabulariesForPropertyTypes(
dto.getProperties().stream()
.map(PropertyDto::getType)
.map(PropertyTypeDto::getCode)
.collect(Collectors.toList()));


List<PropertyDto> properties = dto.getProperties().stream()
.filter(property -> shouldRenderProperty(property, currentUser))
.peek(property -> propertyTypeService.completePropertyType(property.getType()))
.map(property -> {
List<Vocabulary> vocabularies = allowedVocabularies.getOrDefault(property.getType().getCode(), Collections.emptyList());
if (!vocabularies.isEmpty()) {
property.getType().setAllowedVocabularies(
VocabularyBasicMapper.INSTANCE.toDto(vocabularies));
}
return property;
})
.collect(Collectors.toList());

dto.setProperties(properties);
}


private boolean shouldRenderProperty(PropertyDto property, User user) {
// hidden properties have to be always rendered
//return (!property.getType().isHidden() || (user != null && user.isModerator()));
return true;
}


protected List<ItemExtBasicDto> getItemHistory(String persistentId, Long versionId) {
I item = loadItemVersion(persistentId, versionId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public int countAllRelatedItems(Item item) {
return itemRelatedItemRepository.countAllBySubjectId(item.getId()) + itemRelatedItemRepository.countAllByObjectId(item.getId());
}

public Map<Long, Long> countAllRelatedItems(List<Long> itemsIds) {
List<Long[]> oc = itemRelatedItemRepository.countAllByObject(itemsIds);
List<Long[]> sc = itemRelatedItemRepository.countAllBySubject(itemsIds);

Map<Long, Long> result = new HashMap<>();
oc.forEach(objectsArray -> result.put(objectsArray[0], objectsArray[1]));

sc.forEach(subjectArray -> result.compute(subjectArray[0], (key, objCount) -> objCount == null ? subjectArray[1] : objCount + subjectArray[1]));

return result;
}

public List<RelatedItemDto> getItemRelatedItems(Item item) {
long itemId = item.getId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import eu.sshopencloud.marketplace.services.search.IndexItemService;
import eu.sshopencloud.marketplace.services.sources.SourceService;
import eu.sshopencloud.marketplace.services.vocabularies.PropertyTypeService;
import eu.sshopencloud.marketplace.services.vocabularies.VocabularyService;
import eu.sshopencloud.marketplace.validators.publications.PublicationFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -44,12 +45,13 @@ public PublicationService(PublicationRepository publicationRepository, Publicati
ItemVisibilityService itemVisibilityService, ItemUpgradeRegistry<Publication> itemUpgradeRegistry,
DraftItemRepository draftItemRepository, ItemRelatedItemService itemRelatedItemService,
PropertyTypeService propertyTypeService, IndexItemService indexItemService, UserService userService,
MediaStorageService mediaStorageService, SourceService sourceService, ApplicationEventPublisher eventPublisher) {
MediaStorageService mediaStorageService, SourceService sourceService, ApplicationEventPublisher eventPublisher,
VocabularyService vocabularyService) {

super(
itemRepository, versionedItemRepository, itemVisibilityService, itemUpgradeRegistry, draftItemRepository,
itemRelatedItemService, propertyTypeService, indexItemService, userService, mediaStorageService, sourceService,
eventPublisher
eventPublisher, vocabularyService
);

this.publicationRepository = publicationRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import eu.sshopencloud.marketplace.services.search.IndexItemService;
import eu.sshopencloud.marketplace.services.sources.SourceService;
import eu.sshopencloud.marketplace.services.vocabularies.PropertyTypeService;
import eu.sshopencloud.marketplace.services.vocabularies.VocabularyService;
import eu.sshopencloud.marketplace.validators.workflows.StepFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
Expand Down Expand Up @@ -60,11 +61,11 @@ public StepService(StepRepository stepRepository, StepsTreeRepository stepsTreeR
ItemUpgradeRegistry<Step> itemUpgradeRegistry, DraftItemRepository draftItemRepository,
ItemRelatedItemService itemRelatedItemService, PropertyTypeService propertyTypeService,
IndexItemService indexItemService, UserService userService, MediaStorageService mediaStorageService,
SourceService sourceService, ApplicationEventPublisher eventPublisher) {
SourceService sourceService, ApplicationEventPublisher eventPublisher, VocabularyService vocabularyService) {

super(itemRepository, versionedItemRepository, itemVisibilityService, itemUpgradeRegistry, draftItemRepository,
itemRelatedItemService, propertyTypeService, indexItemService, userService, mediaStorageService,
sourceService, eventPublisher);
sourceService, eventPublisher, vocabularyService);

this.stepRepository = stepRepository;
this.stepsTreeRepository = stepsTreeRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import eu.sshopencloud.marketplace.services.search.IndexItemService;
import eu.sshopencloud.marketplace.services.sources.SourceService;
import eu.sshopencloud.marketplace.services.vocabularies.PropertyTypeService;
import eu.sshopencloud.marketplace.services.vocabularies.VocabularyService;
import eu.sshopencloud.marketplace.validators.tools.ToolFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -44,12 +45,13 @@ public ToolService(ToolRepository toolRepository, ToolFactory toolFactory,
ItemVisibilityService itemVisibilityService, ItemUpgradeRegistry<Tool> itemUpgradeRegistry,
DraftItemRepository draftItemRepository, ItemRelatedItemService itemRelatedItemService,
PropertyTypeService propertyTypeService, IndexItemService indexItemService, UserService userService,
MediaStorageService mediaStorageService, SourceService sourceService, ApplicationEventPublisher eventPublisher) {
MediaStorageService mediaStorageService, SourceService sourceService, ApplicationEventPublisher eventPublisher,
VocabularyService vocabularyService) {

super(
itemRepository, versionedItemRepository, itemVisibilityService, itemUpgradeRegistry, draftItemRepository,
itemRelatedItemService, propertyTypeService, indexItemService, userService, mediaStorageService, sourceService,
eventPublisher
eventPublisher, vocabularyService
);

this.toolRepository = toolRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import eu.sshopencloud.marketplace.services.search.IndexItemService;
import eu.sshopencloud.marketplace.services.sources.SourceService;
import eu.sshopencloud.marketplace.services.vocabularies.PropertyTypeService;
import eu.sshopencloud.marketplace.services.vocabularies.VocabularyService;
import eu.sshopencloud.marketplace.validators.trainings.TrainingMaterialFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -46,12 +47,13 @@ public TrainingMaterialService(TrainingMaterialRepository trainingMaterialReposi
ItemVisibilityService itemVisibilityService, ItemUpgradeRegistry<TrainingMaterial> itemUpgradeRegistry,
DraftItemRepository draftItemRepository, ItemRelatedItemService itemRelatedItemService,
PropertyTypeService propertyTypeService, IndexItemService indexItemService, UserService userService,
MediaStorageService mediaStorageService, SourceService sourceService, ApplicationEventPublisher eventPublisher) {
MediaStorageService mediaStorageService, SourceService sourceService, ApplicationEventPublisher eventPublisher,
VocabularyService vocabularyService) {

super(
itemRepository, versionedItemRepository, itemVisibilityService, itemUpgradeRegistry, draftItemRepository,
itemRelatedItemService, propertyTypeService, indexItemService, userService, mediaStorageService, sourceService,
eventPublisher
eventPublisher, vocabularyService
);

this.trainingMaterialRepository = trainingMaterialRepository;
Expand Down
Loading

0 comments on commit a4bdbde

Please sign in to comment.