Package org.nextime.ion.backoffice.action.content

Source Code of org.nextime.ion.backoffice.action.content.EditPublicationAction

package org.nextime.ion.backoffice.action.content;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.nextime.ion.backoffice.action.BaseAction;
import org.nextime.ion.backoffice.form.EditPublicationForm;
import org.nextime.ion.backoffice.exception.BackofficeSecurityException;
import org.nextime.ion.backoffice.security.SecurityManagerImpl;

import org.nextime.ion.framework.business.Publication;
import org.nextime.ion.framework.business.User;
import org.nextime.ion.framework.helper.FormGenerator;
import org.nextime.ion.framework.mapping.Mapping;

public class EditPublicationAction extends BaseAction {

  public ActionForward perform(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

    // check if user is correctly logged
    checkUser(request);

    // get the form
    EditPublicationForm sform = (EditPublicationForm) form;
    ActionErrors errors = sform.myValidate(request);

    // retrieve id
    String id =
      (request.getAttribute("id") != null)
        ? request.getAttribute("id").toString()
        : request.getParameter("id").toString();

    // check if publication can be edited
    try {
      Mapping.begin();
      if (!new SecurityManagerImpl().canEditPublication(Publication.getInstance(id), User.getInstance(request.getSession().getAttribute("userLogin")+""))) {
        throw new Exception();
      }
    } catch (Exception e) {
      throw new BackofficeSecurityException();
    } finally {
      Mapping.rollback();
    }

    // user need cancel
    if (request.getParameter("cancel") != null) {
      try {
        Mapping.begin();
        Publication publication = Publication.getInstance(id);

        //        LockManager locks =
        //          (LockManager) servlet.getServletContext().getAttribute(
        //            "locks");
        //        locks.unlock(publication);

        Mapping.rollback();
      } catch (Exception e) {
        Mapping.rollback();
        throw new ServletException(e);
      }
      // Forward to the next page
      return (mapping.findForward("cancel"));
    }

    // fill data | first time
    if (request.getParameter("itsOk") == null) {
      try {
        Mapping.begin();
        Publication publication = Publication.getInstance(id);
        String editForm = FormGenerator.getForm(publication);

        //        LockManager locks =
        //          (LockManager) servlet.getServletContext().getAttribute(
        //            "locks");
        //           
        //        if( locks.isLocked(publication) ) {
        //          throw new Exception("La publication "+publication.getId()+" est bloqu�e par un autre utilisateur." );
        //        }
        //           
        //        locks.lock(
        //          publication,
        //          User.getInstance(
        //            request.getSession().getAttribute("userLogin") + ""));

        Mapping.rollback();

        request.setAttribute("publication", publication);
        request.setAttribute("editForm", editForm);
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        sform.setDate(formatter.format(publication.getDate()));

      } catch (Exception e) {
        Mapping.rollback();
        throw new ServletException(e);
      }

      // Forward to the view page
      return (mapping.findForward("view"));
    }

    // fill data | errors
    if (errors.size() > 0) {
      try {
        Mapping.begin();
        Publication publication = Publication.getInstance(id);
        Hashtable data = new Hashtable();
        Enumeration names = request.getParameterNames();
        while (names.hasMoreElements()) {
          String name = names.nextElement().toString();
          data.put(name, request.getParameter(name));
        }
        String editForm =
          FormGenerator.getForm(publication.getType(), data);
        Mapping.rollback();

        request.setAttribute("publication", publication);
        request.setAttribute("editForm", editForm);
        request.setAttribute(ERROR_KEY, errors);
      } catch (Exception e) {
        Mapping.rollback();
        throw new ServletException(e);
      }

      // Forward to the view page
      return (mapping.findForward("view"));
    }

    // all it's ok : update publication
    try {
      Mapping.begin();

      Publication publication = Publication.getInstance(id);
      Hashtable data = new Hashtable();
      Enumeration names = request.getParameterNames();
      while (names.hasMoreElements()) {
        String name = names.nextElement().toString();
        String value = request.getParameter(name);
        if (!value.trim().equals("")) {
          data.put(name, request.getParameter(name));
        }
      }
      publication.setData(data, true);
      SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
      publication.setDate(formatter.parse(sform.getDate()));

      //      LockManager locks =
      //        (LockManager) servlet.getServletContext().getAttribute("locks");
      //      locks.unlock(publication);

      Mapping.commit();
    } catch (Exception e) {
      Mapping.rollback();
      throw new ServletException(e);
    }

    // Forward to the next page
    return (mapping.findForward("ok"));
  }

}
TOP

Related Classes of org.nextime.ion.backoffice.action.content.EditPublicationAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.