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

import vkc.db.entities.VKCPermission;
import vkc.db.helper.DBHelper;
import vkc.exception.DBException;
import vkc.view.WebShare;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Iterator;

public class VKCPermissionSerializer {
	/** Map of maps containing context permission used as a cache*/
	private static final Map permissions = new HashMap();

	public final static String retrieveContextPermissionsString(String context) throws DBException {
		return retrieveContextPermissionsString(context, false);
	}

	public final static String retrieveContextPermissionsString(String context, boolean enabledOnly) throws DBException {
		List permList = VKCPermissionSerializer.retrieveContextPermissions(context, enabledOnly);

		String permString = "";

		for (Iterator iter = permList.iterator(); iter.hasNext();) {
			VKCPermission perm = (VKCPermission) iter.next();
			permString += perm.getToken();
		}

		return permString;
	}

	public final static List retrieveContextPermissions(String context) throws DBException {
		return retrieveContextPermissions(context, false);
	}

	public final static List retrieveContextPermissions(String context, boolean enabledOnly) throws DBException {
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;

		List allCtxPerms = new ArrayList();

		try {
			conn = WebShare.database.getConnection();
			String sqlStatement = "  SELECT permissionId, context, token, printstring, description, enabled FROM permissions WHERE context = ?";

			if (enabledOnly) {
				sqlStatement += " AND enabled > 0 ";
			}
			
			sqlStatement += " ORDER BY numvalue ASC";

			pStmt = conn.prepareStatement(sqlStatement);
			pStmt.setString(1, context);
			rs = pStmt.executeQuery();

			while (rs.next()) {
				VKCPermission ctxPerm = new VKCPermission();
				ctxPerm.setPermissionId(new Integer(rs.getInt("permissionId")));
				ctxPerm.setContext(rs.getString("context"));
				ctxPerm.setToken((rs.getString("token")).charAt(0));
				ctxPerm.setPrintstring(rs.getString("printstring"));
				ctxPerm.setDescription(rs.getString("description"));
				ctxPerm.setEnabled(new Integer(Integer.parseInt(rs.getString("enabled"))));

				allCtxPerms.add(ctxPerm);
			}

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

	public static final Map getPermissions(String context) throws DBException {
		return getPermissions(context, false);
	}

	public static final Map getPermissions(String context, boolean enabledOnly) throws DBException {
		synchronized (permissions) {
			Map contextPerms = (Map) permissions.get(context);

			if (contextPerms == null) {
				// not available yet --> reload from database
				contextPerms = new HashMap();
				List list = VKCPermissionSerializer.retrieveContextPermissions(context, enabledOnly);

				for (int i = 0; i < list.size(); i++) {
					VKCPermission p = (VKCPermission) list.get(i);
					contextPerms.put(String.valueOf(p.getToken()), p);
				}

				// Add to the map of context permissions
				permissions.put(context, contextPerms);
			}

			return contextPerms;
		}
	}
}