1 package de.spring.example.rest.controllers;
3 import javax.inject.Inject;
5 import org.resthub.web.controller.ServiceBasedRestController;
6 import org.springframework.data.domain.Page;
7 import org.springframework.data.domain.PageRequest;
8 import org.springframework.data.domain.Sort;
9 import org.springframework.data.history.Revision;
10 import org.springframework.util.Assert;
11 import org.springframework.web.bind.annotation.PathVariable;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.RequestMethod;
14 import org.springframework.web.bind.annotation.RequestParam;
15 import org.springframework.web.bind.annotation.ResponseBody;
16 import org.springframework.web.bind.annotation.RestController;
18 import de.spring.example.persistence.domain.AdDescription;
19 import de.spring.example.services.AdDescriptionService;
22 @RequestMapping("/ad-descriptions/")
23 public class AdDescriptionController extends ServiceBasedRestController<AdDescription, Long, AdDescriptionService> {
27 public void setService(AdDescriptionService adDescriptionService) {
28 this.service = adDescriptionService;
31 // I do not have to do anything here because all I need is implemented by ServiceBasedRestController :)
34 /** WE ARE EXTENDING ServiceBasedRestController WITH METHODS FOR RETRIEVING REVISION NUMBERS!!! **/
38 * Returns a {@link Page} of revisions for the entity with the given id
40 * @param page Page number starting from 0. default to 0
41 * @param size Number of resources by pages. default to 10
42 * @param direction Optional sort direction, could be "asc" or "desc"
43 * @param properties Ordered list of comma separeted properies used for sorting resulats. At least one property should be provided if direction is specified
44 * @return OK http status code if the request has been correctly processed, with the a paginated collection of all resource enclosed in the body.
46 @RequestMapping(value="{id}/revisions/", method = RequestMethod.GET)
48 public Page<Revision<Integer, AdDescription>> findRevisionsPaginated(@PathVariable Long id,
49 @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
50 @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
51 @RequestParam(value = "direction", required = false, defaultValue = "") String direction,
52 @RequestParam(value = "properties", required = false) String properties) {
53 Assert.isTrue(page > 0, "Page index must be greater than 0");
54 Assert.isTrue(direction.isEmpty() || direction.equalsIgnoreCase(Sort.Direction.ASC.toString()) || direction.equalsIgnoreCase(Sort.Direction.DESC.toString()), "Direction should be ASC or DESC");
55 if(direction.isEmpty()) {
56 return this.service.findRevisions(id, new PageRequest(page - 1, size));
58 Assert.notNull(properties);
59 return this.service.findRevisions(id, new PageRequest(page - 1, size, new Sort(Sort.Direction.fromString(direction.toUpperCase()), properties.split(","))));