Skip to content

Commit

Permalink
delete multiple items
Browse files Browse the repository at this point in the history
  • Loading branch information
SrdjanStevanetic committed Oct 23, 2024
1 parent 41236ef commit 78fc040
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,43 @@ void deletePinnedItems_EntityUserSets_withEditorUser() throws Exception {

}

@Test
void deleteMultipleItems_EntityUserSets_withEditorUser() throws Exception {
WebUserSetImpl userSet = createTestUserSet(ENTITY_USER_SET_REGULAR, editorUserToken);

List<String> newItems=new ArrayList<>();
String item1="/01/123_pinnedItem";
String item2="/02/123_pinnedItem";
newItems.add(item1);
newItems.add(item2);
getUserSetService().insertMultipleItems(newItems, WebUserSetModelFields.PINNED_POSITION, 0, userSet);

assertEquals(2, userSet.getPinned());
String identifier = userSet.getIdentifier();

JSONArray newItemsJson = new JSONArray();
newItemsJson.put(item2);
//this item is from the input json (ENTITY_USER_SET_REGULAR), so please check it exists there
newItemsJson.put("http://data.europeana.eu/item/08641/1037479000000476591");
//this is not existing item
newItemsJson.put("/03/123_non_existing_item");

mockMvc
.perform(
delete(BASE_URL + "{identifier}/items", identifier)
.content(newItemsJson.toString())
.header(HttpHeaders.AUTHORIZATION, editor2UserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().is(HttpStatus.OK.value())).andReturn().getResponse();

UserSet userSet1 = getUserSetService().getUserSetById(userSet.getIdentifier());
assertEquals(1, userSet1.getPinned());
assertEquals(2, userSet1.getItems().size());

// getUserSetService().deleteUserSet(identifier);

}

private void checkItemCountAndPosition(UserSet existingUserSet, String newItem,
int expectedTotalItems, int expectedPinnedItems, int expectedPositionOfItem) {
assertEquals(expectedPinnedItems, existingUserSet.getPinned());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ private SwaggerConstants() {
public static final String INSERT_MULTIPLE_ITEM_NOTE = "Please create your insert multiple items request using selected parameters.";
public static final String PUBLISH_SET_NOTE = "Please create the request for publishing the set using the provided parameters.";
public static final String CHECK_ITEM_NOTE = "Check if item is already in a user set";
public static final String DELETE_ITEM_NOTE = "Delete a item from the set ";
public static final String DELETE_ITEM_NOTE = "Delete a item from the set";
public static final String DELETE_MULTIPLE_ITEMS_NOTE = "Delete multiple items from the set";
public static final String SEARCH = "Searching user sets. The criteria for filtering results is provided in form of <field>:<value> pairs, using the query and qf request parameters. Currently supported fields are: type, visibility, creator, item, provider and lang (title language).";
public static final String SEARCH_ITEMS_IN_SET = "Searching items within a set indicated by identifier parameter. Use the use * for query and item:<itemId> filter in qf parameter. (Multiple item filters are build an OR search criteria)";
public static final String UPDATE_SAMPLES_JSONLD = SAMPLES_JSONLD + "Please create your JSON update request using selected fields you are going " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ protected ResponseEntity<String> isItemInUserSet(String wsKey, String identifier
}


@Deprecated
@DeleteMapping(value = {"/set/{identifier}/{datasetId}/{localId}"},
produces = {HttpHeaders.CONTENT_TYPE_JSONLD_UTF8, HttpHeaders.CONTENT_TYPE_JSON_UTF8})
@Operation(description = SwaggerConstants.DELETE_ITEM_NOTE, summary = "Delete a item from the set")
Expand All @@ -701,7 +702,7 @@ public ResponseEntity<String> deleteItemFromUserSet(
Authentication authentication = verifyWriteAccess(Operations.DELETE, request);
return deleteItemFromUserSet(authentication, identifier, datasetId, localId);
}

/**
* This method validates input values and deletes item from a user set.
*
Expand Down Expand Up @@ -782,6 +783,88 @@ protected ResponseEntity<String> deleteItemFromUserSet(Authentication authentica
throw new InternalServerException(e);
}
}

@DeleteMapping(value = {"/set/{identifier}/items"},
produces = {HttpHeaders.CONTENT_TYPE_JSONLD_UTF8, HttpHeaders.CONTENT_TYPE_JSON_UTF8})
@Operation(description = SwaggerConstants.DELETE_MULTIPLE_ITEMS_NOTE, summary = "Delete multiple items from the set")
public ResponseEntity<String> deleteMultipleItemsFromUserSet(
@PathVariable(value = WebUserSetFields.PATH_PARAM_SET_ID) String identifier,
@RequestBody List<String> items,
HttpServletRequest request) throws HttpException {
// check user credentials, if invalid respond with HTTP 401,
// or if unauthorized respond with HTTP 403
Authentication authentication = verifyWriteAccess(Operations.DELETE, request);
return deleteMultipleItemsFromUserSet(authentication, identifier, items);
}

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
// retrieve an existing user set based on its identifier
UserSet existingUserSet = getUserSetService().getUserSetById(identifier);

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

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

boolean itemsRemoved=false;
// check if it is a pinned item, decrease the counter by 1 for entity sets
if (existingUserSet.isEntityBestItemsSet()) {
for(String item : items) {
int currentPosition = existingUserSet.getItems().indexOf(item);
if (currentPosition>=0) {
itemsRemoved=true;
if(currentPosition < existingUserSet.getPinned() ) {
existingUserSet.setPinned(existingUserSet.getPinned() - 1);
}
existingUserSet.getItems().remove(item);
}
}
}
else {
itemsRemoved=existingUserSet.getItems().removeAll(items);
}

UserSet updatedUserSet=existingUserSet;
if(itemsRemoved) {
// update an existing user set
updatedUserSet = getUserSetService().updateUserSetInMongo(existingUserSet);
}

//update pagination fields (used only for the response serialization)
getUserSetService().updatePagination(updatedUserSet, getConfiguration());

// serialize to JsonLd
String serializedUserSetJsonLdStr = serializeUserSet(LdProfiles.MINIMAL, updatedUserSet);
String etag =
generateETag(updatedUserSet.getModified(), WebFields.FORMAT_JSONLD, getApiVersion());

// respond with HTTP 200 containing the updated Set description as body.
// serialize Set in JSON-LD following the requested profile
// (if not indicated assume the default, ie. minimal)
// build response entity with headers
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(5);
headers.add(HttpHeaders.ALLOW, UserSetHttpHeaders.ALLOW_PPGHD);
headers.add(UserSetHttpHeaders.PREFERENCE_APPLIED, LdProfiles.MINIMAL.getPreferHeaderValue());
headers.add(UserSetHttpHeaders.ETAG, etag);

return new ResponseEntity<>(serializedUserSetJsonLdStr, headers, HttpStatus.OK);
} catch (UserSetValidationException | UserSetInstantiationException e) {
throw new RequestBodyValidationException(UserSetI18nConstants.USERSET_CANT_PARSE_BODY,
new String[] {e.getMessage()}, e);
} catch (HttpException e) {
throw e;
} catch (RuntimeException | IOException e) {
throw new InternalServerException(e);
}
}


@DeleteMapping(value = {"/set/{identifier}"})
@Operation(summary= "Delete Set", description = "Delete an existing user set")
Expand Down

0 comments on commit 78fc040

Please sign in to comment.