Skip to content

Commit

Permalink
1) created delete items method in the service; 2) some methods renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
SrdjanStevanetic committed Oct 31, 2024
1 parent 78fc040 commit 674b818
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,5 @@ public static String extractItemIdentifier(String dataEuropeanaUri, String item
return StringUtils.substring(dataEuropeanaUri, itemDataEndpoint.length());
}
}

//returns -1 if invalid or not provided
public static int parseItemsPosition(String position) {
int positionFinal = -1;
if (StringUtils.isNotEmpty(position)) {
try {
positionFinal = Integer.parseInt(position);
} catch (RuntimeException e) {
return -1;
}
}
return positionFinal;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import eu.europeana.api.commons.web.exception.ApplicationAuthenticationException;
import eu.europeana.api.commons.web.exception.HttpException;
import eu.europeana.api.commons.web.exception.ParamValidationException;
import eu.europeana.set.definitions.config.UserSetConfiguration;
import eu.europeana.set.definitions.model.UserSet;
import eu.europeana.set.definitions.model.search.UserSetFacetQuery;
import eu.europeana.set.definitions.model.search.UserSetQuery;
Expand Down Expand Up @@ -135,13 +134,9 @@ UserSet insertItem(String datasetId, String localId, String position, UserSet ex
public UserSet insertMultipleItems(List<String> items, String position, int itemsPosition, UserSet existingUserSet)
throws ItemValidationException;

/**
* This method updates existing item list
*
* @param existingUserSet
* @return updated user set
*/
UserSet updateUserSetInMongo(UserSet existingUserSet);
UserSet deleteItem(String item, UserSet existingUserSet);

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

/**
* search user sets using the given query and profile
Expand Down Expand Up @@ -260,7 +255,5 @@ 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;

UserSet updatePagination(UserSet userSet, UserSetConfiguration config);

}
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@ protected ResponseEntity<String> insertItemIntoUserSet(HttpServletRequest reques

UserSet updatedUserSet =
getUserSetService().insertItem(datasetId, localId, position, existingUserSet);
getUserSetService().updatePagination(updatedUserSet, getConfiguration());

String serializedUserSetJsonLdStr = serializeUserSet(LdProfiles.MINIMAL, updatedUserSet);

Expand Down Expand Up @@ -555,7 +554,7 @@ protected ResponseEntity<String> insertMultipleItemsIntoUserSet(HttpServletReque
new String[] {"Pinning item ", existingUserSet.getType()});
}

int itemsPosition=UserSetUtils.parseItemsPosition(position);
int itemsPosition=parseItemsPosition(position);
if(!StringUtils.equals(position, WebUserSetFields.PINNED) && itemsPosition>=0 && itemsPosition < existingUserSet.getPinned()) {
throw new RequestValidationException(UserSetI18nConstants.INVALID_UNPINNED_ITEMS_POSITION, null);
}
Expand All @@ -574,7 +573,6 @@ protected ResponseEntity<String> insertMultipleItemsIntoUserSet(HttpServletReque

UserSet updatedUserSet =
getUserSetService().insertMultipleItems(items, position, itemsPosition, existingUserSet);
getUserSetService().updatePagination(updatedUserSet, getConfiguration());

String serializedUserSetJsonLdStr = serializeUserSet(LdProfiles.MINIMAL, updatedUserSet);

Expand All @@ -598,6 +596,27 @@ protected ResponseEntity<String> insertMultipleItemsIntoUserSet(HttpServletReque
throw new InternalServerException(e);
}
}

//returns -1 if not provided
private int parseItemsPosition(String position) throws ParamValidationException {
if(StringUtils.equals(position, WebUserSetFields.PINNED_POSITION)) {
return 0;
}
int positionFinal = -1;
if(position!=null) {
try {
positionFinal = Integer.parseInt(position);
if(positionFinal<0) {
throw new ParamValidationException(I18nConstants.INVALID_PARAM_VALUE,
I18nConstants.INVALID_PARAM_VALUE, new String[] {WebUserSetFields.PATH_PARAM_POSITION, position});
}
} catch (RuntimeException e) {
throw new ParamValidationException(I18nConstants.INVALID_PARAM_VALUE,
I18nConstants.INVALID_PARAM_VALUE, new String[] {WebUserSetFields.PATH_PARAM_POSITION, position});
}
}
return positionFinal;
}

@RequestMapping(value = {"/set/{identifier}/{datasetId}/{localId}"}, method = {RequestMethod.GET},
produces = {HttpHeaders.CONTENT_TYPE_JSONLD_UTF8, HttpHeaders.CONTENT_TYPE_JSON_UTF8})
Expand Down Expand Up @@ -744,20 +763,7 @@ protected ResponseEntity<String> deleteItemFromUserSet(Authentication authentica
new String[] {datasetId + "/" + localId, identifier});
}

// check if it is a pinned item, decrease the counter by 1 for entity sets
if (existingUserSet.isEntityBestItemsSet()) {
int currentPosition = existingUserSet.getItems().indexOf(newItem);
if (currentPosition < existingUserSet.getPinned()) {
existingUserSet.setPinned(existingUserSet.getPinned() - 1);
}
}
// if already exists - remove item and update modified date
existingUserSet.getItems().remove(newItem);

// update an existing user set
UserSet updatedUserSet = getUserSetService().updateUserSetInMongo(existingUserSet);
//update pagination fields (used only for the response serialization)
getUserSetService().updatePagination(updatedUserSet, getConfiguration());
UserSet updatedUserSet=getUserSetService().deleteItem(newItem, existingUserSet);

// serialize to JsonLd
String serializedUserSetJsonLdStr = serializeUserSet(LdProfiles.MINIMAL, updatedUserSet);
Expand Down Expand Up @@ -813,32 +819,7 @@ protected ResponseEntity<String> deleteMultipleItemsFromUserSet(Authentication a
// 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());
UserSet updatedUserSet=getUserSetService().deleteMultipleItems(items, existingUserSet);

// serialize to JsonLd
String serializedUserSetJsonLdStr = serializeUserSet(LdProfiles.MINIMAL, updatedUserSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,24 @@ public abstract class BaseUserSetServiceImpl implements UserSetService {


//update the pagination fields of the set (used only for the serialization to the output)
@Override
public UserSet updatePagination(UserSet userSet, UserSetConfiguration config) {
protected UserSet updatePagination(UserSet userSet, UserSetConfiguration config) {
return userSetUtils.updatePagination(userSet, config);
}

protected UserSet writeUserSetToDb(UserSet existingUserSet) {
// update total
updateTotal(existingUserSet);
// generate and add a created and modified timestamp to the Set
existingUserSet.setModified(new Date());

// Respond with HTTP 200
// update an existing user set. merge user sets - insert new fields in existing
// object
UserSet updatedUserSet = getMongoPersistence().update((PersistentUserSet) existingUserSet);
//getUserSetUtils().updatePagination(updatedUserSet, getConfiguration());
return updatedUserSet;
}


protected PersistentUserSetService getMongoPersistence() {
return mongoPersistance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,54 @@ int calculatePosition(int position, List<String> items) {
}
return positionFinal;
}

public UserSet deleteItem(String item, UserSet existingUserSet) {
// check if it is a pinned item, decrease the counter by 1 for entity sets
if (existingUserSet.isEntityBestItemsSet()) {
int currentPosition = existingUserSet.getItems().indexOf(item);
if (currentPosition < existingUserSet.getPinned()) {
existingUserSet.setPinned(existingUserSet.getPinned() - 1);
}
}
// if already exists - remove item and update modified date
existingUserSet.getItems().remove(item);

// update an existing user set
UserSet updatedUserSet = writeUserSetToDb(existingUserSet);
//update pagination fields (used only for the response serialization)
updatePagination(updatedUserSet, getConfiguration());
return updatedUserSet;
}

public UserSet deleteMultipleItems(List<String> items, UserSet existingUserSet) {
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 = writeUserSetToDb(existingUserSet);
}

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

public UserSet insertMultipleItems(List<String> items, String position, int itemsPosition, UserSet existingUserSet)
throws ItemValidationException {
Expand All @@ -249,6 +297,7 @@ public UserSet insertMultipleItems(List<String> items, String position, int item
} else {
userSet=updateItemsFromUnpinned(existingUserSet, items, itemsPosition);
}
updatePagination(userSet, getConfiguration());

return userSet;
}
Expand All @@ -275,7 +324,7 @@ private UserSet updateItemsFromPinned(UserSet existingUserSet, List<String> item

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

Expand Down Expand Up @@ -309,7 +358,7 @@ private UserSet updateItemsFromUnpinned(UserSet existingUserSet, List<String> it

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

Expand Down Expand Up @@ -346,7 +395,8 @@ public UserSet insertItem(String datasetId, String localId, String position,
validatePosition(position, existingUserSet.getItems(), existingUserSet.getPinned());
userSet = insertItem(existingUserSet, newItem, positionInt, false);
}
//getUserSetUtils().updatePagination(userSet, getConfiguration());
updatePagination(userSet, getConfiguration());

return userSet;
}

Expand Down Expand Up @@ -376,7 +426,7 @@ private UserSet insertItem(UserSet existingUserSet, String newItem, int position
// add item && create item list if needed
addNewItemToList(existingUserSet, finalPosition, newItem);
updatePinCount(existingUserSet, pinnedItem, -1);
extUserSet = updateUserSetInMongo(existingUserSet);
extUserSet = writeUserSetToDb(existingUserSet);
} else {
// replace item
int oldPosition = existingUserSet.getItems().indexOf(newItem);
Expand All @@ -387,7 +437,7 @@ private UserSet insertItem(UserSet existingUserSet, String newItem, int position
} else {
replaceItem(existingUserSet, finalPosition, newItem);
updatePinCount(existingUserSet, pinnedItem, oldPosition);
extUserSet = updateUserSetInMongo(existingUserSet);
extUserSet = writeUserSetToDb(existingUserSet);
}
}

Expand Down Expand Up @@ -419,26 +469,6 @@ private void updatePinCount(UserSet existingUserSet, boolean pinnedItem, int old
}
}

/*
* (non-Javadoc)
*
* @see eu.europeana.set.web.service.UserSetService#updateItemList(eu.europeana.set.
* definitions.model.UserSet)
*/
public UserSet updateUserSetInMongo(UserSet existingUserSet) {
// update total
updateTotal(existingUserSet);
// generate and add a created and modified timestamp to the Set
existingUserSet.setModified(new Date());

// Respond with HTTP 200
// update an existing user set. merge user sets - insert new fields in existing
// object
UserSet updatedUserSet = getMongoPersistence().update((PersistentUserSet) existingUserSet);
//getUserSetUtils().updatePagination(updatedUserSet, getConfiguration());
return updatedUserSet;
}

/**
* This method replaces item in user set
*
Expand Down

0 comments on commit 674b818

Please sign in to comment.