/*
 * $Id: SystemDefaultsForm.java,v 1.12 2006/11/06 10:48:59 korowa Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: SystemDefaultsForm.java,v $
 * Revision 1.12  2006/11/06 10:48:59  korowa
 * Tabview License und  Admintabview License hinzugefügt
 *
 * Revision 1.11  2006/09/11 19:56:56  marbobo
 * + Worflow-Tab hinzugefügt
 *
 * Revision 1.10  2006/09/11 14:34:05  marbobo
 * + "Registerkarteneinstellungen" etwas überarbeitet
 *
 * Revision 1.9  2006/07/20 21:52:16  marbobo
 * + Registerharten sortiert
 *
 * Revision 1.8  2005/08/09 19:48:55  marbobo
 * + Eigenschaften von registerkarten ändern implementiert
 *
 * Revision 1.7  2005/08/08 14:05:31  marbobo
 * + Registerkarten einstellen (noch keine Funktion)
 *
 * Revision 1.6  2004/12/13 19:19:11  marbobo
 * + Standardsprache festlegen
 *
 * Revision 1.5  2004/07/20 08:09:56  jonyp
 * added copyright info + cvs log
 *
 *
 */

package vkc.administration.form;

import java.text.CollationKey;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import vkc.db.entities.VKCAddAttribute;
import vkc.db.serializer.VKCDocumentViewFieldSerializer;
import vkc.db.serializer.VKCLanguageSerializer;
import vkc.db.serializer.VKCTabViewSerializer;
import vkc.exception.DBException;
import vkc.util.VKCStringUtils;
import vkc.view.PageId;
import vkc.view.TabId;
import vkc.view.bean.TabInfo;
import vkc.view.form.BasicActionForm;

public class SystemDefaultsForm extends BasicActionForm {
	public static final String TABVIEWSETTINGS = "TABVIEWSETTINGS";

	public static final String DEFAULTLANGUAGES = "DEFAULTLANGUAGES";

	public static final String DOCUMENTVIEWFIELDS = "DOCUMENTVIEWFIELDS";

	public static final String ADDCATATTRIBUTES = "ADDCATATTRIBUTES";

	public static final String ADDDOCATTRIBUTES = "ADDDOCATTRIBUTES";

	private HashMap documentViewFields = null;

	private HashMap tabViews = null;

	private Integer selectedId = null;

	private Integer selectedSystemViewId = PageId.CATEGORY_VIEW;

	private Integer selectedTabViewId = new Integer(TabId.DOCUMENT_TAB);

	private Integer uniqueId = new Integer(0);

	private Integer[] selectedIds = new Integer[0];

	private Integer[] nullIds = new Integer[0];

	private Integer[] deleteIds = new Integer[0];

	private List addCatAttributes = new ArrayList();

	private List addDocAttributes = new ArrayList();

	private List languages = null;

	private List systemViews = null;

	private String selectedContext = DOCUMENTVIEWFIELDS;

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

		documentViewFields = null;
		tabViews = null;

		selectedId = null;
		selectedSystemViewId = PageId.CATEGORY_VIEW;
		selectedTabViewId = new Integer(TabId.DOCUMENT_TAB);
		uniqueId = new Integer(0);

		selectedIds = new Integer[0];
		nullIds = new Integer[0];
		deleteIds = new Integer[0];

		addCatAttributes = new ArrayList();
		addDocAttributes = new ArrayList();
		languages = null;
		systemViews = null;

		selectedContext = DOCUMENTVIEWFIELDS;
	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		if (request.getParameter("action.save.addcatattributes") != null) {
			for (int i = 0; i < getAddCatAttributes().size(); i++) {
				VKCAddAttribute attribute = (VKCAddAttribute) getAddCatAttributes()
						.get(i);

				if (isEmpty(attribute.getName())) {
					errors.add("name_" + (i + 1), new ActionError(
							"vkc.administration.error.attribute.name", ""
									+ (i + 1)));
				}
			}
		} else if (request.getParameter("action.save.adddocattributes") != null) {
			for (int i = 0; i < getAddDocAttributes().size(); i++) {
				VKCAddAttribute attribute = (VKCAddAttribute) getAddDocAttributes()
						.get(i);

				if (isEmpty(attribute.getName())) {
					errors.add("name_" + (i + 1), new ActionError(
							"vkc.administration.error.attribute.name", ""
									+ (i + 1)));
				}
			}
		}

		return errors;
	}

	public List getAddCatAttributes() {
		return addCatAttributes;
	}

	public List getAddDocAttributes() {
		return addDocAttributes;
	}

	public List getLanguages() {
		if (languages == null) {
			languages = VKCLanguageSerializer.getLanguages();
		}

		return languages;
	}

	public void setAddCatAttributes(List list) {
		addCatAttributes = list;
	}

	public void setAddDocAttributes(List list) {
		addDocAttributes = list;
	}

	public String getSelectedContext() {
		return selectedContext;
	}

	public void setSelectedContext(String context) {
		selectedContext = context;
	}

	public List getContextNames() {
		List contextNames = new ArrayList();

		contextNames.add(TABVIEWSETTINGS);
		contextNames.add(DEFAULTLANGUAGES);
		contextNames.add(DOCUMENTVIEWFIELDS);
		contextNames.add(ADDCATATTRIBUTES);
		contextNames.add(ADDDOCATTRIBUTES);

		return contextNames;
	}

	public String getContextName(String context) {
		String name = "";

		if (TABVIEWSETTINGS.equals(context)) {
			name = getResources().getMessage(getLocale(),
					"vkc.administration.systemdefaults.tabviewsettings");
		} else if (DEFAULTLANGUAGES.equals(context)) {
			name = getResources().getMessage(getLocale(),
					"vkc.administration.systemdefaults.defaultlanguages");
		} else if (DOCUMENTVIEWFIELDS.equals(context)) {
			name = getResources().getMessage(getLocale(),
					"vkc.administration.systemdefaults.documentviewfields");
		} else if (ADDCATATTRIBUTES.equals(context)) {
			name = getResources().getMessage(getLocale(),
					"vkc.administration.systemdefaults.addcatattributes");
		} else if (ADDDOCATTRIBUTES.equals(context)) {
			name = getResources().getMessage(getLocale(),
					"vkc.administration.systemdefaults.adddocattributes");
		}

		return name;
	}

	public Integer[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(Integer[] integers) {
		selectedIds = integers;
	}

	public HashMap getDocumentViewFields() throws DBException {
		if (documentViewFields == null) {
			documentViewFields = VKCDocumentViewFieldSerializer
					.getAllDocumentViewFields();
		}

		return documentViewFields;
	}

	public Object getDocumentViewField(String key) throws DBException {
		return getDocumentViewFields().get(key);
	}

	public Integer[] getNullIds() {
		return nullIds;
	}

	public void setNullIds(Integer[] integers) {
		nullIds = integers;
	}

	public Integer getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(Integer integer) {
		selectedId = integer;
	}

	public Integer getUniqueId() {
		return uniqueId;
	}

	public void setUniqueId(Integer integer) {
		uniqueId = integer;
	}

	public VKCAddAttribute getAddCatAttribute(int index) {
		if (index >= getAddCatAttributes().size()) { // Array vergrössern
			for (int i = getAddCatAttributes().size(); i <= index; i++) {
				VKCAddAttribute attribute = new VKCAddAttribute();
				getAddCatAttributes().add(attribute);
			}
		}

		return (VKCAddAttribute) getAddCatAttributes().get(index);
	}

	public VKCAddAttribute getAddDocAttribute(int index) {
		if (index >= getAddDocAttributes().size()) { // Array vergrössern
			for (int i = getAddDocAttributes().size(); i <= index; i++) {
				VKCAddAttribute attribute = new VKCAddAttribute();
				getAddDocAttributes().add(attribute);
			}
		}

		return (VKCAddAttribute) getAddDocAttributes().get(index);
	}

	public Integer[] getDeleteIds() {
		return deleteIds;
	}

	public void setDeleteIds(Integer[] integers) {
		deleteIds = integers;
	}

	public List getSystemViews() {
		if (systemViews == null) {
			systemViews = new ArrayList();

			TabInfo tabInfo = new TabInfo();
			tabInfo.setTabId(PageId.CATEGORY_VIEW);
			tabInfo.setTabName(getResources().getMessage(getLocale(),
					"vkc.systemviewtype.categoryview"));
			systemViews.add(tabInfo);

			tabInfo = new TabInfo();
			tabInfo.setTabId(PageId.PROJECT_VIEW);
			tabInfo.setTabName(getResources().getMessage(getLocale(),
					"projectview"));
			systemViews.add(tabInfo);

			tabInfo = new TabInfo();
			tabInfo.setTabId(PageId.FUNCTIONAL_VIEW);
			tabInfo.setTabName(getResources().getMessage(getLocale(),
					"functionview"));
			systemViews.add(tabInfo);

			tabInfo = new TabInfo();
			tabInfo.setTabId(PageId.ADMINISTRATION_VIEW);
			tabInfo.setTabName(getResources().getMessage(getLocale(),
					"vkc.systemviewtype.adminview"));
			systemViews.add(tabInfo);

			tabInfo = new TabInfo();
			tabInfo.setTabId(PageId.UNDEFINED_VIEW);
			tabInfo.setTabName(getResources().getMessage(getLocale(),
					"vkc.sharedtabs"));
			systemViews.add(tabInfo);

			final Collator collator = Collator.getInstance(getUser()
					.getLanguage().getLocale());

			Comparator c = new Comparator() {
				public int compare(Object o1, Object o2) {
					TabInfo t1 = (TabInfo) o1;
					TabInfo t2 = (TabInfo) o2;

					CollationKey key1 = collator.getCollationKey(VKCStringUtils
							.decodeXmlEntities(t1.getTabName()));
					CollationKey key2 = collator.getCollationKey(VKCStringUtils
							.decodeXmlEntities(t2.getTabName()));

					return key1.compareTo(key2);
				}
			};

			Collections.sort(systemViews, c);
		}

		return systemViews;
	}

	public HashMap getTabViews() {
		if (tabViews == null) {
			tabViews = VKCTabViewSerializer.getTabViews(getLocale(),
					getResources());
		}

		return tabViews;
	}

	public List getTabViewList(int systemViewId) {
		ArrayList tabs = new ArrayList();

		if (PageId.ADMINISTRATION_VIEW.intValue() == systemViewId) {
			tabs
					.add(getTabView(String
							.valueOf(TabId.ADMIN_USERMANAGEMENT_TAB)));
			tabs
					.add(getTabView(String
							.valueOf(TabId.ADMIN_GROUPMANAGEMENT_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.ADMIN_ACTIVEUSERS_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.ADMIN_SENDEMAIL_TAB)));
			tabs.add(getTabView(String
					.valueOf(TabId.ADMIN_SYSTEMPARAMETERS_TAB)));
			tabs
					.add(getTabView(String
							.valueOf(TabId.ADMIN_SYSTEMDEFAULTS_TAB)));
			tabs.add(getTabView(String
					.valueOf(TabId.ADMIN_SYSTEMMAINTENANCE_TAB)));
			tabs
					.add(getTabView(String
							.valueOf(TabId.ADMIN_NEWSMANAGEMENT_TAB)));
			tabs
					.add(getTabView(String
							.valueOf(TabId.ADMIN_SHOWSTATISTICS_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.ADMIN_LDAP_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.ADMIN_LICENSE_TAB)));
		} else if (PageId.CATEGORY_VIEW.intValue() == systemViewId) {
			tabs.add(getTabView(String.valueOf(TabId.DOCUMENT_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.WORKFLOW_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.TRASH_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.FAVORITES_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.HISTORY_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.LATESTADDITIONS_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.BESTSELLER_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.SEARCH_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.MESSENGER_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.NEWS_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.LICENSE_TAB)));
		} else if (PageId.FUNCTIONAL_VIEW.intValue() == systemViewId) {
			// keine Sichtspezifische Registerkarten
		} else if (PageId.PROJECT_VIEW.intValue() == systemViewId) {
			// keine Sichtspezifische Registerkarten
		} else if (PageId.UNDEFINED_VIEW.intValue() == systemViewId) {
			tabs.add(getTabView(String.valueOf(TabId.PROFILE_TAB)));
			tabs.add(getTabView(String.valueOf(TabId.HELP_TAB)));
		}

		final Collator collator = Collator.getInstance(getUser().getLanguage()
				.getLocale());

		Comparator c = new Comparator() {
			public int compare(Object o1, Object o2) {
				TabInfo t1 = (TabInfo) o1;
				TabInfo t2 = (TabInfo) o2;

				CollationKey key1 = collator.getCollationKey(VKCStringUtils
						.decodeXmlEntities(t1.getTabName()));
				CollationKey key2 = collator.getCollationKey(VKCStringUtils
						.decodeXmlEntities(t2.getTabName()));

				return key1.compareTo(key2);
			}
		};

		Collections.sort(tabs, c);

		return tabs;
	}

	public TabInfo getTabView(String id) {
		return (TabInfo) getTabViews().get(Integer.valueOf(id));
	}

	public Integer getSelectedTabViewId() {
		return selectedTabViewId;
	}

	public void setSelectedTabViewId(Integer selectedTabViewId) {
		this.selectedTabViewId = selectedTabViewId;
	}

	public Integer getSelectedSystemViewId() {
		return selectedSystemViewId;
	}

	public void setSelectedSystemViewId(Integer selectedSystemViewId) {
		this.selectedSystemViewId = selectedSystemViewId;
	}

	public HashMap getSystemViewParams(String systemViewId) {
		HashMap params = new HashMap();

		params.put("selectedContext", getSelectedContext());
		params.put("selectedSystemViewId", systemViewId);

		Integer id = Integer.valueOf(systemViewId);

		if (PageId.ADMINISTRATION_VIEW.equals(id)) {
			params.put("selectedTabViewId", String
					.valueOf(TabId.ADMIN_USERMANAGEMENT_TAB));
		} else if (PageId.CATEGORY_VIEW.equals(id)) {
			params.put("selectedTabViewId", String.valueOf(TabId.DOCUMENT_TAB));
		} else if (PageId.FUNCTIONAL_VIEW.equals(id)) {
			params.put("selectedTabViewId", "-1");
		} else if (PageId.PROJECT_VIEW.equals(id)) {
			params.put("selectedTabViewId", "-1");
		} else if (PageId.UNDEFINED_VIEW.equals(id)) {
			params.put("selectedTabViewId", String.valueOf(TabId.PROFILE_TAB));
		}

		return params;
	}

	public HashMap getTabViewParams(String tabViewId) {
		HashMap params = new HashMap();

		params.put("selectedContext", getSelectedContext());
		params.put("selectedSystemViewId", getSelectedSystemViewId());
		params.put("selectedTabViewId", tabViewId);

		return params;
	}
}