/*
 * $Id: TabViewForm.java,v 1.7 2007/08/08 12:14:59 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: TabViewForm.java,v $
 * Revision 1.7  2007/08/08 12:14:59  marbobo
 * + License-Funktionalität komplett umgeschrieben
 * + Bugfix bzgl. Beschreibungsfelder von Entitäten
 * + Bugfix mailing
 *
 * Revision 1.6  2006/11/06 12:40:09  korowa
 * bugfixing
 *
 * Revision 1.5  2006/11/06 10:48:59  korowa
 * Tabview License und  Admintabview License hinzugefügt
 *
 * Revision 1.4  2006/09/07 01:54:03  marbobo
 * + TabIds überarbeitet
 *
 * Revision 1.3  2006/06/08 21:36:08  marbobo
 * + Tabnamen verkürzt
 *
 * Revision 1.2  2006/06/01 19:43:19  marbobo
 * + Statistik-Tab kann aktiviert und deaktiviert werden
 *
 * Revision 1.1  2006/05/12 22:12:16  marbobo
 * + Initiale Version
 *
 */
package vkc.administration.form;

import org.apache.struts.action.ActionMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import vkc.db.entities.VKCCategory;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.db.serializer.VKCTabViewSerializer;
import vkc.help.form.HelpViewForm;
import vkc.view.PageId;
import vkc.view.TabId;
import vkc.view.WebShare;
import vkc.view.bean.TabInfo;
import vkc.view.form.BasicActionForm;

public class TabViewForm extends BasicActionForm {
	public static final int NUM_ADDITIONAL_TABS = 20;

	protected boolean reloadMainview = false;

	protected Integer lastCategoryId = new Integer(VKCCategory.ROOT_CATEGORY_ID);
	protected Integer lastFunctionalId = new Integer(-1);
	protected Integer lastProjectId = new Integer(-1);
	protected Integer selectedAddTabId = null;
	protected Integer selectedTabId = null;
	protected Integer selectedViewId = PageId.ADMINISTRATION_VIEW;
	protected Integer viewId = null;

	protected List additionalTabInfos = new ArrayList();
	protected List systemTabInfos = new ArrayList();
	protected List tabInfos = new ArrayList();

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		super.reset(mapping, request);

		reloadMainview = false;

		lastCategoryId = new Integer(VKCCategory.ROOT_CATEGORY_ID);
		lastFunctionalId = new Integer(-1);
		lastProjectId = new Integer(-1);
		selectedAddTabId = new Integer(-1);
		selectedTabId = new Integer(-1);
		selectedViewId = PageId.ADMINISTRATION_VIEW;
		viewId = PageId.ADMINISTRATION_VIEW;

		additionalTabInfos = new ArrayList();
		systemTabInfos = new ArrayList();
		tabInfos = new ArrayList();
	}

	public Integer getViewId() {
		return viewId;
	}

	public void setViewId(Integer integer) {
		viewId = integer;
		additionalTabInfos = new ArrayList();
		systemTabInfos = new ArrayList();
		tabInfos = new ArrayList();
	}

	public Integer getSelectedTabId() {
		return selectedTabId;
	}

	public void setSelectedTabId(Integer integer) {
		selectedTabId = integer;
	}

	public List getTabInfos() {
		if (getUser() == null) {
			return new ArrayList();
		}

		if (tabInfos == null || tabInfos.isEmpty()) {
			HashMap tabViews = VKCTabViewSerializer.getTabViews(getLocale(), getResources());
			tabInfos = new ArrayList();

			TabInfo tabInfo = (TabInfo)tabViews.get(new Integer(TabId.ADMIN_USERMANAGEMENT_TAB));
			
			if (tabInfo.isEnabled()) {
				if (getUser().hasAdminPrivileges() || getUser().isDedicatedUsersAdmin()) {
					tabInfo.setTabLink("/userList.do");
					tabInfos.add(tabInfo);
				}
			}
			
			tabInfo = (TabInfo)tabViews.get(new Integer(TabId.ADMIN_GROUPMANAGEMENT_TAB));

			if (tabInfo.isEnabled()) {
				if (getUser().hasAdminPrivileges() || getUser().isDedicatedGroupsAdmin()) {
					tabInfo.setTabLink("/groupList.do");
					tabInfos.add(tabInfo);
				}
			}
			
			if (getUser().hasAdminPrivileges()) {
				tabInfo = (TabInfo)tabViews.get(new Integer(TabId.ADMIN_ACTIVEUSERS_TAB));

				if (tabInfo.isEnabled()) {
					tabInfo.setTabLink("/activeUserList.do");
					tabInfos.add(tabInfo);
				}
				
				tabInfo = (TabInfo)tabViews.get(new Integer(TabId.ADMIN_SENDEMAIL_TAB));

				if (tabInfo.isEnabled()) {
					tabInfo.setTabLink("/sendEmail.do");
					tabInfos.add(tabInfo);
				}
				
				tabInfo = (TabInfo)tabViews.get(new Integer(TabId.ADMIN_SYSTEMPARAMETERS_TAB));

				if (tabInfo.isEnabled()) {
					tabInfo.setTabLink("/systemParameters.do");
					tabInfos.add(tabInfo);
				}
				
				tabInfo = (TabInfo)tabViews.get(new Integer(TabId.ADMIN_SYSTEMDEFAULTS_TAB));

				if (tabInfo.isEnabled()) {
					tabInfo.setTabLink("/systemDefaults.do");
					tabInfos.add(tabInfo);
				}
				
				
				tabInfo = (TabInfo)tabViews.get(new Integer(TabId.ADMIN_SYSTEMMAINTENANCE_TAB));

				if (tabInfo.isEnabled()) {
					tabInfo.setTabLink("/systemMaintenance.do");
					tabInfos.add(tabInfo);
				}
				
				tabInfo = (TabInfo)tabViews.get(new Integer(TabId.ADMIN_NEWSMANAGEMENT_TAB));

				if (tabInfo.isEnabled()) {
					tabInfo.setTabLink("/newsList.do");
					tabInfos.add(tabInfo);
				}
				
				tabInfo = (TabInfo)tabViews.get(new Integer(TabId.ADMIN_SHOWSTATISTICS_TAB));

				if (tabInfo.isEnabled()) {
					if (Integer.parseInt(WebShare.getSysParameterValue("STATISTICS", "EnableStatistics", "0")) != 0){   
						tabInfo.setTabLink("/showStatistics.do");
						tabInfos.add(tabInfo);
					} 
				}
				
				tabInfo = (TabInfo)tabViews.get(new Integer(TabId.ADMIN_LDAP_TAB));

				if (tabInfo.isEnabled()) {
			        if (Integer.parseInt(WebShare.getSysParameterValue("LDAP", "EnableLdapSupport", "0")) != 0){   
						tabInfo.setTabLink("/ldapServerList.do");
						tabInfos.add(tabInfo);
					} 
				}
				
				tabInfo = (TabInfo)tabViews.get(new Integer(TabId.ADMIN_LICENSE_TAB));

				if (tabInfo.isEnabled()) {
			        if (Integer.parseInt(WebShare.getSysParameterValue("LICENSE", "EnableLicensemanagement", "0")) != 0){   
						tabInfo.setTabLink("/licenseList.do");
						tabInfos.add(tabInfo);
					} 
				}
			}
		}

		return tabInfos;
	}

	public void setTabInfos(List list) {
		tabInfos = list != null ? list : new ArrayList();
	}

	protected int getSelectedTabIndex() {
		for (int i = 0; i < getTabInfos().size(); i++) {
			TabInfo info = (TabInfo)getTabInfos().get(i);
			
			if (info.getTabId().intValue() == getSelectedTabId().intValue()) {
				return i;
			}
		}
		
		return -1;
	}

	public TabInfo getFirstTabInfo() {
		if (getTabInfos().size() > 0) {
			return (TabInfo)getTabInfos().get(0);
		} 

		return null;
	}

	public TabInfo getLastTabInfo() {
		if (getTabInfos().size() > 0) {
			return (TabInfo)getTabInfos().get(getTabInfos().size() - 1);
		} 

		return null;
	}

	public TabInfo getFirstLeftInvisibleTabInfo() {
		if (getHasMoreLeftSideTabs()) {
			return (TabInfo)getTabInfos().get(getSelectedTabIndex() - NUM_ADDITIONAL_TABS + 1);
		} 

		return null;
	}

	public TabInfo getFirstRightInvisibleTabInfo() {
		if (getHasMoreRightSideTabs()) {
			return (TabInfo)getTabInfos().get(getSelectedTabIndex() + NUM_ADDITIONAL_TABS - 1);
		} 

		return null;
	}

	public boolean getHasMoreLeftSideTabs() {
		int tabIndex = getSelectedTabIndex();

		if (tabIndex < 0) {
			return false;
		} 

		return tabIndex - NUM_ADDITIONAL_TABS > 0;
	}

	public boolean getHasMoreRightSideTabs() {
		int tabIndex = getSelectedTabIndex();

		if (tabIndex < 0) {
			return false;
		} 

		return getTabInfos().size() > tabIndex + (NUM_ADDITIONAL_TABS + 1) ;
	}

	public List getVisibleTabInfos() {
		List tabInfos = getTabInfos();
		
		if (tabInfos.size() <= (NUM_ADDITIONAL_TABS * 2 + 1)) {
			return tabInfos;
		}

		List visibleTabInfos = new ArrayList();

		int selectedIndex = getSelectedTabIndex();
		int startIndex = selectedIndex - NUM_ADDITIONAL_TABS;
		int endIndex   = selectedIndex + NUM_ADDITIONAL_TABS;

		if (startIndex < 0) {
			endIndex -= startIndex;
			startIndex = 0;
		}

		if (endIndex >= tabInfos.size()) {
			endIndex = tabInfos.size() - 1;
		}
		
		for (int i = startIndex; i <= endIndex; i++) {
			visibleTabInfos.add(tabInfos.get(i));
		}

		return visibleTabInfos;
	}

	public Integer getSelectedViewId() {
		return selectedViewId;
	}

	public void setSelectedViewId(Integer integer) {
		selectedViewId = integer;
	}
	
	public boolean isReloadMainview() {
		return reloadMainview;
	}

	public void setReloadMainview(boolean reloadMainview) {
		this.reloadMainview = reloadMainview;
	}

	public String getReloadPath() {
		if (getUser().hasAdminPrivileges() || getUser().isDedicatedUsersAdmin()) {
			return "/userList.do";
		}
		
		if (getUser().isDedicatedGroupsAdmin()) {
			return "/groupList.do";
		}
		
		return "";
	}
	
	public List getAdditionalTabInfos() {
		if (getUser() == null) {
			return new ArrayList();
		}

		if (additionalTabInfos == null || additionalTabInfos.isEmpty()) {
			HashMap tabViews = VKCTabViewSerializer.getTabViews(getLocale(), getResources());
			additionalTabInfos = new ArrayList();
			
			// Help-Tab
			vkc.view.bean.TabInfo tabInfo = (vkc.view.bean.TabInfo)tabViews.get(new Integer(TabId.HELP_TAB));
			
			if (tabInfo.isEnabled()) {
				tabInfo.setTabLink("/helpView.do?viewId=" + HelpViewForm.ADMIN_VIEW);
				tabInfo.setTabDescription(getResources().getMessage(getLocale(), "vkc.open.help"));
				additionalTabInfos.add(tabInfo);
			}
		}
		
		return additionalTabInfos;
	}

	public void setAdditionalTabInfos(List additionalTabInfos) {
		this.additionalTabInfos = additionalTabInfos != null ? additionalTabInfos : new ArrayList();
	}

	public Integer getSelectedAddTabId() {
		return selectedAddTabId;
	}

	public void setSelectedAddTabId(Integer selectedAddTabId) {
		this.selectedAddTabId = selectedAddTabId;
	}

	public List getSystemTabInfos() {
		if (getUser() == null) {
			return new ArrayList();
		}

		if (systemTabInfos == null || systemTabInfos.isEmpty()) {
			systemTabInfos = new ArrayList();
			vkc.view.bean.TabInfo tabInfo = null;
			
			if (getUser().isAllowManageSystemViewType()) {
				tabInfo = new vkc.view.bean.TabInfo();
				tabInfo.setTabId(PageId.CATEGORY_VIEW);
				tabInfo.setTabName(getResources().getMessage(getLocale(), "vkc.systemviewtype.categoryview"));
				tabInfo.setTabDescription(getResources().getMessage(getLocale(), "vkc.systemviewtype.categoryview.switch"));
				systemTabInfos.add(tabInfo);

				if (VKCSysParameterSerializer.isProjectManagementEnabled()) {
					tabInfo = new vkc.view.bean.TabInfo();
					tabInfo.setTabId(PageId.PROJECT_VIEW);
					tabInfo.setTabName(getResources().getMessage(getLocale(), "projectview"));
					tabInfo.setTabDescription(getResources().getMessage(getLocale(), "projectview.switch"));
					systemTabInfos.add(tabInfo);
		
					tabInfo = new vkc.view.bean.TabInfo();
					tabInfo.setTabId(PageId.FUNCTIONAL_VIEW);
					tabInfo.setTabName(getResources().getMessage(getLocale(), "functionview"));
					tabInfo.setTabDescription(getResources().getMessage(getLocale(), "functionview.switch"));
					systemTabInfos.add(tabInfo);
				}
			} else if (PageId.CATEGORY_VIEW.equals(getUser().getSystemViewTypeId())) {
				tabInfo = new vkc.view.bean.TabInfo();
				tabInfo.setTabId(PageId.CATEGORY_VIEW);
				tabInfo.setTabName(getResources().getMessage(getLocale(), "vkc.systemviewtype.categoryview"));
				tabInfo.setTabDescription(getResources().getMessage(getLocale(), "vkc.systemviewtype.categoryview.switch"));
				systemTabInfos.add(tabInfo);
			}  else if (PageId.PROJECT_VIEW.equals(getUser().getSystemViewTypeId())	|| PageId.FUNCTIONAL_VIEW.equals(getUser().getSystemViewTypeId())) {
				if (VKCSysParameterSerializer.isProjectManagementEnabled()) {
					tabInfo = new vkc.view.bean.TabInfo();
					tabInfo.setTabId(PageId.PROJECT_VIEW);
					tabInfo.setTabName(getResources().getMessage(getLocale(), "projectview"));
					tabInfo.setTabDescription(getResources().getMessage(getLocale(), "projectview.switch"));
					systemTabInfos.add(tabInfo);
		
					tabInfo = new vkc.view.bean.TabInfo();
					tabInfo.setTabId(PageId.FUNCTIONAL_VIEW);
					tabInfo.setTabName(getResources().getMessage(getLocale(), "functionview"));
					tabInfo.setTabDescription(getResources().getMessage(getLocale(), "functionview.switch"));
					systemTabInfos.add(tabInfo);
				}
			}

			if (getUser().hasAdminPrivileges() || getUser().isDedicatedAdmin()) {
				tabInfo = new vkc.view.bean.TabInfo();
				tabInfo.setTabId(PageId.ADMINISTRATION_VIEW);
				tabInfo.setTabName(getResources().getMessage(getLocale(), "vkc.systemviewtype.adminview"));
				tabInfo.setTabDescription(getResources().getMessage(getLocale(), "vkc.systemviewtype.adminview.switch"));
				systemTabInfos.add(tabInfo);
			}
		}
		
		return systemTabInfos;
	}

	public void setSystemTabInfos(List systemTabInfos) {
		this.systemTabInfos = systemTabInfos != null ? systemTabInfos : new ArrayList();
	}

	public Integer getLastFunctionalId() {
		return lastFunctionalId;
	}

	public void setLastFunctionalId(Integer lastFunctionalId) {
		this.lastFunctionalId = lastFunctionalId;
	}

	public Integer getLastProjectId() {
		return lastProjectId;
	}

	public void setLastProjectId(Integer lastProjectId) {
		this.lastProjectId = lastProjectId;
	}

	public Integer getLastCategoryId() {
		return lastCategoryId;
	}

	public void setLastCategoryId(Integer lastCategoryId) {
		this.lastCategoryId = lastCategoryId;
	}
}