Spring JPA: generic classes for implementing Controllers for revisions.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 27 Jul 2016 14:11:12 +0000 (16:11 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 27 Jul 2016 14:11:12 +0000 (16:11 +0200)
Servicies and entities. Following RESTHUB implementation.

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-resources/src/main/java/de/spring/example/rest/controllers/AdDescriptionRevisionController.java [new file with mode: 0644]
SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdRevisionController.java [new file with mode: 0644]
SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/RepositoryBasedRevisionRestController.java [new file with mode: 0644]
SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/RevisionRestController.java [new file with mode: 0644]
SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/ServiceBasedRevisionRestController.java [new file with mode: 0644]
SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/AdDescriptionRevisionService.java [new file with mode: 0644]
SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/AdDescriptionRevisionServiceImpl.java [new file with mode: 0644]
SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/RevisionServiceImpl.java

index be35bcb..93316e3 100644 (file)
@@ -3,16 +3,7 @@ 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;
@@ -34,38 +25,4 @@ public class AdController extends RepositoryBasedRestController<Ad, Long, AdRepo
     // 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(","))));
-        }
-    }
 }
index a0bbf4f..4ea0e9d 100644 (file)
@@ -3,16 +3,7 @@ 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;
@@ -29,34 +20,5 @@ 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(","))));
-        }
-    }
 }
diff --git a/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdDescriptionRevisionController.java b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdDescriptionRevisionController.java
new file mode 100644 (file)
index 0000000..b68fc68
--- /dev/null
@@ -0,0 +1,24 @@
+package de.spring.example.rest.controllers;
+
+import javax.inject.Inject;
+
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import de.spring.example.persistence.domain.AdDescription;
+import de.spring.example.services.AdDescriptionRevisionService;
+
+@RestController
+@RequestMapping("/ad-descriptions/")
+public class AdDescriptionRevisionController
+               extends ServiceBasedRevisionRestController<AdDescription, Long, Integer, AdDescriptionRevisionService> {
+       
+       @Override
+       @Inject
+    public void setService(AdDescriptionRevisionService adDescriptionRevisionService) {
+        this.service = adDescriptionRevisionService;
+    }
+
+       // I do not have to do anything here because all I need is implemented by ServiceBasedRevisionRestController :)
+       
+}
diff --git a/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdRevisionController.java b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdRevisionController.java
new file mode 100644 (file)
index 0000000..53779d1
--- /dev/null
@@ -0,0 +1,21 @@
+package de.spring.example.rest.controllers;
+
+import javax.inject.Inject;
+
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import de.spring.example.persistence.domain.Ad;
+import de.spring.example.persistence.repository.AdRepository;
+
+@RestController
+@RequestMapping("/ads/")
+public class AdRevisionController extends
+               RepositoryBasedRevisionRestController<Ad, Long, Integer, AdRepository> {
+
+    @Override
+    @Inject
+    public void setRepository(AdRepository repository) {
+        this.repository = repository;
+    }
+}
diff --git a/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/RepositoryBasedRevisionRestController.java b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/RepositoryBasedRevisionRestController.java
new file mode 100644 (file)
index 0000000..1e1247b
--- /dev/null
@@ -0,0 +1,47 @@
+package de.spring.example.rest.controllers;
+
+import java.io.Serializable;
+
+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.history.Revision;
+import org.springframework.data.repository.history.RevisionRepository;
+import org.springframework.util.Assert;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestParam;
+
+public abstract class RepositoryBasedRevisionRestController<T, ID extends Serializable, N extends Number & Comparable<N>, R extends RevisionRepository<T, ID, N>>
+               implements RevisionRestController<T, ID, N> {
+    protected R repository;
+
+    protected Logger logger = LoggerFactory.getLogger(RepositoryBasedRevisionRestController.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;
+    }
+
+
+    @Override
+    public Page<Revision<N, T>> findRevisionsPaginated(@PathVariable ID 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(","))));
+        }
+    }
+}
diff --git a/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/RevisionRestController.java b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/RevisionRestController.java
new file mode 100644 (file)
index 0000000..80d1c8e
--- /dev/null
@@ -0,0 +1,31 @@
+package de.spring.example.rest.controllers;
+
+import java.io.Serializable;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.history.Revision;
+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;
+
+public interface RevisionRestController<T, ID extends Serializable, N extends Number & Comparable<N>> {
+
+    /**
+     * 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<N, T>> findRevisionsPaginated(@PathVariable ID 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);
+}
diff --git a/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/ServiceBasedRevisionRestController.java b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/ServiceBasedRevisionRestController.java
new file mode 100644 (file)
index 0000000..b5b3fbb
--- /dev/null
@@ -0,0 +1,58 @@
+package de.spring.example.rest.controllers;
+
+import java.io.Serializable;
+
+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 de.spring.example.services.RevisionService;
+
+public abstract class ServiceBasedRevisionRestController<T, ID extends Serializable, N extends Number & Comparable<N>, S extends RevisionService<T, ID, N>>
+               implements RevisionRestController<T, ID, N> {
+       
+    protected S service;
+
+    /**
+     * You should override this setter in order to inject your service with @Inject annotation
+     *
+     * @param service The service to be injected
+     */
+    public void setService(S service) {
+        this.service = service;
+    }
+
+
+    /**
+     * 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<N, T>> findRevisionsPaginated(@PathVariable ID 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(","))));
+        }
+    }
+}
diff --git a/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/AdDescriptionRevisionService.java b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/AdDescriptionRevisionService.java
new file mode 100644 (file)
index 0000000..498068c
--- /dev/null
@@ -0,0 +1,8 @@
+package de.spring.example.services;
+
+import de.spring.example.persistence.domain.AdDescription;
+
+public interface AdDescriptionRevisionService
+       extends RevisionService<AdDescription, Long, Integer> {
+       
+}
diff --git a/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/AdDescriptionRevisionServiceImpl.java b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/AdDescriptionRevisionServiceImpl.java
new file mode 100644 (file)
index 0000000..4b76189
--- /dev/null
@@ -0,0 +1,18 @@
+package de.spring.example.services.impl;
+
+import javax.inject.Inject;
+
+import de.spring.example.persistence.domain.AdDescription;
+import de.spring.example.persistence.repository.AdDescriptionRepository;
+import de.spring.example.services.AdDescriptionRevisionService;
+
+public class AdDescriptionRevisionServiceImpl
+       extends RevisionServiceImpl<AdDescription, Long, Integer, AdDescriptionRepository>
+       implements AdDescriptionRevisionService {
+
+       @Override
+       @Inject
+    public void setRepository(AdDescriptionRepository repository) {
+        this.repository = repository;
+    }
+}
index 5b9df7c..f4a235b 100644 (file)
@@ -5,16 +5,31 @@ import java.io.Serializable;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.history.Revision;
+import org.springframework.data.repository.history.RevisionRepository;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.Assert;
 
 import de.spring.example.services.RevisionService;
 
-public class RevisionServiceImpl<T, ID extends Serializable, N extends Number & Comparable<N>> implements RevisionService<T, ID, N> {
+public class RevisionServiceImpl<T, ID extends Serializable, N extends Number & Comparable<N>, R extends RevisionRepository<T, ID, N>>
+       implements RevisionService<T, ID, N> {
 
+    protected R repository;
+
+    /**
+     * @param repository the repository to set
+     */
+    public void setRepository(R repository) {
+        this.repository = repository;
+    }
+    
        @Override
-       public Page<Revision<N, T>> findRevisions(Serializable id, Pageable pageable) {
+       @Transactional
+       public Page<Revision<N, T>> findRevisions(ID id, Pageable pageable) {
+               Assert.notNull(pageable, "page request can't be null");
+               Assert.notNull(id, "Resource ID can't be null");
                
-               // TODO Auto-generated method stub
-               return null;
+               return this.repository.findRevisions(id, pageable);
        }
 
 }