/*
 * $Id: VKCLanguageSerializer.java,v 1.13 2007/03/05 09:49:53 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCLanguageSerializer.java,v $
 * Revision 1.13  2007/03/05 09:49:53  marbobo
 * + Defaultsprache auf Englisch gesetzt
 *
 * Revision 1.12  2006/03/27 22:08:11  marbobo
 * + Bugfixes und Refactoring
 *
 * Revision 1.11  2004/12/13 19:18:42  marbobo
 * + saveLanguages()
 *
 * Revision 1.10  2004/12/09 15:19:17  jonyp
 * getDefaultLanguage now looks up after defaultLanguage-field of table language
 *
 * Revision 1.9  2004/12/09 14:09:16  jonyp
 * renamed getLanguageByMappingValue to guessLanguageByMappingValue
 *
 * Revision 1.8  2004/12/05 22:57:46  marbobo
 * + Statement auf PreparedStatement umgestellt
 *
 * Revision 1.7  2004/11/24 09:25:53  lawern
 * Hibernate ist VKC-Geschichte!
 *
 * Revision 1.6  2004/10/26 09:42:04  jonyp
 * added ldap.mapping.language
 *
 * Revision 1.5  2004/10/26 08:48:33  jonyp
 * delete unnecessary creation of ArrayLists()
 *
 * Revision 1.4  2004/07/20 08:23:20  jonyp
 * added copyright info + cvs log
 *
 *
 */
package vkc.db.serializer;

import vkc.db.entities.VKCLanguage;
import vkc.db.helper.DBHelper;
import vkc.exception.DBException;
import vkc.misc.VKCLogManager;
import vkc.view.WebShare;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import java.sql.Connection;

public class VKCLanguageSerializer {
	private static List languages = new ArrayList();

	private VKCLanguageSerializer() {
	}

	/**
	 * Methode initialisiert die Liste der verfügbaren Languages
	 */
	public final static void initLanguages() throws DBException {
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;

		try {
			conn = WebShare.database.getConnection();
			pStmt = conn.prepareStatement("SELECT * FROM languages");
			rs = pStmt.executeQuery();

			while (rs.next()) {
				VKCLanguage lang = new VKCLanguage();
				lang.setLanguageId(new Integer(rs.getInt("languageId")));
				lang.setName(rs.getString("name"));
				lang.setName_key(rs.getString("name_key"));
				lang.setShortName(rs.getString("shortName"));
				lang.setIsoCode(rs.getString("isoCode"));
				lang.setDefaultLanguage(new Integer(rs.getInt("defaultLanguage")));
				languages.add(lang);
			}
		} catch (Exception e) {
			VKCLogManager.errorLog(VKCLanguageSerializer.class, "1", "Error retrieving VKC-languages", e);
			throw new DBException(e);
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
			DBHelper.close(conn);
		}
	}

	public static final List getLanguages() {
		return languages;
	}

	public static final VKCLanguage getLanguage(Integer id) {
		for (Iterator iter = languages.iterator(); iter.hasNext();) {
			VKCLanguage lang = (VKCLanguage) iter.next();

			if (lang.getLanguageId().intValue() == id.intValue()) {
				return lang;
			}
		}

		return null;
	}

	public static final VKCLanguage getLanguageByShortName(String shortName) {
		for (Iterator iter = languages.iterator(); iter.hasNext();) {
			VKCLanguage lang = (VKCLanguage) iter.next();

			if (lang.getShortName().equalsIgnoreCase(shortName)) {
				return lang;
			}
		}

		return null;
	}

	public static final VKCLanguage getDefaultLanguage() {
		// try to find the language marked as default
		for (Iterator iter = languages.iterator(); iter.hasNext();) {
			VKCLanguage lang = (VKCLanguage) iter.next();

			if (lang.isDefaultLanguage()) {
				return lang;
			}
		}

		// not found, this is the default of the default
		return getLanguageByShortName("en");
	}

	public static final VKCLanguage guessLanguageByMappingValue(String mappingValue) {
		for (Iterator iter = languages.iterator(); iter.hasNext();) {
			VKCLanguage lang = (VKCLanguage) iter.next();

			// language's name is mapping value
			if (mappingValue.equalsIgnoreCase(lang.getName())) {
				return lang;
			}
		}

		return null;
	}

	public final static void saveLanguage(List langs) throws DBException {
		if (langs == null) {
			return;
		}

		Connection conn = null;

		try {
			conn = WebShare.database.getConnection();

			for (int i = 0; i < langs.size(); i++) {
				VKCLanguage language = (VKCLanguage) langs.get(i);
				saveLanguage(language, conn);
			}

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

	public final static void saveLanguage(VKCLanguage language) throws DBException {
		Connection conn = null;

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

	public final static void saveLanguage(VKCLanguage language, Connection conn) throws DBException {
		if (language == null) {
			return;
		}

		PreparedStatement pStmt = null;

		try {
			if (DBHelper.existsEntry(conn, "languages", "languageId", language.getLanguageId())) {
				pStmt = conn.prepareStatement("UPDATE languages SET name = ?, name_key = ?, shortName = ?, isoCode = ?, defaultLanguage = ? WHERE languageId = ?");
			} else {
				language.setLanguageId(DBHelper.getNewId(conn, "languages", "languageId"));
				pStmt = conn.prepareStatement("INSERT INTO languages ( name, name_key, shortName, isoCode, defaultLanguage, languageId ) values ( ?, ?, ?, ?, ?, ? )");
			}

			pStmt.setString(1, language.getName());
			pStmt.setString(2, language.getName_key());
			pStmt.setString(3, language.getShortName());
			pStmt.setString(4, language.getIsoCode());
			pStmt.setInt(5, language.isDefaultLanguage() ? 1 : 0);
			pStmt.setInt(6, language.getLanguageId().intValue());
			pStmt.execute();
		} catch (Exception ex) {
			throw new DBException(ex);
		} finally {
			DBHelper.close(pStmt);
		}
	}
}