-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditCustomerRep.jsp
145 lines (137 loc) · 4.77 KB
/
editCustomerRep.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, javax.servlet.http.*, javax.servlet.*"%>
<%@ page import="com.cs336.pkg.ApplicationDB" %>
<%@ page import="java.util.*" %>
<%
String role = (String) session.getAttribute("role");
if (role == null || !role.equals("manager")) {
response.sendRedirect("login.jsp");
return;
}
String username = request.getParameter("username");
String fname = request.getParameter("fname");
String name = request.getParameter("name");
String ssn = request.getParameter("ssn");
String password = request.getParameter("password");
Connection con = null;
PreparedStatement ps = null;
String message = null;
String errorMessage = null;
if (request.getMethod().equalsIgnoreCase("POST")) {
try {
ApplicationDB db = new ApplicationDB();
con = db.getConnection();
String sql = "UPDATE Employee SET fname = ?, name = ?, ssn = ?, password = ? WHERE username = ? AND role = 'customer_rep'";
ps = con.prepareStatement(sql);
ps.setString(1, fname);
ps.setString(2, name);
ps.setString(3, ssn);
ps.setString(4, password);
ps.setString(5, username);
int rowsUpdated = ps.executeUpdate();
if (rowsUpdated > 0) {
message = "Customer representative updated successfully.";
} else {
errorMessage = "Failed to update customer representative.";
}
} catch (SQLException e) {
errorMessage = "Error updating customer representative: " + e.getMessage();
} finally {
try { if (ps != null) ps.close(); } catch (SQLException e) {}
}
} else {
try {
ApplicationDB db = new ApplicationDB();
con = db.getConnection();
String sql = "SELECT fname, name, ssn, password FROM Employee WHERE username = ? AND role = 'customer_rep'";
ps = con.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
fname = rs.getString("fname");
name = rs.getString("name");
ssn = rs.getString("ssn");
password = rs.getString("password");
} else {
errorMessage = "Customer representative not found.";
}
rs.close();
} catch (SQLException e) {
errorMessage = "Error fetching customer representative: " + e.getMessage();
} finally {
try { if (ps != null) ps.close(); } catch (SQLException e) {}
}
}
%>
<!DOCTYPE html>
<html>
<head>
<title>Edit Customer Representative</title>
<style>
body {
font-family: Arial, sans-serif;
}
table {
border-collapse: collapse;
width: 60%;
margin: 20px auto;
}
th, td {
border: 1px solid #999;
padding: 10px;
text-align: left;
}
th {
background-color: #eee;
}
.message {
text-align: center;
margin: 20px;
font-size: 18px;
color: red;
}
.back-link {
display: inline-block;
margin: 10px;
padding: 8px;
background-color: lightblue;
text-decoration: none;
color: black;
border-radius: 5px;
}
.back-link:hover {
background-color: lightcoral;
}
</style>
</head>
<body>
<h1>Edit Customer Representative</h1>
<%
if (errorMessage != null) {
%>
<p style="color: red;"><%= errorMessage %></p>
<%
} else if (message != null) {
%>
<p style="color: green;"><%= message %></p>
<%
} else {
%>
<form method="post">
<label for="fname">First Name:</label>
<input type="text" id="fname" name="fname" value="<%= fname %>" required><br>
<label for="name">Last Name:</label>
<input type="text" id="name" name="name" value="<%= name %>" required><br>
<label for="ssn">SSN:</label>
<input type="text" id="ssn" name="ssn" value="<%= ssn %>" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" value="<%= password %>" required><br>
<input type="hidden" name="username" value="<%= username %>">
<button type="submit">Update</button>
</form>
<%
}
%>
<a href="manageCustomerRepresentatives.jsp" class="back-link">Back to Manage Customer Representatives</a>
</body>
</html>