Skip to content

Commit

Permalink
Merge pull request #340 from dbmdz/list&page-response
Browse files Browse the repository at this point in the history
Remove additional `pageRequest` from `PageResponse` by using generics
  • Loading branch information
daforster authored Jun 2, 2022
2 parents 6c19682 + a2e1250 commit 26c02e3
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import de.digitalcollections.model.identifiable.entity.work.Work;
import de.digitalcollections.model.identifiable.resource.FileResource;
import de.digitalcollections.model.identifiable.web.Webpage;
import de.digitalcollections.model.list.ListRequest;
import de.digitalcollections.model.list.ListResponse;
import de.digitalcollections.model.list.sorting.Sorting;
import de.digitalcollections.model.security.User;
Expand All @@ -35,7 +36,7 @@

@JsonDeserialize(as = ListResponse.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class ListResponseMixIn<T> extends ListResponse<T> {
public abstract class ListResponseMixIn<T, R extends ListRequest> extends ListResponse<T, R> {

@JsonTypeInfo(use = Id.NAME, property = "objectType", visible = true)
@JsonSubTypes({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
Expand All @@ -28,6 +29,7 @@
import de.digitalcollections.model.identifiable.entity.work.Work;
import de.digitalcollections.model.identifiable.resource.FileResource;
import de.digitalcollections.model.identifiable.web.Webpage;
import de.digitalcollections.model.list.paging.PageRequest;
import de.digitalcollections.model.list.paging.PageResponse;
import de.digitalcollections.model.list.sorting.Sorting;
import de.digitalcollections.model.security.User;
Expand Down Expand Up @@ -77,6 +79,10 @@ public abstract class PageResponseMixIn<T> extends PageResponse<T> {
@Override
public abstract int getNumberOfElements();

@JsonProperty(value = "pageRequest")
@Override
public abstract PageRequest getRequest();

@JsonIgnore
@Override
public abstract int getSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class ListResponseTest extends BaseJsonSerializationTest {

private ListResponse<User> createObject() {
private ListResponse<User, ListRequest> createObject() {
ListRequest listRequest = new ListRequest();
// filtering
FilterCriterion filterCriteria1 =
Expand All @@ -41,7 +41,7 @@ private ListResponse<User> createObject() {

@Test
public void testSerializeDeserialize() throws Exception {
ListResponse<User> listResponse = createObject();
ListResponse<User, ListRequest> listResponse = createObject();
checkSerializeDeserialize(listResponse, "serializedTestObjects/list/ListResponse.json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private PageResponse<User> createObject() {
Filtering filtering = Filtering.builder().add(filterCriteria1).add(filterCriteria2).build();
pageRequest.setFiltering(filtering);

pageResponse.setPageRequest(pageRequest);
pageResponse.setRequest(pageRequest);
pageResponse.setExecutedSearchTerm("Hello");

return pageResponse;
Expand All @@ -64,7 +64,7 @@ public void differentTypes() throws Exception {

PageRequest pageRequest = new PageRequest("Label", 3, 15, null);
PageResponse<Identifiable> resp = new PageResponse<>();
resp.setPageRequest(pageRequest);
resp.setRequest(pageRequest);
resp.setContent(content);
resp.setTotalElements(2);
resp.setExecutedSearchTerm("Label");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"enabled" : true,
"roles" : [ ]
} ],
"listRequest" : {
"request" : {
"filtering" : {
"filterCriteria" : [ {
"expression" : "longField",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"roles" : [ ]
} ],
"executedSearchTerm" : "Hello",
"totalElements" : 1,
"pageRequest" : {
"filtering" : {
"filterCriteria" : [ {
Expand All @@ -24,6 +25,5 @@
"searchTerm" : "Hello",
"pageNumber" : 3,
"pageSize" : 15
},
"totalElements" : 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"type" : "RESOURCE"
} ],
"executedSearchTerm" : "Label",
"totalElements" : 2,
"pageRequest" : {
"searchTerm" : "Label",
"pageNumber" : 3,
"pageSize" : 15
},
"totalElements" : 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
* Container for list information.
*
* @param <T> object type of list items
*/
public class ListResponse<T> implements Iterable<T> {
public class ListResponse<T, R extends ListRequest> implements Iterable<T> {

protected List<T> content;
protected String executedSearchTerm;
protected ListRequest listRequest;
protected R request;
protected long total;

public ListResponse() {
Expand All @@ -26,27 +27,27 @@ public ListResponse() {
* Constructor with the given content and the given governing {@link ListRequest}.
*
* @param content the content of this list, must not be {@literal null}.
* @param listRequest the request information, can be {@literal null}.
* @param request the request information, can be {@literal null}.
*/
public ListResponse(List<T> content, ListRequest listRequest) {
public ListResponse(List<T> content, R request) {
this();

assert content != null : "content must not be null!";
this.content.addAll(content);
this.listRequest = listRequest;
this.request = request;
this.total = content.size();
}

/**
* Constructor with the given content and the given governing {@link ListRequest}.
*
* @param content the content of this list, must not be {@literal null}.
* @param listRequest the request information, can be {@literal null}.
* @param request the request information, can be {@literal null}.
* @param executedSearchTerm finally executed search term based on given search term (e.g. after
* escaping special characters etc.)
*/
public ListResponse(List<T> content, ListRequest listRequest, String executedSearchTerm) {
this(content, listRequest);
public ListResponse(List<T> content, R request, String executedSearchTerm) {
this(content, request);
this.executedSearchTerm = executedSearchTerm;
}

Expand All @@ -55,26 +56,15 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof ListResponse<?>)) {
if (!(obj instanceof ListResponse<?, ?>)) {
return false;
}

ListResponse<?> that = (ListResponse<?>) obj;

boolean contentEqual = this.content.equals(that.content);
boolean executedSearchTermEqual =
(this.executedSearchTerm == null
? that.executedSearchTerm == null
: this.executedSearchTerm.equals(that.executedSearchTerm));
boolean listRequestEqual =
this.listRequest == null
? that.listRequest == null
: this.listRequest.equals(that.listRequest);

ListResponse<?, ?> that = (ListResponse<?, ?>) obj;
return (this.total == that.total)
&& contentEqual
&& executedSearchTermEqual
&& listRequestEqual;
&& Objects.equals(content, that.content)
&& Objects.equals(executedSearchTerm, that.executedSearchTerm)
&& Objects.equals(request, that.request);
}

/**
Expand All @@ -87,23 +77,23 @@ public List<T> getContent() {
public String getExecutedSearchTerm() {
if (executedSearchTerm == null) {
// no changes on original searchTerm
return listRequest != null ? listRequest.getSearchTerm() : null;
return request != null ? request.getSearchTerm() : null;
}
return executedSearchTerm;
}

/**
* @return the ListRequest used to get this ListResponse
* @return the request used to get this response
*/
public ListRequest getListRequest() {
return listRequest;
public R getRequest() {
return request;
}

/**
* @return the sorting parameters for the {@link ListResponse}.
*/
public Sorting getSorting() {
return listRequest == null ? null : listRequest.getSorting();
return request == null ? null : request.getSorting();
}

/**
Expand All @@ -128,7 +118,7 @@ public int hashCode() {
int result = 17;

result += 31 * (int) (total ^ total >>> 32);
result += 31 * (listRequest == null ? 0 : listRequest.hashCode());
result += 31 * (request == null ? 0 : request.hashCode());
result += 31 * content.hashCode();
result += 31 * (executedSearchTerm == null ? 0 : executedSearchTerm.hashCode());

Expand Down Expand Up @@ -159,8 +149,8 @@ public void setExecutedSearchTerm(String executedSearchTerm) {
this.executedSearchTerm = executedSearchTerm;
}

public void setListRequest(ListRequest listRequest) {
this.listRequest = listRequest;
public void setRequest(R request) {
this.request = request;
}

public void setTotalElements(long totalElements) {
Expand Down
Loading

0 comments on commit 26c02e3

Please sign in to comment.