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.Conference;
21  import edu.drexel.cmsDb.datamodel.User;
22  
23  public class NewPaperInterceptor extends ActionSupport implements Interceptor{
24    private static final long serialVersionUID = 20070504L;
25    private static final Log LOG = LogFactory.getLog(NewPaperInterceptor.class);
26    private static final String PAGE_ERROR = "error.new.paper.page";
27    
28    public void destroy() {}
29    public void init() {}
30  
31    public String intercept(ActionInvocation ai) throws Exception {
32      HttpServletRequest request = ServletActionContext.getRequest();
33      try{
34        User user = (User)request.getSession().getAttribute(SessionConstants.USER_ATTRIBUTE);
35        if(user == null){
36          addErrors(ai, getText(PLEASE_LOGIN));
37          return ActionSupport.LOGIN;
38        }
39        Long conferenceId = Long.valueOf(request.getParameter("conferenceId"));
40        Conference conference = CmsDAO.getConference(user, conferenceId, false);
41        request.setAttribute(SessionConstants.CONFERENCE_ATTRIBUTE, conference);
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  }