Skip to content

Commit

Permalink
add missing validations and small refactorings #EA-3949
Browse files Browse the repository at this point in the history
  • Loading branch information
gsergiu committed Nov 21, 2024
1 parent 375e9d6 commit d3d865c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package eu.europeana.set.web.service.controller.jsonld;

import eu.europeana.set.definitions.model.vocabulary.WebUserSetModelFields;

public class WeUserSetRequestUtils {

public static boolean isPinnRequest(String position) {
return WebUserSetModelFields.PINNED_POSITION.equals(position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,16 @@ private ResponseEntity<String> processRetrieveSetPageRequest(String identifier,
if (profiles.isEmpty()) {
profiles.add(SetPageProfile.ITEMS);
}

validateMultipleProfiles(profiles, profile);
SetPageProfile searializationProfile = getUserSetService().getProfileForPagination(profiles);
pageNr =
parseIntegerParam(CommonApiConstants.QUERY_PARAM_PAGE, page, -1, WebUserSetFields.DEFAULT_PAGE);
SetPageProfile searializationProfile = getUserSetService().getProfileForPagination(profiles);

pageNr = parseIntegerParam(CommonApiConstants.QUERY_PARAM_PAGE, page, -1,
WebUserSetFields.DEFAULT_PAGE);
pageNr = (pageNr == null) ? Integer.valueOf(WebUserSetFields.DEFAULT_PAGE) : pageNr;
int maxPageSize = getConfiguration().getMaxPageSize(searializationProfile.getProfileParamValue());

int maxPageSize =
getConfiguration().getMaxPageSize(searializationProfile.getProfileParamValue());

pageItems = parseIntegerParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, pageSize, maxPageSize,
UserSetConfigurationImpl.MIN_ITEMS_PER_PAGE);
pageItems =
Expand Down Expand Up @@ -524,7 +525,8 @@ protected ResponseEntity<String> insertItemIntoUserSet(HttpServletRequest reques
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add(HttpHeaders.ALLOW, UserSetHttpHeaders.ALLOW_PPGHD);
headers.add(UserSetHttpHeaders.VARY, HttpHeaders.PREFER);
headers.add(UserSetHttpHeaders.PREFERENCE_APPLIED, SetPageProfile.META.getPreferenceApplied());
headers.add(UserSetHttpHeaders.PREFERENCE_APPLIED,
SetPageProfile.META.getPreferenceApplied());
headers.add(UserSetHttpHeaders.ETAG, etag);
return new ResponseEntity<>(serializedUserSetJsonLdStr, headers, HttpStatus.OK);

Expand Down Expand Up @@ -558,39 +560,43 @@ protected ResponseEntity<String> insertMultipleItemsIntoUserSet(HttpServletReque
// retrieve an existing user set based on its identifier
UserSet existingUserSet = getUserSetService().getUserSetById(identifier);

//4. check if user is authorized check visibility level for given user
// 4. check if user is authorized check visibility level for given user
getUserSetService().verifyPermissionToUpdate(existingUserSet, authentication, true);

//5. assign contribtor for entityBestItemsSet
// 5. assign contribtor for entityBestItemsSet
// for entity user sets, add users with 'editor' role as contributors
addContributorForEntitySet(existingUserSet, authentication);

//6. check if set is closed

// 6. check if set is closed
verifyIfClosedSet(existingUserSet);

int itemsPosition = parseAndValidateItemPosition(position, existingUserSet);
//7. verify size for Galleries
//TODO

//8. pinned is available only for entityBestItemsSet
// 8. pinned is available only for entityBestItemsSet
// if set is not entity best item set and position is "pin", throw exception
if (!existingUserSet.isEntityBestItemsSet()
&& StringUtils.equals(position, PINNED_POSITION)) {
throw new RequestValidationException(UserSetI18nConstants.USER_SET_OPERATION_NOT_ALLOWED,
new String[] {"Pinning item ", existingUserSet.getType()});
}

//9. verify if possition is higher than pinned
//TODO



// 9. verify if position is higher than pinned
if (!WeUserSetRequestUtils.isPinnRequest(position) && itemsPosition > -1
&& itemsPosition < existingUserSet.getPinned()) {
throw new RequestValidationException(UserSetI18nConstants.USER_SET_OPERATION_NOT_ALLOWED,
new String[] {"Position smaller than pinned is not allowed for non pin request!",
itemsPosition + " < " + existingUserSet.getPinned()});
}


// check timestamp if provided within the “If-Match” HTTP header, if false
// respond with HTTP 412
String eTagOrigin =
generateETag(existingUserSet.getModified(), FORMAT_JSONLD, getApiVersion());
checkIfMatchHeader(eTagOrigin, request);

// 7. (verify size for Galleries) & 11-13. (process items)
UserSet updatedUserSet =
getUserSetService().insertMultipleItems(items, position, itemsPosition, existingUserSet);

Expand All @@ -602,7 +608,8 @@ protected ResponseEntity<String> insertMultipleItemsIntoUserSet(HttpServletReque
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add(HttpHeaders.ALLOW, UserSetHttpHeaders.ALLOW_PPGHD);
headers.add(UserSetHttpHeaders.VARY, HttpHeaders.PREFER);
headers.add(UserSetHttpHeaders.PREFERENCE_APPLIED, SetPageProfile.META.getPreferenceApplied());
headers.add(UserSetHttpHeaders.PREFERENCE_APPLIED,
SetPageProfile.META.getPreferenceApplied());
headers.add(UserSetHttpHeaders.ETAG, etag);
return new ResponseEntity<>(serializedUserSetJsonLdStr, headers, HttpStatus.OK);

Expand Down Expand Up @@ -819,7 +826,8 @@ protected ResponseEntity<String> deleteItemFromUserSet(Authentication authentica
// build response entity with headers
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(5);
headers.add(HttpHeaders.ALLOW, UserSetHttpHeaders.ALLOW_PPGHD);
headers.add(UserSetHttpHeaders.PREFERENCE_APPLIED, SetPageProfile.META.getPreferenceApplied());
headers.add(UserSetHttpHeaders.PREFERENCE_APPLIED,
SetPageProfile.META.getPreferenceApplied());
headers.add(UserSetHttpHeaders.ETAG, etag);

return new ResponseEntity<>(serializedUserSetJsonLdStr, headers, HttpStatus.OK);
Expand Down Expand Up @@ -849,24 +857,25 @@ public ResponseEntity<String> deleteMultipleItemsFromUserSet(
protected ResponseEntity<String> deleteMultipleItemsFromUserSet(Authentication authentication,
String identifier, List<String> items) throws HttpException {
try {
//3. check if the Set exists, if not respond with HTTP 404
// 3. check if the Set exists, if not respond with HTTP 404
// retrieve an existing user set based on its identifier
UserSet existingUserSet = getUserSetService().getUserSetById(identifier);

//4. Check if the Set it is a closed set
// 4. Check if the Set it is a closed set
verifyIfClosedSet(existingUserSet);
//5. Check if the user is authorised

// 5. Check if the user is authorised
// check if the user is the owner/creator of the set or admin,
// OR Editor for Entity sets, otherwise respond with
// 403
getUserSetService().verifyPermissionToUpdate(existingUserSet, authentication, true);

//6. If the “type” of the set is “EntityBestItemsSet” assign the user associated to the JWT token
// 6. If the “type” of the set is “EntityBestItemsSet” assign the user associated to the JWT
// token
// for entity user sets, add users with 'editor' role as contributors
addContributorForEntitySet(existingUserSet, authentication);

//7. & 8. remove items, update pinned, update modified
// 7. & 8. remove items, update pinned, update modified
UserSet updatedUserSet = getUserSetService().deleteMultipleItems(items, existingUserSet);

// serialize to JsonLd
Expand All @@ -879,7 +888,8 @@ protected ResponseEntity<String> deleteMultipleItemsFromUserSet(Authentication a
// build response entity with headers
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(5);
headers.add(HttpHeaders.ALLOW, UserSetHttpHeaders.ALLOW_PPGHD);
headers.add(UserSetHttpHeaders.PREFERENCE_APPLIED, SetPageProfile.META.getPreferenceApplied());
headers.add(UserSetHttpHeaders.PREFERENCE_APPLIED,
SetPageProfile.META.getPreferenceApplied());
headers.add(UserSetHttpHeaders.ETAG, etag);

return new ResponseEntity<>(serializedUserSetJsonLdStr, headers, HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import eu.europeana.set.web.model.search.SearchApiUtils;
import eu.europeana.set.web.model.search.UserSetIdsResultPage;
import eu.europeana.set.web.model.search.UserSetResultPage;
import eu.europeana.set.web.service.controller.jsonld.WeUserSetRequestUtils;
import ioinformarics.oss.jackson.module.jsonld.JsonldModule;

public class UserSetServiceImpl extends BaseUserSetServiceImpl {
Expand Down Expand Up @@ -321,13 +322,17 @@ public UserSet insertMultipleItems(List<String> items, String position, int item

List<String> fullUriItems = validateItemsStrings(items);
List<String> duplicatedItems = computeDuplicateList(existingUserSet, fullUriItems);
boolean isPinnRequest = WebUserSetModelFields.PINNED_POSITION.equals(position);
boolean isPinnRequest = WeUserSetRequestUtils.isPinnRequest(position);

if (duplicatedItems != null) {
processDuplicates(existingUserSet, fullUriItems, duplicatedItems, isPinnRequest);
}

addItems(existingUserSet, fullUriItems, itemsPosition, isPinnRequest);
if(existingUserSet.isGallery()) {
//7. check that gallery size is smaller than the predefined limit
validateGallerySize(existingUserSet, 0);
}

// UserSet userSet;
// //update items
Expand All @@ -353,11 +358,7 @@ private void processDuplicates(UserSet existingUserSet, List<String> items,
if (existingUserSet.getPinned() > 0) {
final List<String> pinnedItems =
existingUserSet.getItems().subList(0, existingUserSet.getPinned());
for (String pinnedItem : pinnedItems) {
if (duplicatedItems.contains(pinnedItem)) {
duplicatedPinned++;
}
}
duplicatedPinned = countDupplicatedItems(duplicatedItems, pinnedItems);
}
// remove all duplicates from the set
existingUserSet.getItems().removeAll(duplicatedItems);
Expand All @@ -379,6 +380,16 @@ private void processDuplicates(UserSet existingUserSet, List<String> items,

}

private int countDupplicatedItems(List<String> duplicatedItems, final List<String> pinnedItems) {
int counter = 0;
for (String pinnedItem : pinnedItems) {
if (duplicatedItems.contains(pinnedItem)) {
counter++;
}
}
return counter;
}

private void addItems(UserSet existingUserSet, List<String> items, int position,
boolean isPinnRequest) {
if (isPinnRequest) {
Expand Down Expand Up @@ -410,64 +421,7 @@ private List<String> computeDuplicateList(@NonNull UserSet existingUserSet,
return res;
}

private void 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 of removed pinned items, to
* change the pinned field
*/
for (String newItem : items) {
int itemindex = usersetItems.indexOf(newItem);
if (itemindex >= 0) {
if (itemindex < existingUserSet.getPinned()) {
existingUserSet.setPinned(existingUserSet.getPinned() - 1);
}
usersetItems.remove(newItem);
}
}
}

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

private void 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
*/
for (String newItem : newItemsCopy) {
int itemindex = usersetItems.indexOf(newItem);
if (itemindex >= 0) {
if (itemindex < existingUserSet.getPinned()) {
items.remove(newItem);
} else {
usersetItems.remove(newItem);
}
}
}
// validation of the number of items for type Gallery
if (existingUserSet.isGallery()
&& (usersetItems.size() + items.size()) > getConfiguration().getGalleryMaxSize()) {
throw new ItemValidationException(UserSetI18nConstants.USERSET_ITEMS_LIMIT_REACHED,
new String[] {String.valueOf(getConfiguration().getGalleryMaxSize())});
}
}

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

/*
* (non-Javadoc)
Expand All @@ -484,15 +438,15 @@ public UserSet insertItem(String datasetId, String localId, String position,
UserSetUtils.buildItemUrl(getConfiguration().getItemDataEndpoint(), datasetId, localId);

// check max number of items for the sets of type Collection
if (existingUserSet.isCollection() && !existingUserSet.hasItem(newItem)) {
if (existingUserSet.isGallery() && !existingUserSet.hasItem(newItem)) {
validateGallerySize(existingUserSet, 1);
}

// check if the position is "pin" and is a EntityBestItem set then
// insert the item at the 0 position
UserSet userSet;

if (WebUserSetModelFields.PINNED_POSITION.equals(position)
if (WeUserSetRequestUtils.isPinnRequest(position)
&& existingUserSet.isEntityBestItemsSet()) {
userSet = insertItem(existingUserSet, newItem, 0, true);
} else {
Expand Down

0 comments on commit d3d865c

Please sign in to comment.