/*
 * $Id: FunctionalViewUtils.java,v 1.1 2006/10/04 03:15:15 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: FunctionalViewUtils.java,v $
 * Revision 1.1  2006/10/04 03:15:15  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.8  2006/09/13 19:55:39  marbobo
 * + Bugfix (ascending ersetzt mit asc)
 *
 * Revision 1.7  2006/07/12 12:21:55  marbobo
 * + Bugfix
 *
 * Revision 1.6  2006/07/05 21:00:08  marbobo
 * + History-Funktionalität für Projekt- und Funktionssicht-Tabs implementiert
 *
 * Revision 1.5  2004/12/07 15:02:10  marbobo
 * + Statement auf PreparedStatement umgestellt
 *
 * Revision 1.4  2004/11/24 09:25:57  lawern
 * Hibernate ist VKC-Geschichte!
 *
 * Revision 1.3  2004/07/20 08:58:38  jonyp
 * added copyright info + cvs log
 *
 *
 */
package vkc.procoba.function;

import vkc.db.entities.VKCCategory;
import vkc.db.entities.VKCUser;
import vkc.db.helper.DBHelper;
import vkc.db.serializer.VKCAclSerializer;
import vkc.exception.DBException;
import vkc.security.PermissionManager;
import vkc.security.VKCAcl;
import vkc.view.WebShare;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class FunctionalViewUtils {
	private FunctionalViewUtils() {
	}

	public final static List getFunctionalViewInfos(VKCUser user) throws DBException {
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		List infos = new ArrayList();

		try {
			Integer rootId = WebShare.getCategorySettings().getFunctionalViewRootId();

			if (rootId.intValue() > 0) {

				conn = WebShare.database.getConnection();
				pStmt = conn.prepareStatement("SELECT NAME, CATEGORYID, ACLID from CATEGORIES WHERE PARENTCATEGORYID = ? AND flag <> ? ORDER BY NAME ASC");
				pStmt.setInt(1, rootId.intValue());
				pStmt.setInt(2, VKCCategory.FLAG_DELETED.intValue());
				rs = pStmt.executeQuery();

				while (rs.next()) {
					VKCAcl acl = VKCAclSerializer.retrieveAcl(new Integer(rs.getInt("ACLID")), conn);

					if (PermissionManager.hasPermission(acl, user, 'r')) {
						FunctionalViewInfo info = new FunctionalViewInfo();
						info.setCategoryId(new Integer(rs.getInt("CATEGORYID")));
						info.setName(rs.getString("NAME"));

						infos.add(info);
					}
				}
			}

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