I would like to see if it is possible to optimize the following code. Indeed before I was in php and the execution was much faster. but I had to switch to java and unfortunately the execution of the code takes twice as long. Does anyone have a solution to give me to improve this code.
This code is used to exploit a resultset to feed a database.
private String queryExtractor(JdbcTemplate template, Integer idExecution, String replaceSql, RequeteLite req ){
return (String) template.query(replaceSql, new ResultSetExtractor() {
int idRequete = req.getIdRequete();
@Override
public Integer extractData(ResultSet rs) throws SQLException, DataAccessException {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
//parcourir nb de ligne
while(rs.next()){
String cleAno="";
cleAno= String.format("%sid_requete=%s", cleAno,req.getIdRequete());
Map<String,String> hmap = new HashMap<>();
//parcourir nb de colonne
for (int j = 1; j <= columnCount ; j++) {
String colName = rsmd.getColumnName(j);
Integer coltype = rsmd.getColumnType(j);
boolean test = coltype.equals(java.sql.Types.DOUBLE);
if(test){
Double colValeur = rs.getDouble(j);
String valeur = colValeur.toString();
if("".equals(valeur) || valeur == null){
valeur = "NULL";
}
hmap.put(colName, valeur);
}else{
String colValeur = rs.getString(j);
if("".equals(colValeur) || colValeur == null){
colValeur = "NULL";
}
hmap.put(colName, colValeur);
}
}
Map<String,String> sortedMap = new TreeMap<>(hmap);
for(Map.Entry<String, String> entry : sortedMap.entrySet()) {
cleAno = String.format("%s&%s=%s", cleAno,entry.getKey(),entry.getValue());
}
//On vérifie si l'anomalie existe déja.
if(!ligneAnomalieDAO.existAnomalie(cleAno)){
//L'anomalie n'existe pas
//On insère l'anomalie
LigneAnomalie ligneAno = new LigneAnomalie(idRequete, cleAno);
Integer idLigneAnomalie = ligneAnomalieManager.addLigne(ligneAno);
//On insère les champs dans la table champLigneAnomalie
for(Map.Entry<String, String> entry : sortedMap.entrySet()) {
ChampLigneAnomalie champLigneAno = new ChampLigneAnomalie(idLigneAnomalie, entry.getKey(), entry.getValue());
champligneAnoManager.addChamp(champLigneAno);
}
}else{
//l'anomalie existe
//On vérifie si l'anomalie possède une justification
Integer idLigneAno = ligneAnomalieDAO.findLigneByCle(cleAno).getIdLigneAnomalie();
if(justificationDAO.existJustification(idLigneAno)){
//L'anomalie possède une justification
JustificationAnomalie justificationAno = justificationDAO.findJustifByIdLigneAnomalie(idLigneAno);
if(justificationAno.getType().equals("correction")){
// la justification est une correction, on annule la correction car il ne devrait pas avoir d'erreur si la ligne est corrigée
justificationAno.setCommentaire("L annomalie n a pas été corrigée (La requête "+ req.getTitre() +" a de nouveau rencontré l anomalie)");
justificationAno.setValidJustification(0);
justificationDAO.updateJustif(justificationAno);
}
}
}
Integer idLigneAno = ligneAnomalieDAO.findLigneByCle(cleAno).getIdLigneAnomalie();
LLigneAnomalieExecutionJeu lligneAnoExe = new LLigneAnomalieExecutionJeu(idExecution, idLigneAno);
lligneAnoExeManager.addLLAE(lligneAnoExe);
}
return null;
}
});
}