Skip to content

Commit

Permalink
1) added type for Agents in the serialization; 2) added .jon get method;
Browse files Browse the repository at this point in the history
3) pagination starts with 1
  • Loading branch information
SrdjanStevanetic committed Jun 25, 2024
1 parent a507767 commit 9d2b8aa
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class UserSetUtils {

public static final String EUROPEANA_ID_FIELD_REGEX = "^[a-zA-Z0-9_]*$";
public static final Pattern EUROPEANA_ID = Pattern.compile("^/[a-zA-Z0-9_]*/[a-zA-Z0-9_]*$");
public static final int DEFAULT_PAGE = 1;
/**
* This method converts string value to Map<String,String> values for given key - language.
*
Expand Down Expand Up @@ -84,13 +85,10 @@ public UserSet updatePagination(UserSet userSet, UserSetConfiguration config) {
userSet.setTotal(total);
//NOTE: the first and last properties are not used now and might be deprecated, they should not be stored in the database
if (total > 0) {
int first = 0;
int first = UserSetUtils.DEFAULT_PAGE;
String firstPageStr = fillPage(userSet, config, first, UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE);
userSet.setFirst(firstPageStr);
int last = (int) Math.ceil( (double)total / UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE);
if(last > 0) {
last = last - 1; // we start counting by 0
}
int last = ((int) Math.ceil( (double)total / UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE)) + UserSetUtils.DEFAULT_PAGE - 1;
String lastPageStr = fillPage(userSet, config, last, UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE);
userSet.setLast(lastPageStr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ public void searchItemsInSet() throws Exception {
String[] qf = new String[] {"item:/08641/1037479000000476467",
"item:/08641/1037479000000476875", "item:/11654/_Botany_U_1419207", "item:/2048128/618580",
"item:/2048128/618580", "item:/2048128/notexisting", "item:/2048128/notexisting1"};
String result = callSearchItemsInSet(setIdentifier, qf, "1", "2", null);
String result = callSearchItemsInSet(setIdentifier, qf, String.valueOf(UserSetUtils.DEFAULT_PAGE + 1), "2", null);
// check ids
String searchUri = "/set/" + setIdentifier + "/search";
assertTrue(StringUtils.contains(result, searchUri));
Expand All @@ -609,7 +609,7 @@ public void searchItemsInSet() throws Exception {
// last page no next
assertTrue(!containsKeyOrValue(result, WebUserSetFields.NEXT));

result = callSearchItemsInSet(setIdentifier, qf, "0", "2", null);
result = callSearchItemsInSet(setIdentifier, qf, String.valueOf(UserSetUtils.DEFAULT_PAGE), "2", null);
// check ids
assertTrue(StringUtils.contains(result, searchUri));
assertTrue(containsKeyOrValue(result, WebUserSetFields.TOTAL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void getCloseUserSet_ItemDescriptionsWithPageValues() throws Exception {
// get the identifier
MvcResult response = mockMvc.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.ITEMDESCRIPTIONS.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "1")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, String.valueOf(UserSetUtils.DEFAULT_PAGE + 1))
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "100")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)).andReturn();
Expand All @@ -130,11 +130,11 @@ public void getCloseUserSet_ItemDescriptionsWithPageValues() throws Exception {
.replaceFirst(getConfiguration().getApiBasePath(), "");
String requestedPage = baseUrl + response.getRequest().getPathInfo();
// int pageSize = 100;
// int page = 1;
// int page = 2;
final String collectionUrl = getUserSetService().buildResultsPageUrl(requestedPage,
response.getRequest().getQueryString(), null);
final String resultPageId =
getUserSetService().buildPageUrl(collectionUrl, 1, 100, LdProfiles.ITEMDESCRIPTIONS);
getUserSetService().buildPageUrl(collectionUrl, UserSetUtils.DEFAULT_PAGE + 1, 100, LdProfiles.ITEMDESCRIPTIONS);
assertTrue(containsKeyOrValue(result, resultPageId));

// check part of ID
Expand Down Expand Up @@ -176,7 +176,7 @@ public void getCloseUserSet_ItemDescriptionsWithLastPage() throws Exception {
MockHttpServletResponse response =
mockMvc.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.ITEMDESCRIPTIONS.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "3")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "4")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "100")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)).andReturn().getResponse();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import eu.europeana.api.commons.definitions.vocabulary.CommonLdConstants;
import eu.europeana.api.set.integration.BaseUserSetTestUtils;
import eu.europeana.set.definitions.config.UserSetConfigurationImpl;
import eu.europeana.set.definitions.model.utils.UserSetUtils;
import eu.europeana.set.definitions.model.vocabulary.LdProfiles;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields;
import eu.europeana.set.web.model.WebUserSetImpl;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void getUserSetPagination() throws Exception {
MockHttpServletResponse response = mockMvc
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.STANDARD.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "1")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "2")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "5")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
Expand Down Expand Up @@ -145,7 +146,7 @@ public void getPageForEmptyUserSet() throws Exception {
MockHttpServletResponse response = mockMvc
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.STANDARD.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "0")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, String.valueOf(UserSetUtils.DEFAULT_PAGE))
// .queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "5")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
Expand Down Expand Up @@ -189,7 +190,7 @@ public void getUserSetPaginationDefaultPageSize() throws Exception {
MockHttpServletResponse response = mockMvc
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.STANDARD.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "1")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "2")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andReturn().getResponse();
Expand All @@ -216,7 +217,7 @@ public void getUserSetPaginationItemDescriptions() throws Exception {
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE,
LdProfiles.ITEMDESCRIPTIONS.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "1")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "2")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andReturn().getResponse();
Expand Down Expand Up @@ -245,7 +246,7 @@ public void getUserSetSecondPageItemDescriptions() throws Exception {
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE,
LdProfiles.ITEMDESCRIPTIONS.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "1")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "2")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "100")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
Expand Down Expand Up @@ -275,7 +276,7 @@ public void getUserSetPaginationItemDescriptionsOrder() throws Exception {
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE,
LdProfiles.ITEMDESCRIPTIONS.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "0")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, String.valueOf(UserSetUtils.DEFAULT_PAGE))
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "10")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
Expand Down Expand Up @@ -358,7 +359,7 @@ public void getUserSetPaginationPageSizeExceeded() throws Exception {
MockHttpServletResponse response = mockMvc
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.STANDARD.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "0")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, String.valueOf(UserSetUtils.DEFAULT_PAGE))
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "200")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Arrays;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jettison.json.JSONObject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -27,6 +28,7 @@
import eu.europeana.set.definitions.model.UserSet;
import eu.europeana.set.definitions.model.search.UserSetQuery;
import eu.europeana.set.definitions.model.utils.UserSetUtils;
import eu.europeana.set.definitions.model.vocabulary.AgentTypes;
import eu.europeana.set.definitions.model.vocabulary.LdProfiles;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields;
import eu.europeana.set.web.exception.request.ItemValidationException;
Expand Down Expand Up @@ -162,6 +164,7 @@ public void getUserSet_Success() throws Exception {
// without page in request, it is not a collection page
assertFalse(containsKeyOrValue(result, CommonLdConstants.COLLECTION_PAGE));
assertFalse(containsKeyOrValue(result, WebUserSetFields.PART_OF));
assertEquals(((JSONObject)(new JSONObject(result)).get("creator")).getString("type"),AgentTypes.PERSON.name());
}

// Update user set Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import eu.europeana.set.definitions.model.UserSet;
import eu.europeana.set.definitions.model.search.UserSetFacetQuery;
import eu.europeana.set.definitions.model.search.UserSetQuery;
import eu.europeana.set.definitions.model.utils.UserSetUtils;
import eu.europeana.set.definitions.model.vocabulary.UserSetTypes;
import eu.europeana.set.definitions.model.vocabulary.VisibilityTypes;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields;
Expand Down Expand Up @@ -408,7 +409,7 @@ public ResultSet<PersistentUserSet> find(UserSetQuery query) {

@SuppressWarnings("deprecation")
private void setPaginationOptions(Query<PersistentUserSet> mongoQuery, UserSetQuery query) {
mongoQuery.offset(query.getPageNr() * query.getPageSize());
mongoQuery.offset((query.getPageNr() - UserSetUtils.DEFAULT_PAGE) * query.getPageSize());
mongoQuery.limit(query.getPageSize());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package eu.europeana.set.web.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import eu.europeana.set.definitions.model.agent.impl.Person;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetModelFields;

Expand All @@ -15,7 +13,7 @@ public WebUser() {
}

@Override
@JsonIgnore
@JsonProperty(WebUserSetModelFields.TYPE)
public String getType() {
return super.getType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import eu.europeana.set.definitions.config.UserSetConfiguration;
import eu.europeana.set.definitions.exception.UserSetProfileValidationException;
import eu.europeana.set.definitions.model.UserSet;
import eu.europeana.set.definitions.model.utils.UserSetUtils;
import eu.europeana.set.definitions.model.vocabulary.LdProfiles;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields;
import eu.europeana.set.stats.service.UsageStatsService;
Expand Down Expand Up @@ -295,7 +296,7 @@ public String getApiVersion() {

protected ResponseEntity<String> buildGetResponse(UserSet userSet, LdProfiles profile, Integer pageNr, int pageSize, HttpServletRequest request) throws IOException, HttpException {
String jsonBody = "";
if(pageNr == null || pageNr < 0) {
if(pageNr == null || pageNr < UserSetUtils.DEFAULT_PAGE) {
jsonBody = serializeUserSet(profile, userSet);
}else {
CollectionPage itemPage = getUserSetService().buildCollectionPage(userSet, profile, pageNr, pageSize, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ResponseEntity<String> searchUserSet(
@RequestParam(value = CommonApiConstants.QUERY_PARAM_QF, required = false) String[] qf,
@RequestParam(value = CommonApiConstants.QUERY_PARAM_SORT, required = false) String sort,
@RequestParam(value = CommonApiConstants.QUERY_PARAM_PAGE, required = false,
defaultValue = "0") int page,
defaultValue = "" + UserSetUtils.DEFAULT_PAGE) int page,
@RequestParam(value = CommonApiConstants.QUERY_PARAM_PAGE_SIZE, required = false,
defaultValue = "" + CommonApiConstants.DEFAULT_PAGE_SIZE) int pageSize,
@RequestParam(value = CommonApiConstants.QUERY_PARAM_FACET, required = false) String facet,
Expand All @@ -70,6 +70,7 @@ public ResponseEntity<String> searchUserSet(
HttpServletRequest request) throws HttpException {

try {

// authorization
Authentication authentication = verifyReadAccess(request);

Expand Down Expand Up @@ -131,7 +132,7 @@ public ResponseEntity<String> searchItemsInSet(
defaultValue = UserSetQueryBuilder.SEARCH_ALL) String query,
@RequestParam(value = CommonApiConstants.QUERY_PARAM_QF, required = false) String[] qf,
@RequestParam(value = CommonApiConstants.QUERY_PARAM_PAGE, required = false,
defaultValue = "0") int page,
defaultValue = "" + UserSetUtils.DEFAULT_PAGE) int page,
@RequestParam(value = CommonApiConstants.QUERY_PARAM_PAGE_SIZE, required = false,
defaultValue = "" + UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE) int pageSize,
// @RequestParam(value = CommonApiConstants.QUERY_PARAM_PROFILE, required = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected ResponseEntity<String> storeUserSet(String userSetJsonLdStr,
int derefItems =
getDerefItemsCount(storedUserSet, UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE);
storedUserSet = getUserSetService().fetchItems(storedUserSet, null, null,
CommonApiConstants.DEFAULT_PAGE, derefItems, profile);
UserSetUtils.DEFAULT_PAGE, derefItems, profile);
}

String serializedUserSetJsonLdStr = serializeUserSet(profile, storedUserSet);
Expand Down Expand Up @@ -155,7 +155,7 @@ protected ResponseEntity<String> storeUserSet(String userSetJsonLdStr,

}

@GetMapping(value = {"/set/{identifier}", "/set/{identifier}.jsonld"},
@GetMapping(value = {"/set/{identifier}", "/set/{identifier}.json", "/set/{identifier}.jsonld"},
produces = {HttpHeaders.CONTENT_TYPE_JSONLD_UTF8, HttpHeaders.CONTENT_TYPE_JSON_UTF8})
@Operation(description = SwaggerConstants.SEARCH_HELP_NOTE, summary = "Retrieve a user set")
public ResponseEntity<String> getUserSet(
Expand Down Expand Up @@ -227,7 +227,7 @@ private ResponseEntity<String> getUserSet(String profileStr, String identifier,

if (mustFetchItems(userSet, profile)) {
// pageNr, if empty default value 0 is sent for fetching items
int page = (pageNr != null) ? pageNr : CommonApiConstants.DEFAULT_PAGE;
int page = (pageNr != null) ? pageNr : UserSetUtils.DEFAULT_PAGE;
userSet = getUserSetService().fetchItems(userSet, sort, sortOrder, page, pageSize, profile);
}
return buildGetResponse(userSet, profile, pageNr, pageSize, request);
Expand Down Expand Up @@ -323,7 +323,7 @@ protected ResponseEntity<String> updateUserSet(HttpServletRequest request, Authe
if (mustFetchItems(updatedUserSet, profile)) {
int derefItems = getDerefItemsCount(updatedUserSet, UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE);
updatedUserSet = getUserSetService().fetchItems(updatedUserSet, null, null,
CommonApiConstants.DEFAULT_PAGE, derefItems, profile);
UserSetUtils.DEFAULT_PAGE, derefItems, profile);
}

return buildGetResponse(updatedUserSet, profile, -1, -1, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ protected CollectionOverview buildCollectionOverview(String pageId, String pagin

// do not generate first and last if pageSize=0
if (totalInCollection > 0 && pageSize > 0) {
first = buildPageUrl(paginationBaseUrl, 0, pageSize, profile);
first = buildPageUrl(paginationBaseUrl, UserSetUtils.DEFAULT_PAGE, pageSize, profile);
last = buildPageUrl(paginationBaseUrl, lastPage, pageSize, profile);
}
return new CollectionOverview(pageId, totalInCollection, first, last, type);
Expand All @@ -288,12 +288,12 @@ protected CollectionOverview buildCollectionOverview(String pageId, String pagin
* @return
*/
protected int getLastPage(long totalResults, int pageSize) {
long lastPage = 0;
long lastPage = UserSetUtils.DEFAULT_PAGE;
// avoid null divizion if pages size is 0
if (totalResults > 0 && pageSize > 0) {
long reaminder = (totalResults % pageSize);
int extraPage = (reaminder == 0 ? 0 : 1);
lastPage = ((totalResults / pageSize) + extraPage) - 1;
lastPage = ((totalResults / pageSize) + extraPage) + UserSetUtils.DEFAULT_PAGE - 1;
}

return Math.toIntExact(lastPage);
Expand Down Expand Up @@ -366,7 +366,7 @@ protected CollectionOverview buildCollectionOverview(String collectionUrl, int p
String last = null;

if (totalInCollection > 0) {
first = buildPageUrl(collectionUrl, 0, pageSize, profile);
first = buildPageUrl(collectionUrl, UserSetUtils.DEFAULT_PAGE, pageSize, profile);
last = buildPageUrl(collectionUrl, lastPage, pageSize, profile);
}
return new CollectionOverview(collectionUrl, totalInCollection, first, last, type);
Expand Down
Loading

0 comments on commit 9d2b8aa

Please sign in to comment.