Advertisement
Guest User

Untitled

a guest
Mar 1st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. package servlets;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.SQLException;
  6. import java.util.HashMap;
  7.  
  8. import javax.servlet.RequestDispatcher;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.annotation.WebServlet;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. import beans.Customer;
  16. import beans.Login;
  17. import dbconn.ConnectionUtils;
  18. import dbconn.DBUtils;
  19. import utils.SessionUtils;
  20.  
  21. @WebServlet(urlPatterns = {"/editAccountDetails"})
  22. public class EditAccountDetailsServlet extends HttpServlet {
  23.  
  24.     private static final long serialVersionUID = 1L;
  25.     private Connection conn = null;
  26.  
  27.     public EditAccountDetailsServlet() {
  28.         super();
  29.     }
  30.  
  31.     public void updateUserRecords(Connection conn, HashMap<String, String> map) throws SQLException {
  32.         String customerID = map.get("id");
  33.  
  34.         // CUSTOMERS table
  35.         if (map.get("firstName") != null && map.get("firstName").length() > 0){
  36.             DBUtils.updateCustomerRecord(conn, customerID, "FIRSTNAME", map.get("firstName"));
  37.         }
  38.         if (map.get("surname") != null && map.get("surname").length() > 0) {
  39.             DBUtils.updateCustomerRecord(conn, customerID, "SURNAME", map.get("surname"));
  40.         }
  41.         if (map.get("homeAddress") != null && map.get("homeAddress").length() > 0) {
  42.             DBUtils.updateCustomerRecord(conn, customerID, "ADDRESS", map.get("homeAddress"));
  43.         }
  44.         if (map.get("phoneNumber") != null && map.get("phoneNumber").length() > 0) {
  45.             DBUtils.updateCustomerRecord(conn, customerID, "PHONENUMBER", map.get("phoneNumber"));
  46.         }
  47.         if (map.get("emailAddress") != null && map.get("emailAddress").length() > 0) {
  48.             DBUtils.updateCustomerRecord(conn, customerID, "EMAIL", map.get("emailAddress"));
  49.         }
  50.  
  51.         // LOGINCREDS table
  52.         if (map.get("userName") != null && map.get("userName").length() > 0) {
  53.             DBUtils.updateLoginRecord(conn, customerID, "USERNAME", map.get("surname"));
  54.         }
  55.         if (map.get("password") != null && map.get("password").length() > 0) {
  56.             DBUtils.updateLoginRecord(conn, customerID, "PASSWORD", map.get("surname"));
  57.         }
  58.     }
  59.  
  60.     @Override
  61.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  62.             throws ServletException, IOException {
  63.  
  64.        
  65.  
  66.         RequestDispatcher dispatcher //
  67.                 = this.getServletContext().getRequestDispatcher("/WEB-INF/views/editAccountDetails.jsp");
  68.  
  69.         dispatcher.forward(request, response);
  70.     }
  71.  
  72.     @Override
  73.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  74.             throws ServletException, IOException {
  75.  
  76.         // User details
  77.         String firstName = request.getParameter("firstName");
  78.         String surname = request.getParameter("surname");
  79.         String homeAddress = request.getParameter("homeAddress");
  80.         String phoneNumber = request.getParameter("phoneNumber");
  81.         String emailAddress = request.getParameter("emailAddress");
  82.  
  83.         // Username & Password
  84.         String userName = request.getParameter("userName");
  85.         String password = request.getParameter("password");
  86.  
  87.         Customer loggedInCustomer = SessionUtils.getLoggedInCustomer(request.getSession());
  88.         Login customerLogin = null;
  89.  
  90.         HashMap<String, String> map = new HashMap<>();
  91.         map.put("id", loggedInCustomer.getCustomerID());
  92.         map.put("firstName", firstName);
  93.         map.put("surname", surname);
  94.         map.put("homeAddress", homeAddress);
  95.         map.put("phoneNumber", phoneNumber);
  96.         map.put("emailAddress", emailAddress);
  97.         map.put("password", password);
  98.         map.put("userName", userName);
  99.  
  100.         Connection conn;
  101.  
  102.         try {
  103.             conn = ConnectionUtils.getConnection();
  104.             updateUserRecords(conn, map);
  105.             loggedInCustomer = DBUtils.findRegisteredCustomer(conn, userName, password);
  106.             customerLogin = DBUtils.retrieveLoginRecord(conn, loggedInCustomer.getCustomerID());
  107.  
  108.             request.setAttribute("loggedInCustomer", loggedInCustomer);
  109.             request.setAttribute("customerCredentials", customerLogin);
  110.  
  111.             // Forward to /WEB-INF/views/register.jsp
  112.             RequestDispatcher dispatcher //
  113.                     = this.getServletContext().getRequestDispatcher("/WEB-INF/views/account.jsp");
  114.  
  115.             dispatcher.forward(request, response);
  116.  
  117.         } catch (ClassNotFoundException | SQLException | NullPointerException ex) {
  118.             ex.printStackTrace();
  119.         }
  120.  
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement