From eca1f6c9c25042ec0ba9d01c4c3dbeee1b9f95da Mon Sep 17 00:00:00 2001 From: r-monti <127495690+r-monti@users.noreply.github.com> Date: Fri, 19 Jan 2024 18:05:29 +0100 Subject: [PATCH] Bugfix for Sonarcloud --- .../java/controller/ExerciseEvaluator.java | 6 +++- src/main/java/model/DAO/DAOExercise.java | 31 +++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/controller/ExerciseEvaluator.java b/src/main/java/controller/ExerciseEvaluator.java index d7dfbf4..0d178d4 100644 --- a/src/main/java/controller/ExerciseEvaluator.java +++ b/src/main/java/controller/ExerciseEvaluator.java @@ -160,7 +160,11 @@ private int evaluateCrossword(String[][] execution, String[][] solution) { } } } - return (int)((right /total)*100); + if(total != 0){ + return (int)((right /total)*100); + } + + return 0; } private int evaluateAudio(int exerciseId, int userId, Date d) throws IOException, ExecutionException, InterruptedException { diff --git a/src/main/java/model/DAO/DAOExercise.java b/src/main/java/model/DAO/DAOExercise.java index c9866fa..850d248 100644 --- a/src/main/java/model/DAO/DAOExercise.java +++ b/src/main/java/model/DAO/DAOExercise.java @@ -94,13 +94,14 @@ public Exercise getExerciseByPk(int userID, int exerciseID, Date insertDate) { public List retrieveNotDoneExercises(int patientId) { List exercises = new ArrayList<>(); + PreparedStatement stmt = null; try { connection = connection.isClosed() ? DAOConnection.getConnection() : connection; String query = "SELECT e.ID_exercise, e.ID_user, eg.ExerciseName, e.InsertionDate, eg.ExerciseDescription, e.Feedback, eg.Difficulty, eg.Target, eg.Type, e.Evaluation FROM exercise e" + " JOIN exercise_glossary eg ON e.ID_exercise = eg.ID_exercise" + " WHERE e.CompletionDate IS NULL AND e.ID_user = ? ORDER BY InsertionDate"; - PreparedStatement stmt = connection.prepareStatement(query); + stmt = connection.prepareStatement(query); stmt.setInt(1, patientId); ResultSet rs = stmt.executeQuery(); @@ -121,19 +122,27 @@ public List retrieveNotDoneExercises(int patientId) { } } catch(SQLException e) { logger.error("Error query", e); + } finally { + try { + if (stmt != null) stmt.close(); + DAOConnection.releaseConnection(connection); + } catch (SQLException e) { + logger.error("Error finally", e); + } } return exercises; } public List retrieveDoneExercises(int patientId) { List exercises = new ArrayList<>(); + PreparedStatement stmt = null; try { connection = connection.isClosed() ? DAOConnection.getConnection() : connection; String query = "SELECT e.ID_exercise, e.ID_user, eg.ExerciseName, e.InsertionDate, eg.ExerciseDescription, e.Feedback, eg.Difficulty, eg.Target, eg.Type, e.Evaluation FROM exercise e" + " JOIN exercise_glossary eg ON e.ID_exercise = eg.ID_exercise" + " WHERE e.CompletionDate IS NOT NULL AND e.ID_user = ?"; - PreparedStatement stmt = connection.prepareStatement(query); + stmt = connection.prepareStatement(query); stmt.setInt(1, patientId); ResultSet rs = stmt.executeQuery(); @@ -154,6 +163,13 @@ public List retrieveDoneExercises(int patientId) { } } catch(SQLException e) { logger.error("Error query", e); + } finally { + try { + if (stmt != null) stmt.close(); + DAOConnection.releaseConnection(connection); + } catch (SQLException e) { + logger.error("Error finally", e); + } } return exercises; } @@ -412,13 +428,14 @@ public boolean AddExerciseRecommendation(int idExercise, int idPatient) { public List getExerciseToApprove(int therapistId){ List exercises = new ArrayList<>(); + PreparedStatement stmt = null; try { connection = connection.isClosed() ? DAOConnection.getConnection() : connection; String query = "SELECT e.ID_exercise, e.ID_user, eg.ExerciseName, e.InsertionDate, eg.ExerciseDescription, e.Feedback, eg.Difficulty, eg.Target, eg.Type, e.Evaluation " + "FROM exercise e JOIN exercise_glossary eg ON e.ID_exercise = eg.ID_exercise JOIN user u ON e.ID_user = u.ID " + "WHERE e.CompletionDate IS NULL AND u.ID_Therapist = ? AND e.Recommended = 0"; - PreparedStatement stmt = connection.prepareStatement(query); + stmt = connection.prepareStatement(query); stmt.setInt(1, therapistId); ResultSet rs = stmt.executeQuery(); @@ -439,6 +456,14 @@ public List getExerciseToApprove(int therapistId){ } } catch(SQLException e) { logger.error("Error query", e); + } finally { + try { + if (stmt != null) stmt.close(); + DAOConnection.releaseConnection(connection); + } catch (SQLException e) { + // Handle the exception (e.g., log or throw) + logger.error("Error finally", e); + } } return exercises; }