Advertisement
Guest User

EstabelecimentoDao

a guest
Mar 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.14 KB | None | 0 0
  1. package br.com.rang.social.persistence.dao;
  2.  
  3. import static org.springframework.http.HttpMethod.DELETE;
  4. import static org.springframework.http.HttpMethod.GET;
  5. import static org.springframework.http.HttpMethod.POST;
  6. import static org.springframework.http.HttpMethod.PUT;
  7.  
  8. import java.io.Serializable;
  9. import java.util.List;
  10.  
  11. import javax.inject.Inject;
  12.  
  13. import org.springframework.core.ParameterizedTypeReference;
  14. import org.springframework.http.HttpMethod;
  15. import org.springframework.http.ResponseEntity;
  16. import org.springframework.web.util.UriComponents;
  17. import org.springframework.web.util.UriComponentsBuilder;
  18.  
  19. import br.com.rang.social.annotation.ExceptionHandler;
  20. import br.com.rang.social.bean.estabelecimento.EstabelecimentoUtil;
  21. import br.com.rang.social.custom.CustomRestTemplate;
  22. import br.com.rang.social.persistence.model.Estabelecimento;
  23. import br.com.rang.social.util.ApiUtil;
  24. import br.com.rang.social.util.JsonUtil;
  25.  
  26. public class EstabelecimentoDAO implements Serializable{
  27.    
  28.     private final String LISTAR_URL = ApiUtil.BASE_URL + "/estabelecimento/list";
  29.     private final String DELETE_OR_FIND_ONE_URL = ApiUtil.BASE_URL + "/estabelecimento/{id}";
  30.     private final String CREATE_UPDATE_URL = ApiUtil.BASE_URL + "/estabelecimento/";
  31.     private final String BUSCA_DESTINO = ApiUtil.BASE_URL + "/estabelecimento/buscaPorNome";
  32.     private final String BUSCA_CARRO = ApiUtil.BASE_URL + "/estabelecimento/buscaPorCNPJ";
  33.     private final String VERIFICA_CNPJ = ApiUtil.BASE_URL + "/estabelecimento/verificacnpj";
  34.     private final String VERIFICA_CNPJ_ALTERAR = ApiUtil.BASE_URL + "/estabelecimento/verificacnpjAlterar";
  35.    
  36.     private final CustomRestTemplate restTemplate;
  37.     private final JsonUtil jsonUtil;
  38.    
  39.     @Inject
  40.     public EstabelecimentoDAO(CustomRestTemplate restTemplate,
  41.                      JsonUtil jsonUtil) {
  42.         this.restTemplate = restTemplate;
  43.         this.jsonUtil = jsonUtil;
  44.     }
  45.  
  46.     @ExceptionHandler
  47.     public List<Estabelecimento> list(String query) {
  48.         UriComponents url = UriComponentsBuilder.fromUriString(LISTAR_URL).queryParam("query", query).build();
  49.         ResponseEntity<List<Estabelecimento>> exchange = restTemplate.exchange(url.toUriString(), GET, jsonUtil.tokenizedHttpEntityHeader(),new ParameterizedTypeReference<List<Estabelecimento>>() {});
  50.         return exchange.getBody();
  51.     }
  52.    
  53.     @ExceptionHandler
  54.     public List<Estabelecimento> buscaPorNome(String query) {
  55.         UriComponents url = UriComponentsBuilder.fromUriString(BUSCA_DESTINO).queryParam("query", query).build();
  56.         ResponseEntity<List<Object[]>> exchange = restTemplate.exchange(url.toUriString(), GET, jsonUtil.tokenizedHttpEntityHeader(),new ParameterizedTypeReference<List<Object[]>>() {});
  57.         return EstabelecimentoUtil.converteBuscaPorNome(exchange.getBody());
  58.     }
  59.    
  60.     @ExceptionHandler
  61.     public List<Estabelecimento> buscaPorCNPJ(String query) {
  62.         UriComponents url = UriComponentsBuilder.fromUriString(BUSCA_CARRO).queryParam("query", query).build();
  63.         ResponseEntity<List<Object[]>> exchange = restTemplate.exchange(url.toUriString(), GET, jsonUtil.tokenizedHttpEntityHeader(),new ParameterizedTypeReference<List<Object[]>>() {});
  64.         return EstabelecimentoUtil.converteBuscaPorCNPJ(exchange.getBody());
  65.     }
  66.    
  67.     /*@ExceptionHandler
  68.     public Estabelecimento carregaAlterar(long id) {
  69.         ResponseEntity<Object[]> responseEntity = restTemplate.exchange(ALTERAR_URL, GET, jsonUtil.tokenizedHttpEntityHeader(), Object[].class, id);
  70.         return EstabelecimentoUtil.carregaAlterar(responseEntity.getBody());
  71.     }*/
  72.    
  73.     /*@ExceptionHandler
  74.     public Estabelecimento carregaVer(long id) {
  75.         ResponseEntity<Object[]> responseEntity = restTemplate.exchange(VER_URL, GET, jsonUtil.tokenizedHttpEntityHeader(), Object[].class, id);
  76.         return EstabelecimentoUtil.carregaVer(responseEntity.getBody());
  77.     }*/
  78.    
  79.     public Estabelecimento update(Estabelecimento estabelecimento) {
  80.         return createOrUpdate(PUT, estabelecimento);
  81.     }
  82.    
  83.     public Estabelecimento create(Estabelecimento estabelecimento) {
  84.         return createOrUpdate(POST, estabelecimento);
  85.     }
  86.    
  87.     private Estabelecimento createOrUpdate(HttpMethod httpMethod, Estabelecimento estabelecimento) {
  88.         System.out.println("create or update estabelecimentoDAO");
  89.         return restTemplate.exchange(CREATE_UPDATE_URL, httpMethod, jsonUtil.tokenizedHttpEntityHeader(estabelecimento), Estabelecimento.class).getBody();
  90.     }
  91.  
  92.     public void delete(Estabelecimento estabelecimento) {
  93.         restTemplate.exchange(DELETE_OR_FIND_ONE_URL, DELETE,
  94.                 jsonUtil.tokenizedHttpEntityHeader(estabelecimento),
  95.                 Estabelecimento.class,
  96.                 estabelecimento.getId());
  97.     }
  98.    
  99.     @ExceptionHandler
  100.     public Integer verificaCNPJ(String cnpj) {
  101.         UriComponents url = UriComponentsBuilder.fromUriString(VERIFICA_CNPJ).queryParam("cnpj", cnpj).build();
  102.         return restTemplate.exchange(url.toUriString(), GET, jsonUtil.tokenizedHttpEntityHeader(), Integer.class, cnpj).getBody();
  103.     }
  104.    
  105.     @ExceptionHandler
  106.     public Integer verificaCNPJAlterar(String cnpj, Long id) {
  107.         UriComponents url = UriComponentsBuilder.fromUriString(VERIFICA_CNPJ_ALTERAR).queryParam("cnpj", cnpj).queryParam("id", id).build();
  108.         return restTemplate.exchange(url.toUriString(), GET, jsonUtil.tokenizedHttpEntityHeader(), Integer.class, cnpj, id).getBody();
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement