feat: chatRCT

feature/issue-14/chatrct
Frédérik Benoist 2024-02-12 23:33:21 +01:00
parent 60b8d222b4
commit 851a927265
8 changed files with 200 additions and 26 deletions

View File

@ -0,0 +1,15 @@
meta {
name: PromptDistributor
type: http
seq: 3
}
get {
url: {{host}}/hdpos/api/chatrct/distributor/1?itemId=XY10000-08-3A
body: none
auth: none
}
query {
itemId: XY10000-08-3A
}

View File

@ -0,0 +1,11 @@
meta {
name: PromptRCT
type: http
seq: 2
}
get {
url: {{host}}/hdpos/api/chatrct/rct/XY10000-08-3A
body: none
auth: none
}

11
bruno/chatrct/hello.bru Normal file
View File

@ -0,0 +1,11 @@
meta {
name: Hello
type: http
seq: 1
}
get {
url: {{host}}/hdpos/api/chatrct/hello
body: none
auth: none
}

View File

@ -0,0 +1,132 @@
package com.example.services;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@Path("/chatrct")
public class ChatRctService {
private static final Logger logger = LoggerFactory.getLogger(ChatRctService.class);
@GET
@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
public String getHello() {
return "# Bonjour'\\nIndiquez-moi la RCT sur laquelle vous souhaitez faire des recherches ...";
}
@GET
@Path("/rct/{itemId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getRctById(@PathParam("itemId") String itemId) {
if (itemId == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("{\"error\":\"itemId parameter is required\"}").build();
}
DriverManager.setLoginTimeout(5); // Set timeout to 5 seconds
try (DatabaseConnectDOTSOFT databaseConnection = new DatabaseConnectDOTSOFT("omni")) {
String call = "{ call fbe_hdpos.chatrct_prompt_rct(?, ?) }";
logger.info(call);
try (CallableStatement callableStatement = databaseConnection.getConnection().prepareCall(call)) {
// Set input parameters
callableStatement.setString(1, itemId);
// Set output parameters
callableStatement.registerOutParameter(2, Types.CLOB);
// Execute the stored procedure
callableStatement.execute();
// Retrieve results
Clob resultClob = callableStatement.getClob(2);
String result = resultClob.getSubString(1, (int) resultClob.length());
ObjectMapper objectMapper = new ObjectMapper();
String jsonResponse = objectMapper.writeValueAsString(result);
return Response.ok(jsonResponse).build();
}
} catch (SQLException e) {
e.printStackTrace();
String errorResponse = "{\"error\":\"" + e.getMessage() + "\"}";
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorResponse).build();
} catch (JsonProcessingException e) {
e.printStackTrace(); // Gérer les exceptions correctement dans un environnement de production
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Error processing JSON\"}").build();
}
}
@GET
@Path("/distributor/{distributorId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getDistributorById(
@PathParam("distributorId") String distributorId,
@QueryParam("itemId") String itemId,
@QueryParam("produitId") String produitId) {
if (distributorId == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("{\"error\":\"distributorId parameter is required\"}").build();
}
if (itemId == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("{\"error\":\"itemId parameter is required\"}").build();
}
DriverManager.setLoginTimeout(5); // Set timeout to 5 seconds
try (DatabaseConnectDOTSOFT databaseConnection = new DatabaseConnectDOTSOFT("omni")) {
String call = "{ call fbe_hdpos.chatrct_prompt_distributor(?,?,?,?) }";
logger.info(call);
try (CallableStatement callableStatement = databaseConnection.getConnection().prepareCall(call)) {
// Set input parameters
callableStatement.setString(1, distributorId);
callableStatement.setString(2, itemId);
callableStatement.setString(3, produitId);
// Set output parameters
callableStatement.registerOutParameter(4, Types.CLOB);
// Execute the stored procedure
callableStatement.execute();
// Retrieve results
Clob resultClob = callableStatement.getClob(4);
String result = resultClob.getSubString(1, (int) resultClob.length());
ObjectMapper objectMapper = new ObjectMapper();
String jsonResponse = objectMapper.writeValueAsString(result);
return Response.ok(jsonResponse).build();
}
} catch (SQLException e) {
e.printStackTrace();
String errorResponse = "{\"error\":\"" + e.getMessage() + "\"}";
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorResponse).build();
} catch (JsonProcessingException e) {
e.printStackTrace(); // Gérer les exceptions correctement dans un environnement de production
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Error processing JSON\"}").build();
}
}
}

View File

@ -7,7 +7,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

View File

@ -1,8 +1,5 @@
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;
@ -14,10 +11,12 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import org.json.JSONObject;
import org.slf4j.Logger;
@ -48,6 +47,9 @@ public class ProximisService {
try (DatabaseConnectOBI databaseConnection = new DatabaseConnectOBI()) {
String query = "SELECT authorization_value FROM obi.obi_param_access_token WHERE authorization_header = ?"
+ " and cloud_type = 'PROXIMIS' AND application = 'OBI_ACCESS_TOKEN' AND version = 'V2'";
logger.info(query);
PreparedStatement stmt = databaseConnection.getConnection().prepareStatement(query);
stmt.setString(1, "Authorization");
@ -61,28 +63,29 @@ public class ProximisService {
if (bearerToken == null || bearerToken.isEmpty()) {
return Response.status(Response.Status.NOT_FOUND).entity("{\"error\":\"Empty bearer token\"}").build();
} else {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url + "/project/fulfillment/" + orderCode))
Client client = ClientBuilder.newClient();
WebTarget target = client.target(url + "/project/fulfillment/" + orderCode);
Response response = target.request(MediaType.APPLICATION_JSON)
.header("Authorization", bearerToken)
.GET()
.build();
.get();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
JSONObject commonObject = new JSONObject();
commonObject.put("urlApp", urlApp);
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("common", commonObject);
jsonResponse.put("oms", new JSONObject(response.body()));
return Response.ok(jsonResponse.toString()).build();
} else if (response.statusCode() == 401) {
return Response.status(Response.Status.UNAUTHORIZED).entity("{\"error\":\"Invalid token\"}").build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity("{\"error\":\"Failed to get order detail\"}").build();
try {
if (response.getStatus() == 200) {
JSONObject commonObject = new JSONObject();
commonObject.put("urlApp", urlApp);
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("common", commonObject);
jsonResponse.put("oms", new JSONObject(response.readEntity(String.class)));
return Response.ok(jsonResponse.toString()).build();
} else if (response.getStatus() == 401) {
return Response.status(Response.Status.UNAUTHORIZED).entity("{\"error\":\"Invalid token\"}").build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity("{\"error\":\"Failed to get order detail\"}").build();
}
} catch (Exception e) {
return Response.serverError().entity("{\"error\":\"Failed to communicate with server\"}").build();
}
}
} catch (Exception e) {

View File

@ -2,16 +2,19 @@
dev.dotsoft.db.url=jdbc:oracle:thin:@v-aspd-b01-irdc.adic.lan:1521/MASPDI
dev.dotsoft.db.oai.password=base
dev.dotsoft.db.com02.password=B1Xto9pAbtBCOxuecG7W
dev.dotsoft.db.omni.password=k5.Omni.Paspdi
# Pre-production DOTSOFT environment settings
preprod.dotsoft.db.url=jdbc:oracle:thin:@v-aspd-b01-ii-d.adic.lan:1521/IASPDI
preprod.dotsoft.db.oai.password=base
preprod.dotsoft.db.com02.password=B1Xto9pAbtBCOxuecG7W
preprod.dotsoft.db.omni.password=k5.Omni.Paspdi
# Production DOTSOFT environment settings
prod.dotsoft.db.url=jdbc:oracle:thin:@v-aspd-b03-ip-d.adic.lan:1521/PASPDI
prod.dotsoft.db.oai.password=base
prod.dotsoft.db.com02.password=com20
prod.dotsoft.db.omni.password=k5.Omni.Paspdi
# XSTORE environment settings
xstore.db.url=jdbc:oracle:thin:@HOST:1521/XSTORE

View File

@ -1,2 +1,2 @@
# Indicates the current environment (dev, preprod, prod, etc.).
environment=prod
environment=preprod