Skip to content

Commit

Permalink
update isShownBy based on the first item
Browse files Browse the repository at this point in the history
  • Loading branch information
SrdjanStevanetic committed Nov 12, 2024
1 parent 074858a commit c8a90ff
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ ItemIdsResultPage buildItemIdsResultsPage(String setId, List<String> 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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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()) {
Expand All @@ -262,8 +288,11 @@ public UserSet deleteMultipleItems(List<String> 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)
Expand All @@ -276,24 +305,33 @@ public UserSet insertMultipleItems(List<String> 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;
}

private UserSet updateItemsFromPinned(UserSet existingUserSet, List<String> items) {
List<String> usersetItems=existingUserSet.getItems();
if(usersetItems==null) {
usersetItems=new ArrayList<>();
existingUserSet.setItems(usersetItems);
}
else {
/*remove all duplicate items from the set and count the number
Expand All @@ -312,14 +350,15 @@ private UserSet updateItemsFromPinned(UserSet existingUserSet, List<String> item

usersetItems.addAll(0, items);
existingUserSet.setPinned(existingUserSet.getPinned() + items.size());
return writeUserSetToDb(existingUserSet);
return existingUserSet;
}

private UserSet updateItemsFromUnpinned(UserSet existingUserSet, List<String> items, int position) throws ItemValidationException {
List<String> usersetItems=existingUserSet.getItems();
List<String> 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
Expand All @@ -343,7 +382,7 @@ private UserSet updateItemsFromUnpinned(UserSet existingUserSet, List<String> it

int positionFinal=calculatePosition(position, usersetItems);
usersetItems.addAll(positionFinal, items);
return writeUserSetToDb(existingUserSet);
return existingUserSet;
}

/*
Expand Down Expand Up @@ -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;
}

}

0 comments on commit c8a90ff

Please sign in to comment.