All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.resthub.web.controller.RepositoryBasedRestController Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package org.resthub.web.controller;

import org.resthub.common.exception.NotFoundException;
import org.resthub.common.exception.NotImplementedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.Serializable;

/**
 * Abstract REST controller using a repository implementation
 * 

*

* You should extend this class when you want to use a 2 layers pattern : Repository and Controller. This is the default * controller implementation to use if you have no service (also called business) layer. You will be able to transform * it to a ServiceBasedRestController later easily if needed. *

* *

Default implementation uses "id" field (usually a Long) in order to identify resources in web request. * If your want to identity resources by a slug (human readable identifier), your should override findById() method with for example : * *

 * 
   {@literal @}Override
   public Sample findById({@literal @}PathVariable String id) {
        Sample sample = this.repository.findByName(id);
        if (sample == null) {
            throw new NotFoundException();
        }
        return sample;
   }
   
 * 
* * * @param Your resource class to manage, maybe an entity or DTO class * @param Resource id type, usually Long or String * @param The repository class * @see ServiceBasedRestController */ public abstract class RepositoryBasedRestController implements RestController { protected R repository; protected Logger logger = LoggerFactory.getLogger(RepositoryBasedRestController.class); /** * You should override this setter in order to inject your repository with @Inject annotation * * @param repository The repository to be injected */ public void setRepository(R repository) { this.repository = repository; } /** * {@inheritDoc} */ @Override public T create(@RequestBody T resource) { return (T)this.repository.save(resource); } /** * {@inheritDoc} */ @Override public T update(@PathVariable ID id, @RequestBody T resource) { Assert.notNull(id, "id cannot be null"); T retrievedResource = (T)this.findById(id); if (retrievedResource == null) { throw new NotFoundException(); } return (T)this.repository.save(resource); } /** * {@inheritDoc} */ @Override public Iterable findAllXml() { throw new NotImplementedException("XML findAll() is currently not implemented"); } /** * {@inheritDoc} */ @Override public Iterable findAll() { return repository.findAll(); } /** * {@inheritDoc} */ @Override public Page findPaginated(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size, @RequestParam(value = "direction", required = false, defaultValue = "") String direction, @RequestParam(value = "properties", required = false) String properties) { Assert.isTrue(page > 0, "Page index must be greater than 0"); Assert.isTrue(direction.isEmpty() || direction.equalsIgnoreCase(Sort.Direction.ASC.toString()) || direction.equalsIgnoreCase(Sort.Direction.DESC.toString()), "Direction should be ASC or DESC"); if(direction.isEmpty()) { return this.repository.findAll(new PageRequest(page - 1, size)); } else { Assert.notNull(properties); return this.repository.findAll(new PageRequest(page - 1, size, new Sort(Sort.Direction.fromString(direction.toUpperCase()), properties.split(",")))); } } /** * {@inheritDoc} */ @Override public T findById(@PathVariable ID id) { T entity = (T)this.repository.findOne(id); if (entity == null) { throw new NotFoundException(); } return entity; } /** * {@inheritDoc} */ @Override public void delete() { Iterable list = repository.findAll(); for (T entity : list) { repository.delete(entity); } } /** * {@inheritDoc} */ @Override public void delete(@PathVariable ID id) { T resource = (T)this.findById(id); this.repository.delete(resource); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy