Spring JPA: AdDescription revisions
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 26 Jul 2016 21:09:32 +0000 (23:09 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 26 Jul 2016 21:09:32 +0000 (23:09 +0200)
SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/audit/MyCustomRevision.java
SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdController.java
SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdDescriptionController.java
SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/AdDescriptionService.java
SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/RevisionService.java [new file with mode: 0644]
SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java
SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/RevisionServiceImpl.java [new file with mode: 0644]

index d388d04..a119bca 100644 (file)
@@ -27,7 +27,8 @@ public class MyCustomRevision /** extends DefaultRevisionEntity **/ {
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name="ID", updatable=false, nullable=false)
        @RevisionNumber
-       private Long id;
+       // BE CAREFUL!!!! spring-data-envers JUST WORKS (I couldn't make it work with anything else) WITH Integer. NOT WITH Long :(
+       private Integer id;
        
        @Column(name="REVISION_DATE")
        @Temporal(TemporalType.TIMESTAMP)
@@ -52,7 +53,7 @@ public class MyCustomRevision /** extends DefaultRevisionEntity **/ {
         * WARNING: JPA REQUIRES GETTERS!!!
         */
        
-       public Long getId() {
+       public Integer getId() {
                return id;
        }
        
index 3a980f3..be35bcb 100644 (file)
@@ -52,7 +52,7 @@ public class AdController extends RepositoryBasedRestController<Ad, Long, AdRepo
      * @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)
+    @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,
index d3c1849..a0bbf4f 100644 (file)
@@ -3,7 +3,16 @@ package de.spring.example.rest.controllers;
 import javax.inject.Inject;
 
 import org.resthub.web.controller.ServiceBasedRestController;
+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.AdDescription;
@@ -20,4 +29,34 @@ public class AdDescriptionController extends ServiceBasedRestController<AdDescri
     }
 
        // I do not have to do anything here because all I need is implemented by ServiceBasedRestController :)
+       
+       
+    /** WE ARE EXTENDING ServiceBasedRestController 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, AdDescription>> 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.service.findRevisions(id, new PageRequest(page - 1, size));
+        } else {
+            Assert.notNull(properties);
+            return this.service.findRevisions(id, new PageRequest(page - 1, size, new Sort(Sort.Direction.fromString(direction.toUpperCase()), properties.split(","))));
+        }
+    }
 }
index bbcf1fa..7a571b9 100644 (file)
@@ -3,10 +3,20 @@ package de.spring.example.services;
 import org.resthub.common.service.CrudService;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
+import org.springframework.data.history.Revision;
 
 import de.spring.example.persistence.domain.AdDescription;
 
 public interface AdDescriptionService extends CrudService<AdDescription, Long> {
 
        public Page<AdDescription> queryDslExample(Pageable pageRequest);
+       
+       /**
+        * Returns a {@link Page} of revisions for the entity with the given id.
+        * 
+        * @param id must not be {@literal null}.
+        * @param pageable
+        * @return
+        */
+       Page<Revision<Integer, AdDescription>> findRevisions(Long id, Pageable pageable);
 }
diff --git a/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/RevisionService.java b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/RevisionService.java
new file mode 100644 (file)
index 0000000..abc891d
--- /dev/null
@@ -0,0 +1,19 @@
+package de.spring.example.services;
+
+import java.io.Serializable;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.history.Revision;
+
+public interface RevisionService<T, ID extends Serializable, N extends Number & Comparable<N>> {
+
+       /**
+        * Returns a {@link Page} of revisions for the entity with the given id.
+        * 
+        * @param id must not be {@literal null}.
+        * @param pageable
+        * @return
+        */
+       Page<Revision<N, T>> findRevisions(ID id, Pageable pageable);
+}
index 830709b..39c1bcc 100644 (file)
@@ -6,6 +6,7 @@ import javax.inject.Named;
 import org.resthub.common.service.CrudServiceImpl;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
+import org.springframework.data.history.Revision;
 
 import com.querydsl.core.types.dsl.BooleanExpression;
 
@@ -51,4 +52,9 @@ public class AdDescriptionServiceImpl
                 */
                return null;
        }
+
+       @Override
+       public Page<Revision<Integer, AdDescription>> findRevisions(Long id, Pageable pageable) {
+               return this.repository.findRevisions(id, pageable);
+       }
 }
diff --git a/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/RevisionServiceImpl.java b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/RevisionServiceImpl.java
new file mode 100644 (file)
index 0000000..5b9df7c
--- /dev/null
@@ -0,0 +1,20 @@
+package de.spring.example.services.impl;
+
+import java.io.Serializable;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.history.Revision;
+
+import de.spring.example.services.RevisionService;
+
+public class RevisionServiceImpl<T, ID extends Serializable, N extends Number & Comparable<N>> implements RevisionService<T, ID, N> {
+
+       @Override
+       public Page<Revision<N, T>> findRevisions(Serializable id, Pageable pageable) {
+               
+               // TODO Auto-generated method stub
+               return null;
+       }
+
+}