/*
 * $Id$
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log$
 * Revision 1.8  2006/11/06 10:48:59  korowa
 * Tabview License und  Admintabview License hinzugefügt
 *
 * Revision 1.7  2006/09/07 01:54:03  marbobo
 * + TabIds überarbeitet
 *
 * Revision 1.6  2006/07/11 23:40:02  marbobo
 * + PostgreSQL ist pingelig und erlaubt kein get/setBoolean auf Int-Spalten
 *
 * Revision 1.5  2005/10/05 14:12:08  marbobo
 * + Bugfix
 *
 * Revision 1.4  2005/09/13 09:52:31  jonyp
 * cleaning the code
 *
 * Revision 1.3  2005/08/25 01:44:41  marbobo
 * + Permanente Registerkarten hinzugefügt
 *
 * Revision 1.2  2005/08/10 19:11:14  marbobo
 * + Hide-Parameter hinzugefügt
 *
 * Revision 1.1  2005/08/09 19:50:14  marbobo
 * + Initiale Version
 *
 */

package vkc.db.serializer;

import vkc.db.helper.DBHelper;
import vkc.exception.DBException;
import vkc.misc.VKCLogManager;
import vkc.util.ObjectCloner;
import vkc.view.TabId;
import vkc.view.WebShare;
import vkc.view.bean.TabInfo;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;

import java.sql.Connection;

import org.apache.struts.util.MessageResources;

public class VKCTabViewSerializer {
	private static HashMap tabViews = null;
	
	private VKCTabViewSerializer() {
	}

	/**
	 * Methode initialisiert die Liste der verfügbaren TabViews
	 */
	private final static void initTabViews() throws DBException {
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		
		try {
			conn = WebShare.database.getConnection();
			pStmt = conn.prepareStatement("SELECT * FROM tabviews");
			rs = pStmt.executeQuery();

			while (rs.next()) {
				if (tabViews == null) {
					tabViews = new HashMap();
				}
				
				TabInfo tab = new TabInfo();
				tab.setTabId(new Integer(rs.getInt("tabviewid")));
				tab.setUniqueName(rs.getString("uniquename"));
				tab.setTabFlag(new Integer(rs.getInt("flag")));
				
				if (rs.wasNull()) {
					tab.setTabFlag(null);
				}
				
				tab.setTabStatus(new Integer(rs.getInt("status")));
				
				if (rs.wasNull()) {
					tab.setTabStatus(null);
				}

				tab.setViewTypeId(new Integer(rs.getInt("viewtypeid")));

				if (rs.wasNull()) {
					tab.setViewTypeId(null);
				}

				tab.setEnabled(rs.getInt("enabled") != 0);
				
				tab.setHide(rs.getInt("hide") != 0);

				TabInfo pTab = WebShare.getPermanentTabView(tab.getTabId());
				
				if (pTab != null) {
					tab.setUniqueName(pTab.getUniqueName());
					tab.setTabFlag(pTab.getTabFlag());
					tab.setTabStatus(pTab.getTabStatus());
					tab.setViewTypeId(pTab.getViewTypeId());
					tab.setEnabled(pTab.isEnabled());
					tab.setHide(pTab.isHide());
				}
				
				tabViews.put(tab.getTabId(), tab);
			}
		} catch (Exception e) {
			VKCLogManager.errorLog(VKCTabViewSerializer.class, "1", "Error retrieving TabViews", e);
			throw new DBException(e);
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
			DBHelper.close(conn);
		}
	}

	private static final HashMap getTabViews() {
		if (tabViews == null) {
			try {
				initTabViews();
			} catch (DBException e) {
				tabViews = null;
			}
		}
		
		return tabViews;
	}

	public static final HashMap getTabViews(Locale locale, MessageResources resources) {
		HashMap tabs = null;
		
		try {
			tabs = (HashMap)ObjectCloner.deepCopy(getTabViews());
		} catch (Exception e) {
			tabs = new HashMap();
		}
		
		//Dokumentablage-Tab
		TabInfo tab = (TabInfo)tabs.get(new Integer(TabId.DOCUMENT_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.tab.documentstorage"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.documentstorage.descr"));
		
	    // Workflow-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.WORKFLOW_TAB));
		tab.setTabName(resources.getMessage(locale, "wf.workflow"));
		tab.setTabDescription(resources.getMessage(locale, "wf.workflow"));
		
	    // Trash-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.TRASH_TAB));
	    tab.setTabName(resources.getMessage(locale, "vkc.tab.trash"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.trash.descr"));
		
	    // Favorite-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.FAVORITES_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.tab.favorites"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.favorites.descr"));

		//History-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.HISTORY_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.tab.history"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.history.descr"));

		//Neuerscheinungen-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.LATESTADDITIONS_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.tab.latestadditions"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.latestadditions.descr"));

		//Bestseller-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.BESTSELLER_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.tab.bestseller"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.bestseller.descr"));

		//Erweiterte Suche-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.SEARCH_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.tab.advancedsearch"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.advancedsearch.descr"));

		//Messenger-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.MESSENGER_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.tab.messenger"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.messenger.descr"));

		//Profil-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.PROFILE_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.tab.profile"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.profile.descr"));

		//News-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.NEWS_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.tab.news"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.news.descr"));

		//Help-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.HELP_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.tab.help"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.help.descr"));
		
		//Userlizense-Tab
		tab = (TabInfo)tabs.get(new Integer(TabId.LICENSE_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.tab.license"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.tab.license.descr"));

		//Benutzermanagement-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.ADMIN_USERMANAGEMENT_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.administration.usermanagement"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.administration.usermanagement.descr"));

		//Gruppenmanagement-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.ADMIN_GROUPMANAGEMENT_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.administration.groupmanagement"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.administration.groupmanagement.descr"));

		//Sitzungen-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.ADMIN_ACTIVEUSERS_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.administration.activeUsers"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.administration.activeUsers.descr"));

		//EMail-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.ADMIN_SENDEMAIL_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.administration.sendEmail"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.administration.sendEmail.descr"));

		//System-Konfiguration-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.ADMIN_SYSTEMPARAMETERS_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.administration.systemparameters"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.administration.systemparameters.descr"));

		//System-Voreinstellungen-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.ADMIN_SYSTEMDEFAULTS_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.administration.systemdefaults"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.administration.systemdefaults.descr"));

		//System-Pflege-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.ADMIN_SYSTEMMAINTENANCE_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.administration.systemmaintenance"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.administration.systemmaintenance.descr"));

		//Newsmanagement-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.ADMIN_NEWSMANAGEMENT_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.administration.newsmanagement"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.administration.newsmanagement.descr"));

		//Statistik-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.ADMIN_SHOWSTATISTICS_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.administration.showstatistics"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.administration.showstatistics.descr"));

		//LDAP-Tab
	    tab = (TabInfo)tabs.get(new Integer(TabId.ADMIN_LDAP_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.administration.ldap"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.administration.ldap.descr"));
		
		//ADMIN-LICENSE-Tab
		tab = (TabInfo)tabs.get(new Integer(TabId.ADMIN_LICENSE_TAB));
		tab.setTabName(resources.getMessage(locale, "vkc.administration.license"));
		tab.setTabDescription(resources.getMessage(locale, "vkc.administration.license.descr"));

		return tabs;
	}
	
	public static final TabInfo getTabInfo(Integer id) throws Exception {
		return (TabInfo)ObjectCloner.deepCopy(getTabViews().get(id));
	}
	
	public final static void saveTabView(HashMap tabs) throws DBException {
		if (tabs == null) {
			return;
		}

		Connection conn = null;

		try {
			conn = WebShare.database.getConnection();
			
			for (Iterator it = tabs.values().iterator(); it.hasNext();) {
				TabInfo tab = (TabInfo) it.next();
				saveTabView(tab, conn);
			}

			conn.commit();
		} catch (Exception e) {
			VKCLogManager.errorLog(VKCTabViewSerializer.class, "1", "Error occured while saving TabViews!", e);
			DBHelper.rollBack(conn);
			throw new DBException(e);
		} finally {
			DBHelper.close(conn);
		}
	}

	public final static void saveTabView(TabInfo tab) throws DBException {
		Connection conn = null;

		try {
			conn = WebShare.database.getConnection();
			saveTabView(tab, conn);
			conn.commit();
		} catch (Exception e) {
			VKCLogManager.errorLog(VKCTabViewSerializer.class, "1", "Error occured while saving TabViews!", e);
			DBHelper.rollBack(conn);
			throw new DBException(e);
		} finally {
			DBHelper.close(conn);
		}
	}

	public final static void saveTabView(TabInfo tab, Connection conn) throws DBException {
		if (tab == null) {
			return;
		}

		TabInfo pTab = WebShare.getPermanentTabView(tab.getTabId());
		
		if (pTab != null) {
			return;
		}

		PreparedStatement pStmt = null;

		try {
			if (DBHelper.existsEntry(conn, "tabviews", "tabviewid", tab.getTabId())) {
				pStmt = conn.prepareStatement("UPDATE tabviews SET flag = ?, status = ?, viewtypeid = ?, enabled = ? WHERE tabviewid = ?");				
			} else {
			    tab.setTabId(DBHelper.getNewId(conn, "tabviews", "tabviewid"));
				pStmt = conn.prepareStatement("INSERT INTO tabviews (flag, status, viewtypeid, enabled, tabviewid) values (?, ?, ?, ?, ?)");
			}

			if (tab.getTabFlag() != null) {
				pStmt.setInt(1, tab.getTabFlag().intValue());
			} else {
				pStmt.setNull(1, Types.INTEGER);
			}

			if (tab.getTabStatus() != null) {
				pStmt.setInt(2, tab.getTabStatus().intValue());
			} else {
				pStmt.setNull(2, Types.INTEGER);
			}

			if (tab.getViewTypeId() != null) {
				pStmt.setInt(3, tab.getViewTypeId().intValue());
			} else {
				pStmt.setNull(3, TabInfo.VIEWTYPEID_CLASSIC.intValue());
			}

			pStmt.setInt(4, tab.isEnabled() ? 1 : 0);
			pStmt.setInt(5, tab.getTabId().intValue());
			pStmt.execute();
			tabViews.put(tab.getTabId(), tab);
		} catch (Exception ex) {
			throw new DBException(ex);
		} finally {
			DBHelper.close(pStmt);
		}
	}

	public final static HashMap getTabIdsMappedByUniqueName() throws DBException {
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		HashMap tabIds = new HashMap();
		
		try {
			conn = WebShare.database.getConnection();
			pStmt = conn.prepareStatement("SELECT * FROM tabviews");
			rs = pStmt.executeQuery();

			while (rs.next()) {
				tabIds.put(rs.getString("uniquename"), new Integer(rs.getInt("tabviewid")));
			}
			
			return tabIds;
		} catch (Exception e) {
			VKCLogManager.errorLog(VKCTabViewSerializer.class, "1", "Error retrieving TabViews", e);
			throw new DBException(e);
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
			DBHelper.close(conn);
		}
	}
}