All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.antiaction.raptor.sql.mssql.MSSql_SecurityTreeChain Maven / Gradle / Ivy

/*
 * Created on 15/09/2010
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

package com.antiaction.raptor.sql.mssql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.antiaction.raptor.dao.SecurityChain;
import com.antiaction.raptor.dao.SecurityPermission;

public class MSSql_SecurityTreeChain {

	private static Logger logger = Logger.getLogger( MSSql_SecurityTreeChain.class.getName() );

	public static int insert(Connection conn, SecurityChain chain) {
		String sql;
		PreparedStatement stm = null;
		ResultSet rs = null;

		try {
			sql = "INSERT INTO security_tree_chain(permission_id) ";
			sql += "VALUES(?) ";
			sql += "SELECT id FROM security_tree_chain WHERE id = @@identity ";

			stm = conn.prepareStatement( sql );
			stm.clearParameters();
			stm.setInt( 1, chain.permission_id );

			rs = stm.executeQuery();
			if ( rs.next() ) {
				chain.id = rs.getInt( 1 );
			}

			rs.close();
			rs = null;
			stm.close();
			stm = null;
		}
		catch (SQLException e) {
			logger.log( Level.SEVERE, e.toString(), e );
		}
		finally {
			if ( rs != null ) {
				try {
					rs.close();
				}
				catch (SQLException e) {
					logger.log( Level.SEVERE, e.toString(), e );
				}
				rs = null;
			}
			if ( stm != null ) {
				try {
					stm.close();
				}
				catch (SQLException e) {
					logger.log( Level.SEVERE, e.toString(), e );
				}
				stm = null;
			}
		}

		return chain.id;
	}

	public static SecurityChain getById(Connection conn, int id) {
		SecurityChain chain = null;

		String sql;
		PreparedStatement stm = null;
		ResultSet rs = null;

		try {
			sql = "SELECT id, permission_id FROM security_tree_chain ";
			sql += "WHERE id = ? ";

			stm = conn.prepareStatement( sql );
			stm.clearParameters();
			stm.setInt( 1, id );

			rs = stm.executeQuery();
			if ( rs.next() ) {
				chain = new SecurityChain();
				chain.id = rs.getInt( 1 );
				chain.permission_id = rs.getInt( 2 );
			}

			rs.close();
			rs = null;
			stm.close();
			stm = null;
		}
		catch (SQLException e) {
			chain = null;
			logger.log( Level.SEVERE, e.toString(), e );
		}
		finally {
			if ( rs != null ) {
				try {
					rs.close();
				}
				catch (SQLException e) {
					logger.log( Level.SEVERE, e.toString(), e );
				}
				rs = null;
			}
			if ( stm != null ) {
				try {
					stm.close();
				}
				catch (SQLException e) {
					logger.log( Level.SEVERE, e.toString(), e );
				}
				stm = null;
			}
		}

		return chain;
	}

	public static void update(Connection conn, SecurityChain chain) {
		String sql;
		PreparedStatement stm = null;

		try {
			sql = "UPDATE security_tree_chain SET ";
			sql += "permission_id = ? ";
			sql += "WHERE id = ? ";

			stm = conn.prepareStatement( sql );
			stm.clearParameters();
			stm.setInt( 1, chain.permission_id );
			stm.setInt( 2, chain.id );
			stm.executeUpdate();
			stm.close();
			stm = null;
		}
		catch (SQLException e) {
			logger.log( Level.SEVERE, e.toString(), e );
		}
		finally {
			if ( stm != null ) {
				try {
					stm.close();
				}
				catch (SQLException e) {
					logger.log( Level.SEVERE, e.toString(), e );
				}
				stm = null;
			}
		}
	}

	public static void deleteByObj(Connection conn, SecurityChain chain) {
		if ( chain != null && chain.id > 0 ) {
			deleteById( conn, chain.id );
		}
	}

	public static void deleteById(Connection conn, int id) {
		String sql;
		PreparedStatement stm = null;

		try {
			sql = "DELETE security_tree_chain ";
			sql += "WHERE id = ? ";

			stm = conn.prepareStatement( sql );
			stm.clearParameters();
			stm.setInt( 1, id );
			stm.executeUpdate();
			stm.close();
			stm = null;
		}
		catch (SQLException e) {
			logger.log( Level.SEVERE, e.toString(), e );
		}
		finally {
			if ( stm != null ) {
				try {
					stm.close();
				}
				catch (SQLException e) {
					logger.log( Level.SEVERE, e.toString(), e );
				}
				stm = null;
			}
		}
	}

	public static void deleteByPermissionObj(Connection conn, SecurityPermission permission) {
		if ( permission != null && permission.id > 0 ) {
			deleteByPermissionId( conn, permission.id );
		}
	}

	public static void deleteByPermissionId(Connection conn, int permission_id) {
		String sql;
		PreparedStatement stm = null;

		try {
			sql = "DELETE security_tree_chain ";
			sql += "WHERE permission_id = ? ";

			stm = conn.prepareStatement( sql );
			stm.clearParameters();
			stm.setInt( 1, permission_id );
			stm.executeUpdate();
			stm.close();
			stm = null;
		}
		catch (SQLException e) {
			logger.log( Level.SEVERE, e.toString(), e );
		}
		finally {
			if ( stm != null ) {
				try {
					stm.close();
				}
				catch (SQLException e) {
					logger.log( Level.SEVERE, e.toString(), e );
				}
				stm = null;
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy