From 86b290115cc87c010342b579bdfac82475ee9593 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 27 Jul 2016 16:11:12 +0200 Subject: [PATCH] Spring JPA: generic classes for implementing Controllers for revisions. Servicies and entities. Following RESTHUB implementation. --- .../example/rest/controllers/AdController.java | 43 ---------------- .../rest/controllers/AdDescriptionController.java | 38 -------------- .../AdDescriptionRevisionController.java | 24 +++++++++ .../rest/controllers/AdRevisionController.java | 21 ++++++++ .../RepositoryBasedRevisionRestController.java | 47 ++++++++++++++++++ .../rest/controllers/RevisionRestController.java | 31 ++++++++++++ .../ServiceBasedRevisionRestController.java | 58 ++++++++++++++++++++++ .../services/AdDescriptionRevisionService.java | 8 +++ .../impl/AdDescriptionRevisionServiceImpl.java | 18 +++++++ .../example/services/impl/RevisionServiceImpl.java | 23 +++++++-- 10 files changed, 226 insertions(+), 85 deletions(-) create mode 100644 SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdDescriptionRevisionController.java create mode 100644 SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdRevisionController.java create mode 100644 SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/RepositoryBasedRevisionRestController.java create mode 100644 SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/RevisionRestController.java create mode 100644 SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/ServiceBasedRevisionRestController.java create mode 100644 SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/AdDescriptionRevisionService.java create mode 100644 SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/AdDescriptionRevisionServiceImpl.java diff --git a/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdController.java b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdController.java index be35bcb..93316e3 100644 --- a/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdController.java +++ b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdController.java @@ -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> 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(",")))); - } - } } diff --git a/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdDescriptionController.java b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdDescriptionController.java index a0bbf4f..4ea0e9d 100644 --- a/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdDescriptionController.java +++ b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdDescriptionController.java @@ -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> 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 index 0000000..b68fc68 --- /dev/null +++ b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdDescriptionRevisionController.java @@ -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 { + + @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 index 0000000..53779d1 --- /dev/null +++ b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/AdRevisionController.java @@ -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 { + + @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 index 0000000..1e1247b --- /dev/null +++ b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/RepositoryBasedRevisionRestController.java @@ -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, R extends RevisionRepository> + implements RevisionRestController { + 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> 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 index 0000000..80d1c8e --- /dev/null +++ b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/RevisionRestController.java @@ -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> { + + /** + * 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> 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 index 0000000..b5b3fbb --- /dev/null +++ b/SpringJava/JPA/spring-jpa-resources/src/main/java/de/spring/example/rest/controllers/ServiceBasedRevisionRestController.java @@ -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, S extends RevisionService> + implements RevisionRestController { + + 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> 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 index 0000000..498068c --- /dev/null +++ b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/AdDescriptionRevisionService.java @@ -0,0 +1,8 @@ +package de.spring.example.services; + +import de.spring.example.persistence.domain.AdDescription; + +public interface AdDescriptionRevisionService + extends RevisionService { + +} 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 index 0000000..4b76189 --- /dev/null +++ b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/AdDescriptionRevisionServiceImpl.java @@ -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 + implements AdDescriptionRevisionService { + + @Override + @Inject + public void setRepository(AdDescriptionRepository repository) { + this.repository = repository; + } +} 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 index 5b9df7c..f4a235b 100644 --- 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 @@ -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> implements RevisionService { +public class RevisionServiceImpl, R extends RevisionRepository> + implements RevisionService { + protected R repository; + + /** + * @param repository the repository to set + */ + public void setRepository(R repository) { + this.repository = repository; + } + @Override - public Page> findRevisions(Serializable id, Pageable pageable) { + @Transactional + public Page> 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); } } -- 2.1.4