using System;
using System.Data;
using System.Data.SqlClient;

using Municipalidad.Utils;
using Seguridad.DataAccess;
using Seguridad.SingleObjects;

namespace Seguridad.BusinessObjects
{
	public abstract class RlsAuditoriaCRUD
	{
		/// <summary>
		/// Constructor
		/// <summary>
		public RlsAuditoriaCRUD()
		{
		}

		/// <summary>
		/// 
		/// </summary>
		public DSAuditoria LoadAll()
		{
			//SqlConnection utilizado para ejecutar el SqlCommand
			SqlConnection connection = null;

			//Almacena retorno del DataAccess
			DSAuditoria ds;

			try
			{
				//Abre la conexion
				connection = UtilsDB.GetConnection(UtilsConnectionManager.GetConnection(eConnectionString.Seguridad));

				Auditoria da = new Auditoria(connection);
				ds = da.SelectAll();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if(connection != null)
					connection.Dispose();
			}

			return ds;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="idAuditoria"></param>
		/// <returns></returns>
		public DSAuditoria LoadByID(int idAuditoria)
		{
			//SqlConnection utilizado para ejecutar el SqlCommand
			SqlConnection connection = null;

			//Almacena retorno del DataAccess
			DSAuditoria ds;

			try
			{
				//Abre la conexion
				connection = UtilsDB.GetConnection(UtilsConnectionManager.GetConnection(eConnectionString.Seguridad));

				Auditoria da = new Auditoria(connection);
				ds = da.SelectByID(idAuditoria);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if(connection != null)
					connection.Dispose();
			}

			return ds;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sglAuditoria"></param>
		/// <returns></returns>
		public int Add(SglAuditoria sglAuditoria)
		{
			return Save(sglAuditoria, false);

		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sglAuditoria"></param>
		public void Save(SglAuditoria sglAuditoria)
		{
			Save(sglAuditoria, true);

		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="idAuditoria"></param>
		public void Delete(int idAuditoria)
		{
			//SqlConnection utilizado para ejecutar el SqlCommand
			SqlConnection connection = null;

			try
			{
				//Abre la conexion
				connection = UtilsDB.GetConnection(UtilsConnectionManager.GetConnection(eConnectionString.Seguridad));

				Auditoria da = new Auditoria(connection);
				da.Delete(idAuditoria);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if(connection != null)
					connection.Dispose();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sglAuditoria"></param>
		/// <param name="operation"></param>
		/// <returns></returns>
		private int Save(SglAuditoria sglAuditoria, bool operation)
		{
			//SqlConnection utilizado para ejecutar el SqlCommand
			SqlConnection connection = null;

			//Almacena retorno del DataAccess
			int newId;

			try
			{
				//Abre la conexion
				connection = UtilsDB.GetConnection(UtilsConnectionManager.GetConnection(eConnectionString.Seguridad));

				Auditoria da = new Auditoria(connection);
				if (operation)
				{
					da.Insert(sglAuditoria);
					newId = int.MinValue;
				}
				else
					newId = (int)da.Insert(sglAuditoria);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if(connection != null)
					connection.Dispose();
			}

			return newId;
		}
	}
}