refactor: Improve QueryNeo4JSolver performance and readability
This commit is contained in:
@@ -52,41 +52,57 @@ public class QueryNeo4JSolver extends StepSolver {
|
||||
logger.info("Solving step: " + this.step.getName());
|
||||
loadParameters();
|
||||
logParameters();
|
||||
StringBuilder resultString = new StringBuilder();
|
||||
|
||||
|
||||
try (Session session = graphDriver.session()) {
|
||||
|
||||
String query = this.cypherQuery;
|
||||
Result result = session.run(query);
|
||||
while(result.hasNext()){
|
||||
Record record = result.next();
|
||||
|
||||
String output = formatResultToString(result);
|
||||
|
||||
Value nodeValue = record.get("sourceCode");
|
||||
String nodeString = nodeValue.toString();
|
||||
|
||||
if(nodeString.isEmpty()){
|
||||
nodeValue = record.get("output");
|
||||
nodeString = nodeValue.toString();
|
||||
}
|
||||
|
||||
if (resultString.length() > 0) {
|
||||
resultString.append(", ");
|
||||
}
|
||||
resultString.append(nodeString);
|
||||
}
|
||||
|
||||
this.scenarioExecution.getExecSharedMap().put(this.outputField, resultString.toString());
|
||||
this.scenarioExecution.getExecSharedMap().put(this.outputField, output);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error executing query: " + e.getMessage());
|
||||
this.scenarioExecution.getExecSharedMap().put(this.outputField, "Error executing query: " + e.getMessage());
|
||||
|
||||
}
|
||||
|
||||
return this.scenarioExecution;
|
||||
}
|
||||
|
||||
public static String formatResultToString(Result result) {
|
||||
StringBuilder resultString = new StringBuilder();
|
||||
|
||||
// Itera attraverso tutti i record nel StatementResult
|
||||
while (result.hasNext()) {
|
||||
Record record = result.next();
|
||||
|
||||
// Itera attraverso tutti i campi del record
|
||||
for (String key : record.keys()) {
|
||||
Value value = record.get(key);
|
||||
|
||||
// Aggiungi il nome del campo e il suo valore alla stringa risultante
|
||||
resultString.append(key).append(": ").append(value.toString()).append(", ");
|
||||
}
|
||||
|
||||
// Rimuovi l'ultima virgola e lo spazio
|
||||
if (resultString.length() > 0) {
|
||||
resultString.setLength(resultString.length() - 2);
|
||||
}
|
||||
|
||||
// Aggiungi una nuova riga per ogni record (opzionale)
|
||||
resultString.append("\n");
|
||||
}
|
||||
|
||||
// Rimuovi l'ultima nuova riga se presente
|
||||
if (resultString.length() > 0 && resultString.charAt(resultString.length() - 1) == '\n') {
|
||||
resultString.setLength(resultString.length() - 1);
|
||||
}
|
||||
|
||||
return resultString.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user