From 07752435e53c6308a2862e8cad98bbca7964ca01 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 26 Jul 2016 23:09:32 +0200 Subject: [PATCH] Spring JPA: AdDescription revisions --- .../persistence/domain/audit/MyCustomRevision.java | 5 +-- .../example/rest/controllers/AdController.java | 2 +- .../rest/controllers/AdDescriptionController.java | 39 ++++++++++++++++++++++ .../example/services/AdDescriptionService.java | 10 ++++++ .../spring/example/services/RevisionService.java | 19 +++++++++++ .../services/impl/AdDescriptionServiceImpl.java | 6 ++++ .../example/services/impl/RevisionServiceImpl.java | 20 +++++++++++ 7 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/RevisionService.java create mode 100644 SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/RevisionServiceImpl.java diff --git a/SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/audit/MyCustomRevision.java b/SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/audit/MyCustomRevision.java index d388d04..a119bca 100644 --- a/SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/audit/MyCustomRevision.java +++ b/SpringJava/JPA/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/audit/MyCustomRevision.java @@ -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; } 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 3a980f3..be35bcb 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 @@ -52,7 +52,7 @@ public class AdController extends RepositoryBasedRestController> findRevisionsPaginated(@PathVariable Long id, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page, 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 d3c1849..a0bbf4f 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,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> 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-services/src/main/java/de/spring/example/services/AdDescriptionService.java b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/AdDescriptionService.java index bbcf1fa..7a571b9 100644 --- a/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/AdDescriptionService.java +++ b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/AdDescriptionService.java @@ -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 { public Page 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> 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 index 0000000..abc891d --- /dev/null +++ b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/RevisionService.java @@ -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> { + + /** + * Returns a {@link Page} of revisions for the entity with the given id. + * + * @param id must not be {@literal null}. + * @param pageable + * @return + */ + Page> findRevisions(ID id, Pageable pageable); +} diff --git a/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java index 830709b..39c1bcc 100644 --- a/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java +++ b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java @@ -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> 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 index 0000000..5b9df7c --- /dev/null +++ b/SpringJava/JPA/spring-jpa-services/src/main/java/de/spring/example/services/impl/RevisionServiceImpl.java @@ -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> implements RevisionService { + + @Override + public Page> findRevisions(Serializable id, Pageable pageable) { + + // TODO Auto-generated method stub + return null; + } + +} -- 2.1.4