Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. package de.hsos.kbse.agilboard.DAL.Services.Superclass;
  2.  
  3. import de.hsos.kbse.agilboard.DAL.DAOs.Superclass.EntityWithID;
  4. import de.hsos.kbse.agilboard.DAL.Services.Consts;
  5. import java.io.Serializable;
  6. import java.util.List;
  7. import java.util.Optional;
  8. import javax.ejb.Stateless;
  9. import javax.ws.rs.client.Client;
  10. import javax.ws.rs.client.ClientBuilder;
  11. import javax.ws.rs.client.Entity;
  12. import javax.ws.rs.client.WebTarget;
  13. import javax.ws.rs.core.GenericType;
  14. import javax.ws.rs.core.MediaType;
  15. import javax.ws.rs.core.Response;
  16. //KBSE
  17. public abstract class GenericClient<T extends EntityWithID> implements Serializable{
  18. protected Client client;
  19. protected WebTarget webTarget; //TODO: WebTarget muss geclosed werden wo machen wir das am besten!?
  20. protected String entityResourceName;
  21. protected GenericType<T> genericTypeEntity;
  22. protected GenericType<List<T>> genericTypeList;
  23.  
  24. public GenericClient(String entityResourceName, GenericType<T> genericTypeEntity, GenericType<List<T>> genericTypeList) {
  25. this.client = ClientBuilder.newClient();
  26. this.webTarget = client.target(Consts.BASEURI);
  27. this.entityResourceName = entityResourceName;
  28. this.genericTypeEntity = genericTypeEntity;
  29. this.genericTypeList = genericTypeList;
  30. }
  31.  
  32. public GenericClient(){
  33. this.client = ClientBuilder.newClient();
  34. this.webTarget = client.target(Consts.BASEURI);
  35. };
  36.  
  37. protected WebTarget getResourceWebTarget(){
  38. return client
  39. .target(Consts.BASEURI)
  40. .path(entityResourceName);
  41. }
  42.  
  43. protected Optional<List<T>> retrieveEntityList(Response response){
  44. if(response.getStatus() == 200){
  45. return Optional.of(response.readEntity(this.genericTypeList));
  46. }else{
  47. return Optional.empty();
  48. }
  49. }
  50.  
  51. protected Optional<T> retrieveEntity(Response response){
  52. if(response.getStatus() == 200){
  53. return Optional.of(response.readEntity(this.genericTypeEntity));
  54. }else{
  55. return Optional.empty();
  56. }
  57. }
  58.  
  59. public Optional<List<T>> getAll(){
  60. Response response = this.getResourceWebTarget()
  61. .request(MediaType.APPLICATION_JSON)
  62. .get(Response.class);
  63. return this.retrieveEntityList(response);
  64. }
  65.  
  66. public Optional<T> getByID(int id){
  67. Response response = this.getResourceWebTarget()
  68. .path(String.valueOf(id))
  69. .request(MediaType.APPLICATION_JSON)
  70. .get(Response.class);
  71. return this.retrieveEntity(response);
  72.  
  73. }
  74.  
  75. public Optional<T> create(T entity){
  76. Response response = this.getResourceWebTarget()
  77. .request(MediaType.APPLICATION_JSON)
  78. .post(Entity.entity(entity, MediaType.APPLICATION_JSON),
  79. Response.class);
  80. return this.retrieveEntity(response);
  81. }
  82.  
  83. public Optional<T> update(T entity){
  84. Response response = this.getResourceWebTarget()
  85. .request(MediaType.APPLICATION_JSON)
  86. .put(Entity.entity(entity, MediaType.APPLICATION_JSON), Response.class);
  87. return this.retrieveEntity(response);
  88. }
  89.  
  90. public boolean delete(T entity){
  91. if(entity == null || entity.getId() == null) return false;
  92. return this.deleteById(entity.getId());
  93. }
  94.  
  95. public boolean deleteById(int id){
  96. int status = this.getResourceWebTarget()
  97. .path(String.valueOf(id))
  98. .request(MediaType.APPLICATION_JSON)
  99. .delete().getStatus();
  100. return status == 200;
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement