Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.hsos.kbse.agilboard.DAL.Services.Superclass;
- import de.hsos.kbse.agilboard.DAL.DAOs.Superclass.EntityWithID;
- import de.hsos.kbse.agilboard.DAL.Services.Consts;
- import java.io.Serializable;
- import java.util.List;
- import java.util.Optional;
- import javax.ejb.Stateless;
- import javax.ws.rs.client.Client;
- import javax.ws.rs.client.ClientBuilder;
- import javax.ws.rs.client.Entity;
- import javax.ws.rs.client.WebTarget;
- import javax.ws.rs.core.GenericType;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.Response;
- //KBSE
- public abstract class GenericClient<T extends EntityWithID> implements Serializable{
- protected Client client;
- protected WebTarget webTarget; //TODO: WebTarget muss geclosed werden wo machen wir das am besten!?
- protected String entityResourceName;
- protected GenericType<T> genericTypeEntity;
- protected GenericType<List<T>> genericTypeList;
- public GenericClient(String entityResourceName, GenericType<T> genericTypeEntity, GenericType<List<T>> genericTypeList) {
- this.client = ClientBuilder.newClient();
- this.webTarget = client.target(Consts.BASEURI);
- this.entityResourceName = entityResourceName;
- this.genericTypeEntity = genericTypeEntity;
- this.genericTypeList = genericTypeList;
- }
- public GenericClient(){
- this.client = ClientBuilder.newClient();
- this.webTarget = client.target(Consts.BASEURI);
- };
- protected WebTarget getResourceWebTarget(){
- return client
- .target(Consts.BASEURI)
- .path(entityResourceName);
- }
- protected Optional<List<T>> retrieveEntityList(Response response){
- if(response.getStatus() == 200){
- return Optional.of(response.readEntity(this.genericTypeList));
- }else{
- return Optional.empty();
- }
- }
- protected Optional<T> retrieveEntity(Response response){
- if(response.getStatus() == 200){
- return Optional.of(response.readEntity(this.genericTypeEntity));
- }else{
- return Optional.empty();
- }
- }
- public Optional<List<T>> getAll(){
- Response response = this.getResourceWebTarget()
- .request(MediaType.APPLICATION_JSON)
- .get(Response.class);
- return this.retrieveEntityList(response);
- }
- public Optional<T> getByID(int id){
- Response response = this.getResourceWebTarget()
- .path(String.valueOf(id))
- .request(MediaType.APPLICATION_JSON)
- .get(Response.class);
- return this.retrieveEntity(response);
- }
- public Optional<T> create(T entity){
- Response response = this.getResourceWebTarget()
- .request(MediaType.APPLICATION_JSON)
- .post(Entity.entity(entity, MediaType.APPLICATION_JSON),
- Response.class);
- return this.retrieveEntity(response);
- }
- public Optional<T> update(T entity){
- Response response = this.getResourceWebTarget()
- .request(MediaType.APPLICATION_JSON)
- .put(Entity.entity(entity, MediaType.APPLICATION_JSON), Response.class);
- return this.retrieveEntity(response);
- }
- public boolean delete(T entity){
- if(entity == null || entity.getId() == null) return false;
- return this.deleteById(entity.getId());
- }
- public boolean deleteById(int id){
- int status = this.getResourceWebTarget()
- .path(String.valueOf(id))
- .request(MediaType.APPLICATION_JSON)
- .delete().getStatus();
- return status == 200;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement