Skip to content

Commit

Permalink
Provided legal page acceptance
Browse files Browse the repository at this point in the history
-DAOUser and UserData methods to change notifications time and Analytics
-RegistrationServlet GET method to change the above settings
-RegistrationServlet now takes care of the Session Attributes so you don't need to login after registration
-Provided the Javascript code for the page legal.jsp
  • Loading branch information
panuozzo77 committed Dec 27, 2023
1 parent 169d1d1 commit f17b5cf
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 52 deletions.
19 changes: 18 additions & 1 deletion src/main/java/controller/RegistrationServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,24 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
response.getWriter().write(String.valueOf(result));
if(result == 0) {
setSessionAttributes(email, request);
response.sendRedirect("JSP/legal.jsp");
response.sendRedirect("/JSP/legal.jsp");
}
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
UserData ud = new UserData();
HttpSession session = request.getSession();
String parameter = request.getParameter("type");
if(parameter.equals("analytics")) {
Boolean value = Boolean.parseBoolean(request.getParameter("accept"));
ud.updateAnalyticsPreference(String.valueOf(session.getAttribute("id")), value);
}
if(parameter.equals("emailTime")) {
String start = request.getParameter("startTime");
String end = request.getParameter("endTime");
String time = start + "|" + end;
ud.updateEmailTime(String.valueOf(session.getAttribute("id")), time);
response.sendRedirect("/JSP/welcome.jsp");
}
}

Expand Down
98 changes: 98 additions & 0 deletions src/main/java/model/DAO/DAOUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,102 @@ public boolean resetPassword(String email, String newPassword) {
// Default to false if an exception occurs
return false;
}

/**
* Updates the user's analytics choice in the User table.
*
* @param userId The id of the user.
* @param value The analytics choice value to set for the user.
* @return true if the choice was successfully updated; false otherwise.
*/
public boolean updateAnalyticsPreference(String userId, boolean value) {
Connection connection = null;
PreparedStatement preparedStatement = null;

try {
// Get connection
connection = DAOConnection.getConnection();

// Query to update analytics choice for the given userId
String query = "UPDATE user SET Analytics = ? WHERE ID = ?";

// Prepare the statement
preparedStatement = connection.prepareStatement(query);

// Set the parameters
preparedStatement.setBoolean(1, value);
preparedStatement.setString(2, userId);

// Execute the update query
int rowsModified = preparedStatement.executeUpdate();

// If rowsModified is greater than 0, then a row has been updated.
// So, return true. If not, return false.
return rowsModified > 0;
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
} finally {
try {
// Close everything properly
if (preparedStatement != null) preparedStatement.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}

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

/**
* Updates the user's Email Time in the User table.
*
* @param id The id of the user.
* @param value The email time value to set for the user.
* @return true if the email time was successfully updated; false otherwise.
*/
public boolean updateEmailTime(String id, String value) {
Connection connection = null;
PreparedStatement preparedStatement = null;

try {
// Get connection
connection = DAOConnection.getConnection();

// Query to update Email Time for the given id
String query = "UPDATE user SET NotificationTime = ? WHERE ID = ?";

// Prepare the statement
preparedStatement = connection.prepareStatement(query);

// Set the parameters
preparedStatement.setString(1, value);
preparedStatement.setString(2, id);

// Execute the update query
int rowsModified = preparedStatement.executeUpdate();

// If rowsModified is greater than 0, then a row has been updated.
// So, return true. If not, return false.
return rowsModified > 0;
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
} finally {
try {
// Close everything properly
if (preparedStatement != null) preparedStatement.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}

// Default to false if an exception occurs
return false;
}
}
8 changes: 8 additions & 0 deletions src/main/java/model/service/user/UserData.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ public User getUserByIdOrEmail(Object idOrEmail) {
public boolean isTherapist(User user){
return user.getIdTherapist() == 0;
}

public boolean updateAnalyticsPreference(String id, Boolean value) {
return daoUser.updateAnalyticsPreference(id, value);
}

public boolean updateEmailTime(String id, String value) {
return daoUser.updateEmailTime(id, value);
}
}
52 changes: 52 additions & 0 deletions src/main/webapp/JS/legal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Function to show the popup
function mostraPopup() {
document.getElementById("overlay").style.display = "block";
}

// Function called when the user accepts the sharing
function accettaCondivisione() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "../register?type=analytics&accept=true", true);
xhr.send();

document.getElementById("overlay").style.display = "none";
}

// Show the popup when the page is fully loaded
window.onload = mostraPopup;

function impostaOrario() {
event.preventDefault();
let start = document.getElementById('startTime').value = '';
let end = document.getElementById('endTime').value = '';
let time = start + "|" + end;
var xhr = new XMLHttpRequest();
xhr.open("GET", "../register?type=emailTime&time="+time, true);
xhr.send();
}

function nonAccetto() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "../register?type=analytics&accept=false", true);
xhr.send();

document.getElementById("overlay").style.display = "none";
}

function validateTimes() {
var startTime = new Date();
var endTime = new Date();

// Convert time strings into Date objects
startTime.setHours(document.getElementById('startTime').value.split(':')[0]);
startTime.setMinutes(document.getElementById('startTime').value.split(':')[1]);
endTime.setHours(document.getElementById('endTime').value.split(':')[0]);
endTime.setMinutes(document.getElementById('endTime').value.split(':')[1]);

// Check if the start time is greater than the end time
if (startTime > endTime) {
alert("L'ora di inizio deve essere inferiore all'ora di fine");
document.getElementById('startTime').value = '';
document.getElementById('endTime').value = '';
}
}
55 changes: 6 additions & 49 deletions src/main/webapp/JSP/legal.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<%@page contentType="text/html;charset=UTF-8"%>

<%
String userId = (String) session.getAttribute("id");
Integer userId = (Integer) session.getAttribute("id");
if(userId == null) {
response.sendRedirect("www.google.it");
response.sendRedirect("../errorPage/403.html");
}
%>
Expand Down Expand Up @@ -265,7 +265,8 @@
</div>
</div>

<form id="timeForm">
<form id="timeForm" action="../register" method="GET">
<input type="hidden" name="type" value="emailTime">
<div>
<h3 style="text-align:center" >Seleziona una fascia oraria dove preferisci ricevere delle notifiche</h3>
</div>
Expand All @@ -279,51 +280,7 @@
</div>
<button class="button" type="submit">Salva</button>
</form>

<script>
// Funzione per mostrare il popup
function mostraPopup() {
document.getElementById("overlay").style.display = "block";
}
// Funzione chiamata quando l'utente accetta la condivisione
function accettaCondivisione() {
// Puoi aggiungere qui la logica per gestire l'accettazione
// Ad esempio, inviare una richiesta AJAX al server per registrare il consenso
// Qui potresti anche chiudere il popup o reindirizzare l'utente a una pagina successiva
document.getElementById("overlay").style.display = "none";
}
// Mostra il popup quando la pagina è completamente caricata
window.onload = mostraPopup;
function accetta() {
// Logica per gestire l'accettazione
window.location.href = "login.jsp";
}
function nonAccetto() {
// Logica per gestire il rifiuto
window.location.href = "https://www.google.it/?hl=it";
}
function validateTimes() {
var startTime = new Date();
var endTime = new Date();
// Converti le stringhe di tempo in oggetti Date
startTime.setHours(document.getElementById('startTime').value.split(':')[0]);
startTime.setMinutes(document.getElementById('startTime').value.split(':')[1]);
endTime.setHours(document.getElementById('endTime').value.split(':')[0]);
endTime.setMinutes(document.getElementById('endTime').value.split(':')[1]);
// Controlla se l'ora di inizio è maggiore dell'ora di fine
if (startTime > endTime) {
alert("L'ora di inizio deve essere inferiore all'ora di fine");
document.getElementById('startTime').value = '';
document.getElementById('endTime').value = '';
}
}
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="../JS/legal.js"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<location>/errorPage/404.html</location>
</error-page>

<error-page>
<!--<error-page>
<error-code>500</error-code>
<location>/errorPage/500.html</location>
</error-page>
</error-page> -->
</web-app>

0 comments on commit f17b5cf

Please sign in to comment.