hdpos/src/main/java/com/example/services/FluxService.java

115 lines
5.9 KiB
Java

package com.example.services;
import com.example.services.flux.bl.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Path("/flux")
public class FluxService {
private static final Logger logger = LoggerFactory.getLogger(DatabaseConnectDOTSOFT.class);
/**
* Retrieves the FluxBlNotSent object from the server.
*
* @return the FluxBlNotSent object obtained from the server
*/
@GET
@Path("/bl/notsent")
@Produces(MediaType.APPLICATION_JSON)
public Response getFluxBlNotSent() {
DriverManager.setLoginTimeout(5); // Définir le timeout à 5 secondes
try (DatabaseConnectDOTSOFT databaseConnection = new DatabaseConnectDOTSOFT("com02")) {
String query = "WITH err_bl AS (" +
"SELECT id_bon_livraison FROM omni.aspd_xsto_adrr_bl WHERE job_id = (" +
"SELECT MAX(job_id) FROM omni.aspd_xsto_adrr_bl))" +
"SELECT DISTINCT s.id_distrib, dis.libelle AS libelle_dis, s.id_structure, TRIM(s.nom) AS nom, s.enseigne, mq.code AS marque, m.code_saison, p.code_reference AS ref_r," +
"p.code_reference || '-' || pavc.code_externe AS ref_rc, p.code_reference || '-' || pavc.code_externe || '-' || TRIM(p.troc_axe2) AS ref_rct, p.nom AS nom_produit," +
"p.id_produit, bl.code_externe, bl.id_bon_livraison, bl.fdate AS date_bl, bl.id_structure AS id_expediteur, TRIM(st2.nom) AS expediteur, bl.remarques " +
"FROM COM02.bon_livraison bl " +
"JOIN err_bl ON err_bl.id_bon_livraison = bl.id_bon_livraison " +
"JOIN COM02.structure s ON s.id_structure = bl.id_destinataire " +
"LEFT OUTER JOIN COM02.STRUCTURE st2 ON st2.id_structure = bl.id_structure " +
"JOIN COM02.distributeur dis ON dis.id_distrib = s.id_distrib " +
"JOIN COM02.bon_livraison_colis blc ON blc.id_bordereau = bl.id_bon_livraison " +
"JOIN COM02.bon_livraison_colis_ligne blcl ON blcl.id_colis = blc.id_colis " +
"JOIN COM02.produit p ON p.id_produit = blcl.id_produit " +
"JOIN COM02.param_axe_valeur pavc ON pavc.id = p.id_type_axe_valeur1 " +
"JOIN COM02.modele m ON m.id_modele = p.code_modele " +
"JOIN COM02.marque mq ON mq.id_marque = m.id_marque " +
"LEFT OUTER JOIN OMNI.xst_produit xp ON (xp.id_distrib = s.id_distrib AND xp.id_produit = blcl.id_produit) " +
"WHERE bl.date_envoi_bl_numerique IS NULL AND blc.id_etat = 4 AND blcl.quantite != 0 AND xp.id_produit IS NULL " +
"ORDER BY s.id_distrib, dis.libelle, s.id_structure, TRIM(s.nom), m.code_saison";
logger.info(query);
try (PreparedStatement statement = databaseConnection.getConnection().prepareStatement(query)) {
ResultSet resultSet = statement.executeQuery();
List<FluxBlNotSent> blNotSentList = new ArrayList<>();
while (resultSet.next()) {
FluxBlNotSent blnotsent = mapResultSetToBlNotSent(resultSet);
blNotSentList.add(blnotsent);
}
ObjectMapper objectMapper = new ObjectMapper();
String jsonResponse;
try {
jsonResponse = objectMapper.writeValueAsString(blNotSentList);
return Response.ok(jsonResponse).build();
} catch (JsonProcessingException e) {
e.printStackTrace();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"Error processing JSON\"}").build();
}
}
} catch (SQLException e) {
e.printStackTrace();
String errorResponse = "{\"error\":\"" + e.getMessage() + "\"}";
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorResponse).build();
}
}
private FluxBlNotSent mapResultSetToBlNotSent(ResultSet resultSet) throws SQLException {
FluxBlNotSent blNotSent = new FluxBlNotSent();
blNotSent.setIdDistrib(resultSet.getInt("id_distrib"));
blNotSent.setLibelleDis(resultSet.getString("libelle_dis"));
blNotSent.setIdStructure(resultSet.getInt("id_structure"));
blNotSent.setNomStructure(resultSet.getString("nom"));
blNotSent.setEnseigne(resultSet.getString("enseigne"));
blNotSent.setMarque(resultSet.getString("marque"));
blNotSent.setCodeSaison(resultSet.getString("code_saison"));
blNotSent.setRefR(resultSet.getString("ref_r"));
blNotSent.setRefRc(resultSet.getString("ref_rc"));
blNotSent.setRefRct(resultSet.getString("ref_rct"));
blNotSent.setNomProduit(resultSet.getString("nom_produit"));
blNotSent.setIdProduit(resultSet.getInt("id_produit"));
blNotSent.setCodeExterne(resultSet.getString("code_externe"));
blNotSent.setIdBonLivraison(resultSet.getInt("id_bon_livraison"));
blNotSent.setDateBl(resultSet.getDate("date_bl"));
blNotSent.setIdExpediteur(resultSet.getInt("id_expediteur"));
blNotSent.setExpediteur(resultSet.getString("expediteur"));
blNotSent.setRemarques(resultSet.getString("remarques"));
return blNotSent;
}
}