Esempio n. 1
0
        /// <summary>
        /// Elimina la asociación entre registros de tablas de modelo de datos y categorías.
        /// </summary>
        /// <param name="associations">Listado de asociaciones a eliminar.</param>
        /// <param name="sessionId">Identificador de sesión.</param>
        /// <returns>True si la operación se realizó con éxito.</returns>
        public bool RemoveAssociations(Collection <RegisterAssociationData> associations, string sessionId)
        {
            if (associations == null)
            {
                throw new ArgumentNullException("associations", "Associations must have at least one RegisterAssociation.");
            }
            if (sessionId == null)
            {
                throw new ArgumentNullException("sessionId", "SessionId must not be null.");
            }
            if (associations.Count == 0)
            {
                throw new ArgumentException("Associations list must have at least one RegisterAssociation.", "associations");
            }

            bool result = true;
            RegisterAssociation RAClient = new RegisterAssociation();
            Collection <RegisterAssociationEntity> registerAssociationList = ConvertFromRegisterAssociationData(associations, sessionId);

            foreach (RegisterAssociationEntity registerAssociationEntity in registerAssociationList)
            {
                // Si el valor de retorno no es nulo, ha ocurrido un error.
                if (RAClient.Delete(registerAssociationEntity, sessionId) != null)
                {
                    result = false;
                }
            }

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Convertir los tipos de datos usados en el cliente a tipos usados en el servidor
        /// </summary>
        /// <param name="listOfRAData">Listado a convertir</param>
        /// <param name="sessionId">Identificador de sesión</param>
        /// <returns>Una colección de asociaciones</returns>
        private static Collection <RegisterAssociationEntity> ConvertFromRegisterAssociationData(Collection <RegisterAssociationData> listOfRegisterAssociationData, string sessionId)
        {
            RegisterAssociation RAClient = new RegisterAssociation();
            Collection <RegisterAssociationEntity> result = new Collection <RegisterAssociationEntity>();

            StoreEntity userStore = SessionManager.Instance.StoreFromUserSession(sessionId);

            // Convertir los objetos de entidad
            foreach (RegisterAssociationData registerAssociation in listOfRegisterAssociationData)
            {
                bool notFound = true;
                RegisterAssociationEntity registerAssociationEntity;
                TableEntity table = ObtainTableEntity(registerAssociation.TableName, userStore, sessionId);

                Collection <RegisterAssociationEntity> tableFilteredRAEntities = RAClient.GetRegisterAssociationWhereEqual(RegisterAssociationEntity.DBIdTable, table.Id.ToString(System.Globalization.NumberFormatInfo.InvariantInfo), sessionId);
                for (int i = 0; i < tableFilteredRAEntities.Count && notFound; i++)
                {
                    registerAssociationEntity = tableFilteredRAEntities[i];

                    if (registerAssociationEntity.IdRegister == registerAssociation.RegisterId)
                    {
                        notFound = false;
                        result.Add(registerAssociationEntity);
                    }
                }

                if (notFound)
                {
                    throw new UtnEmallBusinessLogicException("At least one of the register associations does not exist.");
                }
            }

            return(result);
        }
        public IList <IEntity> SortByUserPreferences(IList <IEntity> originalList, int idTable, int idUser, string sessionString)
        {
            // Get user preferences
            // IDictionary<Category,double> preferences
            Customer       customerManager = new Customer();
            CustomerEntity customer        = customerManager.GetCustomer(idUser, true, sessionString);
            var            preferenceMap   = new Dictionary <int, PreferenceEntity>();

            if (customer != null)
            {
                foreach (PreferenceEntity preference in customer.Preferences)
                {
                    // add preference only if its active and level is greater than 0
                    if (preference.Active && preference.Level > 0)
                    {
                        preferenceMap.Add(preference.IdCategory, preference);
                    }
                }
            }

            // Get a list of Categories associated with table's registers
            var raManager = new RegisterAssociation();
            var raList    = raManager.GetRegisterAssociationWhereEqual(RegisterAssociationEntity.DBIdTable, idTable, sessionString);
            // Caculate an index for each register based
            // on how many categories the register match:
            //      registerIndex = Sum( Category_Preference_Index * associations.Contains(category) ? 1 : 0 )
            var raeMap = new Dictionary <int, RegisterAssociationEntity>();

            foreach (var rae in raList)
            {
                raeMap.Add(rae.IdRegister, rae);
            }

            // map of IdRegister | Points
            var registerPointsMap = new Dictionary <int, double>();

            foreach (IEntity entity in originalList)
            {
                // check if register has associated categories
                if (raeMap.ContainsKey(entity.Id))
                {
                    var rae = raeMap[entity.Id];
                    foreach (var raec in rae.RegisterAssociationCategories)
                    {
                        if (preferenceMap.ContainsKey(raec.IdCategory))
                        {
                            if (registerPointsMap.ContainsKey(entity.Id))
                            {
                                registerPointsMap[entity.Id] = registerPointsMap[entity.Id] + 1 * preferenceMap[entity.Id].Level;
                            }
                            else
                            {
                                registerPointsMap.Add(entity.Id, 1 * preferenceMap[entity.Id].Level);
                            }
                        }
                    }
                }
            }

            // Sort register using calculated index, greater index best possition
            // consider filtering registers with 0 points
            var sortedList = new SortedList <double, IEntity>();

            foreach (IEntity entity in originalList)
            {
                double value = 0;
                if (registerPointsMap.ContainsKey(entity.Id))
                {
                    value = registerPointsMap[entity.Id];
                }
                sortedList.Add(value, entity);
            }

            return(sortedList.Values);
        }