package com.github.cstroe.spendhawk.web.transaction;

import com.github.cstroe.spendhawk.entity.Account;
import com.github.cstroe.spendhawk.entity.Transaction;
import com.github.cstroe.spendhawk.util.HibernateUtil;
import com.github.cstroe.spendhawk.web.AccountServlet;

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 java.io.IOException;

import static com.github.cstroe.spendhawk.util.ServletUtil.servletPath;

@WebServlet("/transaction")
public class TransactionView extends HttpServlet {

    private static final String TEMPLATE = "/template/transactions/view.ftl";

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String transactionIdRaw = request.getParameter("id");
            if(transactionIdRaw != null) {
                Long transactionId = Long.parseLong(transactionIdRaw);
                // Begin unit of work
                HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
                Transaction transaction = Transaction.findById(transactionId);
                if(transaction == null) {
                    throw new IllegalArgumentException("Transaction not found.");
                }
                request.setAttribute("transaction", transaction);
                request.setAttribute("expenses", transaction.getExpenses());
                request.getRequestDispatcher(TEMPLATE).forward(request,response);
            }
            // End unit of work
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
        } catch (Exception ex) {
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
            throw new ServletException(ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Account account;
        try {
            String transactionIdRaw = request.getParameter("id");
            if(transactionIdRaw != null) {
                Long transactionId = Long.parseLong(transactionIdRaw);
                // Begin unit of work
                HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
                Transaction transaction = Transaction.findById(transactionId);
                if(transaction == null) {
                    throw new IllegalArgumentException("Transaction not found.");
                }
                account = transaction.getAccount();
                transaction.delete();
            } else {
                throw new IllegalAccessException("Transaction id is required.");
            }
            // End unit of work
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
        } catch (Exception ex) {
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
            throw new ServletException(ex);
        }

        response.sendRedirect(request.getContextPath() + servletPath(AccountServlet.class) + "?id=" + account.getId());
    }
}

@@ -149,24 +149,74 @@
 ransaction;%0A
+import com.github.cstroe.spendhawk.util.DateUtil;%0A
 import com.g
@@ -250,32 +250,32 @@
 .HibernateUtil;%0A
-
 import com.githu
@@ -552,24 +552,47 @@
 IOException;
+%0Aimport java.util.Date;
 %0A%0Aimport sta
@@ -2207,16 +2207,44 @@
 ccount;%0A
+        Date effectiveDate;%0A
         
@@ -2770,32 +2770,96 @@
               %7D%0A
+                effectiveDate = transaction.getEffectiveDate();%0A
                 
@@ -3448,16 +3448,32 @@
 class) +
+%0A               
  %22?id=%22 
@@ -3489,16 +3489,101 @@
 .getId()
+ + %22&relDate=%22 + AccountServlet.formatter.format(DateUtil.asLocalDate(effectiveDate))
 );%0A    %7D

Fix deleting of Transactions.