diff --git a/src/main/java/controller/ManageAIExercise.java b/src/main/java/controller/ManageAIExercise.java new file mode 100644 index 0000000..1d7c8be --- /dev/null +++ b/src/main/java/controller/ManageAIExercise.java @@ -0,0 +1,36 @@ +package controller; + +import com.google.gson.Gson; +import model.entity.SlimmerExercise; +import model.service.exercise.ExerciseManager; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet("/ManageExercise") +public class ManageAIExercise extends HttpServlet +{ + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ + String action = request.getParameter("action"); + ExerciseManager em = new ExerciseManager(); + Gson g = new Gson(); + + if(action.equalsIgnoreCase("Approve") || action.equalsIgnoreCase("Remove")){ + SlimmerExercise exercise = g.fromJson(request.getParameter("exercise"), SlimmerExercise.class); + em.changeRaccomandation(action, exercise.getId(), exercise.getInsertionDate(), exercise.getUserId()); + + }else if (action.equalsIgnoreCase("ApproveAll") || action.equalsIgnoreCase("RemoveAll")) { + if(action.equalsIgnoreCase("ApproveAll")){ + action = "Approve"; + }else if (action.equalsIgnoreCase("RemoveAll")){ + action = "Remove"; + } + em.changeMultipleReccomandation(action, Integer.parseInt(request.getParameter("userId"))); + } + response.sendRedirect(request.getHeader("Referer")); + } +} \ No newline at end of file diff --git a/src/main/java/model/DAO/DAOExercise.java b/src/main/java/model/DAO/DAOExercise.java index c443f03..131e2c1 100644 --- a/src/main/java/model/DAO/DAOExercise.java +++ b/src/main/java/model/DAO/DAOExercise.java @@ -92,7 +92,7 @@ public List retrieveNotDoneExercises(int patientId) { List exercises = new ArrayList<>(); try { connection = connection.isClosed() ? DAOConnection.getConnection() : connection; - String query = "SELECT e.ID_exercise, eg.ExerciseName, e.InsertionDate, e.Evaluation FROM exercise e" + + 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 ASC"; @@ -103,8 +103,14 @@ public List retrieveNotDoneExercises(int patientId) { while(rs.next()) { SlimmerExercise exercise = new SlimmerExercise( rs.getInt("ID_exercise"), + rs.getInt("ID_user"), rs.getString("ExerciseName"), + rs.getString("ExerciseDescription"), + rs.getInt("Feedback"), rs.getDate("InsertionDate"), + rs.getInt("Difficulty"), + rs.getString("Target"), + rs.getString("Type"), rs.getInt("Evaluation") ); exercises.add(exercise); @@ -119,7 +125,7 @@ public List retrieveDoneExercises(int patientId) { List exercises = new ArrayList<>(); try { connection = connection.isClosed() ? DAOConnection.getConnection() : connection; - String query = "SELECT e.ID_exercise, eg.ExerciseName, e.InsertionDate, e.Evaluation FROM exercise e" + + 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 = ?"; @@ -130,8 +136,14 @@ public List retrieveDoneExercises(int patientId) { while(rs.next()) { SlimmerExercise exercise = new SlimmerExercise( rs.getInt("ID_exercise"), + rs.getInt("ID_user"), rs.getString("ExerciseName"), + rs.getString("ExerciseDescription"), + rs.getInt("Feedback"), rs.getDate("InsertionDate"), + rs.getInt("Difficulty"), + rs.getString("Target"), + rs.getString("Type"), rs.getInt("Evaluation") ); exercises.add(exercise); @@ -351,4 +363,139 @@ public boolean setExerciseFeedback(int userID, int exerciseID, Date insertDate, } } } + + public List getExerciseToApprove(int therapistId){ + List exercises = new ArrayList<>(); + 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.setInt(1, therapistId); + ResultSet rs = stmt.executeQuery(); + + while(rs.next()) { + SlimmerExercise exercise = new SlimmerExercise( + rs.getInt("ID_exercise"), + rs.getInt("ID_user"), + rs.getString("ExerciseName"), + rs.getString("ExerciseDescription"), + rs.getInt("Feedback"), + rs.getDate("InsertionDate"), + rs.getInt("Difficulty"), + rs.getString("Target"), + rs.getString("Type"), + rs.getInt("Evaluation") + ); + exercises.add(exercise); + } + } catch(SQLException e) { + e.printStackTrace(); + } + return exercises; + } + + public boolean approveExercise(int exerciseId, Date insertDate, int userId){ + String query = "UPDATE exercise SET Recommended = 1 WHERE ID_user = ? AND ID_exercise = ? AND InsertionDate = ? AND Recommended = 0;"; + PreparedStatement preparedStatement = null; + + try { + connection = connection.isClosed() ? DAOConnection.getConnection() : connection; + preparedStatement = connection.prepareStatement(query); + preparedStatement.setInt(1, userId); + preparedStatement.setInt(2, exerciseId); + preparedStatement.setDate(3, insertDate); + + return preparedStatement.executeUpdate() > 0; + + } catch (SQLException e) { + e.printStackTrace(); + return false; + } finally { + try { + if (preparedStatement != null) preparedStatement.close(); + DAOConnection.releaseConnection(connection); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + public boolean deleteExercise(int exerciseId, Date insertDate, int userId){ + String query = "DELETE FROM exercise WHERE ID_user = ? AND ID_exercise = ? AND InsertionDate = ? AND Recommended = 0;"; + PreparedStatement preparedStatement = null; + + try { + connection = connection.isClosed() ? DAOConnection.getConnection() : connection; + preparedStatement = connection.prepareStatement(query); + preparedStatement.setInt(1, userId); + preparedStatement.setInt(2, exerciseId); + preparedStatement.setDate(3, insertDate); + + return preparedStatement.executeUpdate() > 0; + + } catch (SQLException e) { + e.printStackTrace(); + return false; + } finally { + try { + if (preparedStatement != null) preparedStatement.close(); + DAOConnection.releaseConnection(connection); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + public boolean approveMultipleExercise(int userId){ + String query = "UPDATE exercise SET Recommended = 1 WHERE ID_user = ? AND Recommended = 0;"; + PreparedStatement preparedStatement = null; + + try { + connection = connection.isClosed() ? DAOConnection.getConnection() : connection; + preparedStatement = connection.prepareStatement(query); + preparedStatement.setInt(1, userId); + + return preparedStatement.executeUpdate() > 0; + + } catch (SQLException e) { + e.printStackTrace(); + return false; + } finally { + try { + if (preparedStatement != null) preparedStatement.close(); + DAOConnection.releaseConnection(connection); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + public boolean deleteMultipleExercise(int userId){ + String query = "DELETE FROM exercise WHERE ID_user = ? AND Recommended = 0;"; + PreparedStatement preparedStatement = null; + + try { + connection = connection.isClosed() ? DAOConnection.getConnection() : connection; + preparedStatement = connection.prepareStatement(query); + preparedStatement.setInt(1, userId); + + return preparedStatement.executeUpdate() > 0; + + } catch (SQLException e) { + e.printStackTrace(); + return false; + } finally { + try { + if (preparedStatement != null) preparedStatement.close(); + DAOConnection.releaseConnection(connection); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + } diff --git a/src/main/java/model/DAO/DAOUser.java b/src/main/java/model/DAO/DAOUser.java index f13b8af..f1caf99 100644 --- a/src/main/java/model/DAO/DAOUser.java +++ b/src/main/java/model/DAO/DAOUser.java @@ -8,6 +8,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; /** * DAOUser is a class that provides methods for accessing the User table in the database. @@ -485,4 +486,57 @@ public ArrayList getUsersAndPersonalInfoByIdTherapist(int idTherapist) return null; // or you may throw an exception here } + + public HashMap getMapUsersAndPersonalInfoByIdTherapist(int idTherapist) { + Connection connection = null; + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + HashMap userMap = new HashMap(); + + try { + connection = DAOConnection.getConnection(); + String query = null; + + query = "SELECT ID,Email,ActivationDate,Firstname,Lastname,DateOfBirth,Gender,Address,SSN,Phone FROM user,personal_info WHERE ID_Therapist = ? AND user.ID= personal_info.ID_USER;"; + + preparedStatement = connection.prepareStatement(query); + preparedStatement.setObject(1, idTherapist); + + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + UserInfo u=new UserInfo(); + int userId = resultSet.getInt("ID"); + u.setId(userId); + u.setEmail(resultSet.getString("Email")); + u.setActivationDate(resultSet.getTimestamp("ActivationDate")); + u.setFirstname(resultSet.getString("Firstname")); + u.setLastname(resultSet.getString("Lastname")); + u.setDateOfBirth(resultSet.getDate("DateOfBirth")); + u.setGender(resultSet.getString("Gender")); + u.setAddress(resultSet.getString("Address")); + u.setSsn(resultSet.getString("SSN")); + u.setPhone(resultSet.getString("Phone")); + + userMap.put(userId, u); + } + return userMap; + } catch (SQLException e) { + // Handle the exception (e.g., log or throw) + e.printStackTrace(); + } finally { + try { + if (resultSet != null) resultSet.close(); + if (preparedStatement != null) preparedStatement.close(); + DAOConnection.releaseConnection(connection); + } catch (SQLException e) { + // Handle the exception (e.g., log or throw) + e.printStackTrace(); + } + } + + return null; // or you may throw an exception here + } + + } diff --git a/src/main/java/model/entity/SlimmerExercise.java b/src/main/java/model/entity/SlimmerExercise.java index 50e5b23..8312a88 100644 --- a/src/main/java/model/entity/SlimmerExercise.java +++ b/src/main/java/model/entity/SlimmerExercise.java @@ -4,15 +4,36 @@ public class SlimmerExercise { private int id; + private String name; + private Date insertionDate; private int evaluation; - public SlimmerExercise(int id, String name, Date insertionDate, int evaluation) { + private String description; + + private int feedback; + + private int difficulty; + + private String target; + + private String type; + + private int userId; + + + public SlimmerExercise(int id, int userId, String name, String description, int feedback, Date insertionDate, int difficulty, String target, String type, int evaluation) { this.id = id; + this.userId = userId; this.name = name; + this.description = description; + this.feedback = feedback; this.insertionDate = insertionDate; + this.difficulty = difficulty; + this.target = target; + this.type = type; this.evaluation = evaluation; } @@ -40,11 +61,56 @@ public void setInsertionDate(Date insertionDate) { this.insertionDate = insertionDate; } - public int getEvaluation() { - return evaluation; + public int getFeedback() { + return feedback; } - public void setEvaluation(int vote) { - this.evaluation = evaluation; + public void setFeedback(int feedback) { + this.feedback = feedback; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public int getDifficulty() { + return difficulty; } + + public void setDifficulty(int difficulty) { + this.difficulty = difficulty; + } + + public String getTarget() { + return target; + } + + public void setTarget(String target) { + this.target = target; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public int getEvaluation() { return evaluation; } + + public void setEvaluation(int evaluation) { this.evaluation = evaluation; } + } \ No newline at end of file diff --git a/src/main/java/model/service/exercise/ExerciseManager.java b/src/main/java/model/service/exercise/ExerciseManager.java index 1f19ade..6f1d8a6 100644 --- a/src/main/java/model/service/exercise/ExerciseManager.java +++ b/src/main/java/model/service/exercise/ExerciseManager.java @@ -47,4 +47,24 @@ public List retrieveDoneExercises(int patientId) { public List retrieveNotDoneExercises(int patientId) { return daoE.retrieveNotDoneExercises(patientId); } + + public List retrieveAiRaccomandation(int therapistId){ + return daoE.getExerciseToApprove(therapistId); + } + + public boolean changeRaccomandation(String action, int exerciseId, Date insertDate, int userId){ + if(action.equalsIgnoreCase("Approve")){ + return daoE.approveExercise(exerciseId, insertDate, userId); + }else{ + return daoE.deleteExercise(exerciseId, insertDate, userId); + } + } + + public boolean changeMultipleReccomandation(String action, int userId){ + if(action.equalsIgnoreCase("Approve")){ + return daoE.approveMultipleExercise(userId); + }else{ + return daoE.deleteMultipleExercise(userId); + } + } } diff --git a/src/main/java/model/service/user/UserData.java b/src/main/java/model/service/user/UserData.java index 5cd222a..81ac44b 100644 --- a/src/main/java/model/service/user/UserData.java +++ b/src/main/java/model/service/user/UserData.java @@ -5,6 +5,7 @@ import model.entity.UserInfo; import java.util.ArrayList; +import java.util.HashMap; public class UserData implements UserDataInterface { DAOUser db; @@ -40,4 +41,6 @@ public boolean updateEmailTime(String id, String value) { } public ArrayList getUsersAndPersonalInfoByIdTherapist(int ID_Therapist) {return db.getUsersAndPersonalInfoByIdTherapist(ID_Therapist);} + + public HashMap getMappedUserInfoByIdTherapist(int therapistId){ return db.getMapUsersAndPersonalInfoByIdTherapist(therapistId);} } diff --git a/src/main/webapp/JSP/TESTAIAPPROVE.jsp b/src/main/webapp/JSP/TESTAIAPPROVE.jsp new file mode 100644 index 0000000..9e9f2f0 --- /dev/null +++ b/src/main/webapp/JSP/TESTAIAPPROVE.jsp @@ -0,0 +1,135 @@ +<%@ page import="model.entity.SlimmerExercise" %> +<%@ page import="java.util.List" %> +<%@ page import="java.util.ArrayList" %> +<%@ page import="model.service.exercise.ExerciseManager" %> +<%@ page import="model.service.user.UserData" %> +<%@ page import="model.entity.UserInfo" %> +<%@ page import="com.google.gson.Gson" %> +<%@ page import="com.google.gson.GsonBuilder" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + TESTING + + + + <% + ExerciseManager em = new ExerciseManager(); + List exercises = em.retrieveAiRaccomandation(9); //TODO: Prendilo dalla sessione + + UserData ud = new UserData(); + ArrayList u = ud.getUsersAndPersonalInfoByIdTherapist(9); //TODO: Prendilo dalla sessione + + Gson g = new GsonBuilder().disableHtmlEscaping().create(); + %> + + <% for (UserInfo user : u) {%> + + +

Paziente: <%=user.getFirstname()%> <%=user.getLastname()%>

+ + + + <% for (SlimmerExercise ex : exercises){ + if(ex.getUserId() == user.getId()){ + %> + + + + + + + + + + <% + } + } + %> + +
<%= ex.getUserId() %><%= ex.getName() %><%= ex.getDescription() %><%= ex.getDifficulty() %><%= ex.getTarget() %><%= ex.getType() %> + + +
+ <% + } + %> + + + + + +