/*
 * $Id: VKCDocumentClassSerializer.java,v 1.9 2006/03/27 22:08:16 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCDocumentClassSerializer.java,v $
 * Revision 1.9  2006/03/27 22:08:16  marbobo
 * + Bugfixes und Refactoring
 *
 * Revision 1.8  2004/12/09 21:07:40  marbobo
 * + kleine Optimierungen und Bugfixes
 * +an einigen Stellen Rreihenfolge der close-Anweisungen vereinheitlicht
 *
 * Revision 1.7  2004/12/02 14:20:19  marbobo
 * + Statement auf PreparedStatement umgestellt
 *
 * Revision 1.6  2004/11/24 09:25:54  lawern
 * Hibernate ist VKC-Geschichte!
 *
 * Revision 1.5  2004/11/10 15:02:44  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.4  2004/07/20 08:23:21  jonyp
 * added copyright info + cvs log
 *
 *
 */
package vkc.db.serializer;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import java.sql.Connection;

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

public class VKCDocumentClassSerializer {
	private static HashMap documentClasses = new HashMap();

	private VKCDocumentClassSerializer() {
	}

	/**
	 * Methode holt alle VKCDocumentClass-Objekte aus der  Datenbank
	 */
	public final static void initDocumentClasses() throws DBException {
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;

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

			while (rs.next()) {
				VKCDocumentClass c = new VKCDocumentClass();
				c.setDocClassId(new Integer(rs.getInt("DOCCLASSID")));
				c.setNameKey(rs.getString("NAME_KEY"));
				c.setDescriptionKey(rs.getString("DESCR_KEY"));
				c.setFlag(new Integer(rs.getInt("FLAG")));
				documentClasses.put(c.getDocClassId(), c);
			}
		} catch (Exception e) {
			VKCLogManager.errorLog(VKCDocumentClassSerializer.class, "1", "Error while retrieving document classes: ", e);
			throw new DBException(e);
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
			DBHelper.close(conn);
		}
	}

	public final static List getDocumentClasses() {
		List list = new ArrayList(documentClasses.values());

		Comparator comparator = new Comparator() {
			public int compare(Object o1, Object o2) {
				VKCDocumentClass c1 = (VKCDocumentClass) o1;
				VKCDocumentClass c2 = (VKCDocumentClass) o2;

				return c1.getDocClassId().compareTo(c2.getDocClassId());
			}
		};

		Collections.sort(list, comparator);

		return list;
	}

	public final static VKCDocumentClass getDocumentClass(Integer id) {
		return (VKCDocumentClass) documentClasses.get(id);
	}
}