Skip to content

Commit

Permalink
pagination work
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenp committed Jan 5, 2014
1 parent c0cc59e commit 1af3eb4
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;

public interface IOperations<T extends Serializable> {

// read - one

T findOne(final long id);

// read - all

List<T> findAll();

Page<T> findPaginated(int page, int size);

// write

T create(final T entity);

T update(final T entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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;

Expand All @@ -12,18 +14,29 @@
@Transactional
public abstract class AbstractService<T extends Serializable> implements IOperations<T> {

// read - one

@Override
@Transactional(readOnly = true)
public T findOne(final long id) {
return getDao().findOne(id);
}

// read - all

@Override
@Transactional(readOnly = true)
public List<T> findAll() {
return Lists.newArrayList(getDao().findAll());
}

@Override
public Page<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@

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;
Expand All @@ -42,29 +46,39 @@ public 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<Foo> 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<Foo> 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<Foo> resultPage = service.findPaginated(page, size);
if (page > resultPage.getTotalPages()) {
throw new MyResourceNotFoundException();
}
// eventPublisher.publishEvent(new PaginatedResultsRetrievedEvent<Foo>(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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand Down Expand Up @@ -50,7 +51,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgume

// 404

@ExceptionHandler(value = { EntityNotFoundException.class })
@ExceptionHandler(value = { EntityNotFoundException.class, MyResourceNotFoundException.class })
protected ResponseEntity<Object> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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> T checkFound(final T resource) {
if (resource == null) {
throw new MyResourceNotFoundException();
}

return resource;
}

}

0 comments on commit 1af3eb4

Please sign in to comment.