Commit 66c40d42 authored by Matija Obreza's avatar Matija Obreza

DescriptorList API using ShortFilterService#processFilter

- Avoids JsonMappingException
parent b8649f16
......@@ -34,8 +34,8 @@ import org.genesys.catalog.service.DescriptorService;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys.catalog.service.ShortFilterService.FilterInfo;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
......@@ -195,73 +195,35 @@ public class DescriptorListController {
/**
* My descriptor lists.
*
* @param filterCode the filter code
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param descriptorListFilter the descriptor list filter
* @param filter the filter
* @return the page
* @throws IOException
* @throws IOException Signals that an I/O exception has occurred.
*/
@PostMapping(value = "/list-mine")
public FilteredPage<DescriptorList> myDescriptorLists(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
@RequestParam(name = "l", required = false, defaultValue = "50") final int pageSize,
@RequestParam(name = "d", required = false, defaultValue = "DESC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "lastModifiedDate") final String[] sort,
@RequestParam(name = "f", required = false) String filterCode,
public FilteredPage<DescriptorList> myDescriptorLists(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) DescriptorListFilter filter) throws IOException {
FilterInfo<DescriptorListFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, DescriptorListFilter.class);
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, descriptorListService.listDescriptorListsForCurrentUser(filterInfo.filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, descriptorListService.listDescriptorListsForCurrentUser(filterInfo.filter, page.toPageRequest(100, Sort.Direction.DESC, "lastModifiedDate")));
}
/**
* List descriptor lists.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filterCode short filter code -- overrides filter in body
* @param page the page
* @param filter the descriptor list filter
* @return the page
* @throws IOException Signals that an I/O exception has occurred.
*/
@PostMapping(value = "/list")
public FilteredPage<DescriptorList> listDescriptorLists(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
@RequestParam(name = "l", required = false, defaultValue = "50") final int pageSize,
@RequestParam(name = "d", required = false, defaultValue = "ASC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "id") final String[] sort,
@RequestParam(name = "f", required = false) String filterCode,
public FilteredPage<DescriptorList> listDescriptorLists(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) DescriptorListFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, DescriptorListFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
return new FilteredPage<>(filterCode, filter, descriptorListService.listDescriptorLists(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
}
/**
* Filter descriptor lists by filter code.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filterCode the filter code
* @return the filtered page
* @throws IOException Signals that an I/O exception has occurred.
* @deprecated Use {@link #listDescriptorLists(int, int, org.springframework.data.domain.Sort.Direction, String[], String, DescriptorListFilter)}
*/
@PostMapping(value = "/list/{filterCode}")
public FilteredPage<DescriptorList> listDescriptorListsByCode(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
@RequestParam(name = "l", required = false, defaultValue = "50") final int pageSize,
@RequestParam(name = "d", required = false, defaultValue = "ASC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "id") final String[] sort, @PathVariable("filterCode") final String filterCode) throws IOException {
final DescriptorListFilter filter = shortFilterService.filterByCode(filterCode, DescriptorListFilter.class);
return new FilteredPage<>(filterCode, filter, descriptorListService.listDescriptorLists(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
FilterInfo<DescriptorListFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, DescriptorListFilter.class);
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, descriptorListService.listDescriptorLists(filterInfo.filter, page.toPageRequest(100)));
}
/**
......
......@@ -92,7 +92,6 @@ public class PartnerController {
public FilteredPage<Partner> listPartners(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) PartnerFilter filter) throws IOException {
System.err.println("Paginate " + page);
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, PartnerFilter.class);
} else {
......
......@@ -23,63 +23,106 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
/**
* Data pagination request
*
* Data pagination request.
*
* @author Matija Obreza
*/
/**
* @author Matija Obreza
*
*/
public class Pagination {
/** The default sort properties. */
private final String[] DEFAULT_SORT_PROPERTIES = { "id" };
/**
* Page (0-based)
*/
/** Page (0-based). */
private Integer p;
/**
* Page size (length)
*/
/** Page size (length). */
private Integer l;
/**
* Sort direction
*/
/** Sort direction. */
private Sort.Direction d;
/**
* Sort properties
*/
/** Sort properties. */
private String[] s;
/**
* Gets the p.
*
* @return the p
*/
public int getP() {
return p;
}
/**
* Sets the p.
*
* @param p the new p
*/
public void setP(int p) {
this.p = p;
}
/**
* Gets the l.
*
* @return the l
*/
public int getL() {
return l;
}
/**
* Sets the l.
*
* @param l the new l
*/
public void setL(int l) {
this.l = l;
}
/**
* Gets the d.
*
* @return the d
*/
public Sort.Direction getD() {
return d;
}
/**
* Sets the d.
*
* @param d the new d
*/
public void setD(Sort.Direction d) {
this.d = d;
}
/**
* Gets the s.
*
* @return the s
*/
public String[] getS() {
return s;
}
/**
* Sets the s.
*
* @param s the new s
*/
public void setS(String[] s) {
this.s = s;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Pagination page=" + p + ", pageSize=" + l + ", dir=" + d + ", sort=" + Arrays.toString(s);
......@@ -87,29 +130,44 @@ public class Pagination {
/**
* Get sort direction or {@link Sort.Direction#ASC} if null.
*
*
* @param defaultDir the default dir
* @return sort direction or {@link Sort.Direction#ASC}
*/
private Direction getDirection() {
return d == null ? Sort.Direction.ASC : d;
private Direction getDirection(Direction defaultDir) {
return d == null ? defaultDir : d;
}
/**
* Gets list of sort properties, returns the {@link #DEFAULT_SORT_PROPERTIES} if
* null
*
* @return provided properties or {@link #DEFAULT_SORT_PROPERTIES}
* Gets list of sort properties or provided defaults.
*
* @param defaultSortProps the default sort props
* @return provided properties or defaultSortProps
*/
private String[] getSortProperties() {
return s == null || s.length == 0 ? DEFAULT_SORT_PROPERTIES : s;
private String[] getSortProperties(String[] defaultSortProps) {
return s == null || s.length == 0 ? defaultSortProps : s;
}
/**
* @param maxPageSize
* @return
* To page request using the {@link #DEFAULT_SORT_PROPERTIES} and ASC sort
*
* @param maxPageSize the max page size
* @return the pageable
*/
public Pageable toPageRequest(int maxPageSize) {
return new PageRequest(p == null ? 0 : p, Integer.min(l == null ? 100 : l, maxPageSize), getDirection(), getSortProperties());
return new PageRequest(p == null ? 0 : p, Integer.min(l == null ? 100 : l, maxPageSize), getDirection(Sort.Direction.ASC), getSortProperties(DEFAULT_SORT_PROPERTIES));
}
/**
* To page request.
*
* @param maxPageSize the max page size
* @param defaultDir the default dir
* @param defaultSort the default sort
* @return the pageable
*/
public Pageable toPageRequest(int maxPageSize, Direction defaultDir, String... defaultSort) {
return new PageRequest(p == null ? 0 : p, Integer.min(l == null ? 100 : l, maxPageSize), getDirection(defaultDir), getSortProperties(defaultSort));
}
}
......@@ -241,7 +241,7 @@ public class DescriptorListControllerTest extends AbstractApiTest {
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.post(DescriptorListController.CONTROLLER_URL + "/list/{filterCode}", filterCode)
.perform(RestDocumentationRequestBuilders.post(DescriptorListController.CONTROLLER_URL + "/list?f={filterCode}", filterCode)
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment