/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.octal.supinbank.servlet;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.octal.supinbank.dao.AccountDao;
import net.octal.supinbank.dao.CustomerDao;
import net.octal.supinbank.entity.Account;
import net.octal.supinbank.entity.AccountType;
import net.octal.supinbank.entity.Person;
import net.octal.supinbank.service.CustomerService;
import net.octal.tools.base.GenerationException;
import net.octal.tools.base.Generator;
import net.octal.tools.base.PasswordGenerator;
/**
*
* @author octal
*/
@WebServlet(name = "AddAccountServlet", urlPatterns = {"/auth/admin/addAccount"})
public class AddAccountServlet extends HttpServlet {
@EJB
private CustomerDao customerDao;
@EJB
private CustomerService customerService;
@EJB
private AccountDao accountDao;
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final String action = request.getParameter("action");
if (action != null && action.equals("cancel")) {
final HttpSession session = request.getSession();
if (session.getAttribute("customer") != null) {
session.removeAttribute("customer");
}
response.sendRedirect(getServletContext().getContextPath() + "/auth/admin/advisor");
return;
}
/*
final Map<String, String> interestPlans = new HashMap<String, String>();
for (AccountType at : AccountType.values()) {
interestPlans.put(at.name(), at.getDisplayName());
}
*/
request.setAttribute("interestPlans", AccountType.values());
try {
final String sCustomerId = request.getParameter("customerId");
if (sCustomerId != null) {
final Long customerId = Long.parseLong(sCustomerId);
final Person customer = customerDao.findCustomerById(customerId);
request.setAttribute("customer", customer);
}
} catch (NumberFormatException e) {
}
request.getRequestDispatcher("../../addAccount.jsp").forward(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final String accountName = request.getParameter("accountName");
final String interestPlan = request.getParameter("interestPlan");
final String customerId = request.getParameter("customerId");
Account account = new Account();
account.setName(accountName);
account.setTotalAmount(0.0);
account.setType(AccountType.valueOf(interestPlan));
/* The customerId is empty, which means we must create the Customer */
if (customerId.isEmpty())
{
final Person customer = (Person) request.getSession().getAttribute("customer");
PasswordGenerator generator = Generator.newPasswordGenerator(8, true, true, true, true);
try {
final String password = generator.generate();
HashFunction hf = Hashing.sha1();
HashCode hc = hf.newHasher()
.putString(password)
.hash();
customer.setPassword(hc.toString());
customer.addAccount(account);
customerService.processCustomer(customer, password);
response.sendRedirect(getServletContext().getContextPath() +
String.format("/auth/admin/customer?id=%d", customer.getId()));
} catch (GenerationException ex) {
}
}
else
{
try {
final Long id = Long.parseLong(customerId);
final Person customer = customerDao.findCustomerById(id);
if (customer != null) {
customer.addAccount(account);
accountDao.addAccount(account);
response.sendRedirect(getServletContext().getContextPath() +
String.format("/auth/admin/customer?id=%d", id));
}
else
{
response.sendRedirect(getServletContext().getContextPath() + "/auth/admin/advisor");
}
}
catch (NumberFormatException ex) {
}
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}