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

import vkc.db.entities.VKCAclType;
import vkc.db.helper.DBHelper;
import vkc.exception.DBException;
import vkc.view.WebShare;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;

import java.sql.Connection;

public class VKCAclTypeSerializer {
	private VKCAclTypeSerializer() {
	}

	public final static HashMap retrieveAclTypes() throws DBException {
		Connection conn = null;

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

			return retrieveAclTypes(conn);
		} catch (Exception e) {
			throw new DBException(e);
		} finally {
			DBHelper.close(conn);
		}
	}

	/**
	 * Liefert alle Acl Typen als HashMap
	 *
	 * @return HashMap aclTypen mit Typ als Schlüssel
	 *
	 * @throws DBException
	 */
	public final static HashMap retrieveAclTypes(Connection conn) throws DBException {
		PreparedStatement pStmt = null;
		ResultSet rs = null;

		try {
			pStmt = conn.prepareStatement("SELECT acltypeid, name, rank, description FROM acltypes");
			rs = pStmt.executeQuery();

			HashMap allAclTypes = new HashMap();

			while (rs.next()) {
				VKCAclType aclType = new VKCAclType();
				aclType.setAclTypeId(new Integer(rs.getInt("acltypeid")));
				aclType.setName(rs.getString("name"));
				aclType.setRank(new Integer(rs.getInt("rank")));
				aclType.setDescription(rs.getString("description"));

				allAclTypes.put(aclType.getName(), aclType);
			}

			return allAclTypes;
		} catch (Exception e) {
			throw new DBException(e);
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
		}
	}
}