From c8a90ff3e5e286089f3fb3c62ce4babcc9f4eeff Mon Sep 17 00:00:00 2001 From: Srdjan Stevanetic Date: Tue, 12 Nov 2024 13:22:25 +0100 Subject: [PATCH] update isShownBy based on the first item --- .../set/web/model/search/SearchApiUtils.java | 11 ++-- .../set/web/service/UserSetService.java | 2 +- .../web/service/impl/UserSetServiceImpl.java | 59 ++++++++++++++++--- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/set-web/src/main/java/eu/europeana/set/web/model/search/SearchApiUtils.java b/set-web/src/main/java/eu/europeana/set/web/model/search/SearchApiUtils.java index 1ec9a446..ed20976b 100644 --- a/set-web/src/main/java/eu/europeana/set/web/model/search/SearchApiUtils.java +++ b/set-web/src/main/java/eu/europeana/set/web/model/search/SearchApiUtils.java @@ -57,13 +57,14 @@ public String buildSearchApiUrl(UserSet userSet, String apiKey, String searchUrl * Will create the Search Api post request url * eg : https://api.europeana.eu/record/v2/search.json?wskey=api2demo * - * @param userSet the user set for which the - * @param apiKey apiKey used to invoke the search api - * @param baseSearchApiUrl the base url of the search api + * @param baseSearchApiUrl + * @param baseItemUrl + * @param itemId + * @param apiKey * @param profile * @return */ - public String buildSearchApiUrlForItem(String baseSearchApiUrl, String itemId, String apiKey, String profile) { + public String buildSearchApiUrlForItem(String baseSearchApiUrl, String baseItemUrl, String itemId, String apiKey, String profile) { StringBuilder url = new StringBuilder(); url.append(getBaseSearchUrl(baseSearchApiUrl)); @@ -74,7 +75,7 @@ public String buildSearchApiUrlForItem(String baseSearchApiUrl, String itemId, S url.append('&').append(CommonApiConstants.QUERY_PARAM_PROFILE).append('=').append(profile); } - String europeanaId = UserSetUtils.extractItemIdentifier(itemId); + String europeanaId = itemId.startsWith(baseItemUrl) ? UserSetUtils.extractItemIdentifier(itemId) : itemId; final String searchQuery = "europeana_id:\"" + europeanaId + "\""; url.append("&query=").append(URLEncoder.encode(searchQuery, StandardCharsets.UTF_8)); diff --git a/set-web/src/main/java/eu/europeana/set/web/service/UserSetService.java b/set-web/src/main/java/eu/europeana/set/web/service/UserSetService.java index e12c93ec..a4b6bddf 100644 --- a/set-web/src/main/java/eu/europeana/set/web/service/UserSetService.java +++ b/set-web/src/main/java/eu/europeana/set/web/service/UserSetService.java @@ -257,7 +257,7 @@ ItemIdsResultPage buildItemIdsResultsPage(String setId, List itemIds, in UserSet publishUnpublishUserSet(String userSetId, Date issued, Authentication authentication, boolean publish) throws HttpException; void validateGallerySize(UserSet webUserSet, int newItems) throws ItemValidationException; - + WebResource generateDepiction(UserSet userSet) throws SearchApiClientException; } \ No newline at end of file diff --git a/set-web/src/main/java/eu/europeana/set/web/service/impl/UserSetServiceImpl.java b/set-web/src/main/java/eu/europeana/set/web/service/impl/UserSetServiceImpl.java index 867e7f2d..42564c61 100644 --- a/set-web/src/main/java/eu/europeana/set/web/service/impl/UserSetServiceImpl.java +++ b/set-web/src/main/java/eu/europeana/set/web/service/impl/UserSetServiceImpl.java @@ -242,7 +242,33 @@ public UserSet deleteItem(String item, UserSet existingUserSet) { return updatedUserSet; } + private void updateIsShownBy(UserSet userSet, String firstItemOld) { + String firstItemNew=null; + if(userSet.getItems()!=null && !userSet.getItems().isEmpty()) { + firstItemNew=userSet.getItems().get(0); + } + if(!StringUtils.equals(firstItemOld, firstItemNew)) { + try { + final WebResource isShownBy = generateDepiction(userSet); + userSet.setIsShownBy(isShownBy); + } catch (SearchApiClientException e) { + if(getLogger().isInfoEnabled()) { + getLogger().info("Cannot generate depiciton for set: {}. Exception: {}.", userSet.getIdentifier(), e.getMessage()); + } + } + } + } + public UserSet deleteMultipleItems(List items, UserSet existingUserSet) { + //keep the first item to check if it is changed, for the re-creation of the isShownBy field + String firstItemOld=null; + if(existingUserSet.getItems()!=null && !existingUserSet.getItems().isEmpty()) { + firstItemOld=existingUserSet.getItems().get(0); + } + else { + return existingUserSet; + } + boolean itemsRemoved=false; // check if it is a pinned item, decrease the counter by 1 for entity sets if (existingUserSet.isEntityBestItemsSet()) { @@ -262,8 +288,11 @@ public UserSet deleteMultipleItems(List items, UserSet existingUserSet) UserSet updatedUserSet=existingUserSet; if(itemsRemoved) { - // update an existing user set - updatedUserSet = writeUserSetToDb(existingUserSet); + //update isShownBy + updateIsShownBy(updatedUserSet, firstItemOld); + + // update a user set in db + updatedUserSet = writeUserSetToDb(updatedUserSet); } //update pagination fields (used only for the response serialization) @@ -276,17 +305,25 @@ public UserSet insertMultipleItems(List items, String position, int item validateItemsStrings(items); - // check if the position is "pin" and is a EntityBestItem set then - // insert the item at the 0 position + //keep the first item to check if it is changed, for the re-creation of the isShownBy field + String firstItemOld=null; + if(existingUserSet.getItems()!=null && !existingUserSet.getItems().isEmpty()) { + firstItemOld=existingUserSet.getItems().get(0); + } + + //update items UserSet userSet; - if (WebUserSetModelFields.PINNED_POSITION.equals(position)) { userSet=updateItemsFromPinned(existingUserSet, items); } else { userSet=updateItemsFromUnpinned(existingUserSet, items, itemsPosition); } - updatePagination(userSet, getConfiguration()); + //update isShownBy + updateIsShownBy(userSet, firstItemOld); + + writeUserSetToDb(userSet); + updatePagination(userSet, getConfiguration()); return userSet; } @@ -294,6 +331,7 @@ private UserSet updateItemsFromPinned(UserSet existingUserSet, List item List usersetItems=existingUserSet.getItems(); if(usersetItems==null) { usersetItems=new ArrayList<>(); + existingUserSet.setItems(usersetItems); } else { /*remove all duplicate items from the set and count the number @@ -312,7 +350,7 @@ private UserSet updateItemsFromPinned(UserSet existingUserSet, List item usersetItems.addAll(0, items); existingUserSet.setPinned(existingUserSet.getPinned() + items.size()); - return writeUserSetToDb(existingUserSet); + return existingUserSet; } private UserSet updateItemsFromUnpinned(UserSet existingUserSet, List items, int position) throws ItemValidationException { @@ -320,6 +358,7 @@ private UserSet updateItemsFromUnpinned(UserSet existingUserSet, List it List newItemsCopy=new ArrayList<>(items); if(usersetItems==null) { usersetItems=new ArrayList<>(); + existingUserSet.setItems(usersetItems); } else { /*remove from the new items the ones that were pinned before *and from the user set the ones that are duplicates @@ -343,7 +382,7 @@ private UserSet updateItemsFromUnpinned(UserSet existingUserSet, List it int positionFinal=calculatePosition(position, usersetItems); usersetItems.addAll(positionFinal, items); - return writeUserSetToDb(existingUserSet); + return existingUserSet; } /* @@ -943,10 +982,12 @@ public WebResource generateDepiction(UserSet userSet) throws SearchApiClientExce String itemId = userSet.getItems().get(0); String url = SearchApiUtils.getInstance().buildSearchApiUrlForItem(getConfiguration().getSearchApiUrl(), - itemId, getConfiguration().getSearchApiKey(), getConfiguration().getSearchApiProfileForItemDescriptions()); + getConfiguration().getItemDataEndpoint(), itemId, getConfiguration().getSearchApiKey(), + getConfiguration().getSearchApiProfileForItemDescriptions()); WebResource depiction = new WebResource(); getSearchApiClient().fillDepiction(url, itemId, depiction); return depiction; } + }