View Javadoc

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