/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.octal.supinbank.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.jms.*;
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.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import net.octal.supinbank.dao.AccountDao;
import net.octal.supinbank.dao.OperationDao;
import net.octal.supinbank.entity.Account;
import net.octal.supinbank.entity.Operation;
import net.octal.supinbank.entity.Person;
import net.octal.banking.BasicBankAccountNumber;
import net.octal.supinbank.service.AccountService;
import net.octal.supinbank.service.TransfertService;
/**
*
* @author octal
*/
@WebServlet(name = "TransfertServlet", urlPatterns = {"/auth/customer/transfert"})
public class TransfertServlet extends HttpServlet {
private static final String ESTABLISHMENT_CODE = "66506";
@EJB
private TransfertService transfertService;
@EJB
private AccountService accountService;
// <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 Person customer = (Person) request.getSession().getAttribute("customer");
final List<Account> accounts = accountService.getAccountsByCustomer(customer);
request.setAttribute("accounts", accounts);
request.getRequestDispatcher("../../transfert.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 Long accountFromId = Long.parseLong(request.getParameter("accountFrom"));
final String scope = request.getParameter("scope");
final Double amount = Double.parseDouble(request.getParameter("amount"));
final String description = request.getParameter("description");
/* Must perform an internal transfert */
if (scope.equals("internal")) {
final Long accountToId = Long.parseLong(request.getParameter("accountTo"));
final Account accountFrom = accountService.findAccountById(accountFromId);
final Account accountTo = accountService.findAccountById(accountToId);
if (accountFrom != null && accountTo != null) {
if (amount > accountFrom.getTotalAmount()) {
request.setAttribute("error", true);
request.setAttribute("errorString", "You can't perform this transfert with your account amount");
}
else if (accountFromId.equals(accountToId)) {
request.setAttribute("error", true);
request.setAttribute("errorString", "You can't transfert to the same account");
}
else
{
transfertService.performInternalTransfert(accountFrom, accountTo, amount, description);
request.setAttribute("informationString", "Your transfert has succeeded");
}
doGet(request, response);
}
}
else if (scope.equals("external")) {
final Account accountFrom = accountService.findAccountById(accountFromId);
final String bbanEstablishmentCode = request.getParameter("bbanEstablishmentCode");
final String bbanBranchCode = request.getParameter("bbanBranchCode");
final String bbanAccountNumber = request.getParameter("bbanAccountNumber");
final String bbanKey = request.getParameter("bbanKey");
final BasicBankAccountNumber bban = new BasicBankAccountNumber(bbanEstablishmentCode,
bbanBranchCode, bbanAccountNumber, bbanKey);
if (amount > accountFrom.getTotalAmount()) {
request.setAttribute("error", true);
request.setAttribute("errorString", "You can't perform this transfert with your account amount");
}
/*
if (!bban.isKeyValid()) {
request.setAttribute("error", true);
request.setAttribute("errorString", "The BBAN Key is invalid");
doGet(request, response);
}
*/
else if (bbanEstablishmentCode.equals(ESTABLISHMENT_CODE)) {
final Long accountToId = Long.parseLong(
BasicBankAccountNumber.normalizedAccountNumber(bbanAccountNumber));
final Account accountTo = accountService.findAccountById(accountToId);
if (accountTo != null) {
transfertService.performAnotherCustomerTransfert(accountFrom, accountTo, bban.toString(),
amount, description);
request.setAttribute("informationString", "Your transfert has succeeded");
}
else
{
request.setAttribute("error", true);
request.setAttribute("errorString", "The account number is invalid");
}
}
else
{
boolean result = transfertService.performAnotherBankTransfert(accountFrom, bban,
ESTABLISHMENT_CODE, amount, description);
if (!result) {
request.setAttribute("error", true);
request.setAttribute("errorString", "An error occured during the transfert");
}
else
{
request.setAttribute("informationString", "Your transfert request has been sent to the central bank");
}
}
doGet(request, response);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}