diff --git a/src/main/java/model/DAO/DAOExercise.java b/src/main/java/model/DAO/DAOExercise.java index ae2f755..e59658d 100644 --- a/src/main/java/model/DAO/DAOExercise.java +++ b/src/main/java/model/DAO/DAOExercise.java @@ -2,6 +2,7 @@ import model.entity.Exercise; import model.entity.Schedule; +import model.entity.SlimmerExercise; import java.sql.*; import java.util.ArrayList; @@ -87,69 +88,55 @@ public Exercise getExerciseByPk(int userID, int exerciseID, Date insertDate) { return null; } - public List retrieveAllNewPatientExercise(int userID) { - String query = "SELECT * FROM exercise WHERE ID_user = ? ORDER BY InsertionDate DESC;"; - PreparedStatement preparedStatement = null; - ResultSet resultSet = null; - List exercises = new ArrayList<>(); - + public List retrieveNotDoneExercises(int patientId) { + List exercises = new ArrayList<>(); try { connection = connection.isClosed() ? DAOConnection.getConnection() : connection; - preparedStatement = connection.prepareStatement(query); - preparedStatement.setInt(1, userID); - - resultSet = preparedStatement.executeQuery(); - - while (resultSet.next()) { - Exercise exercise = extractExerciseFromResultSet(resultSet); + String query = "SELECT e.ID_exercise, eg.ExerciseName, e.InsertionDate FROM exercise e" + + " JOIN exercise_glossary eg ON e.ID_exercise = eg.ID_exercise" + + " WHERE e.CompletionDate IS NULL AND e.ID_user = ?"; + + PreparedStatement stmt = connection.prepareStatement(query); + stmt.setInt(1, patientId); + ResultSet rs = stmt.executeQuery(); + + while(rs.next()) { + SlimmerExercise exercise = new SlimmerExercise( + rs.getInt("ID_exercise"), + rs.getString("ExerciseName"), + rs.getDate("InsertionDate") + ); exercises.add(exercise); } - - } catch (SQLException e) { + } catch(SQLException e) { e.printStackTrace(); - } finally { - try { - if (resultSet != null) resultSet.close(); - if (preparedStatement != null) preparedStatement.close(); - DAOConnection.releaseConnection(connection); - } catch (SQLException e) { - e.printStackTrace(); - } } - return exercises; } - public List retrieveAllNewPatientExerciseNotDone(int userID) { - String query = "SELECT * FROM exercise WHERE ID_user = ? AND CompletionDate IS NULL ORDER BY InsertionDate DESC;"; - PreparedStatement preparedStatement = null; - ResultSet resultSet = null; - List exercises = new ArrayList<>(); - + public List retrieveDoneExercises(int patientId) { + List exercises = new ArrayList<>(); try { connection = connection.isClosed() ? DAOConnection.getConnection() : connection; - preparedStatement = connection.prepareStatement(query); - preparedStatement.setInt(1, userID); - - resultSet = preparedStatement.executeQuery(); - - while (resultSet.next()) { - Exercise exercise = extractExerciseFromResultSet(resultSet); + String query = "SELECT e.ID_exercise, eg.ExerciseName, e.InsertionDate 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.setInt(1, patientId); + ResultSet rs = stmt.executeQuery(); + + while(rs.next()) { + SlimmerExercise exercise = new SlimmerExercise( + rs.getInt("ID_exercise"), + rs.getString("ExerciseName"), + rs.getDate("InsertionDate") + ); exercises.add(exercise); } - - } catch (SQLException e) { + } catch(SQLException e) { e.printStackTrace(); - } finally { - try { - if (resultSet != null) resultSet.close(); - if (preparedStatement != null) preparedStatement.close(); - DAOConnection.releaseConnection(connection); - } catch (SQLException e) { - e.printStackTrace(); - } } - return exercises; } diff --git a/src/main/java/model/entity/SlimmerExercise.java b/src/main/java/model/entity/SlimmerExercise.java new file mode 100644 index 0000000..d202211 --- /dev/null +++ b/src/main/java/model/entity/SlimmerExercise.java @@ -0,0 +1,39 @@ +package model.entity; + +import java.sql.Date; + +public class SlimmerExercise { + private int id; + private String name; + private Date insertionDate; + + public SlimmerExercise(int id, String name, Date insertionDate) { + this.id = id; + this.name = name; + this.insertionDate = insertionDate; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Date getInsertionDate() { + return insertionDate; + } + + public void setInsertionDate(Date insertionDate) { + this.insertionDate = insertionDate; + } +} diff --git a/src/main/java/model/service/exercise/ExerciseManager.java b/src/main/java/model/service/exercise/ExerciseManager.java index 3733a96..317b77f 100644 --- a/src/main/java/model/service/exercise/ExerciseManager.java +++ b/src/main/java/model/service/exercise/ExerciseManager.java @@ -4,6 +4,7 @@ import model.DAO.DAOExerciseGlossary; import model.entity.Exercise; import model.entity.ExerciseGlossary; +import model.entity.SlimmerExercise; import java.sql.Blob; import java.sql.Date; @@ -31,15 +32,15 @@ public boolean saveEvaluation(int userID, int exerciseId, Date insertDate, int e return daoE.setExerciseEvaluation(userID, exerciseId, insertDate, evaluation); } - public List retrieveAllNewPatientExercise(int userID){ - return daoE.retrieveAllNewPatientExercise(userID); - } - public List retrieveAllPatientExerciseDone(int userID){ return daoE.retrieveAllPatientExerciseDone(userID); } - public List retrieveAllNewPatientExerciseNotDone(int userID){ - return daoE.retrieveAllNewPatientExerciseNotDone(userID); + public List retrieveDoneExercises(int patientId) { + return daoE.retrieveDoneExercises(patientId); + } + + public List retrieveNotDoneExercises(int patientId) { + return daoE.retrieveNotDoneExercises(patientId); } } diff --git a/src/main/webapp/JSP/homePagePatient.jsp b/src/main/webapp/JSP/homePagePatient.jsp index 12675d8..260df25 100644 --- a/src/main/webapp/JSP/homePagePatient.jsp +++ b/src/main/webapp/JSP/homePagePatient.jsp @@ -1,20 +1,18 @@ <%@ page import="model.service.exercise.ExerciseManager" %> -<%@ page import="model.entity.Exercise" %> <%@ page import="java.util.List" %> -<%@ page import="model.entity.ExerciseGlossary" %> <%@ page import="model.entity.PersonalInfo" %> <%@ page import="model.service.user.UserRegistry" %> +<%@ page import="model.entity.SlimmerExercise" %> <% - Integer userIdp = (Integer) session.getAttribute("id"); - if(userIdp == null) { + if(session.getAttribute("id") == null) { response.sendRedirect("../errorPage/403.html"); } else { int userId = (Integer) session.getAttribute("id"); UserRegistry ur = new UserRegistry(); - PersonalInfo data= ur.getPersonalInfo(userIdp); + PersonalInfo data= ur.getPersonalInfo(userId); %> <%@page contentType="text/html;charset=UTF-8"%> @@ -50,41 +48,6 @@ - @@ -102,14 +65,13 @@
<% - ExerciseManager exerciseManager1 = new ExerciseManager(); - List list1 = exerciseManager1.retrieveAllNewPatientExerciseNotDone(userId); - ExerciseGlossary exerciseGlossary1 = new ExerciseGlossary(); + ExerciseManager exerciseManager = new ExerciseManager(); + List list1 = exerciseManager.retrieveNotDoneExercises(userId); + if(!list1.isEmpty()){ int maxCardsToShow = 5; int cardCounter = 0; - for(Exercise exercise : list1) { - exerciseGlossary1 = exerciseManager1.getExercise(exercise.getIdExercise()); + for(SlimmerExercise exercise : list1) { %>
@@ -117,9 +79,9 @@
-
<%=exerciseGlossary1.getExerciseName()%>
+
<%=exercise.getName()%>
- + @@ -157,14 +119,11 @@
<% - ExerciseManager exerciseManager2 = new ExerciseManager(); - List list2 = exerciseManager2.retrieveAllPatientExerciseDone(userId); - ExerciseGlossary exerciseGlossary2 = new ExerciseGlossary(); + List list2 = exerciseManager.retrieveDoneExercises(userId); if(!list2.isEmpty()){ int maxCardsToShow = 5; int cardCounter = 0; - for(Exercise exercise : list2) { - exerciseGlossary2 = exerciseManager2.getExercise(exercise.getIdExercise()); + for(SlimmerExercise exercise : list2) { %>
@@ -172,11 +131,11 @@
-
<%=exerciseGlossary2.getExerciseName()%>
+
<%=exercise.getName()%>
- + - + <% @@ -220,7 +179,4 @@ - -<% - } -%> \ No newline at end of file +<% } %> \ No newline at end of file