/*
 * $Id: TabViewAction.java,v 1.3 2006/10/04 03:14:59 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: TabViewAction.java,v $
 * Revision 1.3  2006/10/04 03:14:59  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.2  2006/05/31 13:58:40  marbobo
 * + Bugfix
 *
 * Revision 1.1  2006/05/12 22:12:16  marbobo
 * + Initiale Version
 *
 */
package vkc.administration.action;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.util.List;

import vkc.administration.form.TabViewForm;
import vkc.misc.VKCLogManager;
import vkc.view.PageId;
import vkc.view.TabId;
import vkc.view.WebShare;
import vkc.view.bean.TabInfo;

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

public class TabViewAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/administration/TabView.jsp", false);

	public ActionForward execute(ActionMapping mapping,
								 ActionForm f,
								 HttpServletRequest request,
								 HttpServletResponse response)
	throws ServletException {
		try {
			TabViewForm form = (TabViewForm)f;
			
			if (request.getParameter("action.change.tabview") != null) {
				return handleChangeTabView(form, request);
			} 

			return handleInitializeAction(form, request);
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}

	protected ActionForward handleChangeTabView(TabViewForm form, HttpServletRequest request) {
		request.getSession().setAttribute(WebShare.SYSTEMVIEWTYPEID_KEY, form.getSelectedViewId());

		if (PageId.CATEGORY_VIEW.equals(form.getSelectedViewId())) {
			return new ActionForward("/vkcTabView.do?reloadMainview=true&selectedTabId=" + TabId.DOCUMENT_TAB 
									+ "&lastCategoryId=" + form.getLastCategoryId()
									+ "&lastFunctionalId=" + form.getLastFunctionalId()
									+ "&lastProjectId=" + form.getLastProjectId(), true);
		}
		
		if (PageId.FUNCTIONAL_VIEW.equals(form.getSelectedViewId()) || PageId.PROJECT_VIEW.equals(form.getSelectedViewId())) {
			return new ActionForward("/procobaTab.do?reloadMainview=true&viewId=" + form.getSelectedViewId() 
									+ "&selectedViewId=" + form.getSelectedViewId()
									+ "&lastCategoryId=" + form.getLastCategoryId()
									+ "&lastFunctionalId=" + form.getLastFunctionalId()
									+ "&lastProjectId=" + form.getLastProjectId(), true);
		}

		return DEFAULT_FORWARD;
	}

	protected ActionForward handleInitializeAction(TabViewForm form, HttpServletRequest request) throws ServletException {
		try {
			if (form.getSelectedTabId().intValue() <= 0 && form.getSelectedAddTabId().intValue() <= 0) {
				List tabInfos = form.getTabInfos();
				
				if (tabInfos.size() > 0) {
					TabInfo tabInfo = (TabInfo)tabInfos.get(0);
					form.setSelectedTabId(tabInfo.getTabId());
				}
			}

			return DEFAULT_FORWARD;
		} catch (Exception e) {
			VKCLogManager.errorLog(this, "", e.getMessage(), e);
			throw new ServletException(e);
		}
	}
}