/*
 * $Id: VKCLanguagePackSerializer.java,v 1.6 2006/03/27 22:08:13 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCLanguagePackSerializer.java,v $
 * Revision 1.6  2006/03/27 22:08:13  marbobo
 * + Bugfixes und Refactoring
 *
 * Revision 1.5  2004/12/05 22:57:46  marbobo
 * + Statement auf PreparedStatement umgestellt
 *
 * Revision 1.4  2004/11/24 09:25:55  lawern
 * Hibernate ist VKC-Geschichte!
 *
 * Revision 1.3  2004/07/20 08:23:20  jonyp
 * added copyright info + cvs log
 *
 *
 */
package vkc.db.serializer;

import vkc.db.entities.VKCLanguagePack;
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.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import java.sql.Connection;

public class VKCLanguagePackSerializer {
	private static List languagePacks = new ArrayList();

	private VKCLanguagePackSerializer() {
	}

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

		try {
			languagePacks = new ArrayList();
			conn = WebShare.database.getConnection();
			pStmt = conn.prepareStatement("SELECT * FROM languagepacks");
			rs = pStmt.executeQuery();

			while (rs.next()) {
				VKCLanguagePack p = new VKCLanguagePack();
				setFields(p, rs);
				languagePacks.add(p);
			}
		} catch (Exception e) {
			VKCLogManager.errorLog(VKCLanguagePackSerializer.class, "1", "Error retrieving VKCLanguagePacks", e);
			throw new DBException(e);
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
			DBHelper.close(conn);
		}
	}

	private static final void setFields(VKCLanguagePack p, ResultSet rs) throws SQLException {
		p.setLangPackId(new Integer(rs.getInt("langPackId")));
		p.setCountryCode(rs.getString("countryCode"));
		p.setName(rs.getString("name"));
		p.setNameKey(rs.getString("name_key"));
		p.setDescription(rs.getString("description"));
		p.setDescrKey(rs.getString("descr_key"));
		p.setClassName(rs.getString("className"));
		p.setClassPath(rs.getString("classPath"));
		p.setEnabled(new Integer(rs.getInt("enabled")));
	}

	public static final List getLanguagePacks() {
		return new ArrayList(languagePacks);
	}

	public static final VKCLanguagePack getLanguagePackById(Integer id) {
		for (Iterator iter = languagePacks.iterator(); iter.hasNext();) {
			VKCLanguagePack p = (VKCLanguagePack) iter.next();

			if (p.getLangPackId().intValue() == id.intValue()) {
				return p;
			}
		}

		return null;
	}

	/**
	 * Sucht ein VKCLanguagePack anhand des Ländercodes
	 *
	 * @param countryCode CountryCode des LanguagePacks das gesucht werden soll
	 */
	public static final VKCLanguagePack getLanguagePackByCountryCode(String countryCode) {
		for (Iterator iter = languagePacks.iterator(); iter.hasNext();) {
			VKCLanguagePack p = (VKCLanguagePack) iter.next();

			if (countryCode.equals(p.getCountryCode())) {
				return p;
			}
		}

		return null;
	}
}