Servicies and entities. Following RESTHUB implementation.
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;
// 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(","))));
- }
- }
}
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;
}
// 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(","))));
- }
- }
}
--- /dev/null
+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 :)
+
+}
--- /dev/null
+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;
+ }
+}
--- /dev/null
+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(","))));
+ }
+ }
+}
--- /dev/null
+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);
+}
--- /dev/null
+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(","))));
+ }
+ }
+}
--- /dev/null
+package de.spring.example.services;
+
+import de.spring.example.persistence.domain.AdDescription;
+
+public interface AdDescriptionRevisionService
+ extends RevisionService<AdDescription, Long, Integer> {
+
+}
--- /dev/null
+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;
+ }
+}
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);
}
}