package gov.nih.nci.calab.ui.core;

import gov.nih.nci.calab.exception.InvalidSessionException;
import gov.nih.nci.calab.exception.NoAccessException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public abstract class AbstractDispatchAction extends DispatchAction {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = null;

		// response.setHeader("Cache-Control", "no-cache");

		if (isCancelled(request))
			return mapping.findForward("cancel");
		// TODO fill in the common operations */
		if (!loginRequired() || loginRequired() && isUserLoggedIn(request)) {
			if (loginRequired()) {
				String dispatch = request.getParameter("dispatch");
				// check whether user have access to the class
				boolean accessStatus = canUserExecute(request.getSession());
				//if dispatch is setupView, download, or accessStatus is true don't throw exception
				if (dispatch!=null && !dispatch.equals("setupView") && !dispatch.equals("download") && !accessStatus) {
					throw new NoAccessException(
							"You don't have access to class: "
									+ this.getClass().getName());
				}
			}
			forward = super.execute(mapping, form, request, response);
		} else {
			throw new InvalidSessionException();
		}
		return forward;
	}

	/**
	 * 
	 * @param request
	 * @return whether the user is successfully logged in.
	 */
	private boolean isUserLoggedIn(HttpServletRequest request) {
		boolean isLoggedIn = false;
		if (request.getSession().getAttribute("user") != null) {
			isLoggedIn = true;
		}
		return isLoggedIn;
	}

	public abstract boolean loginRequired();

	/**
	 * Check whether the current user in the session can perform the action
	 * 
	 * @param session
	 * @return
	 * @throws Exception
	 */
	public boolean canUserExecute(HttpSession session) throws Exception {
		return InitSessionSetup.getInstance().canUserExecuteClass(session,
				this.getClass());
	}
}
@@ -849,51 +849,9 @@
 %22);%0A
-%09%09// TODO fill in the common operations */
 %0A
+
 %09%09if
@@ -1125,16 +1125,53 @@
 %09%09%09//if 
+have access or if have no access but 
 dispatch
@@ -1174,16 +1174,23 @@
 atch is 
+either 
 setupVie
@@ -1190,17 +1190,19 @@
 etupView
-,
+ or
  downloa
@@ -1206,72 +1206,87 @@
 load
-, or accessStatus is true don't throw exception%0A%09%09%09%09if (
+%0A%09%09%09%09//do forward%0A%09%09%09%09if (accessStatus%0A%09%09%09%09%09%09%7C%7C !accessStatus%0A%09%09%09%09%09%09&& 
 dispatch
 !=nu
@@ -1285,19 +1285,27 @@
 atch
+ 
 !=
+ 
 null
- && !
+%0A%09%09%09%09%09%09&& (
 disp
@@ -1333,20 +1333,19 @@
 w%22) 
-&& !
+%7C%7C 
 dispatch
 .equ
@@ -1332,32 +1332,41 @@
 ew%22) %7C%7C dispatch
+%0A%09%09%09%09%09%09%09%09
 .equals(%22downloa
@@ -1372,28 +1372,140 @@
 ad%22)
- && !accessStatus) %7B
+)) %7B%0A%09%09%09%09%09forward = super.execute(mapping, form, request, response);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09request.getSession().removeAttribute(%22user%22);
 %0A%09%09%09
@@ -1631,69 +1631,8 @@
 %09%09%7D%0A
-%09%09%09forward = super.execute(mapping, form, request, response);
 %0A%09%09%7D

update no access logic to reflect changes in tabs