/*
 * $Id: VKCDocumentTypeSerializer.java,v 1.14 2006/03/27 22:08:14 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCDocumentTypeSerializer.java,v $
 * Revision 1.14  2006/03/27 22:08:14  marbobo
 * + Bugfixes und Refactoring
 *
 * Revision 1.13  2005/07/14 12:50:41  korowa
 * +3 neue dateitypen sind in Datenbank reingekommen(odt,fh11,fh8)
 * +neue Eigenschaft(editable) von Dokumenttype, die Editierbarkeit eines Dokumentes in VKC anzeigt
 *
 * Revision 1.12  2005/06/23 22:28:13  marbobo
 * + Fehlende Icon-Bilder hinmzugefügt
 * + Alle Icon-Pfade korrigiert
 * + Alle Icon-Methoden korrigiert und optimiert
 *
 * Revision 1.11  2005/06/23 13:11:16  aram
 * + Bugfix
 *
 * Revision 1.10  2005/06/23 11:45:12  korowa
 * Image-Path für Icons wird jetzt aus Datenbank geholt
 * neuer Attribut für Icon IMG_EXTENSION=grafische Format
 *
 * Revision 1.9  2004/12/02 20:29:00  marbobo
 * + Statement auf PreparedStatement umgestellt
 *
 * Revision 1.8  2004/11/24 09:25:56  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/11/08 12:31:08  koppy
 * +getConvertibleDocumentTypes
 *
 * Revision 1.5  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.Iterator;
import java.util.List;

import java.sql.Connection;

import vkc.db.entities.VKCDocumentType;

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

public class VKCDocumentTypeSerializer {
	private static HashMap documentTypes = new HashMap();

	private VKCDocumentTypeSerializer() {
	}

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

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

			while (rs.next()) {
				VKCDocumentType type = new VKCDocumentType();

				type.setDocTypeId(new Integer(rs.getInt("DOCTYPEID")));
				type.setExtensionKey(rs.getString("EXTENSION_KEY"));
				type.setExtension(rs.getString("EXTENSION"));
				type.setDescription(rs.getString("DESCRIPTION"));
				type.setDescriptionKey(rs.getString("DESCR_KEY"));
				type.setShortDescription(rs.getString("DESCRIPTION_SHORT"));
				type.setInternal(new Integer(rs.getInt("INTERNAL")));
				type.setMimeType(rs.getString("MIMETYPE"));
				type.setImgPath(rs.getString("IMGPATH"));
				type.setIconExtension(rs.getString("ICON_EXTENSION"));
				type.setConvertible(new Integer(rs.getInt("CONVERTIBLE")));
				type.setEditable(new Integer(rs.getInt("EDITABLE")));
				documentTypes.put(type.getDocTypeId(), type);
			}
		} catch (Exception e) {
			VKCLogManager.errorLog(VKCDocumentTypeSerializer.class, "1", "Error while retrieving document types: ", e);
			throw new DBException(e);
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
			DBHelper.close(conn);
		}
	}

	public final static List getDocumentTypes() {
		return new ArrayList(documentTypes.values());
	}

	public final static List getConvertibleDocumentTypes() {
		List convertible = new ArrayList();

		for (Iterator iter = getDocumentTypes().iterator(); iter.hasNext();) {
			VKCDocumentType type = (VKCDocumentType) iter.next();

			if (!type.isConvertible()) {
				continue;
			}

			convertible.add(type);
		}

		return convertible;
	}

	public final static VKCDocumentType getDocumentTypeById(Integer id) {
		return (VKCDocumentType) documentTypes.get(id);
	}

	public final static VKCDocumentType getDocumentTypeById(int id) {
		return (VKCDocumentType) documentTypes.get(new Integer(id));
	}

	public final static VKCDocumentType getDocumentTypeByExtension(String extension) {
		for (Iterator iter = getDocumentTypes().iterator(); iter.hasNext();) {
			VKCDocumentType type = (VKCDocumentType) iter.next();

			if (extension.equalsIgnoreCase(type.getExtension())) {
				return type;
			}
		}

		return null;
	}
}