Subversion Repositories bacoAlunos

Rev

Rev 201 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.web.controllers.blogs;

import pt.estgp.estgweb.web.controllers.AddRolesController;
import pt.estgp.estgweb.web.utils.RequestUtils;
import pt.estgp.estgweb.web.utils.DomainUtils;
import pt.estgp.estgweb.web.form.blogs.BlogForm;
import pt.estgp.estgweb.web.UserSessionProxy;
import pt.estgp.estgweb.web.exceptions.NoCookiesException;
import pt.estgp.estgweb.domain.*;
import pt.estgp.estgweb.domain.views.BlogView;
import pt.estgp.estgweb.domain.views.BlogPostView;
import pt.estgp.estgweb.filters.exceptions.NotFoundException;
import pt.utl.ist.berserk.logic.serviceManager.IServiceManager;
import pt.utl.ist.berserk.logic.serviceManager.ServiceManager;
import pt.utl.ist.berserk.logic.serviceManager.exceptions.FilterChainFailedException;
import pt.utl.ist.berserk.logic.serviceManager.exceptions.ServiceManagerException;
import pt.utl.ist.berserk.logic.serviceManager.exceptions.ExecutedFilterException;
import pt.utl.ist.berserk.logic.filterManager.exceptions.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import org.hibernate.ObjectNotFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.Serializable;

/**
 * @author Jorge Machado
 * @date 26/Fev/2008
 * @time 18:01:54
 * @see pt.estgp.estgweb.web
 */

public class BlogsController extends AddRolesController
{

    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(BlogsController.class);


    /**
     * Load and forward a blog
     * @param id
     * @param mapping
     * @param request
     * @param response
     * @return
     * @throws Throwable
     * @throws InvalidFilterException
     * @throws ServiceManagerException
     * @throws InvalidFilterExpressionException
     * @throws IncompatibleFilterException
     * @throws FilterRetrieveException
     * @throws NoCookiesException
     */

    private ActionForward loadBlogAndForward(long id, ActionMapping mapping, HttpServletRequest request, HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, InvalidFilterException, ServiceManagerException, InvalidFilterExpressionException, IncompatibleFilterException, FilterRetrieveException, NoCookiesException
    {
        BlogView bV = loadBlogView(id, mapping, request, response);
        request.setAttribute("BlogView", bV);
        return mapping.findForward("load");
    }

    /**
     * Load and forward a blog given a name
     * @param name
     * @param mapping
     * @param request
     * @param response
     * @return
     * @throws Throwable
     * @throws InvalidFilterException
     * @throws ServiceManagerException
     * @throws InvalidFilterExpressionException
     * @throws IncompatibleFilterException
     * @throws FilterRetrieveException
     * @throws NoCookiesException
     */


    private ActionForward loadBlogAndForward(1.5.0/docs/api/java/lang/String.html">String name, ActionMapping mapping, HttpServletRequest request, HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, InvalidFilterException, ServiceManagerException, InvalidFilterExpressionException, IncompatibleFilterException, FilterRetrieveException, NoCookiesException
    {
        BlogView bV = loadBlogView(name, mapping, request, response);
        request.setAttribute("BlogView", bV);
        return mapping.findForward("load");
    }

    /**
     * Load a Blog View
     * @param id
     * @param mapping
     * @param request
     * @param response
     * @return
     * @throws Throwable
     * @throws InvalidFilterException
     * @throws ServiceManagerException
     * @throws InvalidFilterExpressionException
     * @throws IncompatibleFilterException
     * @throws FilterRetrieveException
     * @throws NoCookiesException
     */

    private BlogView loadBlogView(long id, ActionMapping mapping, HttpServletRequest request, HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, InvalidFilterException, ServiceManagerException, InvalidFilterExpressionException, IncompatibleFilterException, FilterRetrieveException, NoCookiesException
    {
        IServiceManager sm = ServiceManager.getInstance();
        1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
        5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{id};
        try
        {
            BlogView bV = (BlogView) sm.execute(RequestUtils.getRequester(request, response), "LoadBlog", args, names);
            return bV;
        }
        catch (ExecutedFilterException e)
        {
            if (e.getCause() instanceof ObjectNotFoundException)
            {
                throw new NotFoundException(e.getCause().toString());
            }
            throw e;
        }
    }

    /**
     * Load a BlogView given a name
     * @param name
     * @param mapping
     * @param request
     * @param response
     * @return
     * @throws Throwable
     * @throws InvalidFilterException
     * @throws ServiceManagerException
     * @throws InvalidFilterExpressionException
     * @throws IncompatibleFilterException
     * @throws FilterRetrieveException
     * @throws NoCookiesException
     */

    private BlogView loadBlogView(1.5.0/docs/api/java/lang/String.html">String name, ActionMapping mapping, HttpServletRequest request, HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, InvalidFilterException, ServiceManagerException, InvalidFilterExpressionException, IncompatibleFilterException, FilterRetrieveException, NoCookiesException
    {
        IServiceManager sm = ServiceManager.getInstance();
        1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{};
        5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{name};
        //First will load ID and only than will call load Service to pass trought access filters with ID serializable available
        1.5.0/docs/api/java/lang/Long.html">Long id = (1.5.0/docs/api/java/lang/Long.html">Long) sm.execute(RequestUtils.getRequester(request, response), "LoadBlogIdByName", args, names);
        if (id == null)
            throw new NotFoundException("blog:" + name + " not found");
        return loadBlogView(id, mapping, request, response);
    }


    /**
     * Save or update a blog from a blogView
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Throwable
     * @throws ServletException
     */

    public ActionForward save(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            BlogForm blogForm = (BlogForm) form;
            ActionErrors errors = blogForm.validate(mapping, request);
            if (!errors.isEmpty())
            {
                saveErrors(request, errors);
                return mapping.findForward("submit");
            }

            IServiceManager sm = ServiceManager.getInstance();
            if (blogForm.getImage() != null && blogForm.getImage().getFileSize() != 0)
            {
                FormFile image = blogForm.getImage();
                1.5.0/docs/api/java/awt/Image.html">Image i = DomainUtils.getNewImage(image, blogForm.getBlogView().getTitle());
                blogForm.getBlogView().setImage(i);
            }
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"object"};
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{blogForm.getBlogView()};
            BlogView bV = blogForm.getBlogView();
            if (bV.getId() <= 0)
            {
                bV = (BlogView) sm.execute(RequestUtils.getRequester(request, response), "CreateBlog", args, names);
                addMessage(request, "blog.created.success", "" + bV.getId());
            }
            else
            {
                bV = (BlogView) sm.execute(RequestUtils.getRequester(request, response), "EditBlog", args, names);
                addMessage(request, "blog.edited.success", "" + bV.getId());
            }
            request.setAttribute("BlogView", bV);
            return mapping.findForward("load");

        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward load(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
            throws ServletException
    {
        try
        {
            return loadBlogAndForward(1.5.0/docs/api/java/lang/Long.html">Long.parseLong(request.getParameter("id")), mapping, request, response);
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (NotFoundException e)
        {
            return mapping.findForward("error404");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            return mapping.findForward("error500");
        }
    }

    public ActionForward loadByName(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
            throws ServletException
    {
        try
        {
            return loadBlogAndForward(request.getParameter("name"), mapping, request, response);
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (NotFoundException e)
        {
            return mapping.findForward("error404");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            return mapping.findForward("error500");
        }
    }

    public ActionForward addPost(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws ServletException
    {
        try
        {
            BlogView bV = loadBlogView(1.5.0/docs/api/java/lang/Long.html">Long.parseLong(request.getParameter("id")), mapping, request, response);
            request.setAttribute("BlogView", bV);
            BlogForm blogForm = (BlogForm) form;
            blogForm.setBlogView(bV);
            BlogPostView bPV = new BlogPostView();
            blogForm.setBlogPostView(bPV);
            request.setAttribute("BlogPostView", bPV);
            return mapping.findForward("load");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            return mapping.findForward("error500");
        }
    }

    public ActionForward savePost(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            BlogForm blogForm = (BlogForm) form;
            ActionErrors errors = blogForm.validatePost(mapping, request);
            if (!errors.isEmpty())
            {
                saveErrors(request, errors);
                BlogView bV = loadBlogView(blogForm.getBlogView().getId(), mapping, request, response);
                request.setAttribute("BlogView", bV);
                request.setAttribute("BlogPostView", blogForm.getBlogPostView());
                return mapping.findForward("load");
            }
            if (blogForm.getBlogPostView().getId() <= 0)
            {
                BlogPostView bPV = blogForm.getBlogPostView();

                IServiceManager sm = ServiceManager.getInstance();

                if (blogForm.getImage() != null && blogForm.getImage().getFileSize() != 0)
                {
                    FormFile image = blogForm.getImage();
                    1.5.0/docs/api/java/awt/Image.html">Image i = DomainUtils.getNewImage(image, bPV.getTitle());
                    bPV.setImage(i);
                }
                1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"object", "serializable"};
                5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{bPV, blogForm.getBlogView().getId()};
                bPV = (BlogPostView) sm.execute(RequestUtils.getRequester(request, response), "CreateBlogPost", args, names);
                addMessage(request, "blog.post.created.success", "" + bPV.getId());

                return loadBlogAndForward(blogForm.getBlogView().getId(), mapping, request, response);
            }
            else
            {
                BlogView bV = blogForm.getBlogView();
                IServiceManager sm = ServiceManager.getInstance();
                1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"object"};
                5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{bV};
                bV = (BlogView) sm.execute(RequestUtils.getRequester(request, response), "EditBlog", args, names);
                addMessage(request, "blog.edited.success", "" + bV.getId());
                return mapping.findForward("load");
            }
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward loadMonth(ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
            throws ServletException
    {
        try
        {
            int month = 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(request.getParameter("month"));
            int year = 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(request.getParameter("year"));
            long blodId = 1.5.0/docs/api/java/lang/Long.html">Long.parseLong(request.getParameter("id"));
            IServiceManager sm = ServiceManager.getInstance();
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{blodId, month, year};
            BlogView bV = (BlogView) sm.execute(RequestUtils.getRequester(request, response), "LoadBlogByDate", args, names);
            request.setAttribute("BlogView", bV);
            return mapping.findForward("loadMonth");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            return mapping.findForward("error500");
        }
    }


    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {
            UserSessionImpl sess = (UserSessionImpl) UserSessionProxy.loadUserSession(request, response);
            IServiceManager sm = ServiceManager.getInstance();
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{new 1.5.0/docs/api/java/lang/Long.html">Long(request.getParameter("id"))};
            BlogView bV = (BlogView) sm.execute(RequestUtils.getRequester(request, response), "LoadEditBlog", args, names);
            BlogForm blogForm = (BlogForm) form;
            blogForm.setBlogView(bV);
            blogForm.setSelectedRoles(bV.getTargetRoles());
            sess.put(TARGET_ROLES_KEY, (1.5.0/docs/api/java/io/Serializable.html">Serializable) bV.getTargetRoles());
            sess.serialize(request, response);
            return mapping.findForward("submit");
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }
    }

    public ActionForward delete(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {

            IServiceManager sm = ServiceManager.getInstance();
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{new 1.5.0/docs/api/java/lang/Long.html">Long(request.getParameter("id"))};
            1.5.0/docs/api/java/lang/Boolean.html">Boolean status = (1.5.0/docs/api/java/lang/Boolean.html">Boolean) sm.execute(RequestUtils.getRequester(request, response), "DeleteBlog", args, names);
            if (status)
            {
                addMessage(request, "blog.deleted.success", "" + request.getParameter("id"));
            }
            else
            {
                addMessage(request, "blog.deleted.fail", "" + request.getParameter("id"));
            }
            return mapping.getInputForward();
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }

    }

    public ActionForward deletePost(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
            throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable, ServletException
    {
        try
        {

            IServiceManager sm = ServiceManager.getInstance();
            1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
            5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{new 1.5.0/docs/api/java/lang/Long.html">Long(request.getParameter("id"))};
            1.5.0/docs/api/java/lang/Boolean.html">Boolean status = (1.5.0/docs/api/java/lang/Boolean.html">Boolean) sm.execute(RequestUtils.getRequester(request, response), "DeleteBlogPost", args, names);
            if (status)
            {
                addMessage(request, "blog.post.deleted.success", "" + request.getParameter("id"));
            }
            else
            {
                addMessage(request, "blog.post.deleted.fail", "" + request.getParameter("id"));
            }
            return loadBlogAndForward(new 1.5.0/docs/api/java/lang/Long.html">Long(request.getParameter("blogId")), mapping, request, response);
        }
        catch (FilterChainFailedException e)
        {
            return mapping.findForward("error401");
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            throw e;
        }

    }
}