/*
 * $Id: TabViewForm.java,v 1.25 2006/11/06 12:40:08 korowa Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: TabViewForm.java,v $
 * Revision 1.25  2006/11/06 12:40:08  korowa
 * bugfixing
 *
 * Revision 1.24  2006/11/06 10:49:00  korowa
 * Tabview License und  Admintabview License hinzugefügt
 *
 * Revision 1.23  2006/09/07 01:54:02  marbobo
 * + TabIds überarbeitet
 *
 * Revision 1.22  2006/05/15 12:26:04  marbobo
 * + Position von Profil- und Hilfe-Tabs nach rechts verschoben
 *
 * Revision 1.21  2006/05/12 22:14:47  marbobo
 * + Administrationssicht implementiert
 *
 * Revision 1.20  2006/05/11 20:25:37  marbobo
 * + Zuletzt besuchte Kategorie-ID, Projekte-ID und Funktionen-ID werden nun gespeichert und beim Sichtenwechsel wiederhergestellt
 *
 * Revision 1.19  2006/03/16 22:42:43  marbobo
 * + Layout-Anpassungen
 *
 * Revision 1.18  2006/02/27 16:15:57  marbobo
 * + Systemsichten hinzugefügt
 * + SysLinks angepasst
 * + Benutzer können falls gewünscht Dokumentqueries anlegen
 *
 * Revision 1.17  2005/08/22 15:42:43  jonyp
 * added trashTab
 *
 * Revision 1.16  2005/08/19 15:11:16  jonyp
 * *** empty log message ***
 *
 * Revision 1.15  2005/08/09 19:51:20  marbobo
 * + Eigenschaften von registerkarten ändern implementiert
 *
 * Revision 1.14  2005/06/15 12:11:01  jonyp
 * trash_tab entfernt
 *
 * Revision 1.13  2005/06/14 14:52:04  jonyp
 * *** empty log message ***
 *
 * Revision 1.12  2005/01/07 21:22:13  jonyp
 * enable display of tooltips for each tab
 *
 * Revision 1.11  2004/10/26 22:28:15  marbobo
 * + kleine Anpassung
 *
 * Revision 1.10  2004/10/20 12:02:28  marbobo
 * + kleine Anpassung wegen ded. Admins
 *
 * Revision 1.9  2004/10/04 13:44:19  marbobo
 * + kleine Anpassung
 *
 * Revision 1.8  2004/09/22 23:25:58  marbobo
 * + kleine Anpassung
 *
 * Revision 1.7  2004/08/30 14:48:05  koppy
 * + Workflow-Tab
 *
 * Revision 1.6  2004/07/28 23:11:45  marbobo
 * + History-Tab für Gastuser deaktiviert
 *
 * Revision 1.5  2004/07/28 14:14:58  marbobo
 * + News aktiviert
 *
 * Revision 1.4  2004/07/26 13:18:22  marbobo
 * - Statistik-Tab deaktiviert
 *
 * Revision 1.3  2004/07/23 15:06:51  jonyp
 * perpare support for news-system (commented out until in use)
 *
 * Revision 1.2  2004/07/11 00:55:08  marbobo
 * + Bugfix
 *
 * Revision 1.1  2004/07/10 22:42:19  marbobo
 * + Initiale Version
 *
 */

package vkc.view.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.exception.DBException;
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;
import vkc.view.form.SysLinkForm;

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.CATEGORY_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.CATEGORY_VIEW;
		viewId = PageId.CATEGORY_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();

			//Dokumentablage-Tab
			TabInfo tabInfo = (TabInfo)tabViews.get(new Integer(TabId.DOCUMENT_TAB));
			
			if (tabInfo.isEnabled()) {
				tabInfo.setTabLink("/showCategory.do?categoryId=" + getLastCategoryId());
				tabInfos.add(tabInfo);
			}
			
		    // Workflow-Tab
			tabInfo = (TabInfo)tabViews.get(new Integer(TabId.WORKFLOW_TAB));
			
			if (tabInfo.isEnabled()) {
				if (WebShare.getProperties().getProperty("vkc.workflow.enabled", "false").equalsIgnoreCase("true")
					    && WebShare.getSysParameterValue("WORKFLOW", "Enable", "0").equals("1")
					    && !getUser().isGuestUser()) {
						tabInfo.setTabLink("/workflow/worklist.do");
						tabInfos.add(tabInfo);
					}
			}
		
			// trash-tab
			if (WebShare.getTrashCategory() != null) {
				tabInfo = (TabInfo)tabViews.get(new Integer(TabId.TRASH_TAB));
				
				if (tabInfo.isEnabled()) {
					if (getUser().hasAdminPrivileges()) {
				        tabInfo.setTabName(WebShare.getTrashCategory().getName());
						tabInfo.setTabLink("/showTrash.do?categoryId=" + WebShare.getTrashCategory().getCategoryId());
				        tabInfos.add(tabInfo);
					}
				}
			}
						
			// Favoriten-Tab
			tabInfo = (TabInfo)tabViews.get(new Integer(TabId.FAVORITES_TAB));

			if (tabInfo.isEnabled()) {
				if (!getUser().isGuestUser()) {
					tabInfo.setTabLink("/showCategoryFavorites.do?initialize=true");
					tabInfos.add(tabInfo);
				}
			}
			
			// History-Tab
			tabInfo = (TabInfo)tabViews.get(new Integer(TabId.HISTORY_TAB));
			
			if (tabInfo.isEnabled()) {
				if (!getUser().isGuestUser()) {
					tabInfo.setTabLink("/showHistory.do");
					tabInfos.add(tabInfo);
				}
			}

			// Neuerscheinungen-Tab
			tabInfo = (TabInfo)tabViews.get(new Integer(TabId.LATESTADDITIONS_TAB));
			
			if (tabInfo.isEnabled()) {
				tabInfo.setTabLink("/showLatestAdditions.do?initialize=true");
				tabInfos.add(tabInfo);
			}

			// Bestseller-Tab
			tabInfo = (TabInfo)tabViews.get(new Integer(TabId.BESTSELLER_TAB));
			
			if (tabInfo.isEnabled()) {
				tabInfo.setTabLink("/showBestseller.do?initialize=true");
				tabInfos.add(tabInfo);
			}

			// Erweiterte Suche-Tab
			tabInfo = (TabInfo)tabViews.get(new Integer(TabId.SEARCH_TAB));
			
			if (tabInfo.isEnabled()) {
				tabInfo.setTabLink("/searchDocuments.do?action.searchInit=true");
				tabInfos.add(tabInfo);
			}

			// Messenger-Tab
			tabInfo = (TabInfo)tabViews.get(new Integer(TabId.MESSENGER_TAB));
			
			if (tabInfo.isEnabled()) {
				if (!getUser().isGuestUser()) {
					tabInfo.setTabLink("/editMessenger.do?initialize=true");
					tabInfos.add(tabInfo);
				}
			}

			// News-Tab
			tabInfo = (TabInfo)tabViews.get(new Integer(TabId.NEWS_TAB));
			
			if (tabInfo.isEnabled()) {
	            try {
					if (VKCSysParameterSerializer.retrieveIntValue("NEWS", "EnableNews", 0) != 0) {
						if (!getUser().isGuestUser()) {
							tabInfo.setTabLink("/showNews.do");
							tabInfos.add(tabInfo);
						}
					}
	            } catch (DBException ex) {
	            	//Nothing do
	            }
			}
			tabInfo = (TabInfo)tabViews.get(new Integer(TabId.LICENSE_TAB));
			
			if (tabInfo.isEnabled()) {
		        if (Integer.parseInt(WebShare.getSysParameterValue("LICENSE", "EnableLicensemanagement", "0")) != 0){   
					tabInfo.setTabLink("/userLicense.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().equals(getSelectedTabId())) {
				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 HashMap getRootCategoryLinkParams() {
		HashMap params = new HashMap();
		params.put("linkType", SysLinkForm.CATEGORY_LINK);
		params.put("objectId", new Integer(VKCCategory.ROOT_CATEGORY_ID));
		
		return params;
	}
	
	public Integer getSelectedViewId() {
		return selectedViewId;
	}

	public void setSelectedViewId(Integer integer) {
		selectedViewId = integer;
	}
	
    public Integer getLastCategoryId() {
        return lastCategoryId;
    }
    
    public void setLastCategoryId(Integer i) {
        lastCategoryId = i;
    }

	public boolean isReloadMainview() {
		return reloadMainview;
	}

	public void setReloadMainview(boolean reloadMainview) {
		this.reloadMainview = reloadMainview;
	}
	
	public String getReloadPath() {
		if (TabId.DOCUMENT_TAB == getSelectedTabId().intValue()) {
			return "/showCategory.do?categoryId=" + getLastCategoryId();
		}
		
		return "";
	}
	
	public List getAdditionalTabInfos() {
		if (getUser() == null) {
			return new ArrayList();
		}

		if (additionalTabInfos == null || additionalTabInfos.isEmpty()) {
			HashMap tabViews = VKCTabViewSerializer.getTabViews(getLocale(), getResources());
			additionalTabInfos = new ArrayList();
			
			// Profil-Tab
			vkc.view.bean.TabInfo tabInfo = (TabInfo)tabViews.get(new Integer(TabId.PROFILE_TAB));
			
			if (tabInfo.isEnabled()) {
				if (!getUser().isGuestUser()) {
					tabInfo.setTabLink("/userProfile.do?initialize=true");
					additionalTabInfos.add(tabInfo);
				}
			}

			// Help-Tab
			tabInfo = (vkc.view.bean.TabInfo)tabViews.get(new Integer(TabId.HELP_TAB));
			
			if (tabInfo.isEnabled()) {
				tabInfo.setTabLink("/helpView.do?viewId=" + HelpViewForm.VKC_VIEW);
				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 boolean isAllowSystemViewTypeSwitch() {
		if (getUser().hasAdminPrivileges() || getUser().isDedicatedAdmin() || VKCSysParameterSerializer.isProjectManagementEnabled() && getUser().isAllowManageSystemViewType()) {
			return true;
		}
		
		return false;
	}

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

		if (systemTabInfos == null || systemTabInfos.isEmpty()) {
			systemTabInfos = new ArrayList();
			
			TabInfo tabInfo = new 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() && getUser().isAllowManageSystemViewType()) {
				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;
	}
}