From 1af3eb4b1f094bc70838858748d03fc7da23d224 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 5 Jan 2014 13:34:53 +0200 Subject: [PATCH] pagination work --- .../org/baeldung/persistence/IOperations.java | 10 ++++ .../service/common/AbstractService.java | 13 +++++ .../web/controller/FooController.java | 30 ++++++++---- .../RestResponseEntityExceptionHandler.java | 3 +- .../MyResourceNotFoundException.java | 21 +++++++++ .../baeldung/web/util/RestPreconditions.java | 47 +++++++++++++++++++ 6 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 spring-security-rest-full/src/main/java/org/baeldung/web/exception/MyResourceNotFoundException.java create mode 100644 spring-security-rest-full/src/main/java/org/baeldung/web/util/RestPreconditions.java diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/IOperations.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/IOperations.java index 3c6f86a78b..d4f3f0982c 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/IOperations.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/IOperations.java @@ -3,12 +3,22 @@ package org.baeldung.persistence; import java.io.Serializable; import java.util.List; +import org.springframework.data.domain.Page; + public interface IOperations { + // read - one + T findOne(final long id); + // read - all + List findAll(); + Page findPaginated(int page, int size); + + // write + T create(final T entity); T update(final T entity); diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/common/AbstractService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/common/AbstractService.java index 406a958eec..5987bbae5f 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/common/AbstractService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/common/AbstractService.java @@ -4,6 +4,8 @@ import java.io.Serializable; import java.util.List; import org.baeldung.persistence.IOperations; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.transaction.annotation.Transactional; @@ -12,18 +14,29 @@ import com.google.common.collect.Lists; @Transactional public abstract class AbstractService implements IOperations { + // read - one + @Override @Transactional(readOnly = true) public T findOne(final long id) { return getDao().findOne(id); } + // read - all + @Override @Transactional(readOnly = true) public List findAll() { return Lists.newArrayList(getDao().findAll()); } + @Override + public Page findPaginated(final int page, final int size) { + return getDao().findAll(new PageRequest(page, size)); + } + + // write + @Override public T create(final T entity) { return getDao().save(entity); diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java index dd87dffc4c..62306ab808 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java @@ -8,17 +8,21 @@ import javax.servlet.http.HttpServletResponse; import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.service.IFooService; +import org.baeldung.web.exception.MyResourceNotFoundException; import org.baeldung.web.util.LinkUtil; import org.baeldung.web.util.ResourceCreated; +import org.baeldung.web.util.RestPreconditions; import org.baeldung.web.util.SingleResourceRetrieved; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.util.UriComponentsBuilder; @@ -42,29 +46,39 @@ public class FooController { // API - // read + // read - one @RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseBody - public Foo findOne(@PathVariable("id") final Long id, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) { - return service.findOne(id); + public Foo findOne(@PathVariable("id") final Long id, final UriComponentsBuilder uriBuilder, final HttpServletRequest request, final HttpServletResponse response) { + final Foo resourceById = RestPreconditions.checkFound(service.findOne(id)); + eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response)); + return resourceById; } + // read - all + @RequestMapping(method = RequestMethod.GET) @ResponseBody public List findAll() { return service.findAll(); } - @RequestMapping(value = "admin/foo/{id}", method = RequestMethod.GET) + @RequestMapping(params = { "page", "size" }, method = RequestMethod.GET) @ResponseBody - public Foo get(@PathVariable("id") final Long id, final HttpServletRequest request, final HttpServletResponse response) { - final Foo resourceById = Preconditions.checkNotNull(service.findOne(id)); + public List findPaginated(@RequestParam("page") final int page, @RequestParam("size") final int size, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) { - eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response)); - return resourceById; + final Page resultPage = service.findPaginated(page, size); + if (page > resultPage.getTotalPages()) { + throw new MyResourceNotFoundException(); + } + // eventPublisher.publishEvent(new PaginatedResultsRetrievedEvent(Foo.class, uriBuilder, response, page, resultPage.getTotalPages(), size)); + + return resultPage.getContent(); } + // discover + @RequestMapping(value = "admin", method = RequestMethod.GET) @ResponseStatus(value = HttpStatus.NO_CONTENT) public void adminRoot(final HttpServletRequest request, final HttpServletResponse response) { diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java b/spring-security-rest-full/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java index 95e8bcb677..be2d4331c1 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java @@ -2,6 +2,7 @@ package org.baeldung.web.error; import javax.persistence.EntityNotFoundException; +import org.baeldung.web.exception.MyResourceNotFoundException; import org.hibernate.exception.ConstraintViolationException; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataIntegrityViolationException; @@ -50,7 +51,7 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH // 404 - @ExceptionHandler(value = { EntityNotFoundException.class }) + @ExceptionHandler(value = { EntityNotFoundException.class, MyResourceNotFoundException.class }) protected ResponseEntity handleBadRequest(final EntityNotFoundException ex, final WebRequest request) { final String bodyOfResponse = "This should be application specific"; return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request); diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/exception/MyResourceNotFoundException.java b/spring-security-rest-full/src/main/java/org/baeldung/web/exception/MyResourceNotFoundException.java new file mode 100644 index 0000000000..14b61f9832 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/exception/MyResourceNotFoundException.java @@ -0,0 +1,21 @@ +package org.baeldung.web.exception; + +public final class MyResourceNotFoundException extends RuntimeException { + + public MyResourceNotFoundException() { + super(); + } + + public MyResourceNotFoundException(final String message, final Throwable cause) { + super(message, cause); + } + + public MyResourceNotFoundException(final String message) { + super(message); + } + + public MyResourceNotFoundException(final Throwable cause) { + super(cause); + } + +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/util/RestPreconditions.java b/spring-security-rest-full/src/main/java/org/baeldung/web/util/RestPreconditions.java new file mode 100644 index 0000000000..18cb8219ec --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/util/RestPreconditions.java @@ -0,0 +1,47 @@ +package org.baeldung.web.util; + +import org.baeldung.web.exception.MyResourceNotFoundException; +import org.springframework.http.HttpStatus; + +/** + * Simple static methods to be called at the start of your own methods to verify correct arguments and state. If the Precondition fails, an {@link HttpStatus} code is thrown + */ +public final class RestPreconditions { + + private RestPreconditions() { + throw new AssertionError(); + } + + // API + + /** + * Check if some value was found, otherwise throw exception. + * + * @param expression + * has value true if found, otherwise false + * @throws MyResourceNotFoundException + * if expression is false, means value not found. + */ + public static void checkFound(final boolean expression) { + if (!expression) { + throw new MyResourceNotFoundException(); + } + } + + /** + * Check if some value was found, otherwise throw exception. + * + * @param expression + * has value true if found, otherwise false + * @throws MyResourceNotFoundException + * if expression is false, means value not found. + */ + public static T checkFound(final T resource) { + if (resource == null) { + throw new MyResourceNotFoundException(); + } + + return resource; + } + +}