Skip to content

Commit

Permalink
Merge pull request #40 from pastore99/merging34and17
Browse files Browse the repository at this point in the history
Merging34and17
  • Loading branch information
panuozzo77 authored Jan 6, 2024
2 parents e7a113c + 5586aff commit 99677d9
Show file tree
Hide file tree
Showing 46 changed files with 2,604 additions and 19 deletions.
38 changes: 38 additions & 0 deletions src/main/java/controller/AddRemovePatientCondition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package controller;

import model.service.condition.ConditionManager;

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("/AddRemovePatientCondition")
public class AddRemovePatientCondition extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
String referer = request.getHeader("Referer");
ConditionManager conditionService= new ConditionManager();

int idPatient = Integer.parseInt(request.getParameter("idPatient"));
int idCondition = Integer.parseInt(request.getParameter("idCondition"));

String operation= request.getParameter("operation");
if (operation.equals("Rimuovi")) //REMOVE
{
conditionService.RemoveConditionPatient(idCondition,idPatient);
}
if (operation.equals("Aggiungi")) //ADD
{
int severity= Integer.parseInt(request.getParameter("severity"));
conditionService.AddConditionPatient(idCondition,idPatient,severity);
}

response.sendRedirect(referer);

}

}
24 changes: 19 additions & 5 deletions src/main/java/controller/Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import model.entity.PersonalInfo;
import model.entity.User;
import model.entity.UserInfo;
import model.service.login.Authenticator;
import model.service.user.UserData;
import model.service.user.UserRegistry;
Expand All @@ -12,6 +13,7 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/login")
public class Login extends HttpServlet {
Expand All @@ -35,16 +37,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
int result = authService.authenticate(email, password);

if (result > 0) {
// Login success, defining its Session attributes
setSessionAttributes(result,request);
response.sendRedirect("JSP/welcome.jsp");
// Login success, defining its Session attributes and the redirect page
response.sendRedirect(setSessionAttributes(result, request));
} else {
// Login failed, redirect back to the login page
response.sendRedirect("JSP/login.jsp?error=1");
}
}

private void setSessionAttributes(int id, HttpServletRequest request){
private String setSessionAttributes(int id, HttpServletRequest request){
HttpSession session = request.getSession();

userData = new UserData();
Expand All @@ -57,12 +58,25 @@ private void setSessionAttributes(int id, HttpServletRequest request){
session.setAttribute("name", personalInfo.getFirstname());

if(!userData.isTherapist(user)) {

session.setAttribute("type", "patient");
session.setAttribute("therapist", user.getIdTherapist());
return "JSP/homePatient.jsp";
}
else {
setPatientsInfo(session);
session.setAttribute("type", "therapist");
return "JSP/homeTherapist.jsp";
}
}
private void setPatientsInfo(HttpSession session){

UserRegistry registry = new UserRegistry();

PersonalInfo infoLogged = registry.getPersonalInfo(((Integer) session.getAttribute("id")));

session.setAttribute("NameSurnameLogged", infoLogged != null ? infoLogged.getFirstname() + " " + infoLogged.getLastname() : null);

}
}

}
224 changes: 224 additions & 0 deletions src/main/java/model/DAO/DAOCondition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package model.DAO;

import model.entity.Condition;
import model.entity.PatientCondition;
import model.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class DAOCondition {
private Connection connection;
public DAOCondition(Connection connection) {this.connection=connection;}

public DAOCondition() {
try{
this.connection=DAOConnection.getConnection();
}catch (SQLException e) {
e.printStackTrace();
}
}

private Condition getConditionFromResultSet(ResultSet resultSet) throws SQLException {
Condition c = new Condition();

c.setIdCondition(resultSet.getInt("ID_condition"));
c.setDisorderDescription(resultSet.getString("DisorderDescription"));
c.setDisorderName(resultSet.getString("DisorderName"));
c.setSeverity(-1);

return c;
}

private Condition getPeronalConditionFromResultSet(ResultSet resultSet) throws SQLException {
Condition c = new Condition();

c.setIdCondition(resultSet.getInt("ID_condition"));
c.setDisorderDescription(resultSet.getString("DisorderDescription"));
c.setDisorderName(resultSet.getString("DisorderName"));
c.setSeverity(resultSet.getInt("Severity"));

return c;
}

private PatientCondition getPatientConditionFromResultSet(ResultSet resultSet) throws SQLException {
PatientCondition pc = new PatientCondition();

pc.setIdCondition(resultSet.getInt("ID_condition"));
pc.setIdPatient(resultSet.getInt("ID_patient"));
pc.setSeverity(resultSet.getInt("Severity"));

return pc;
}

public ArrayList<Condition> getConditionsOfPatient(int id_patient) {

PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
ArrayList<Condition> list_PersonalCondition=new ArrayList<>();
try {

connection = connection.isClosed() ? DAOConnection.getConnection():connection;
String query = null;

query = "SELECT c.ID_Condition,c.DisorderName, c.DisorderDescription, pc.Severity\n" +
"FROM `condition` c\n" +
"JOIN PatientCondition pc ON c.ID_condition = pc.ID_condition\n" +
"WHERE pc.ID_patient = ?;";

preparedStatement = connection.prepareStatement(query);
preparedStatement.setObject(1, id_patient);

resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
list_PersonalCondition.add(getPeronalConditionFromResultSet(resultSet));
}

return list_PersonalCondition;
} 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
}

public ArrayList<Condition> getConditionsNOTOfPatient(int id_patient) {

PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
ArrayList<Condition> list_PersonalCondition=new ArrayList<>();
try {

connection = connection.isClosed() ? DAOConnection.getConnection():connection;
String query = null;

query = "SELECT c.*\n" +
"FROM `condition` c\n" +
"LEFT JOIN PatientCondition pc ON c.ID_condition = pc.ID_condition AND pc.ID_patient = ?\n" +
"WHERE pc.ID_patient IS NULL\n" +
"ORDER BY c.DisorderName;";

preparedStatement = connection.prepareStatement(query);
preparedStatement.setObject(1, id_patient);

resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
list_PersonalCondition.add(getConditionFromResultSet(resultSet));
}
return list_PersonalCondition;
} 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
}


public boolean AddConditionPatient(int ID_condition, int ID_patient, int Severity) {

PreparedStatement preparedStatementPersonalInfo = null;

try {
connection = connection.isClosed() ? DAOConnection.getConnection():connection;
connection.setAutoCommit(false); // Start a transaction

// Insert user data into personal_info table
String queryAnagrafica = "INSERT INTO PatientCondition (ID_condition, ID_patient, Severity)\n" +
"VALUES (?, ?, ?)";
preparedStatementPersonalInfo = connection.prepareStatement(queryAnagrafica);
preparedStatementPersonalInfo.setInt(1, ID_condition);
preparedStatementPersonalInfo.setInt(2, ID_patient);
preparedStatementPersonalInfo.setInt(3, Severity);
preparedStatementPersonalInfo.executeUpdate();

connection.commit(); // Commit the transaction
return true; // User created successfully

} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
try {
if (connection != null) {
connection.rollback(); // Rollback the transaction in case of an exception
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} finally {
try {
if (preparedStatementPersonalInfo != null) preparedStatementPersonalInfo.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}

return false; // Default to false if an exception occurs
}

public boolean RemoveConditionPatient(int ID_condition, int ID_patient) {

PreparedStatement preparedStatementPersonalInfo = null;

try {
connection = connection.isClosed() ? DAOConnection.getConnection():connection;
connection.setAutoCommit(false); // Start a transaction

// Insert user data into personal_info table
String queryAnagrafica = "DELETE FROM PatientCondition\n" +
"WHERE ID_condition = ? AND ID_patient = ?;";
preparedStatementPersonalInfo = connection.prepareStatement(queryAnagrafica);
preparedStatementPersonalInfo.setInt(1, ID_condition);
preparedStatementPersonalInfo.setInt(2, ID_patient);
preparedStatementPersonalInfo.executeUpdate();

connection.commit(); // Commit the transaction
return true; // User created successfully

} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
try {
if (connection != null) {
connection.rollback(); // Rollback the transaction in case of an exception
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} finally {
try {
if (preparedStatementPersonalInfo != null) preparedStatementPersonalInfo.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}

return false; // Default to false if an exception occurs
}
}
Loading

0 comments on commit 99677d9

Please sign in to comment.