Spring JPA: spring-data-envers, serializing data
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 26 Jul 2016 20:25:20 +0000 (22:25 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 26 Jul 2016 20:25:20 +0000 (22:25 +0200)
SpringJava/JPA/spring-jpa-bom/pom.xml
SpringJava/JPA/spring-jpa-resources/pom.xml
SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdController.java

index a5d1208..ff4360e 100644 (file)
                <artifactId>jackson-datatype-jsr310</artifactId>
                <version>2.8.1</version>
                </dependency>
+               <!-- 
+                       Jackson dependency required for serializing and deserializing org.joda.time.DateTime objects.
+                       See: org.springframework.data.history.Revision getRevisionDate
+                -->
+               <dependency>
+               <groupId>com.fasterxml.jackson.datatype</groupId>
+               <artifactId>jackson-datatype-joda</artifactId>
+               <version>2.8.1</version>
+               </dependency>
 
 
                        <!--
index 11c746d..4ea82b5 100644 (file)
                <groupId>com.fasterxml.jackson.datatype</groupId>
                <artifactId>jackson-datatype-jsr310</artifactId>
                </dependency>
+               <!-- 
+                       Jackson dependency required for serializing and deserializing org.joda.time.DateTime objects.
+                       See: org.springframework.data.history.Revision getRevisionDate
+                -->
+               <dependency>
+               <groupId>com.fasterxml.jackson.datatype</groupId>
+               <artifactId>jackson-datatype-joda</artifactId>
+               </dependency>
 
                <!-- Unitary and integration tests -->
                <dependency>
index edbf37d..3a980f3 100644 (file)
@@ -3,7 +3,16 @@ package de.spring.example.rest.controllers;
 import javax.inject.Inject;
 
 import org.resthub.web.controller.RepositoryBasedRestController;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.history.Revision;
+import org.springframework.util.Assert;
+import org.springframework.web.bind.annotation.PathVariable;
 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.RestController;
 
 import de.spring.example.persistence.domain.Ad;
@@ -24,4 +33,39 @@ public class AdController extends RepositoryBasedRestController<Ad, Long, AdRepo
     // @Transactional is implemented by org.springframework.data.jpa.repository.support.SimpleJpaRepository
     // By default, SimpleJpaRepository will be automatically implemented by my
     // Spring JPA repositories: AdRepository and AdDescriptionRepository.
+    
+    
+    
+    
+    
+    
+    
+    /** WE ARE EXTENDING RepositoryBasedRestController WITH METHODS FOR RETRIEVING REVISION NUMBERS!!! **/
+
+
+    /**
+     * Returns a {@link Page} of revisions for the entity with the given id
+     *
+     * @param page       Page number starting from 0. default to 0
+     * @param size       Number of resources by pages. default to 10
+     * @param direction  Optional sort direction, could be "asc" or "desc"
+     * @param properties Ordered list of comma separeted properies used for sorting resulats. At least one property should be provided if direction is specified
+     * @return OK http status code if the request has been correctly processed, with the a paginated collection of all resource enclosed in the body.
+     */
+    @RequestMapping(value="{id}/revisions", method = RequestMethod.GET)
+    @ResponseBody
+    public Page<Revision<Integer, Ad>> findRevisionsPaginated(@PathVariable Long id,
+                                                        @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.findRevisions(id, new PageRequest(page - 1, size));
+        } else {
+            Assert.notNull(properties);
+            return this.repository.findRevisions(id, new PageRequest(page - 1, size, new Sort(Sort.Direction.fromString(direction.toUpperCase()), properties.split(","))));
+        }
+    }
 }