View Javadoc
1   package uk.ac.ebi.intenz.webapp.controller.helper.forwards;
2   
3   import java.sql.Connection;
4   import java.util.Map;
5   
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   
9   import org.apache.log4j.Logger;
10  import org.apache.struts.action.ActionForm;
11  import org.apache.struts.action.ActionForward;
12  import org.apache.struts.action.ActionMapping;
13  import org.apache.struts.action.ActionMessage;
14  import org.apache.struts.action.ActionMessages;
15  import org.apache.struts.actions.ForwardAction;
16  import org.apache.struts.taglib.html.Constants;
17  
18  import uk.ac.ebi.intenz.domain.constants.Status;
19  import uk.ac.ebi.intenz.mapper.EnzymeEntryMapper;
20  import uk.ac.ebi.intenz.webapp.dtos.EnzymeDTO;
21  import uk.ac.ebi.intenz.webapp.utilities.ControlFlowToken;
22  import uk.ac.ebi.intenz.webapp.utilities.EntryLockSingleton;
23  
24  /**
25   * This class ...
26   *
27   * @author Michael Darsow
28   * @version $Revision: 1.3 $ $Date: 2008/11/17 17:14:10 $
29   */
30  public class ModifyEnzymeFWDAction extends ForwardAction {
31  
32    private static final Logger LOGGER =
33  	  Logger.getLogger(ModifyEnzymeFWDAction.class.getName());
34    private final static String SEARCH_BY_ID_ACTION_FWD = "searchId";
35    private final static String ERROR_FWD = "error";
36  
37    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
38                                 HttpServletResponse response) throws Exception {
39      EnzymeDTO enzymeDTO = (EnzymeDTO) request.getSession().getAttribute("enzymeDTO");
40      ActionMessages errors = new ActionMessages();
41      Long enzymeId = new Long(enzymeDTO.getId());
42  
43      // Check if the current entry is still valid.
44      if (request.getParameter(Constants.TOKEN_KEY) != null && !ControlFlowToken.isValid(request)) {
45        Map tokenHashtable = (Map) request.getSession().getAttribute("tokenHashtable");
46        LOGGER.debug("Entry with ID " + enzymeId + " is being discarded ...");
47        enzymeId = (Long) tokenHashtable.get(request.getParameter(Constants.TOKEN_KEY));
48        LOGGER.debug("... entry with ID " + enzymeId + " will be loaded instead.");
49  
50        // Store a message to inform the user about the reload.
51        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.application.entry.reloaded.noec"));
52        saveErrors(request, errors);
53  
54        // Set view where to forward to after the entry has been reloaded.
55        ActionForward forward = mapping.findForward(SEARCH_BY_ID_ACTION_FWD);
56        StringBuffer path = new StringBuffer(forward.getPath());
57        boolean isQuery = (path.indexOf("?") > -1);
58        if (isQuery) {
59          path.append("&view=INTENZ&reload=true&id=");
60        } else {
61          path.append("?view=INTENZ&reload=true&id=");
62        }
63        path.append(enzymeId);
64        request.setAttribute("title", "Reload");
65        return new ActionForward(path.toString());
66      }
67  
68      // Check if a clone already exists.
69      if (enzymeDTO.getStatusCode().equals(Status.APPROVED.getCode())) {
70        EnzymeEntryMapper enzymeEntryMapper = new EnzymeEntryMapper();
71        Connection con = (Connection) request.getSession().getAttribute("connection");
72        if (enzymeEntryMapper.cloneExists(enzymeId, con)) {
73          errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.application.clone.exists", enzymeDTO.getEc()));
74          saveErrors(request, errors);
75          request.setAttribute("title", "Error - IntEnz Curator Application");
76          return mapping.findForward(ERROR_FWD);
77        }
78      }
79  
80      ControlFlowToken.setToken(request, enzymeId); // Set token.
81  
82      // Try to lock the entry.
83      EntryLockSingleton els = (EntryLockSingleton) request.getSession().getServletContext().getAttribute("entryLock");
84      if (!els.setLock("" + enzymeId, request.getUserPrincipal().getName())) {
85        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.application.entry.locked", enzymeDTO.getEc()));
86        saveErrors(request, errors);
87        return mapping.findForward(ERROR_FWD);
88      }
89      
90      return super.execute(mapping, form, request, response);
91    }
92  }