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

import com.github.cstroe.spendhawk.bean.CategoryManagerBean;
import com.github.cstroe.spendhawk.entity.Category;
import com.github.cstroe.spendhawk.entity.User;
import com.github.cstroe.spendhawk.util.Exceptions;
import com.github.cstroe.spendhawk.util.HibernateUtil;
import com.github.cstroe.spendhawk.web.user.UserSummaryServlet;

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

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

@WebServlet("/category/manage")
public class CategoryManagerServlet extends HttpServlet {

    private final static String TEMPLATE = "/template/category/manage.ftl";

    @EJB
    private CategoryManagerBean categoryManager;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long userId = Optional.ofNullable(req.getParameter("user.id"))
                .map(Long::parseLong)
                .orElseThrow(Exceptions::userIdRequired);

        try {
            // Begin unit of work
            HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();

            User currentUser = User.findById(userId).orElseThrow(Exceptions::userNotFound);
            req.setAttribute("user", currentUser);
            req.getRequestDispatcher(TEMPLATE).forward(req, resp);

            // End unit of work
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
        } catch (Exception ex) {
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
            throw ex;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long userId = Optional.ofNullable(req.getParameter("user.id"))
                .map(Long::parseLong)
                .orElseThrow(Exceptions::userIdRequired);

        String categoryName = req.getParameter("category.name");
        String action = req.getParameter("action");

        try {
            // Begin unit of work
            HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();

            User currentUser = User.findById(userId).orElseThrow(Exceptions::userNotFound);

            if("store".equals(action)) {
                Optional<Category> categoryOptional = categoryManager.createCategory(userId, categoryName);

                if(categoryOptional.isPresent()) {
                    resp.sendRedirect(req.getContextPath() + servletPath(UserSummaryServlet.class) + "?user.id=" + userId.toString());
                } else {
                    req.setAttribute("message", categoryManager.getMessage());
                    req.setAttribute("user", currentUser);
                    req.getRequestDispatcher(TEMPLATE).forward(req, resp);
                }
            }

            // End unit of work
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
        } catch (Exception ex) {
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
            throw new ServletException(ex);
        }
    }
}

@@ -1883,18 +1883,40 @@
   throw 
+new ServletException(
 ex
+)
 ;%0A      
@@ -2796,698 +2796,1044 @@
   re
-sp.sendRedirect(req.getContextPath() + servletPath(UserSummaryServlet.class) + %22?user.id=%22 + userId.toString());%0A                %7D else %7B%0A                    req.setAttribute(%22message%22, categoryManager.getMessage());%0A                    req.setAttribute(%22user%22, currentUser);%0A                    req.getRequestDispatcher(TEMPLATE).forward(req, resp);%0A                %7D%0A            %7D%0A%0A            // End unit of work%0A            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();%0A        %7D catch (Exception ex) %7B%0A            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();%0A            throw new ServletException(ex);%0A        %7D
+directToUserSummaryServlet(req, resp, userId);%0A                %7D else %7B%0A                    showError(req, resp, currentUser);%0A                %7D%0A            %7D%0A%0A            // End unit of work%0A            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();%0A        %7D catch (Exception ex) %7B%0A            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();%0A            throw new ServletException(ex);%0A        %7D%0A    %7D%0A%0A    private static void redirectToUserSummaryServlet(HttpServletRequest req, HttpServletResponse resp, Long userId) throws IOException %7B%0A        resp.sendRedirect(req.getContextPath() + servletPath(UserSummaryServlet.class, %22user.id%22, userId));%0A    %7D%0A%0A    private void showError(HttpServletRequest req, HttpServletResponse resp, User currentUser) throws IOException, ServletException %7B%0A        req.setAttribute(%22message%22, categoryManager.getMessage());%0A        req.setAttribute(%22user%22, currentUser);%0A        req.getRequestDispatcher(TEMPLATE).forward(req, resp);
 %0A   

Add helper method on CategoryManagerServlet.