|
@@ -0,0 +1,101 @@
|
|
|
+package com.ademicon.service;
|
|
|
+
|
|
|
+import java.net.ConnectException;
|
|
|
+
|
|
|
+import javax.ws.rs.client.ClientBuilder;
|
|
|
+import javax.ws.rs.client.Entity;
|
|
|
+import javax.ws.rs.core.Form;
|
|
|
+import javax.ws.rs.core.MediaType;
|
|
|
+import javax.ws.rs.core.Response;
|
|
|
+
|
|
|
+import com.ademicon.model.ContratoModel;
|
|
|
+import com.ademicon.model.ContratoResponse;
|
|
|
+import com.ademicon.model.auth.AccessModel;
|
|
|
+import com.ademicon.repository.ContratoRepository;
|
|
|
+import com.ademicon.service.auth.AccessService;
|
|
|
+import com.ademicon.service.auth.TokenService;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+
|
|
|
+public class ContratoService {
|
|
|
+
|
|
|
+ private static AccessService accessService = new AccessService();
|
|
|
+
|
|
|
+ private static TokenService tokenService = new TokenService();
|
|
|
+
|
|
|
+ private static ContratoRepository contratoRepository = new ContratoRepository();
|
|
|
+
|
|
|
+ public ContratoResponse incluir(ContratoModel contrato) throws Exception {
|
|
|
+
|
|
|
+ AccessModel accessModel = accessService.find();
|
|
|
+
|
|
|
+ String token = tokenService.getToken();
|
|
|
+
|
|
|
+ Response response = ClientBuilder.newClient()
|
|
|
+ .target(accessModel.getPlaceti_url_base() + "/api/v1/contrato/incluir")
|
|
|
+ .request(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + token)
|
|
|
+ .buildPost(Entity.entity(contrato, MediaType.APPLICATION_JSON)).invoke();
|
|
|
+
|
|
|
+ int status = response.getStatus();
|
|
|
+ if (status == 200) {
|
|
|
+ String json = response.readEntity(String.class);
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ return mapper.readValue(json, ContratoResponse.class);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ throw new ConnectException();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public ContratoResponse upload(String registro, String arquivo) throws Exception {
|
|
|
+
|
|
|
+ try {
|
|
|
+ AccessModel accessModel = accessService.find();
|
|
|
+
|
|
|
+ String token = tokenService.getToken();
|
|
|
+
|
|
|
+ Form form = new Form();
|
|
|
+ form.param("file", arquivo);
|
|
|
+
|
|
|
+ Response response = ClientBuilder.newClient()
|
|
|
+ .target(accessModel.getPlaceti_url_base()
|
|
|
+ + "/api/v1/contrato/upload/numeroRegistroEletronico/:numeroRegistroEletronico")
|
|
|
+ .queryParam("numeroRegistroEletronico", registro)
|
|
|
+ .request()
|
|
|
+ .header("Content-Type", "multipart/form-data")
|
|
|
+ .header("Accept", "application/json")
|
|
|
+ .header("Authorization", "Bearer " + token)
|
|
|
+ .buildPost(Entity.form(form)).invoke();
|
|
|
+
|
|
|
+ if (response.getStatus() >= 200 && response.getStatus() < 300) {
|
|
|
+ ContratoResponse contrato = response.readEntity(ContratoResponse.class);
|
|
|
+ if (!contrato.getNumeroRegistroEletronico().isEmpty()) {
|
|
|
+ int i = contratoRepository.incluir(contrato);
|
|
|
+ if (i == 1) {
|
|
|
+ return contrato;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ throw new Exception(
|
|
|
+ "Falha ao gravar o número do contrato: " + contrato.getNumeroRegistroEletronico());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ throw new Exception("Falha ao registrar contrato");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ throw new Exception("Falha de conexão");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw e;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|