Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.rang.social.persistence.dao;
- import static org.springframework.http.HttpMethod.DELETE;
- import static org.springframework.http.HttpMethod.GET;
- import static org.springframework.http.HttpMethod.POST;
- import static org.springframework.http.HttpMethod.PUT;
- import java.io.Serializable;
- import java.util.List;
- import javax.inject.Inject;
- import org.springframework.core.ParameterizedTypeReference;
- import org.springframework.http.HttpMethod;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.util.UriComponents;
- import org.springframework.web.util.UriComponentsBuilder;
- import br.com.rang.social.annotation.ExceptionHandler;
- import br.com.rang.social.bean.estabelecimento.EstabelecimentoUtil;
- import br.com.rang.social.custom.CustomRestTemplate;
- import br.com.rang.social.persistence.model.Estabelecimento;
- import br.com.rang.social.util.ApiUtil;
- import br.com.rang.social.util.JsonUtil;
- public class EstabelecimentoDAO implements Serializable{
- private final String LISTAR_URL = ApiUtil.BASE_URL + "/estabelecimento/list";
- private final String DELETE_OR_FIND_ONE_URL = ApiUtil.BASE_URL + "/estabelecimento/{id}";
- private final String CREATE_UPDATE_URL = ApiUtil.BASE_URL + "/estabelecimento/";
- private final String BUSCA_DESTINO = ApiUtil.BASE_URL + "/estabelecimento/buscaPorNome";
- private final String BUSCA_CARRO = ApiUtil.BASE_URL + "/estabelecimento/buscaPorCNPJ";
- private final String VERIFICA_CNPJ = ApiUtil.BASE_URL + "/estabelecimento/verificacnpj";
- private final String VERIFICA_CNPJ_ALTERAR = ApiUtil.BASE_URL + "/estabelecimento/verificacnpjAlterar";
- private final CustomRestTemplate restTemplate;
- private final JsonUtil jsonUtil;
- @Inject
- public EstabelecimentoDAO(CustomRestTemplate restTemplate,
- JsonUtil jsonUtil) {
- this.restTemplate = restTemplate;
- this.jsonUtil = jsonUtil;
- }
- @ExceptionHandler
- public List<Estabelecimento> list(String query) {
- UriComponents url = UriComponentsBuilder.fromUriString(LISTAR_URL).queryParam("query", query).build();
- ResponseEntity<List<Estabelecimento>> exchange = restTemplate.exchange(url.toUriString(), GET, jsonUtil.tokenizedHttpEntityHeader(),new ParameterizedTypeReference<List<Estabelecimento>>() {});
- return exchange.getBody();
- }
- @ExceptionHandler
- public List<Estabelecimento> buscaPorNome(String query) {
- UriComponents url = UriComponentsBuilder.fromUriString(BUSCA_DESTINO).queryParam("query", query).build();
- ResponseEntity<List<Object[]>> exchange = restTemplate.exchange(url.toUriString(), GET, jsonUtil.tokenizedHttpEntityHeader(),new ParameterizedTypeReference<List<Object[]>>() {});
- return EstabelecimentoUtil.converteBuscaPorNome(exchange.getBody());
- }
- @ExceptionHandler
- public List<Estabelecimento> buscaPorCNPJ(String query) {
- UriComponents url = UriComponentsBuilder.fromUriString(BUSCA_CARRO).queryParam("query", query).build();
- ResponseEntity<List<Object[]>> exchange = restTemplate.exchange(url.toUriString(), GET, jsonUtil.tokenizedHttpEntityHeader(),new ParameterizedTypeReference<List<Object[]>>() {});
- return EstabelecimentoUtil.converteBuscaPorCNPJ(exchange.getBody());
- }
- /*@ExceptionHandler
- public Estabelecimento carregaAlterar(long id) {
- ResponseEntity<Object[]> responseEntity = restTemplate.exchange(ALTERAR_URL, GET, jsonUtil.tokenizedHttpEntityHeader(), Object[].class, id);
- return EstabelecimentoUtil.carregaAlterar(responseEntity.getBody());
- }*/
- /*@ExceptionHandler
- public Estabelecimento carregaVer(long id) {
- ResponseEntity<Object[]> responseEntity = restTemplate.exchange(VER_URL, GET, jsonUtil.tokenizedHttpEntityHeader(), Object[].class, id);
- return EstabelecimentoUtil.carregaVer(responseEntity.getBody());
- }*/
- public Estabelecimento update(Estabelecimento estabelecimento) {
- return createOrUpdate(PUT, estabelecimento);
- }
- public Estabelecimento create(Estabelecimento estabelecimento) {
- return createOrUpdate(POST, estabelecimento);
- }
- private Estabelecimento createOrUpdate(HttpMethod httpMethod, Estabelecimento estabelecimento) {
- System.out.println("create or update estabelecimentoDAO");
- return restTemplate.exchange(CREATE_UPDATE_URL, httpMethod, jsonUtil.tokenizedHttpEntityHeader(estabelecimento), Estabelecimento.class).getBody();
- }
- public void delete(Estabelecimento estabelecimento) {
- restTemplate.exchange(DELETE_OR_FIND_ONE_URL, DELETE,
- jsonUtil.tokenizedHttpEntityHeader(estabelecimento),
- Estabelecimento.class,
- estabelecimento.getId());
- }
- @ExceptionHandler
- public Integer verificaCNPJ(String cnpj) {
- UriComponents url = UriComponentsBuilder.fromUriString(VERIFICA_CNPJ).queryParam("cnpj", cnpj).build();
- return restTemplate.exchange(url.toUriString(), GET, jsonUtil.tokenizedHttpEntityHeader(), Integer.class, cnpj).getBody();
- }
- @ExceptionHandler
- public Integer verificaCNPJAlterar(String cnpj, Long id) {
- UriComponents url = UriComponentsBuilder.fromUriString(VERIFICA_CNPJ_ALTERAR).queryParam("cnpj", cnpj).queryParam("id", id).build();
- return restTemplate.exchange(url.toUriString(), GET, jsonUtil.tokenizedHttpEntityHeader(), Integer.class, cnpj, id).getBody();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement