View Javadoc

1   package uk.ac.ebi.intenz.webapp.controller.modification;
2   
3   import org.apache.log4j.Logger;
4   import org.apache.struts.action.*;
5   import org.apache.struts.taglib.html.Constants;
6   import uk.ac.ebi.intenz.webapp.dtos.*;
7   import uk.ac.ebi.intenz.webapp.utilities.ControlFlowToken;
8   import uk.ac.ebi.intenz.webapp.utilities.EntryLockSingleton;
9   
10  import javax.servlet.http.HttpServletRequest;
11  import javax.servlet.http.HttpServletResponse;
12  import java.util.Map;
13  
14  /**
15   * This class checks to see if the entry is still valid.
16   * If its not it reloads it. 
17   *
18   * @author Michael Darsow
19   * @version $Revision: 1.2 $ $Date: 2008/01/28 12:33:07 $
20   */
21  public class CurationAction extends Action {
22  
23    private static final Logger LOGGER =
24  	  Logger.getLogger(CurationAction.class.getName());
25    private final static String SEARCH_BY_ID_ACTION_FWD = "searchId";
26  
27    public ActionForward execute(ActionMapping mapping,
28                                 ActionForm form,
29                                 HttpServletRequest request,
30                                 HttpServletResponse response) throws Exception {
31      LOGGER.info("CurationAction");
32      ActionMessages errors = new ActionMessages();
33  //    EnzymeDTO enzymeDTO = (EnzymeDTO) request.getSession().getAttribute("enzymeDTO");
34      EnzymeDTO enzymeDTO = (EnzymeDTO) form;
35      if(enzymeDTO.getId() == null || enzymeDTO.getId().equals("")) return null; // new enzymes do not need token checking
36      Long enzymeId = new Long(enzymeDTO.getId());
37  
38      // Check if the current entry is still valid.
39      if (request.getParameter(Constants.TOKEN_KEY) != null && !ControlFlowToken.isValid(request)) {
40        Map tokenHashtable = (Map) request.getSession().getAttribute("tokenHashtable");
41        LOGGER.debug("Entry with ID " + enzymeId + " is being discarded ...");
42        enzymeId = (Long) tokenHashtable.get(request.getParameter(Constants.TOKEN_KEY));
43        LOGGER.debug("... entry with ID " + enzymeId + " will be loaded instead.");
44  
45        // Store a message to inform the user about the reload.
46        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.application.entry.reloaded.noec"));
47        saveErrors(request, errors);
48  
49        // Set view where to forward to after the entry has been reloaded.
50        ActionForward forward = mapping.findForward(SEARCH_BY_ID_ACTION_FWD);
51        StringBuffer path = new StringBuffer(forward.getPath());
52        boolean isQuery = (path.indexOf("?") > -1);
53        if (isQuery) {
54          path.append("&view=" + mapping.getInputForward().getPath() + "&reload=true&id=");
55        } else {
56          path.append("?view=" + mapping.getInputForward().getPath() + "&reload=true&id=");
57        }
58        path.append(enzymeId);
59  
60        return new ActionForward(path.toString());
61      }
62  
63      return null;
64    }
65  
66    /**
67     * Keeps the token as it is to ensure that the entry can be reloaded if necessary.
68     *
69     * @param request Used to transfer the token parameter to the token attribute.
70     * @throws NullPointerException if <code>request</code> is <code>null</code>
71     */
72    protected void keepToken(HttpServletRequest request) {
73      if (request == null) throw new NullPointerException("Parameter 'request' must not be null.");
74      if (request.getParameter(Constants.TOKEN_KEY) != null) request.setAttribute(Constants.TOKEN_KEY, request.getParameter(Constants.TOKEN_KEY));
75    }
76  }