Skip to content

Commit

Permalink
Merge pull request #22 from pastore99/15-implementation-provide-sessi…
Browse files Browse the repository at this point in the history
…on-after-login

15 implementation provide session after login
  • Loading branch information
panuozzo77 authored Dec 23, 2023
2 parents dff8501 + 7222e7f commit 6ad12ff
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 69 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

</dependencies>

<build>
Expand Down
39 changes: 33 additions & 6 deletions src/main/java/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package controller;

import model.DAO.DAOPersonalInfo;
import model.entity.PersonalInfo;
import model.entity.User;
import model.service.login.Authenticator;
import model.service.user.UserData;
import model.service.user.UserRegistry;

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 javax.servlet.http.HttpSession;
import java.io.IOException;
import model.service.login.Authenticator;

@WebServlet("/login")
public class LoginController extends HttpServlet {
Expand All @@ -23,20 +30,40 @@ public void init() {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


String email = request.getParameter("email");
String password = request.getParameter("password");

System.out.println(email);
System.out.println(password);
int result = authService.authenticate(email, password);

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

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

UserData userData = new UserData();
UserRegistry userReg = new UserRegistry();

User user = userData.getUserByIdOrEmail(id);
PersonalInfo personalInfo = userReg.getPersonalInfo(id);

session.setAttribute("id", id);
session.setAttribute("name", personalInfo.getFirstname());

if(userData.isTherapist(user)) {
session.setAttribute("type", "patient");
session.setAttribute("therapist", user.getIdTherapist());
}
else {
session.setAttribute("type", "therapist");
}
}

}
63 changes: 0 additions & 63 deletions src/main/java/model/DAO/DAOAPersonalInfo.java

This file was deleted.

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

import model.entity.PersonalInfo;

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

public class DAOPersonalInfo {

private PersonalInfo getPersonalInfoFromResultSet(ResultSet resultSet) throws SQLException {
PersonalInfo personalInfo = new PersonalInfo();

personalInfo.setIdUser(resultSet.getInt("ID_user"));
personalInfo.setFirstname(resultSet.getString("Firstname"));
personalInfo.setLastname(resultSet.getString("Lastname"));
personalInfo.setDateOfBirth(resultSet.getDate("DateOfBirth"));
personalInfo.setGender(resultSet.getString("Gender"));
personalInfo.setAddress(resultSet.getString("Address"));
personalInfo.setSsn(resultSet.getString("SSN"));
personalInfo.setPhone(resultSet.getString("Phone"));

return personalInfo;
}
public boolean createRegistry(int id, String name, String surname) {
Connection connection = null;
PreparedStatement preparedStatementPersonalInfo = null;

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

// Insert user data into personal_info table
String queryAnagrafica = "INSERT INTO personal_info (ID_user, Firstname, Lastname) VALUES (?, ?, ?)";
preparedStatementPersonalInfo = connection.prepareStatement(queryAnagrafica);
preparedStatementPersonalInfo.setInt(1, id);
preparedStatementPersonalInfo.setString(2, name);
preparedStatementPersonalInfo.setString(3, surname);
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 PersonalInfo getPersonalInfo(int id) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

try {
connection = DAOConnection.getConnection();

String sql = "SELECT * FROM personal_info WHERE ID_user = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);

resultSet = preparedStatement.executeQuery();

if (resultSet.next()) {
return getPersonalInfoFromResultSet(resultSet);
}
} 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 null; // Return null if personal_info does not exist
}
}

9 changes: 9 additions & 0 deletions src/main/java/model/service/user/UserData.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model.service.user;

import model.DAO.DAOUser;
import model.entity.User;

public class UserData /*implements UserDataInterface */{
DAOUser daoUser = new DAOUser();
Expand All @@ -12,4 +13,12 @@ public boolean checkIfEmailExists(String email) {
public int createUser(String email, String password, int therapistId) {
return daoUser.createUser(email, password, therapistId);
}

public User getUserByIdOrEmail(Object idOrEmail) {
return daoUser.getUserByIdOrEmail(idOrEmail);
}

public boolean isTherapist(User user){
return user.getIdTherapist() > 0;
}
}
9 changes: 9 additions & 0 deletions src/main/java/model/service/user/UserRegistry.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package model.service.user;

import model.DAO.DAOPersonalInfo;
import model.entity.PersonalInfo;

public class UserRegistry implements UserRegistryInterface {
DAOPersonalInfo db = new DAOPersonalInfo();

public boolean firstAccess(int id, String name, String surname) {//TODO e da inserire nel metodo di registrazione!
System.out.println("hello, this is a placeholder");
return true; //TODO Remove it!
}

public PersonalInfo getPersonalInfo(int id) {
return db.getPersonalInfo(id);
}

}

0 comments on commit 6ad12ff

Please sign in to comment.