Skip to content

Commit

Permalink
Improved patient's exercises loading time
Browse files Browse the repository at this point in the history
DAOExercise: modified method to slimmer ones
- retrieveDoneExercises
- retrieveNotDoneExercises

SlimmerExercise: class entity for getting smaller exercises data

ExerciseManager:
- modified methods according to DAO ones

homePagePatient: better managing of the loading of the exercises assigned to the users
  • Loading branch information
panuozzo77 committed Jan 9, 2024
1 parent aa277f6 commit 4acc9f3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 113 deletions.
83 changes: 35 additions & 48 deletions src/main/java/model/DAO/DAOExercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import model.entity.Exercise;
import model.entity.Schedule;
import model.entity.SlimmerExercise;

import java.sql.*;
import java.util.ArrayList;
Expand Down Expand Up @@ -87,69 +88,55 @@ public Exercise getExerciseByPk(int userID, int exerciseID, Date insertDate) {
return null;
}

public List<Exercise> retrieveAllNewPatientExercise(int userID) {
String query = "SELECT * FROM exercise WHERE ID_user = ? ORDER BY InsertionDate DESC;";
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Exercise> exercises = new ArrayList<>();

public List<SlimmerExercise> retrieveNotDoneExercises(int patientId) {
List<SlimmerExercise> 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<Exercise> 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<Exercise> exercises = new ArrayList<>();

public List<SlimmerExercise> retrieveDoneExercises(int patientId) {
List<SlimmerExercise> 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;
}

Expand Down
39 changes: 39 additions & 0 deletions src/main/java/model/entity/SlimmerExercise.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 7 additions & 6 deletions src/main/java/model/service/exercise/ExerciseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -31,15 +32,15 @@ public boolean saveEvaluation(int userID, int exerciseId, Date insertDate, int e
return daoE.setExerciseEvaluation(userID, exerciseId, insertDate, evaluation);
}

public List<Exercise> retrieveAllNewPatientExercise(int userID){
return daoE.retrieveAllNewPatientExercise(userID);
}

public List<Exercise> retrieveAllPatientExerciseDone(int userID){
return daoE.retrieveAllPatientExerciseDone(userID);
}

public List<Exercise> retrieveAllNewPatientExerciseNotDone(int userID){
return daoE.retrieveAllNewPatientExerciseNotDone(userID);
public List<SlimmerExercise> retrieveDoneExercises(int patientId) {
return daoE.retrieveDoneExercises(patientId);
}

public List<SlimmerExercise> retrieveNotDoneExercises(int patientId) {
return daoE.retrieveNotDoneExercises(patientId);
}
}
74 changes: 15 additions & 59 deletions src/main/webapp/JSP/homePagePatient.jsp
Original file line number Diff line number Diff line change
@@ -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" %>
<!DOCTYPE html>
<html lang="it" style="background-color: #f7fcff; ">
<%
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);
%>
<head>
<%@page contentType="text/html;charset=UTF-8"%>
Expand Down Expand Up @@ -50,41 +48,6 @@
<button id="openAll" class="buttons">Guarda tutti</button>
</div>
</div>
<div class="acards" style="display: none;">

<%
ExerciseManager exerciseManager = new ExerciseManager();
List<Exercise> list = exerciseManager.retrieveAllNewPatientExercise(userId);
ExerciseGlossary exerciseGlossary = new ExerciseGlossary();
if(!list.isEmpty()){
for(Exercise exercise : list) {
exerciseGlossary = exerciseManager.getExercise(exercise.getIdExercise());
%>
<div class="margin20">
<form action="${pageContext.request.contextPath}/exerciseController" method="post">
<div class="card2">
<div class="frame-3"><div class="frame-4"><div class="frame-5"><div class="frame-7">
<div class="illustration-wrapper">
<img class="illustration" src="../images/homepagepatient/exerc.png">
</div></div>
<div class="discovering-english"><%=exerciseGlossary.getExerciseName()%></div>
</div></div></div>
<input type="hidden" name="exerciseID" value="<%=exercise.getIdExercise()%>">
<input type="hidden" name="insertionDate" value="<%=exercise.getInsertionDate()%>">
<button class="button-2" type="submit">Comincia l'esercitazione</button>
</div>
</form>
</div>
<%
}
}
else {
%>
<div class="discovering-english">Esercizi non disponibili</div>
<%
}
%>
</div>
</div>
</div>

Expand All @@ -102,24 +65,23 @@
<div class="cards">

<%
ExerciseManager exerciseManager1 = new ExerciseManager();
List<Exercise> list1 = exerciseManager1.retrieveAllNewPatientExerciseNotDone(userId);
ExerciseGlossary exerciseGlossary1 = new ExerciseGlossary();
ExerciseManager exerciseManager = new ExerciseManager();
List<SlimmerExercise> 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) {
%>
<form action="${pageContext.request.contextPath}/exerciseController" method="post">
<div class="card2">
<div class="frame-3"><div class="frame-4"><div class="frame-5"><div class="frame-7">
<div class="illustration-wrapper">
<img class="illustration" src="../images/homepagepatient/illustration-challenges-1.svg">
</div></div>
<div class="discovering-english"><%=exerciseGlossary1.getExerciseName()%></div>
<div class="discovering-english"><%=exercise.getName()%></div>
</div></div></div>
<input type="hidden" name="exerciseID" value="<%=exercise.getIdExercise()%>">
<input type="hidden" name="exerciseID" value="<%=exercise.getId()%>">
<input type="hidden" name="insertionDate" value="<%=exercise.getInsertionDate()%>">
<button class="button-2" type="submit">Comincia l'esercitazione</button>
</div>
Expand Down Expand Up @@ -157,26 +119,23 @@
<div class="cards">

<%
ExerciseManager exerciseManager2 = new ExerciseManager();
List<Exercise> list2 = exerciseManager2.retrieveAllPatientExerciseDone(userId);
ExerciseGlossary exerciseGlossary2 = new ExerciseGlossary();
List<SlimmerExercise> 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) {
%>
<form action="${pageContext.request.contextPath}/exerciseController" method="post">
<div class="card2">
<div class="frame-3"><div class="frame-4"><div class="frame-5"><div class="frame-7">
<div class="illustration-wrapper">
<img class="illustration" src="../images/homepagepatient/check.png">
</div></div>
<div class="discovering-english"><%=exerciseGlossary2.getExerciseName()%></div>
<div class="discovering-english"><%=exercise.getName()%></div>
</div></div></div>
<input type="hidden" name="exerciseID" value="<%=exercise.getIdExercise()%>">
<input type="hidden" name="exerciseID" value="<%=exercise.getId()%>">
<input type="hidden" name="insertionDate" value="<%=exercise.getInsertionDate()%>">
<button class="button-2" type="submit">Comincia l'esercitazione</button>
<button class="button-2" type="submit">Visualizza Punteggio</button>
</div>
</form>
<%
Expand Down Expand Up @@ -220,7 +179,4 @@

</body>
</html>

<%
}
%>
<% } %>

0 comments on commit 4acc9f3

Please sign in to comment.