/// <summary>
        /// Método invocado cuando se hace click en el botón Aceptar.
        /// </summary>
        /// <param name="sender">
        /// El objeto que genera el evento.
        /// </param>
        /// <param name="e">
        /// Un objeto que contiene información acerca del evento.
        /// </param>
        private void OnOkClicked(object sender, RoutedEventArgs e)
        {
            System.Collections.Generic.List <ServiceCampaignEntity> toRemove = new List <ServiceCampaignEntity>();

            // Agrega los servicios quitadas a la lista.
            foreach (ServiceCampaignEntity serviceCampaign in campaign.ServiceCampaign)
            {
                bool exists = false;

                foreach (ServiceEntity service in Selected)
                {
                    if (serviceCampaign.Service.Name == service.Name)
                    {
                        exists = true;
                        break;
                    }
                }

                if (!exists)
                {
                    toRemove.Add(serviceCampaign);
                }
            }

            // Quitarlas.
            foreach (ServiceCampaignEntity serviceCampaign in toRemove)
            {
                campaign.ServiceCampaign.Remove(serviceCampaign);
            }

            // Agregar los nuevos servicios.
            foreach (ServiceEntity service in Selected)
            {
                bool exists = false;

                foreach (ServiceCampaignEntity serviceCampaign in campaign.ServiceCampaign)
                {
                    if (serviceCampaign.Service.Name == service.Name)
                    {
                        exists = true;
                        break;
                    }
                }

                if (!exists)
                {
                    ServiceCampaignEntity serviceCampaign = new ServiceCampaignEntity();
                    serviceCampaign.Campaign = campaign;

                    campaign.ServiceCampaign.Add(serviceCampaign);
                }
            }

            if (OkSelected != null)
            {
                OkSelected(sender, e);
            }
        }
        /// <summary>
        /// Function to Load the relation Campaign from database.
        /// </summary>
        /// <param name="serviceCampaign">ServiceCampaignEntity parent</param>
        /// <exception cref="ArgumentNullException">
        /// if <paramref name="serviceCampaign"/> is not a <c>ServiceCampaignEntity</c>.
        /// </exception>
        public void LoadRelationCampaign(ServiceCampaignEntity serviceCampaign, Dictionary <string, IEntity> scope)
        {
            if (serviceCampaign == null)
            {
                throw new ArgumentException("The argument can't be null");
            }
            bool closeConnection = false;

            try
            {
                // Create a new connection if needed
                if (dbConnection == null || dbConnection.State.CompareTo(ConnectionState.Closed) == 0)
                {
                    closeConnection = true;
                    dbConnection    = dataAccess.GetNewConnection();
                    dbConnection.Open();
                }
                // Create a command

                string           cmdText    = "SELECT idCampaign FROM [ServiceCampaign] WHERE idServiceCampaign = @idServiceCampaign";
                IDbCommand       sqlCommand = dataAccess.GetNewCommand(cmdText, dbConnection, dbTransaction);
                IDbDataParameter parameter  = dataAccess.GetNewDataParameter("@idServiceCampaign", DbType.Int32);
                // Set command parameters values

                parameter.Value = serviceCampaign.Id;
                sqlCommand.Parameters.Add(parameter);
                // Execute commands

                object idRelation = sqlCommand.ExecuteScalar();
                if (idRelation != null && ((int)idRelation) > 0)
                {
                    // Create data access objects and set connection objects
                    CampaignDataAccess campaignDataAccess = new CampaignDataAccess();
                    campaignDataAccess.SetConnectionObjects(dbConnection, dbTransaction);
                    // Load related object

                    serviceCampaign.Campaign = campaignDataAccess.Load(((int)idRelation), true, scope);
                }
            }
            catch (DbException dbException)
            {
                // Catch and rethrow as custom exception
                throw new UtnEmallDataAccessException(dbException.Message, dbException);
            }
            finally
            {
                // Close connection if initiated by me
                if (closeConnection)
                {
                    dbConnection.Close();
                }
            }
        }
        private void FillSaveParameters(ServiceCampaignEntity serviceCampaign, IDbCommand sqlCommand)
        {
            IDbDataParameter parameter;

            parameter = dataAccess.GetNewDataParameter("@idService", DbType.Int32);

            parameter.Value = serviceCampaign.IdService;
            sqlCommand.Parameters.Add(parameter);
            parameter = dataAccess.GetNewDataParameter("@idCampaign", DbType.Int32);

            parameter.Value = serviceCampaign.IdCampaign;
            sqlCommand.Parameters.Add(parameter);
        }
        /// <summary>
        /// Function to load a ServiceCampaignEntity from database.
        /// </summary>
        /// <param name="id">The ID of the record to load</param>
        /// <param name="loadRelation">if is true load the relation</param>
        /// <param name="scope">Internal structure used to avoid circular reference locks, must be provided if calling from other data access object</param>
        /// <returns>The entity instance</returns>
        /// <exception cref="UtnEmallDataAccessException">
        /// If a DbException occurs while accessing the database.
        /// </exception>
        public ServiceCampaignEntity Load(int id, bool loadRelation, Dictionary <string, IEntity> scope)
        {
            // Build a key for internal scope object
            string scopeKey = id.ToString(NumberFormatInfo.InvariantInfo) + "ServiceCampaign";

            if (scope != null)
            {
                // If scope contains the object it was already loaded,
                // return it to avoid circular references
                if (scope.ContainsKey(scopeKey))
                {
                    return((ServiceCampaignEntity)scope[scopeKey]);
                }
            }
            else
            {
                // If there isn't a current scope create one
                scope = new Dictionary <string, IEntity>();
            }

            ServiceCampaignEntity serviceCampaign = null;

            // Check if the entity was already loaded by current data access object
            // and return it if that is the case

            if (inMemoryEntities.ContainsKey(id))
            {
                serviceCampaign = inMemoryEntities[id];
                // Add current object to current load scope

                scope.Add(scopeKey, serviceCampaign);
            }
            else
            {
                bool closeConnection = false;
                try
                {
                    // Open a new connection if it isn't on a transaction
                    if (dbConnection == null || dbConnection.State.CompareTo(ConnectionState.Closed) == 0)
                    {
                        closeConnection = true;
                        dbConnection    = dataAccess.GetNewConnection();
                        dbConnection.Open();
                    }

                    string cmdText = "SELECT idServiceCampaign, idService, idCampaign, timestamp FROM [ServiceCampaign] WHERE idServiceCampaign = @idServiceCampaign";
                    // Create the command

                    IDbCommand sqlCommand = dataAccess.GetNewCommand(cmdText, dbConnection, dbTransaction);
                    // Create the Id parameter for the query

                    IDbDataParameter parameter = dataAccess.GetNewDataParameter("@idServiceCampaign", DbType.Int32);
                    parameter.Value = id;
                    sqlCommand.Parameters.Add(parameter);
                    // Use a DataReader to get data from db

                    IDataReader reader = sqlCommand.ExecuteReader();
                    serviceCampaign = new ServiceCampaignEntity();

                    if (reader.Read())
                    {
                        // Load fields of entity
                        serviceCampaign.Id = reader.GetInt32(0);

                        serviceCampaign.IdService  = reader.GetInt32(1);
                        serviceCampaign.IdCampaign = reader.GetInt32(2);
                        // Add current object to the scope

                        scope.Add(scopeKey, serviceCampaign);
                        // Add current object to cache of loaded entities

                        inMemoryEntities.Add(serviceCampaign.Id, serviceCampaign);
                        // Read the timestamp and set new and changed properties

                        serviceCampaign.Timestamp = reader.GetDateTime(3);
                        serviceCampaign.IsNew     = false;
                        serviceCampaign.Changed   = false;
                        // Close the reader

                        reader.Close();
                        // Load related objects if required

                        if (loadRelation)
                        {
                            LoadRelationCampaign(serviceCampaign, scope);
                        }
                    }
                    else
                    {
                        reader.Close();
                    }
                }
                catch (DbException dbException)
                {
                    // Catch DBException and rethrow as custom exception
                    throw new UtnEmallDataAccessException(dbException.Message, dbException);
                }
                finally
                {
                    // Close connection if it was opened by ourself
                    if (closeConnection)
                    {
                        dbConnection.Close();
                    }
                }
            }
            // Return the loaded entity
            return(serviceCampaign);
        }
        /// <summary>
        /// Function to Delete a ServiceCampaignEntity from database.
        /// </summary>
        /// <param name="serviceCampaign">ServiceCampaignEntity to delete</param>
        /// <param name="scope">Internal structure to avoid circular reference locks. Must provide an instance while calling from other data access object.</param>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="serviceCampaign"/> is not a <c>ServiceCampaignEntity</c>.
        /// </exception>
        /// <exception cref="UtnEmallDataAccessException">
        /// If an DbException occurs in the try block while accessing the database.
        /// </exception>
        public void Delete(ServiceCampaignEntity serviceCampaign, Dictionary <string, IEntity> scope)
        {
            if (serviceCampaign == null)
            {
                throw new ArgumentException("The argument can't be null");
            }
            try
            {
                // Open connection and initialize a transaction if needed
                if (!isGlobalTransaction)
                {
                    dbConnection = dataAccess.GetNewConnection();
                    dbConnection.Open();
                    dbTransaction = dbConnection.BeginTransaction();
                }
                // Reload the entity to ensure deletion of older data

                serviceCampaign = this.Load(serviceCampaign.Id, true);
                if (serviceCampaign == null)
                {
                    throw new UtnEmallDataAccessException("Error retrieving data while trying to delete.");
                }
                // Create a command for delete
                string     cmdText    = "DeleteServiceCampaign";
                IDbCommand sqlCommand = dataAccess.GetNewCommand(cmdText, dbConnection, dbTransaction);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                // Add values to parameters

                IDbDataParameter parameterID = dataAccess.GetNewDataParameter("@idServiceCampaign", DbType.Int32);
                parameterID.Value = serviceCampaign.Id;
                sqlCommand.Parameters.Add(parameterID);
                // Execute the command

                sqlCommand.ExecuteNonQuery();
                // Delete related objects
                // Commit transaction if is mine
                if (!isGlobalTransaction)
                {
                    dbTransaction.Commit();
                }
                // Remove entity from loaded objects

                inMemoryEntities.Remove(serviceCampaign.Id);
                // Remove entity from current internal scope

                if (scope != null)
                {
                    string scopeKey = serviceCampaign.Id.ToString(NumberFormatInfo.InvariantInfo) + "ServiceCampaign";
                    scope.Remove(scopeKey);
                }
            }
            catch (DbException dbException)
            {
                // Rollback transaction
                if (!isGlobalTransaction)
                {
                    dbTransaction.Rollback();
                }
                // Rethrow as custom exception
                throw new UtnEmallDataAccessException(dbException.Message, dbException);
            }
            finally
            {
                // Close connection if it was initiated by this instance
                if (!isGlobalTransaction)
                {
                    dbConnection.Close();
                    dbConnection  = null;
                    dbTransaction = null;
                }
            }
        }
 /// <summary>
 /// Function to Delete a ServiceCampaignEntity from database.
 /// </summary>
 /// <param name="serviceCampaign">ServiceCampaignEntity to delete</param>
 /// <exception cref="ArgumentNullException">
 /// If <paramref name="serviceCampaign"/> is not a <c>ServiceCampaignEntity</c>.
 /// </exception>
 /// <exception cref="UtnEmallDataAccessException">
 /// If an DbException occurs in the try block while accessing the database.
 /// </exception>
 public void Delete(ServiceCampaignEntity serviceCampaign)
 {
     Delete(serviceCampaign, null);
 }
        /// <summary>
        /// Function to Save a ServiceCampaignEntity in the database.
        /// </summary>
        /// <param name="serviceCampaign">ServiceCampaignEntity to save</param>
        /// <param name="scope">Interna structure to avoid circular reference locks. Provide an instance when calling from other data access object.</param>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="serviceCampaign"/> is not a <c>ServiceCampaignEntity</c>.
        /// </exception>
        /// <exception cref="UtnEmallDataAccessException">
        /// If an DbException occurs in the try block while accessing the database.
        /// </exception>
        public void Save(ServiceCampaignEntity serviceCampaign, Dictionary <string, IEntity> scope)
        {
            if (serviceCampaign == null)
            {
                throw new ArgumentException("The argument can't be null");
            }
            // Create a unique key to identify the object in the internal scope
            string scopeKey = serviceCampaign.Id.ToString(NumberFormatInfo.InvariantInfo) + "ServiceCampaign";

            if (scope != null)
            {
                // If it's on the scope return it, don't save again
                if (scope.ContainsKey(scopeKey))
                {
                    return;
                }
            }
            else
            {
                // Create a new scope if it's not provided
                scope = new Dictionary <string, IEntity>();
            }

            try
            {
                // Open a DbConnection and a new transaction if it isn't on a higher level one
                if (!isGlobalTransaction)
                {
                    dbConnection = dataAccess.GetNewConnection();
                    dbConnection.Open();
                    dbTransaction = dbConnection.BeginTransaction();
                }

                string commandName = "";
                bool   isUpdate    = false;
                // Check if it is an insert or update command

                if (serviceCampaign.IsNew || !DataAccessConnection.ExistsEntity(serviceCampaign.Id, "ServiceCampaign", "idServiceCampaign", dbConnection, dbTransaction))
                {
                    commandName = "SaveServiceCampaign";
                }
                else
                {
                    isUpdate    = true;
                    commandName = "UpdateServiceCampaign";
                }
                // Create a db command
                IDbCommand sqlCommand = dataAccess.GetNewCommand(commandName, dbConnection, dbTransaction);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                // Add parameters values to current command

                IDbDataParameter parameter;
                if (isUpdate)
                {
                    parameter       = dataAccess.GetNewDataParameter("@idServiceCampaign", DbType.Int32);
                    parameter.Value = serviceCampaign.Id;
                    sqlCommand.Parameters.Add(parameter);
                }

                FillSaveParameters(serviceCampaign, sqlCommand);
                // Execute the command
                if (isUpdate)
                {
                    sqlCommand.ExecuteNonQuery();
                }
                else
                {
                    IDbDataParameter parameterIdOutput = dataAccess.GetNewDataParameter("@idServiceCampaign", DbType.Int32);
                    parameterIdOutput.Direction = ParameterDirection.ReturnValue;
                    sqlCommand.Parameters.Add(parameterIdOutput);

                    sqlCommand.ExecuteNonQuery();
                    serviceCampaign.Id = Convert.ToInt32(parameterIdOutput.Value, NumberFormatInfo.InvariantInfo);
                }

                scopeKey = serviceCampaign.Id.ToString(NumberFormatInfo.InvariantInfo) + "ServiceCampaign";
                // Add entity to current internal scope

                scope.Add(scopeKey, serviceCampaign);
                // Save collections of related objects to current entity
                // Save objects related to current entity
                // Update
                // Close transaction if initiated by me
                if (!isGlobalTransaction)
                {
                    dbTransaction.Commit();
                }
                // Update new and changed flags

                serviceCampaign.IsNew   = false;
                serviceCampaign.Changed = false;
            }
            catch (DbException dbException)
            {
                // Rollback transaction
                if (!isGlobalTransaction)
                {
                    dbTransaction.Rollback();
                }
                // Rethrow as custom exception
                throw new UtnEmallDataAccessException(dbException.Message, dbException);
            }
            finally
            {
                // Close connection if initiated by me
                if (!isGlobalTransaction)
                {
                    dbConnection.Close();
                    dbConnection  = null;
                    dbTransaction = null;
                }
            }
        }
 /// <summary>
 /// Function to Save a ServiceCampaignEntity in the database.
 /// </summary>
 /// <param name="serviceCampaign">ServiceCampaignEntity to save</param>
 /// <exception cref="ArgumentNullException">
 /// if <paramref name="serviceCampaign"/> is not a <c>ServiceCampaignEntity</c>.
 /// </exception>
 /// <exception cref="UtnEmallDataAccessException">
 /// If an DbException occurs in the try block while accessing the database.
 /// </exception>
 public void Save(ServiceCampaignEntity serviceCampaign)
 {
     Save(serviceCampaign, null);
 }