/*
 * $Id: VKCDocumentStatusSerializer.java,v 1.10 2006/03/27 22:08:13 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCDocumentStatusSerializer.java,v $
 * Revision 1.10  2006/03/27 22:08:13  marbobo
 * + Bugfixes und Refactoring
 *
 * Revision 1.9  2004/12/02 20:29:00  marbobo
 * + Statement auf PreparedStatement umgestellt
 *
 * Revision 1.8  2004/11/24 09:25:55  lawern
 * Hibernate ist VKC-Geschichte!
 *
 * Revision 1.7  2004/11/10 15:02:44  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.6  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.HashMap;
import java.util.List;

import java.sql.Connection;

import vkc.db.entities.VKCDocumentStatus;
import vkc.db.helper.DBHelper;
import vkc.exception.DBException;
import vkc.misc.VKCLogManager;
import vkc.util.sort.DocumentStatusSortFactory;
import vkc.view.WebShare;

public class VKCDocumentStatusSerializer {
	private static HashMap documentStati = new HashMap();

	private VKCDocumentStatusSerializer() {
	}

	/**
	 * Lädt alle verfügbaren Dokumentstati aus der Datenbank 
	 */
	public final static void initDocumentStati() throws DBException {
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;

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

			while (rs.next()) {
				VKCDocumentStatus docStatus = new VKCDocumentStatus();
				docStatus.setStatus(new Integer(rs.getInt("status")));
				docStatus.setDescriptionKey(rs.getString("desc_key"));
				docStatus.setRemark(rs.getString("remark"));

				documentStati.put(docStatus.getStatus(), docStatus);
			}
		} catch (Exception ex) {
			VKCLogManager.errorLog(VKCDocumentStatusSerializer.class, "", "Error occoured while retrieving documentstati", ex);
			throw new DBException(ex);
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
			DBHelper.close(conn);
		}
	}

	public static List getDocumentStati() {
		List list = new ArrayList(documentStati.values());
		DocumentStatusSortFactory.sort(list, DocumentStatusSortFactory.SORT_BY_STATUS_UP);

		return list;
	}

	public static VKCDocumentStatus getDocumentStatus(Integer id) {
		return (VKCDocumentStatus) documentStati.get(id);
	}
}