protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int id = 0;
    try {
      id = Integer.parseInt(request.getParameter("accountId"));
    } catch (Exception e) {
      request.setAttribute("noAccountSelected", true);
    }

    Customer customer = (Customer) request.getSession().getAttribute("user");
    List<Account> accounts = accountService.readUserAccounts(customer);

    for (Account account : accounts) {
      if (account.getId() == id) {
        request.setAttribute("selectedAccount", account);
      }
    }

    request.setAttribute("accounts", accounts);
    request.getRequestDispatcher("/customer/listOperations.jsp").forward(request, response);
  }
Пример #2
0
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    Customer customer = (Customer) request.getAttribute("customer");

    if (customer == null) {
      request.setAttribute(
          "generalError",
          "An error occured. Please try again. If the problem persists, contact the support");
      doGet(request, response);
      return;
    }

    Account account = new Account();

    int interestPlanId = Integer.parseInt(request.getParameter("plan"));
    InterestPlan plan = genericCrudService.read(InterestPlan.class, interestPlanId);

    account.setName(request.getParameter("name"));

    if (!ValidationUtil.validate(account, "name", request)) {
      request.setAttribute("customer", customer);
      request.setAttribute("account", account);
      doGet(request, response);
      return;
    }

    account.setInterestPlan(plan);
    account.setAccountOwner(customer);
    account.setAmount(BigDecimal.ZERO);

    String outputServlet;

    if (genericCrudService.read(Customer.class, customer.getId()) == null) {
      Random rand = new Random(System.currentTimeMillis());
      String password = Long.toString(rand.nextInt() + rand.nextInt());

      try {
        customer.setPassword(passwordService.createHash(password));
      } catch (Exception e) {
        e.printStackTrace();
      }

      genericCrudService.create(customer);
      mailService.sendConfirmationMail(customer, password);
      outputServlet = "/admin/customers";
    } else {
      outputServlet = "/admin/accounts?id=" + customer.getId();
    }

    accountService.create(account);

    if (customer.getAccounts() == null) {
      customer.setAccounts(new ArrayList<Account>());
    }

    customer.getAccounts().add(account);

    genericCrudService.update(customer);

    response.sendRedirect(getServletContext().getContextPath() + outputServlet);
  }