Skip to content

Commit

Permalink
Merge pull request #274 from europeana/EA-3949-update-isShownBy
Browse files Browse the repository at this point in the history
Ea 3949 update is shown by
  • Loading branch information
gsergiu authored Nov 21, 2024
2 parents 20b52a8 + d3d865c commit 33648db
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public interface UserSet extends PageInfo {
int getPinned();

void setPinned(int pinned);

void increasePinned(int count);

void descreasePinned(int count);

List<String> getContributor();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,12 @@ public BaseWebResource getIsShownBy() {
public void setIsShownBy(BaseWebResource isShownBy) {
this.isShownBy = isShownBy;
}

public void increasePinned(int count) {
this.pinned += count;
}

public void descreasePinned(int count) {
this.pinned = Math.max(0, pinned - count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ void insertMultipleItems_EntityUserSets_withEditorUser() throws Exception {
UserSet existingUserSet = getUserSetService().getUserSetById(userSet.getIdentifier());
//check for the new items
assertEquals(0, existingUserSet.getItems().indexOf(item1));
assertEquals(1, existingUserSet.getItems().indexOf(item2));
String item2FullUrl = UserSetUtils.buildItemUrl(getConfiguration().getItemDataEndpoint(), item2);
assertEquals(1, existingUserSet.getItems().indexOf(item2FullUrl));
assertEquals(2, existingUserSet.getPinned());
assertEquals(4, existingUserSet.getItems().size());

Expand All @@ -493,11 +494,13 @@ void insertMultipleItems_EntityUserSets_withEditorUser() throws Exception {
String item4Existing=existingUserSet.getItems().get(2);
newItems.add(item4Existing);
newItems.add(item3);
getUserSetService().insertMultipleItems(newItems, WebUserSetModelFields.PINNED_POSITION, -1, existingUserSet);
existingUserSet = getUserSetService().insertMultipleItems(newItems, WebUserSetModelFields.PINNED_POSITION, -1, existingUserSet);

//check the new items
assertEquals(0, existingUserSet.getItems().indexOf(item2));
assertEquals(0, existingUserSet.getItems().indexOf(item2FullUrl));
assertEquals(1, existingUserSet.getItems().indexOf(item4Existing));
assertEquals(2, existingUserSet.getItems().indexOf(item3));
String item3FullUrl = UserSetUtils.buildItemUrl(getConfiguration().getItemDataEndpoint(), item3);
assertEquals(2, existingUserSet.getItems().indexOf(item3FullUrl));
assertEquals(3, existingUserSet.getItems().indexOf(item1));
assertEquals(4, existingUserSet.getPinned());
assertEquals(5, existingUserSet.getItems().size());
Expand All @@ -510,14 +513,16 @@ void insertMultipleItems_EntityUserSets_withEditorUser() throws Exception {
newItems.add(item5);
String item6Existing=existingUserSet.getItems().get(4);
newItems.add(item6Existing);
getUserSetService().insertMultipleItems(newItems, "4", 4, existingUserSet);
existingUserSet = getUserSetService().insertMultipleItems(newItems, "4", 4, existingUserSet);

assertEquals(4, existingUserSet.getPinned());
//check the new items
assertEquals(0, existingUserSet.getItems().indexOf(item2));
assertEquals(0, existingUserSet.getItems().indexOf(item2FullUrl));
assertEquals(1, existingUserSet.getItems().indexOf(item4Existing));
assertEquals(2, existingUserSet.getItems().indexOf(item3));
assertEquals(2, existingUserSet.getItems().indexOf(item3FullUrl));
assertEquals(3, existingUserSet.getItems().indexOf(item1));
assertEquals(4, existingUserSet.getPinned());
assertEquals(4, existingUserSet.getItems().indexOf(item5));
String item5FullUrl = UserSetUtils.buildItemUrl(getConfiguration().getItemDataEndpoint(), item5);
assertEquals(4, existingUserSet.getItems().indexOf(item5FullUrl));
assertEquals(5, existingUserSet.getItems().indexOf(item6Existing));
assertEquals(4, existingUserSet.getPinned());
assertEquals(6, existingUserSet.getItems().size());
Expand All @@ -526,18 +531,20 @@ void insertMultipleItems_EntityUserSets_withEditorUser() throws Exception {
newItems.clear();
String item7="/07/123_unPinnedItem";
newItems.add(item7);
getUserSetService().insertMultipleItems(newItems, null, -1, existingUserSet);
existingUserSet = getUserSetService().insertMultipleItems(newItems, null, -1, existingUserSet);
//check the new items
assertEquals(6, existingUserSet.getItems().indexOf(item7));
String item7FullUrl = UserSetUtils.buildItemUrl(getConfiguration().getItemDataEndpoint(), item7);
assertEquals(6, existingUserSet.getItems().indexOf(item7FullUrl));
assertEquals(4, existingUserSet.getPinned());
assertEquals(7, existingUserSet.getItems().size());
//insert item to the position grater than the total size of items
newItems.clear();
String item8="/08/123_unPinnedItem";
newItems.add(item8);
getUserSetService().insertMultipleItems(newItems, "100", 100, existingUserSet);
existingUserSet = getUserSetService().insertMultipleItems(newItems, "100", 100, existingUserSet);
//check the new items
assertEquals(7, existingUserSet.getItems().indexOf(item8));
String item8FullUrl = UserSetUtils.buildItemUrl(getConfiguration().getItemDataEndpoint(), item8);
assertEquals(7, existingUserSet.getItems().indexOf(item8FullUrl));
assertEquals(4, existingUserSet.getPinned());
assertEquals(8, existingUserSet.getItems().size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ UserSet insertMultipleItems(List<String> items, String position, int itemsPositi

UserSet deleteItem(String item, UserSet existingUserSet);

UserSet deleteMultipleItems(List<String> items, UserSet existingUserSet);
UserSet deleteMultipleItems(List<String> items, UserSet existingUserSet) throws ItemValidationException;

/**
* search user sets using the given query and profile
Expand Down
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 @@ -492,11 +493,7 @@ protected ResponseEntity<String> insertItemIntoUserSet(HttpServletRequest reques
// retrieve an existing user set based on its identifier
UserSet existingUserSet = getUserSetService().getUserSetById(identifier);

if (existingUserSet.isOpenSet()) {
// cannot add items to open sets
throw new RequestValidationException(UserSetI18nConstants.USER_SET_OPERATION_NOT_ALLOWED,
new String[] {"'Insert item to existing user set'", "open"});
}
verifyIfClosedSet(existingUserSet);

// if set is not entity set and position is "pin", throw exception
if (!existingUserSet.isEntityBestItemsSet()
Expand Down Expand Up @@ -528,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,42 +556,47 @@ protected ResponseEntity<String> insertMultipleItemsIntoUserSet(HttpServletReque
Authentication authentication, String identifier, List<String> items, String position)
throws HttpException {
try {
// 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);

if (existingUserSet.isOpenSet()) {
// cannot add items to open sets
throw new RequestValidationException(UserSetI18nConstants.USER_SET_OPERATION_NOT_ALLOWED,
new String[] {"'Insert item to existing user set'", "open"});
}
// 4. check if user is authorized check visibility level for given user
getUserSetService().verifyPermissionToUpdate(existingUserSet, authentication, true);

// 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
verifyIfClosedSet(existingUserSet);

int itemsPosition = parseAndValidateItemPosition(position, existingUserSet);

// 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()});
}

int itemsPosition = parseItemsPosition(position);
if (!StringUtils.equals(position, WebUserSetModelFields.PINNED) && itemsPosition >= 0
// 9. verify if position is higher than pinned
if (!WeUserSetRequestUtils.isPinnRequest(position) && itemsPosition > -1
&& itemsPosition < existingUserSet.getPinned()) {
throw new RequestValidationException(UserSetI18nConstants.INVALID_UNPINNED_ITEMS_POSITION,
null);
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 visibility level for given user
getUserSetService().verifyPermissionToUpdate(existingUserSet, authentication, true);

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

// 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 @@ -605,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 All @@ -619,6 +623,25 @@ protected ResponseEntity<String> insertMultipleItemsIntoUserSet(HttpServletReque
}
}

private void verifyIfClosedSet(UserSet existingUserSet) throws RequestValidationException {
if (existingUserSet.isOpenSet()) {
// cannot add items to open sets
throw new RequestValidationException(UserSetI18nConstants.USER_SET_OPERATION_NOT_ALLOWED,
new String[] {"'Insert item to existing user set'", "open"});
}
}

private int parseAndValidateItemPosition(String position, UserSet existingUserSet)
throws ParamValidationException, RequestValidationException {
int itemsPosition = parseItemsPosition(position);
if (!StringUtils.equals(position, WebUserSetModelFields.PINNED) && itemsPosition >= 0
&& itemsPosition < existingUserSet.getPinned()) {
throw new RequestValidationException(UserSetI18nConstants.INVALID_UNPINNED_ITEMS_POSITION,
null);
}
return itemsPosition;
}

// returns -1 if not provided
private int parseItemsPosition(String position) throws ParamValidationException {
if (StringUtils.equals(position, PINNED_POSITION)) {
Expand Down Expand Up @@ -803,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 @@ -833,18 +857,25 @@ public ResponseEntity<String> deleteMultipleItemsFromUserSet(
protected ResponseEntity<String> deleteMultipleItemsFromUserSet(Authentication authentication,
String identifier, List<String> items) throws HttpException {
try {
// 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
verifyIfClosedSet(existingUserSet);

// 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
// for entity user sets, add users with 'editor' role as contributors
addContributorForEntitySet(existingUserSet, authentication);

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

// serialize to JsonLd
Expand All @@ -857,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 @@ -544,14 +544,24 @@ protected void validateItems(List<String> items) throws ItemValidationException
}

//items can be either a uri or a record identifier (e.g. "/1234/XPTO_2")
protected void validateItemsStrings(List<String> items) throws ItemValidationException {
if(items==null || items.isEmpty()) {
return;
protected List<String> validateItemsStrings(List<String> items) throws ItemValidationException {
List<String> itemsWithFullUrls = new ArrayList<String>();
if(items==null) {
return null;
}

List<String> invalidItems = new ArrayList<>();
for(String item : items) {
try {
validateItem(item);
if(item.startsWith(getConfiguration().getItemDataEndpoint())) {
itemsWithFullUrls.add(item);
} else {
//convert to full URL
itemsWithFullUrls.add(
UserSetUtils.buildItemUrl(getConfiguration().getItemDataEndpoint(), item));
}

} catch (ItemValidationException ex) {
logger.trace("Invalid item: {}", item);
invalidItems.add(item);
Expand All @@ -560,6 +570,7 @@ protected void validateItemsStrings(List<String> items) throws ItemValidationExc
if(!invalidItems.isEmpty()) {
throw new ItemValidationException(UserSetI18nConstants.USERSET_ITEM_INVALID_FORMAT, new String[] {invalidItems.toString()} );
}
return itemsWithFullUrls;
}

private void validateItem(String item) throws ItemValidationException {
Expand Down
Loading

0 comments on commit 33648db

Please sign in to comment.