Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

71 implementation therapist approves ia raccomandations [BE] #78

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/controller/ManageAIExercise.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
151 changes: 149 additions & 2 deletions src/main/java/model/DAO/DAOExercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public List<SlimmerExercise> retrieveNotDoneExercises(int patientId) {
List<SlimmerExercise> 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";

Expand All @@ -103,8 +103,14 @@ public List<SlimmerExercise> 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);
Expand All @@ -119,7 +125,7 @@ public List<SlimmerExercise> retrieveDoneExercises(int patientId) {
List<SlimmerExercise> 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 = ?";

Expand All @@ -130,8 +136,14 @@ public List<SlimmerExercise> 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);
Expand Down Expand Up @@ -351,4 +363,139 @@ public boolean setExerciseFeedback(int userID, int exerciseID, Date insertDate,
}
}
}

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


}
54 changes: 54 additions & 0 deletions src/main/java/model/DAO/DAOUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -485,4 +486,57 @@ public ArrayList<UserInfo> getUsersAndPersonalInfoByIdTherapist(int idTherapist)

return null; // or you may throw an exception here
}

public HashMap<Integer, UserInfo> getMapUsersAndPersonalInfoByIdTherapist(int idTherapist) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
HashMap<Integer, UserInfo> userMap = new HashMap<Integer, UserInfo>();

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
}


}
76 changes: 71 additions & 5 deletions src/main/java/model/entity/SlimmerExercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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; }

}
Loading
Loading