View Javadoc

1   package edu.drexel.cms.interceptors;
2   
3   import static edu.drexel.cms.commons.MessageKeys.INVALID_DATA;
4   import static edu.drexel.cms.commons.MessageKeys.PLEASE_LOGIN;
5   
6   import javax.servlet.http.HttpServletRequest;
7   
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  import org.apache.struts2.ServletActionContext;
11  
12  import com.opensymphony.xwork2.ActionInvocation;
13  import com.opensymphony.xwork2.ActionSupport;
14  import com.opensymphony.xwork2.ValidationAware;
15  import com.opensymphony.xwork2.interceptor.Interceptor;
16  
17  import edu.drexel.cms.actions.SessionConstants;
18  import edu.drexel.cms.commons.CmsDataValidationException;
19  import edu.drexel.cms.data.CmsDAO;
20  import edu.drexel.cmsDb.datamodel.Paper;
21  import edu.drexel.cmsDb.datamodel.User;
22  
23  public class NewReviewInterceptor extends ActionSupport implements Interceptor{
24    private static final long serialVersionUID = 20070504L;
25    private static final Log LOG = LogFactory.getLog(NewReviewInterceptor.class);
26    private static final String PAGE_ERROR = "error.new.review.page";
27    public void destroy() {}
28    public void init() {}
29  
30    public String intercept(ActionInvocation ai) throws Exception {
31      HttpServletRequest request = ServletActionContext.getRequest();
32      try{
33        User user = (User)request.getSession().getAttribute(SessionConstants.USER_ATTRIBUTE);
34        if(user == null){
35          addErrors(ai, getText(PLEASE_LOGIN));
36          return ActionSupport.LOGIN;
37        }
38        String paperId = request.getParameter("paperId");
39        Paper paper = CmsDAO.getPaper(user, Long.valueOf(paperId));      
40        request.setAttribute(SessionConstants.PAPER_ATTRIBUTE, paper);
41        
42      }catch (CmsDataValidationException e) {
43        LOG.error(getText(INVALID_DATA), e);
44        addErrors(ai, e.getMessage());
45      }
46      catch (Exception e) {
47        LOG.error(getText(PAGE_ERROR), e);
48        addErrors(ai, getText(PAGE_ERROR));
49      }
50      return ai.invoke();
51    }
52    private void addErrors(ActionInvocation ai, String message){
53      if (ai.getAction() instanceof ValidationAware) {
54        ValidationAware action = (ValidationAware) ai.getAction();
55        action.addActionError(message);
56      }
57    }
58  }