-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-patient.jsp
120 lines (95 loc) · 3.37 KB
/
update-patient.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="database.*, model.*, java.util.*" %>
<%@ page errorPage="error.jsp"%>
<%-- restrict page access --%>
<%@ include file="patientAuthenticationGuard.jsp"%>
<%
Patient patient = (Patient) session.getAttribute("patient-database-obj");
int id = patient.getId();
String name = request.getParameter("patient_name");
String surname = request.getParameter("patient_surname");
String username = request.getParameter("patient_username");
String password = request.getParameter("patient_password");
String ssn = request.getParameter("patient_ssn");
String email = request.getParameter("patient_email");
PatientDataValidator pavalidator = new PatientDataValidator();
int countErrors = 0;
String errorMessage = "";
PatientDAO patientDAO = new PatientDAO();
//Validate data using PatientDataValidator
try {
if(name != null) {
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
}
//validate name
if( !pavalidator.isNameValid( name ) ) {
errorMessage += "<li>Name should have from 3 (min) to 45 (max) characters</li>";
countErrors++;
}
if(surname != null) {
surname = new String(surname.getBytes("ISO-8859-1"), "UTF-8");
}
//validate surname
if( !pavalidator.isSurnameValid( surname ) ) {
errorMessage += "<li>Surname should have from 3 (min) to 45 (max) characters</li>";
countErrors++;
}
if(username != null) {
username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
}
//validate username
if( !pavalidator.isUsernameValid( username ) ) {
errorMessage += "<li>Username should have from 3 (min) to 45 (max) characters</li>";
countErrors++;
}
//get all patient usernames from database
ArrayList<String> usernames = patientDAO.getAllPatientUsernames(username);
//check if usernames exist
if(pavalidator.usernameExistsMoreThanOnce(username, usernames)) {
errorMessage += "<li>Username exists</li>";
countErrors++;
}
//validate password
if( !pavalidator.isPasswordValid( password ) ) {
errorMessage += "<li>Password should have at least 8 characters including a lowercase letter, an uppercase letter and a number</li>";
countErrors++;
}
//validate ssn
if( !pavalidator.isSsnValid( ssn ) ) {
errorMessage += "<li>SSN should have 9 digits</li>";
countErrors++;
}
//validate email
if( !pavalidator.isValidEmailAddress( email ) ) {
errorMessage += "<li>Email is not valid</li>";
countErrors++;
}
//get all patient emails from database
ArrayList<String> emails = patientDAO.getAllPatientEmails(email);
//check if emails exists
if(pavalidator.emailExistsMoreThanOnce(email, emails)) {
errorMessage += "<li>Email exists</li>";
countErrors++;
}
if (countErrors > 0) {
errorMessage = "<ol>" + errorMessage + "</ol>";
request.setAttribute( "error-message", errorMessage);
%>
<jsp:forward page="edit-patient.jsp"/>
<%
}
patient = new Patient(id, name, surname, username, password, ssn, email);
patientDAO.updatePatient(patient, id);
%>
<jsp:forward page="patient-profile.jsp"/>
<%
//usage of custom exceptions
} catch(ApplicationCustomExceptionDatabase e) {
request.setAttribute( "error-message", e.getMessage());
%>
<jsp:forward page="edit-patient.jsp"/>
<%
} catch(Exception e) {
throw new Exception( e.getMessage() );
}
%>