/*
 * $Id: VKCLdapUserSerializer.java,v 1.8 2004/12/09 12:45:40 lawern Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCLdapUserSerializer.java,v $
 * Revision 1.8  2004/12/09 12:45:40  lawern
 * vergessene PreparedStatements geschlossen
 *
 * Revision 1.7  2004/12/07 15:02:10  marbobo
 * + Statement auf PreparedStatement umgestellt
 *
 * Revision 1.6  2004/11/24 13:01:15  lawern
 * Global "conn.setAutoCommit( false );" enfernt, weil überflüssig.
 *
 * Revision 1.5  2004/11/24 09:25:58  lawern
 * Hibernate ist VKC-Geschichte!
 *
 * Revision 1.4  2004/11/18 19:38:24  marbobo
 * + changed order of close-stmt to resultset/connect/session  in finally-blocks
 *
 * Revision 1.3  2004/10/08 09:17:05  jonyp
 * deleted join from retrieve method
 *
 * Revision 1.2  2004/10/08 08:27:55  jonyp
 * added method getLdapUserURL
 *
 * Revision 1.1  2004/10/07 14:41:12  jonyp
 * first strike
 *
 */

package vkc.ldap.db.serializer;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.sql.Connection;

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

public class VKCLdapUserSerializer {
    private VKCLdapUserSerializer() {
    }

	public final static VKCLdapUser retrieveLdapUser( Integer ldapUserId ) throws DBException {
		Connection conn = null;

		try {
			conn = WebShare.database.getConnection();
			
			return retrieveLdapUser( ldapUserId, conn );
		} catch ( Exception ex ) {
			throw new DBException( ex );
		} finally {
			DBHelper.close( conn );
		}
	}
    
	public final static VKCLdapUser retrieveLdapUser( Integer ldapUserId, Connection conn )	throws DBException {
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		
		try {
			if ( ldapUserId == null ) {
				return null;
			}
			
			pStmt = conn.prepareStatement( "SELECT * FROM ldapUsers WHERE ldapUserId = ?" );
			pStmt.setInt( 1, ldapUserId.intValue() );
			rs = pStmt.executeQuery();
			
			if ( rs.next() ) {
				VKCLdapUser ldapUser = new VKCLdapUser();
				ldapUser.setLdapUserId( new Integer( rs.getInt( "ldapUserId" ) ) );
				ldapUser.setLdapServerId( new Integer( rs.getInt( "ldapServerId" ) ) );
				ldapUser.setLdapUserRDN( rs.getString( "ldapUserRDN" ) );
			
				return ldapUser;
			} 

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

	/**
	 * Speichert eine Gruppe in der DB ab Falls schon vorhanden, wird sie
	 * aktualisiert, sonst neu inseriert
	 *
	 * @param conn
	 * @param User die Gruppe
	 */
	public static void saveUser( VKCLdapUser ldapUser )	throws DBException {
		Connection conn = null;

		try {
			conn = WebShare.database.getConnection();
			saveLdapUser( ldapUser, conn );
			conn.commit();
		} catch ( Exception ex ) {
			DBHelper.rollBack( conn );
			throw new DBException( ex );
		} finally {
			DBHelper.close( conn );
		}
	}

	/**
	 * Speichert eine Gruppe in der DB ab Falls schon vorhanden, wird sie
	 * aktualisiert, sonst neu inseriert
	 *
	 * @param conn
	 * @param User die Gruppe
	 */
	public static void saveLdapUser( VKCLdapUser ldapUser, Connection conn  ) throws DBException {
		PreparedStatement pStmt = null;

		try {
			String stmt = null;

			if ( ldapUser.getLdapUserId() == null || ldapUser.getLdapUserId().intValue() <= 0 ) {
				ldapUser.setLdapUserId( DBHelper.getNewId( conn, "ldapUsers", "ldapUserId" ) );
	
				stmt  = "INSERT INTO ldapUsers ( ";
				stmt += "ldapServerId, ";
				stmt += "ldapUserRDN, ";
				stmt += "ldapUserId ) ";
				stmt += " values ( ?, ?, ? ) ";
			} else {
				stmt  = "UPDATE ldapUsers SET ";
				stmt += "ldapServerId = ?, ";
				stmt += "ldapUserRDN = ?, ";
				stmt += "WHERE ldapUserId = ?";
			}

			pStmt = conn.prepareStatement( stmt );
			pStmt.setInt( 1, ldapUser.getLdapServerId().intValue() );
			pStmt.setString( 2, ldapUser.getLdapUserRDN() );
			pStmt.setInt( 3, ldapUser.getLdapUserId().intValue() );

			pStmt.execute();
		} catch ( Exception ex ) {
			throw new DBException( ex );
		} finally {
			DBHelper.close( pStmt );
		}
	}
	
	/**
	 * Löscht eine VKCLdapUser aus der Datenbank
	 *
	 * @param UserId ID der zu löschenden Gruppe
	 *
	 * @throws DBException DOCUMENT ME!
	 */
	public final static void deleteLdapUser(Integer ldapUserId) throws DBException {
		Connection conn = null;
		PreparedStatement pStmt = null;

		try {
			conn = WebShare.database.getConnection();
			
			pStmt = conn.prepareStatement( "DELETE FROM CategoryAcls WHERE AclTypeId = " + VKCAclType.LDAP_USER_TYPE + " AND AccessorId = ?" );
			pStmt.setInt( 1, ldapUserId.intValue() );
			pStmt.execute();
			DBHelper.close(pStmt);
			pStmt = conn.prepareStatement( "DELETE FROM ldapUsers WHERE ldapUserId = ?" );
			pStmt.setInt( 1, ldapUserId.intValue() );
			pStmt.execute();

			conn.commit();
		} catch ( Exception e ) {
			DBHelper.rollBack( conn );
			throw new DBException( e );
		} finally {
			DBHelper.close( pStmt );
			DBHelper.close( conn );
		}
	}     
}