1 package org.resthub.web.controller;
 
   3 import java.io.Serializable;
 
   6 import org.resthub.common.exception.NotFoundException;
 
   7 import org.resthub.common.service.CrudService;
 
   8 import org.springframework.data.domain.Page;
 
   9 import org.springframework.data.domain.PageRequest;
 
  10 import org.springframework.data.domain.Sort;
 
  11 import org.springframework.util.Assert;
 
  12 import org.springframework.web.bind.annotation.PathVariable;
 
  13 import org.springframework.web.bind.annotation.RequestBody;
 
  14 import org.springframework.web.bind.annotation.RequestParam;
 
  17  * Abstract REST controller using a service implementation
 
  19  * <p>You should extend this class when you want to use a 3 layers pattern : Repository, Service and Controller
 
  20  * If you don't have a real service (also called business layer), consider using RepositoryBasedRestController</p>
 
  22  * <p>Default implementation uses "id" field (usually a Long) in order to identify resources in web request.
 
  23  * If your want to identity resources by a slug (human readable identifier), your should override findById() method with for example :
 
  27  * {@literal @}Override
 
  28  * public Sample findById({@literal @}PathVariable String id) {
 
  29  * Sample sample = this.service.findByName(id);
 
  30  * if (sample == null) {
 
  31  * throw new NotFoundException();
 
  38  * @param <T>  Your resource class to manage, maybe an entity or DTO class
 
  39  * @param <ID> Resource id type, usually Long or String
 
  40  * @param <S>  The service class
 
  41  * @see RepositoryBasedRestController
 
  43 public abstract class ServiceBasedRestController<T, ID extends Serializable, S extends CrudService> implements
 
  44         RestController<T, ID> {
 
  49      * You should override this setter in order to inject your service with @Inject annotation
 
  51      * @param service The service to be injected
 
  53     public void setService(S service) {
 
  54         this.service = service;
 
  61     public T create(@RequestBody T resource) {
 
  62         return (T) this.service.create(resource);
 
  69     public T update(@PathVariable ID id, @RequestBody T resource) {
 
  70         Assert.notNull(id, "id cannot be null");
 
  72         T retreivedResource = this.findById(id);
 
  73         if (retreivedResource == null) {
 
  74             throw new NotFoundException();
 
  77         return (T) this.service.update(resource);
 
  84     public Iterable<T> findAll() {
 
  85         return service.findAll();
 
  92     public Page<T> findPaginated(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
 
  93                                  @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
 
  94                                  @RequestParam(value = "direction", required = false, defaultValue = "") String direction,
 
  95                                  @RequestParam(value = "properties", required = false) String properties) {
 
  96         Assert.isTrue(page > 0, "Page index must be greater than 0");
 
  97         Assert.isTrue(direction.isEmpty() || direction.equalsIgnoreCase(Sort.Direction.ASC.toString()) || direction.equalsIgnoreCase(Sort.Direction.DESC.toString()), "Direction should be ASC or DESC");
 
  98         if (direction.isEmpty()) {
 
  99             return this.service.findAll(new PageRequest(page - 1, size));
 
 101             Assert.notNull(properties);
 
 102             return this.service.findAll(new PageRequest(page - 1, size, new Sort(Sort.Direction.fromString(direction.toUpperCase()), properties.split(","))));
 
 110     public T findById(@PathVariable ID id) {
 
 111         T resource = (T) this.service.findById(id);
 
 112         if (resource == null) {
 
 113             throw new NotFoundException();
 
 123     public Iterable<T> findByIds(@RequestParam(value = "ids[]") Set<ID> ids) {
 
 124         Assert.notNull(ids, "ids list cannot be null");
 
 125         return this.service.findByIds(ids);
 
 133     public void delete() {
 
 134         this.service.deleteAllWithCascade();
 
 141     public void delete(@PathVariable ID id) {
 
 142         T resource = this.findById(id);
 
 143         this.service.delete(resource);