a0bbf4ff0a066e91ba89cb36dd4d7ac2969d1b18
[JavaForFun] /
1 package de.spring.example.rest.controllers;
2
3 import javax.inject.Inject;
4
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;
17
18 import de.spring.example.persistence.domain.AdDescription;
19 import de.spring.example.services.AdDescriptionService;
20
21 @RestController
22 @RequestMapping("/ad-descriptions/")
23 public class AdDescriptionController extends ServiceBasedRestController<AdDescription, Long, AdDescriptionService> {
24         
25         @Override
26         @Inject
27     public void setService(AdDescriptionService adDescriptionService) {
28         this.service = adDescriptionService;
29     }
30
31         // I do not have to do anything here because all I need is implemented by ServiceBasedRestController :)
32         
33         
34     /** WE ARE EXTENDING ServiceBasedRestController WITH METHODS FOR RETRIEVING REVISION NUMBERS!!! **/
35
36
37     /**
38      * Returns a {@link Page} of revisions for the entity with the given id
39      *
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.
45      */
46     @RequestMapping(value="{id}/revisions/", method = RequestMethod.GET)
47     @ResponseBody
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));
57         } else {
58             Assert.notNull(properties);
59             return this.service.findRevisions(id, new PageRequest(page - 1, size, new Sort(Sort.Direction.fromString(direction.toUpperCase()), properties.split(","))));
60         }
61     }
62 }