From 6016ce0dabece166ce06662e72258ab15f92a246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9rik=20BENOIST?= Date: Wed, 10 Jan 2024 22:06:11 +0100 Subject: [PATCH] feat: obi and proximis --- pom.xml | 7 +- .../example/services/DatabaseConnectOBI.java | 16 ++- .../com/example/services/OrderService.java | 54 +++++++++- .../com/example/services/ProximisService.java | 102 ++++++++++++++++++ src/main/resources/api.properties | 5 + src/main/resources/db.properties | 8 +- 6 files changed, 175 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/example/services/ProximisService.java create mode 100644 src/main/resources/api.properties diff --git a/pom.xml b/pom.xml index c413cae..ad30694 100644 --- a/pom.xml +++ b/pom.xml @@ -64,13 +64,18 @@ com.fasterxml.jackson.jaxrs jackson-jaxrs-json-provider - 2.13.0 + 2.13.0 org.glassfish.jersey.media jersey-media-json-jackson 2.13 + + org.json + json + 20231013 + diff --git a/src/main/java/com/example/services/DatabaseConnectOBI.java b/src/main/java/com/example/services/DatabaseConnectOBI.java index 30607f8..101d883 100644 --- a/src/main/java/com/example/services/DatabaseConnectOBI.java +++ b/src/main/java/com/example/services/DatabaseConnectOBI.java @@ -15,25 +15,23 @@ public class DatabaseConnectOBI implements AutoCloseable { private Connection connection; - public DatabaseConnectOBI(String username) throws SQLException { + public DatabaseConnectOBI() throws SQLException { String environment = loadEnvironment(); - //TODO TO DELETE - environment="preprod"; + environment = "preprod"; //TODO ATTENTION !!! try { Properties dbProperties = loadDatabaseProperties(); - + String url = dbProperties.getProperty(environment + ".obi.db.url"); - String userpassword = dbProperties.getProperty(environment + ".obi.db." + username + ".password"); - logger.info(url); - logger.info(username); - logger.info(userpassword); + String username = dbProperties.getProperty(environment + ".obi.db.username"); + String userpassword = dbProperties.getProperty(environment + ".obi.db.password"); + connection = DriverManager.getConnection(url, username, userpassword); logger.info("OBI Connection OK for user " + username + " on environment " + environment); } catch (SQLException e) { - logger.error("Failed to connect to OBI database for user " + username + " on environment " + environment, e); + logger.error("Failed to connect to OBI database on environment " + environment, e); throw e; // re-throw the exception } } diff --git a/src/main/java/com/example/services/OrderService.java b/src/main/java/com/example/services/OrderService.java index 4e746b3..278d5bb 100644 --- a/src/main/java/com/example/services/OrderService.java +++ b/src/main/java/com/example/services/OrderService.java @@ -2,6 +2,7 @@ package com.example.services; import com.example.services.order.Order; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -35,6 +36,9 @@ import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import org.json.JSONObject; +import org.json.XML; + @Path("/obi") public class OrderService { private static final Logger logger = LoggerFactory.getLogger(OrderService.class); @@ -50,7 +54,7 @@ public class OrderService { DriverManager.setLoginTimeout(5); // Définir le timeout à 5 secondes - try (DatabaseConnectOBI databaseConnection = new DatabaseConnectOBI("mobi")) { + try (DatabaseConnectOBI databaseConnection = new DatabaseConnectOBI()) { String countQuery = "SELECT COUNT(*) FROM obi_order oo "; String query = "SELECT oo.request_id, " + @@ -133,7 +137,7 @@ public class OrderService { DriverManager.setLoginTimeout(5); // Définir le timeout à 5 secondes - try (DatabaseConnectOBI databaseConnection = new DatabaseConnectOBI("mobi")) { + try (DatabaseConnectOBI databaseConnection = new DatabaseConnectOBI()) { String orderQuery = "SELECT oo.request_id, oo.order_id, oo.requesting_location_cd, oo.requesting_system_cd, " + "oo.customer_id, oo.transaction_no, oo.transaction_type_id, oo.transaction_date, oo.status, " + "oo.submit_ord_msg, oo.fdate_creation, oo.consumed, oo.shipforpickup_location_cd, oo.shipforpickup_system_cd, " @@ -237,6 +241,48 @@ public class OrderService { } } + @GET + @Path("/order/{requestId}/orderMessage") + @Produces(MediaType.APPLICATION_JSON) + public Response getOrderMessage(@PathParam("requestId") Long requestId) throws SQLException { + DriverManager.setLoginTimeout(5); // Définir le timeout à 5 secondes + + try (DatabaseConnectOBI databaseConnection = new DatabaseConnectOBI()) { + String orderXmlQuery = "SELECT oo.submit_ord_msg " + + "FROM obi_order oo " + + "WHERE oo.request_id = ?"; + + logger.info(orderXmlQuery); + + try (PreparedStatement statement = databaseConnection.getConnection().prepareStatement(orderXmlQuery)) { + statement.setLong(1, requestId); + + try (ResultSet resultSet = statement.executeQuery()) { + if (resultSet.next()) { + String xmlString = resultSet.getString("SUBMIT_ORD_MSG"); + JSONObject jsonObject = XML.toJSONObject(xmlString); + + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(jsonObject.toString()); + + return Response.ok(jsonNode).build(); + } else { + return Response.status(Response.Status.NOT_FOUND).entity("{\"error\":\"No order found with the provided ID\"}").build(); + } + } catch (SQLException e) { + e.printStackTrace(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"" + e.getMessage() + "\"}").build(); + } + } catch (SQLException e) { + e.printStackTrace(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"" + e.getMessage() + "\"}").build(); + } + } catch (JsonProcessingException e) { + e.printStackTrace(); // Handle exceptions correctly in a production environment + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Error processing JSON\"}").build(); + } + } + private Order.History mapResultSetToOrderLinesHistory(ResultSet resultSet) throws SQLException { Order.History orderLinesHistory = new Order().new History(); @@ -292,7 +338,7 @@ public class OrderService { order.setMeta(meta); statusData.setCode(resultSet.getString("STATUS")); - statusData.setTitle("TEST"); + statusData.setTitle("TEST"); // TODO A VOIR common.setOrderId(resultSet.getString("ORDER_ID")); @@ -319,7 +365,7 @@ public class OrderService { order.setMeta(meta); statusData.setCode(resultSet.getString("STATUS")); - statusData.setTitle("TEST"); + statusData.setTitle("TEST"); // TODO A VOIR common.setOrderId(resultSet.getString("ORDER_ID")); common.setRequestingLocationCd(resultSet.getString("REQUESTING_LOCATION_CD")); diff --git a/src/main/java/com/example/services/ProximisService.java b/src/main/java/com/example/services/ProximisService.java new file mode 100644 index 0000000..ec5ffc1 --- /dev/null +++ b/src/main/java/com/example/services/ProximisService.java @@ -0,0 +1,102 @@ +package com.example.services; + +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.Properties; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Path("/proximis") +public class ProximisService { + private static final Logger logger = LoggerFactory.getLogger(ProximisService.class); + + @GET + @Path("/project/fulfillment/{orderCode}") + @Produces(MediaType.APPLICATION_JSON) + public Response getOrderDetail(@PathParam("orderCode") String orderCode) { + if (orderCode == null) { + return Response.status(Response.Status.BAD_REQUEST).entity("{\"error\":\"orderCode is required\"}").build(); + } + + String environment = loadEnvironment(); + + try { + Properties apiProperties = loadApiProperties(); + String url = apiProperties.getProperty(environment + ".proximis.api.url"); + String bearerToken = ""; + + DriverManager.setLoginTimeout(5); // Définir le timeout à 5 secondes + + try (DatabaseConnectOBI databaseConnection = new DatabaseConnectOBI()) { + String query = "SELECT authorization_value FROM obi.obi_param_access_token WHERE authorization_header = ?"; + PreparedStatement stmt = databaseConnection.getConnection().prepareStatement(query); + + stmt.setString(1, "Authorization"); + ResultSet rs = stmt.executeQuery(); + + if (rs.next()) { + bearerToken = rs.getString(1); + } + } + + if (bearerToken == null || bearerToken.isEmpty()) { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Failed to access proximis API\"}").build(); + } else { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(url + "/project/fulfillment/" + orderCode)) + .header("Authorization", bearerToken) + .GET() + .build(); + + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + if (response.statusCode() == 200) { + return Response.ok(response.body()).build(); + } else { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Failed to get order detail\"}").build(); + } + } + } catch (Exception e) { + logger.error("Failed to get order detail", e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Failed to get order detail\"}").build(); + } + } + + private String loadEnvironment() { + Properties envProperties = loadProperties("env.properties"); + return envProperties.getProperty("environment"); + } + + private Properties loadApiProperties() { + Properties apiProperties = loadProperties("api.properties"); + return apiProperties; + } + private Properties loadProperties(String fileName) { + Properties properties = new Properties(); + try (InputStream input = getClass().getClassLoader().getResourceAsStream(fileName)) { + properties.load(input); + } catch (IOException e) { + e.printStackTrace(); + // Handle the exception correctly for your application + } + return properties; + } + +} \ No newline at end of file diff --git a/src/main/resources/api.properties b/src/main/resources/api.properties new file mode 100644 index 0000000..1f8552a --- /dev/null +++ b/src/main/resources/api.properties @@ -0,0 +1,5 @@ +# Pre-production API Proximis +preprod.proximis.api.url=https://proximis-ikks-recette.stg.proximis.com/publicAPI.php/V2 + +# production API Proximis +prod.proximis.api.url=https://ikks.omn.proximis.com/publicAPI.php/V2 diff --git a/src/main/resources/db.properties b/src/main/resources/db.properties index 2259485..da3d0a4 100644 --- a/src/main/resources/db.properties +++ b/src/main/resources/db.properties @@ -24,9 +24,11 @@ prod.xadmin.db.dtv.password=oY3poRSprOuqasO # Pre-production OBI environment settings preprod.obi.db.url=jdbc:postgresql://v-tlnd-b01-iidc.tech.ikks.lan:5432/mirobi -preprod.obi.db.mobi.password=obi +preprod.obi.db.username=mobi +preprod.obi.db.password=obi # production OBI environment settings -prod.obi.db.url=FREDjdbc:postgresql://v-tlnd-b01-ipdc.tech.ikks.lan:5432/pirobi -prod.obi.db.pobi.password=FREDy!h`AGZjGVa.ae;(N +prod.obi.db.url=jdbc:postgresql://v-tlnd-b01-ipdc.tech.ikks.lan:5432/pirobi +prod.obi.db.username=pobi +prod.obi.db.password=y!h`AGZjGVa.ae;(N