Ver Fonte

Versão em PRD

Elemar Deckmann há 1 ano atrás
pai
commit
5e1b29dcc1

+ 12 - 4
pom.xml

@@ -29,6 +29,14 @@
   </properties>
 
   <dependencies>
+
+    <dependency>
+      <groupId>com.fluig</groupId>
+      <artifactId>foundation-security-authentication-saml</artifactId>
+      <scope>compile</scope>
+      <version>1.5.13</version>
+    </dependency>
+
     <dependency>
       <groupId>com.fluig</groupId>
       <artifactId>fluig-sdk-api</artifactId>
@@ -66,14 +74,14 @@
       <groupId>com.fasterxml.jackson.core</groupId>
       <artifactId>jackson-core</artifactId>
       <version>2.16.1</version>
-  </dependency>
-  
+    </dependency>
+
     <dependency>
       <groupId>com.fasterxml.jackson.core</groupId>
       <artifactId>jackson-databind</artifactId>
       <version>2.16.1</version>
-  </dependency>
-  
+    </dependency>
+
   </dependencies>
 
   <build>

+ 63 - 46
src/main/java/com/ademicon/controller/ContratoController.java

@@ -24,8 +24,70 @@ public class ContratoController {
 
     private static ContratoService contratoService = new ContratoService();
 
+    @POST
+    @Path("/incluir")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response doPost(String contrato) throws Exception {
+        try {
+            return contratoService.incluir(contrato);
+
+        } catch (Exception e) {
+            System.out.println("ERRO API");
+            System.out.println(e);
+            return Response.status(Status.INTERNAL_SERVER_ERROR).build();
+        }
+    }
+
+    @POST
+    @Path("alterar")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response doAlterar(String contrato) {
+        try {
+            return contratoService.alterar(contrato);
+
+        } catch (Exception e) {
+            return Response.status(Status.INTERNAL_SERVER_ERROR).build();
+
+        }
+    }
+
+    @POST
+    @Path("baixar")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response doBaixar(String contrato) {
+        try {
+            return contratoService.baixar(contrato);
+
+        } catch (Exception e) {
+            return Response.status(Status.INTERNAL_SERVER_ERROR).build();
+
+        }
+    }
+
+    @PUT
+    @Path("/upload/numeroRegistroEletronico/{numeroRegistroEletronico}")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response doUpload(@PathParam(value = "numeroRegistroEletronico") String numeroRegistroEletronico,
+            String json) throws Exception {
+
+        try {
+            Response contratoResponse = contratoService.upload(numeroRegistroEletronico, json);
+            return Response.status(contratoResponse.getStatus()).entity(contratoResponse.getEntity()).build();
+
+        } catch (Exception e) {
+            System.out.println("FALHA RECEBIMENTO DE CONTRATO");
+            System.out.println(e);
+            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e).build();
+
+        }
+    }
+
     @GET
-    @Path("/")
+    @Path("/status")
     @Produces(MediaType.APPLICATION_JSON)
     @Consumes(MediaType.APPLICATION_JSON)
     public Response doGet() throws SDKException {
@@ -71,49 +133,4 @@ public class ContratoController {
         }
     }
 
-    @POST
-    @Path("/incluir")
-    @Consumes(MediaType.APPLICATION_JSON)
-    @Produces(MediaType.APPLICATION_JSON)
-    public Response doPost(String contrato) throws Exception {
-
-        try {
-            System.out.println("Chegou na API");
-            System.out.println(contrato);
-            Response contratoModel = contratoService.incluir(contrato);
-            System.out.println("PASSOU SERVIÇO");
-
-            return Response.status(contratoModel.getStatus()).entity(contratoModel.getEntity()).build();
-
-        } catch (Exception e) {
-            System.out.println("ERRO API");
-            System.out.println(e);
-            return Response.status(Status.INTERNAL_SERVER_ERROR).build();
-        }
-    }
-
-    @PUT
-    @Path("/upload/numeroRegistroEletronico/{numeroRegistroEletronico}")
-    @Consumes(MediaType.APPLICATION_JSON)
-    @Produces(MediaType.APPLICATION_JSON)
-    public Response doUpload(@PathParam(value = "numeroRegistroEletronico") String numeroRegistroEletronico,
-            String arquivo) throws Exception {
-        try {
-
-            System.out.println("RECEBIMENTO DE CONTRATO");
-            System.out.println(numeroRegistroEletronico);
-            System.out.println(arquivo);
-
-            Response contratoResponse = contratoService.upload(numeroRegistroEletronico, arquivo);
-            return Response.status(contratoResponse.getStatus()).entity(contratoResponse.getEntity()).build();
-            // return Response.status(Status.OK).entity("{\"status\":\"ok\"}").build();
-
-        } catch (Exception e) {
-            System.out.println("FALHA RECEBIMENTO DE CONTRATO");
-            System.out.println(e);
-            return Response.status(Status.INTERNAL_SERVER_ERROR).build();
-
-        }
-    }
-
 }

+ 9 - 28
src/main/java/com/ademicon/repository/ContratoRepository.java

@@ -2,44 +2,25 @@ package com.ademicon.repository;
 
 import java.sql.Connection;
 import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 
 import com.ademicon.model.ContratoResponse;
 import com.ademicon.service.ConnectionService;
 
 public class ContratoRepository {
-    private static Connection conn = null;
-    private static PreparedStatement statement = null;
-    private static ResultSet rs = null;
 
     public int incluir(ContratoResponse contrato) throws Exception {
-        try {
-            String queryString = "UPDATE ADE_006_CONTROLE_GARANTIA SET CONTRATO_DETRAN = ? WHERE CHASSI = ?";
-            conn = ConnectionService.getConnection();
-            statement = conn.prepareStatement(queryString);
-            statement.setString(1, contrato.getNumeroRegistroEletronico());
-            statement.setString(2, contrato.getChassi());
-            return statement.executeUpdate();
+
+        String queryString = "UPDATE ADE_006_CONTROLE_GARANTIA SET CONTRATO_DETRAN = ? WHERE CHASSI = ?";
+
+        try (Connection conn = ConnectionService.getConnection();
+                PreparedStatement preparedStatement = conn.prepareStatement(queryString);) {
+            preparedStatement.setString(1, contrato.getNumeroRegistroEletronico());
+            preparedStatement.setString(2, contrato.getChassi());
+            return preparedStatement.executeUpdate();
 
         } catch (Exception e) {
             throw e;
-
-        } finally {
-            try {
-                if (rs != null)
-                    rs.close();
-            } catch (Exception e) {
-            }
-            try {
-                if (statement != null)
-                    statement.close();
-            } catch (Exception e) {
-            }
-            try {
-                if (conn != null)
-                    conn.close();
-            } catch (Exception e) {
-            }
+            
         }
     }
 }

+ 12 - 29
src/main/java/com/ademicon/repository/auth/AccessRepository.java

@@ -10,45 +10,28 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
 
 public class AccessRepository {
-    private static Connection conn = null;
-    private static PreparedStatement statement = null;
-    private static ResultSet rs = null;
 
     public AccessModel find() throws Exception {
-        try {
-            String queryString = "SELECT PARAMETRO, VALOR_STR FROM ADE_006_PARM_CRED_VEIC WHERE PARAMETRO IN ('PLACETI_URL_BASE', 'PLACETI_URL_TOKEN', 'PLACETI_USER_TOKEN')";
-            conn = ConnectionService.getConnection();
-            statement = conn.prepareStatement(queryString);
-            rs = statement.executeQuery();
+
+        String queryString = "SELECT PARAMETRO, VALOR_STR FROM ADE_006_PARM_CRED_VEIC WHERE PARAMETRO IN ('PLACETI_URL_BASE', 'PLACETI_URL_TOKEN', 'PLACETI_USER_TOKEN')";
+
+        try (Connection conn = ConnectionService.getConnection();
+                PreparedStatement preparedStatement = conn.prepareStatement(queryString);
+                ResultSet resultSet = preparedStatement.executeQuery()) {
+
             String json = "{";
-            while (rs.next()) {
-                json += "\"" + rs.getString("PARAMETRO") + "\":\"" + rs.getString("VALOR_STR") + "\",";
+            while (resultSet.next()) {
+                json += "\"" + resultSet.getString("PARAMETRO") + "\":\"" + resultSet.getString("VALOR_STR") + "\",";
+
             }
             json = json.substring(0, json.lastIndexOf(",")) + "}";
             ObjectMapper mapper = new ObjectMapper();
             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-
             return mapper.readValue(json, AccessModel.class);
 
         } catch (Exception e) {
-            throw new Exception(e.getMessage());
-        } finally {
-            try {
-                if (rs != null)
-                    rs.close();
-            } catch (Exception e) {
-            }
-            try {
-                if (statement != null)
-                    statement.close();
-            } catch (Exception e) {
-            }
-            try {
-                if (conn != null)
-                    conn.close();
-            } catch (Exception e) {
-            }
+            throw e;
+
         }
     }
-
 }

+ 25 - 52
src/main/java/com/ademicon/repository/auth/TokenRepository.java

@@ -10,75 +10,48 @@ import com.ademicon.model.auth.TokenModel;
 import com.ademicon.service.ConnectionService;
 
 public class TokenRepository {
-    private static Connection conn = null;
-    private static PreparedStatement statement = null;
-    private static ResultSet rs = null;
 
     public Integer save(TokenModel token) throws Exception {
-        try {
-            String queryString = "UPDATE ADE_006_TOKEN_CONTROL SET ACCESS_TOKEN = ?, EXPIRES_IN = ? WHERE API = 'PLACETI'";
-            conn = ConnectionService.getConnection();
-            statement = conn.prepareStatement(queryString);
-            statement.setString(1, token.getAccess_token());
-            statement.setString(2, token.getExpires_in().toString().substring(0, 19));
-            return statement.executeUpdate();
+
+        String queryString = "UPDATE ADE_006_TOKEN_CONTROL SET ACCESS_TOKEN = ?, EXPIRES_IN = ? WHERE API = 'PLACETI'";
+
+        try (Connection conn = ConnectionService.getConnection();
+                PreparedStatement preparedStatement = conn.prepareStatement(queryString);) {
+            preparedStatement.setString(1, token.getAccess_token());
+            preparedStatement.setString(2, token.getExpires_in().toString().substring(0, 19));
+            return preparedStatement.executeUpdate();
 
         } catch (Exception e) {
-            throw new Exception();
+            throw e;
 
-        } finally {
-            try {
-                if (rs != null)
-                    rs.close();
-            } catch (Exception e) {
-            }
-            try {
-                if (statement != null)
-                    statement.close();
-            } catch (Exception e) {
-            }
-            try {
-                if (conn != null)
-                    conn.close();
-            } catch (Exception e) {
-            }
         }
     }
 
     public TokenModel find() throws Exception {
-        try {
-            String queryString = "SELECT * FROM ADE_006_TOKEN_CONTROL WHERE API = 'PLACETI'";
-            conn = ConnectionService.getConnection();
-            statement = conn.prepareStatement(queryString);
-            rs = statement.executeQuery();
+
+        String queryString = "SELECT * FROM ADE_006_TOKEN_CONTROL WHERE API = 'PLACETI'";
+
+        try (Connection conn = ConnectionService.getConnection();
+                PreparedStatement preparedStatement = conn.prepareStatement(queryString);
+                ResultSet resultSet = preparedStatement.executeQuery()) {
 
             TokenModel newTokenModel = new TokenModel();
-            if (rs.next()) {
-                newTokenModel.setAccess_token(rs.getString("ACCESS_TOKEN"));
-                LocalDateTime localDateTime = getLocalDateTime(rs.getString("EXPIRES_IN"));
+            if (resultSet.next()) {
+                newTokenModel.setAccess_token(resultSet.getString("ACCESS_TOKEN"));
+                LocalDateTime localDateTime = getLocalDateTime(resultSet.getString("EXPIRES_IN"));
+                newTokenModel.setExpires_in(localDateTime);
+
+            } else {
+                newTokenModel.setAccess_token("");
+                LocalDateTime localDateTime = getLocalDateTime("2000-01-01T00:00:00");
                 newTokenModel.setExpires_in(localDateTime);
+                
             }
             return newTokenModel;
 
         } catch (Exception e) {
-            throw new Exception(e.getMessage());
+            throw e;
 
-        } finally {
-            try {
-                if (rs != null)
-                    rs.close();
-            } catch (Exception e) {
-            }
-            try {
-                if (statement != null)
-                    statement.close();
-            } catch (Exception e) {
-            }
-            try {
-                if (conn != null)
-                    conn.close();
-            } catch (Exception e) {
-            }
         }
     }
 

+ 49 - 17
src/main/java/com/ademicon/service/ContratoService.java

@@ -19,6 +19,9 @@ import com.ademicon.service.auth.AccessService;
 import com.ademicon.service.auth.TokenService;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fluig.sdk.api.FluigAPI;
+import com.fluig.sdk.api.common.SDKException;
+import com.fluig.sdk.service.DocumentService;
 
 public class ContratoService {
 
@@ -29,12 +32,8 @@ public class ContratoService {
     public Response incluir(String contrato) throws Exception {
 
         AccessModel accessModel = accessService.find();
-
         String token = tokenService.getToken();
-        System.out.println("PLACE CONTRATO");
-        System.out.println(contrato);
-
-        Response response = ClientBuilder.newClient()
+        return ClientBuilder.newClient()
                 .target(accessModel.getPlaceti_url_base() + "/api/v1/contrato/incluir")
                 .request(MediaType.APPLICATION_JSON)
                 .header("Content-Type", "application/json;charset=UTF-8")
@@ -42,11 +41,38 @@ public class ContratoService {
                 .header("Authorization", "Bearer " + token)
                 .buildPost(Entity.entity(contrato, MediaType.APPLICATION_JSON)).invoke();
 
-        return response;
+    }
+
+    public Response alterar(String contrato) throws Exception {
+
+        AccessModel accessModel = accessService.find();
+        String token = tokenService.getToken();
+        return ClientBuilder.newClient()
+                .target(accessModel.getPlaceti_url_base() + "/api/v1/contrato/alterar")
+                .request(MediaType.APPLICATION_JSON)
+                .header("Content-Type", "application/json;charset=UTF-8")
+                .header("Accept", "application/json;charset=UTF-8")
+                .header("Authorization", "Bearer " + token)
+                .buildPost(Entity.entity(contrato, MediaType.APPLICATION_JSON)).invoke();
+
+    }
+
+    public Response baixar(String contrato) throws Exception {
+
+        AccessModel accessModel = accessService.find();
+        String token = tokenService.getToken();
+        return ClientBuilder.newClient()
+                .target(accessModel.getPlaceti_url_base() + "/api/v1/contrato/baixar")
+                .request(MediaType.APPLICATION_JSON)
+                .header("Content-Type", "application/json;charset=UTF-8")
+                .header("Accept", "application/json;charset=UTF-8")
+                .header("Authorization", "Bearer " + token)
+                .buildPost(Entity.entity(contrato, MediaType.APPLICATION_JSON)).invoke();
 
     }
 
-    public Response upload(String numeroRegistroEletronico, String arquivo) throws Exception {
+    public Response upload(String numeroRegistroEletronico, String arquivo)
+            throws Exception {
         try {
             AccessModel accessModel = accessService.find();
 
@@ -58,36 +84,36 @@ public class ContratoService {
 
             String boundary = "===" + System.currentTimeMillis() + "===";
             URL url = new URL(
-                    accessModel.getPlaceti_url_base() + "/api/v1/contrato/upload/numeroRegistroEletronico/"
+                    accessModel.getPlaceti_url_base() +
+                            "/api/v1/contrato/upload/numeroRegistroEletronico/"
                             + numeroRegistroEletronico);
 
             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
             conn.setRequestMethod("PUT");
             conn.setRequestProperty("Accept-Charset", "UTF-8");
             conn.setUseCaches(false);
-            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
+            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" +
+                    boundary);
             conn.setRequestProperty("User-Agent", "CodeJava Agent");
             conn.setDoInput(true);
             conn.setDoOutput(true);
             conn.setRequestProperty("Authorization", "Bearer " + token);
-            
 
             /////////////////////////////////////////
             OutputStream outputStream = conn.getOutputStream();
-            PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
+            PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream,
+                    "UTF-8"), true);
 
             writer.append("--" + boundary).append("\r\n");
-            writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"contrato.pdf\"")
-                    .append("\r\n");
+            writer.append("Content-Disposition: form-data; name=\"file\";filename=\"contrato.pdf\"").append("\r\n");
             writer.append("Content-Type: application/octet-stream").append("\r\n");
             writer.append("Content-Transfer-Encoding: binary").append("\r\n");
             writer.append("\r\n");
             writer.flush();
 
-            byte[] bytes = file.get("file").asText().getBytes("UTF-8");
-
+            Integer idArq = file.get("file").asInt();
+            byte[] bytes = getDocumentService().getDocumentContentAsBytes(idArq);
             outputStream.write(bytes);
-
             outputStream.flush();
 
             // Fim do campo de arquivo
@@ -110,7 +136,8 @@ public class ContratoService {
                 retCreateUser += (char) c;
             }
 
-            System.out.println(String.format("RESPONSE: %d - %s: data: %s", conn.getResponseCode(),
+            System.out.println(String.format("RESPONSE: %d - %s: data: %s",
+                    conn.getResponseCode(),
                     conn.getResponseMessage(), retCreateUser));
 
             if (conn.getResponseCode() == 200) {
@@ -129,4 +156,9 @@ public class ContratoService {
 
         }
     }
+
+    private DocumentService getDocumentService() throws SDKException {
+        return new FluigAPI().getDocumentService();
+
+    }
 }

+ 0 - 2
src/main/webapp/WEB-INF/jboss-web.xml

@@ -3,7 +3,6 @@
     <context-root>/placeti</context-root>
     <disable-cross-context>false</disable-cross-context>
     <security-domain>TOTVSTech</security-domain>
-
     <security-role>
         <role-name>user</role-name>
         <principal-name>totvstech</principal-name>
@@ -20,7 +19,6 @@
         <role-name>admin</role-name>
         <principal-name>wcmadmin</principal-name>
     </security-role>
-
     <valve>
         <class-name>com.totvs.technology.auth.saml.TOTVSAuthenticatorValve</class-name>
     </valve>

+ 0 - 1
src/main/webapp/WEB-INF/web.xml

@@ -14,7 +14,6 @@
             <role-name>user</role-name>
         </auth-constraint>
     </security-constraint>
-
     <login-config>
         <auth-method>FORM</auth-method>
         <realm-name>TOTVSTech</realm-name>