Esempio n. 1
0
        /// <summary>
        /// Function to Delete a list of related entities from database.
        /// </summary>
        /// <param name="collectionDataAccess">IDataAccess of the relation</param>
        /// <param name="collection">The collection of entities to delete</param>
        /// <param name="scope">Internal structure to keep safe circular referencies</param>
        /// <returns>True if collection not null</returns>
        private bool DeleteRegisterAssociationCategoriesCollection(RegisterAssociationCategoriesDataAccess collectionDataAccess, Collection <RegisterAssociationCategoriesEntity> collection, Dictionary <string, IEntity> scope)
        {
            if (collection == null)
            {
                return(false);
            }
            // Set connection objects of related data access object
            collectionDataAccess.SetConnectionObjects(dbConnection, dbTransaction);
            // Delete related objects

            for (int i = 0; i < collection.Count; i++)
            {
                collectionDataAccess.Delete(collection[i], scope);
            }
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Function to Load the relation RegisterAssociationCategories from database.
        /// </summary>
        /// <param name="registerAssociation">RegisterAssociationEntity parent</param>
        /// <param name="scope">Internal structure to avoid problems with circular referencies</param>
        /// <exception cref="ArgumentNullException">
        /// if <paramref name="registerAssociation"/> is not a <c>RegisterAssociationEntity</c>.
        /// </exception>
        public void LoadRelationRegisterAssociationCategories(RegisterAssociationEntity registerAssociation, Dictionary <string, IEntity> scope)
        {
            if (registerAssociation == null)
            {
                throw new ArgumentException("The argument can't be null");
            }
            // Create data access object for related object
            RegisterAssociationCategoriesDataAccess registerAssociationCategoriesDataAccess = new RegisterAssociationCategoriesDataAccess();

            // Set connection objects to the data access

            registerAssociationCategoriesDataAccess.SetConnectionObjects(dbConnection, dbTransaction);
            // Load related objects

            registerAssociation.RegisterAssociationCategories = registerAssociationCategoriesDataAccess.LoadByRegisterAssociationCollection(registerAssociation.Id, scope);
        }
        private static void CheckForDelete(CategoryEntity entity)
        {
            StoreCategoryDataAccess   varStoreCategoryDataAccess   = new StoreCategoryDataAccess();
            ServiceCategoryDataAccess varServiceCategoryDataAccess = new ServiceCategoryDataAccess();
            RegisterAssociationCategoriesDataAccess varRegisterAssociationCategoriesDataAccess = new RegisterAssociationCategoriesDataAccess();

            if (varStoreCategoryDataAccess.LoadWhere(StoreCategoryEntity.DBIdCategory, entity.Id, false, OperatorType.Equal).Count > 0)
            {
                throw new UtnEmallDataAccessException("Existen tiendas asociadas a esta categoría.");
            }
            if (varServiceCategoryDataAccess.LoadWhere(ServiceCategoryEntity.DBIdCategory, entity.Id, false, OperatorType.Equal).Count > 0)
            {
                throw new UtnEmallDataAccessException("Existen servicos asociados a esta categoría.");
            }
            if (varRegisterAssociationCategoriesDataAccess.LoadWhere(RegisterAssociationCategoriesEntity.DBIdCategory, entity.Id, false, OperatorType.Equal).Count > 0)
            {
                throw new UtnEmallDataAccessException("Existen registros asociados a esta categoría.");
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Updates the database to reflect the current state of the list.
        /// </summary>
        /// <param name="collectionDataAccess">the IDataAccess of the relation</param>
        /// <param name="parent">the parent of the object</param>
        /// <param name="collection">a collection of items</param>
        /// <param name="isNewParent">if the parent is a new object</param>
        /// <param name="scope">internal data structure to aviod problems with circular referencies on entities</param>
        /// <exception cref="UtnEmallDataAccessException">
        /// If an DbException occurs in the try block while accessing the database.
        /// </exception>
        private void SaveRegisterAssociationCategoriesCollection(RegisterAssociationCategoriesDataAccess collectionDataAccess, RegisterAssociationEntity parent, Collection <RegisterAssociationCategoriesEntity> collection, bool isNewParent, Dictionary <string, IEntity> scope)
        {
            if (collection == null)
            {
                return;
            }
            // Set connection objects on collection data access
            collectionDataAccess.SetConnectionObjects(dbConnection, dbTransaction);
            // Set the child/parent relation

            for (int i = 0; i < collection.Count; i++)
            {
                bool changed = collection[i].Changed;
                collection[i].RegisterAssociation = parent;
                collection[i].Changed             = changed;
            }
            // If the parent is new save all childs, else check diferencies with db

            if (isNewParent)
            {
                for (int i = 0; i < collection.Count; i++)
                {
                    collectionDataAccess.Save(collection[i], scope);
                }
            }
            else
            {
                // Check the childs that are not part of the parent any more
                string idList = "0";
                if (collection.Count > 0)
                {
                    idList = "" + collection[0].Id;
                }

                for (int i = 1; i < collection.Count; i++)
                {
                    idList += ", " + collection[i].Id;
                }
                // Returns the ids that doesn't exists in the current collection

                string command = "SELECT idRegisterAssociationCategories FROM [RegisterAssociationCategories] WHERE idRegisterAssociation = @idRegisterAssociation AND idRegisterAssociationCategories NOT IN (" + idList + ")";

                IDbCommand sqlCommand = dataAccess.GetNewCommand(command, dbConnection, dbTransaction);

                IDbDataParameter sqlParameterId = dataAccess.GetNewDataParameter("@idRegisterAssociation", DbType.Int32);
                sqlParameterId.Value = parent.Id;
                sqlCommand.Parameters.Add(sqlParameterId);

                IDataReader reader = sqlCommand.ExecuteReader();
                Collection <RegisterAssociationCategoriesEntity> objectsToDelete = new Collection <RegisterAssociationCategoriesEntity>();
                // Insert Ids on a list

                List <int> listId = new List <int>();
                while (reader.Read())
                {
                    listId.Add(reader.GetInt32(0));
                }

                reader.Close();
                // Load items to be removed

                foreach (int id in listId)
                {
                    RegisterAssociationCategoriesEntity entityToDelete = collectionDataAccess.Load(id, scope);
                    objectsToDelete.Add(entityToDelete);
                }
                // Have to do this because the reader must be closed before
                // deletion of entities

                for (int i = 0; i < objectsToDelete.Count; i++)
                {
                    collectionDataAccess.Delete(objectsToDelete[i], scope);
                }

                System.DateTime timestamp;
                // Check all the properties of the collection items
                // to see if they have changed (timestamp)

                for (int i = 0; i < collection.Count; i++)
                {
                    RegisterAssociationCategoriesEntity item = collection[i];
                    if (!item.Changed && !item.IsNew)
                    {
                        // Create the command
                        string     sql = "SELECT timestamp FROM [RegisterAssociationCategories] WHERE idRegisterAssociationCategories = @idRegisterAssociationCategories";
                        IDbCommand sqlCommandTimestamp = dataAccess.GetNewCommand(sql, dbConnection, dbTransaction);
                        // Set the command's parameters values

                        IDbDataParameter sqlParameterIdPreference = dataAccess.GetNewDataParameter("@idRegisterAssociationCategories", DbType.Int32);
                        sqlParameterIdPreference.Value = item.Id;
                        sqlCommandTimestamp.Parameters.Add(sqlParameterIdPreference);

                        timestamp = ((System.DateTime)sqlCommandTimestamp.ExecuteScalar());
                        if (item.Timestamp != timestamp)
                        {
                            item.Changed = true;
                        }
                    }
                    // Save the item if it changed or is new

                    if (item.Changed || item.IsNew)
                    {
                        collectionDataAccess.Save(item);
                    }
                }
            }
        }