Ejemplo n.º 1
0
        /// <summary>
        /// Delete relations.
        /// </summary>
        /// <param name="dataProvider">Implementation of the data provider used to access data.</param>
        public virtual void DeleteRelations(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider)
        {
            ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider));

            if (Identifier.HasValue == false)
            {
                throw new IntranetRepositoryException(Resource.GetExceptionMessage(ExceptionMessage.IllegalValue, Identifier, "Identifier"));
            }

            FoodItemGroupProxy.DeleteFoodItemGroups(dataProvider, this);
            TranslationProxy.DeleteDomainObjectTranslations(dataProvider, Identifier.Value);
            ForeignKeyProxy.DeleteDomainObjectForeignKeys(dataProvider, Identifier.Value);

            _dataProvider = (IFoodWasteDataProvider)dataProvider;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Save relations.
        /// </summary>
        /// <param name="dataProvider">Implementation of the data provider used to access data.</param>
        /// <param name="isInserting">Indication of whether we are inserting or updating.</param>
        public virtual void SaveRelations(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider, bool isInserting)
        {
            ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider));

            if (Identifier.HasValue == false)
            {
                throw new IntranetRepositoryException(Resource.GetExceptionMessage(ExceptionMessage.IllegalValue, Identifier, "Identifier"));
            }

            if (PrimaryFoodGroup != null && PrimaryFoodGroup.Identifier.HasValue == false)
            {
                throw new IntranetRepositoryException(Resource.GetExceptionMessage(ExceptionMessage.IllegalValue, PrimaryFoodGroup.Identifier, "PrimaryFoodGroup.Identifier"));
            }

            IFoodGroup foodGroupWithoutIdentifier = FoodGroups.FirstOrDefault(foodGroup => foodGroup.Identifier.HasValue == false);

            if (foodGroupWithoutIdentifier != null)
            {
                throw new IntranetRepositoryException(Resource.GetExceptionMessage(ExceptionMessage.IllegalValue, foodGroupWithoutIdentifier.Identifier, "FoodGroups[].Identifier"));
            }

            IList <FoodItemGroupProxy> foodItemGroups = FoodItemGroupProxy.GetFoodItemGroups(dataProvider, Identifier.Value).ToList();

            if (PrimaryFoodGroup?.Identifier != null && foodItemGroups.SingleOrDefault(foodItemGroup => foodItemGroup.FoodGroupIdentifier.HasValue && foodItemGroup.FoodGroupIdentifier.Value == PrimaryFoodGroup.Identifier.Value) == null)
            {
                using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone())
                {
                    foodItemGroups.Add(subDataProvider.Add(FoodItemGroupProxy.Build(this, PrimaryFoodGroup, true)));
                }
            }

            IFoodGroup missingFoodGroup = FoodGroups.FirstOrDefault(foodGroup => foodGroup.Identifier.HasValue && foodItemGroups.Any(foodItemGroup => foodItemGroup.FoodGroupIdentifier.HasValue && foodItemGroup.FoodGroupIdentifier.Value == foodGroup.Identifier.Value) == false);

            while (missingFoodGroup != null)
            {
                using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone())
                {
                    foodItemGroups.Add(subDataProvider.Add(FoodItemGroupProxy.Build(this, missingFoodGroup, false)));
                }
                missingFoodGroup = FoodGroups.FirstOrDefault(foodGroup => foodGroup.Identifier.HasValue && foodItemGroups.Any(foodItemGroup => foodItemGroup.FoodGroupIdentifier.HasValue && foodItemGroup.FoodGroupIdentifier.Value == foodGroup.Identifier.Value) == false);
            }

            FoodItemGroupProxy noLongerExistingFoodItemGroup = foodItemGroups.FirstOrDefault(foodItemGroup => foodItemGroup.FoodGroupIdentifier.HasValue && FoodGroups.Any(foodGroup => foodGroup.Identifier.HasValue && foodGroup.Identifier.Value == foodItemGroup.FoodGroupIdentifier.Value) == false);

            while (noLongerExistingFoodItemGroup != null)
            {
                using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone())
                {
                    subDataProvider.Delete(noLongerExistingFoodItemGroup);
                    foodItemGroups.Remove(noLongerExistingFoodItemGroup);
                }
                noLongerExistingFoodItemGroup = foodItemGroups.FirstOrDefault(foodItemGroup => foodItemGroup.FoodGroupIdentifier.HasValue && FoodGroups.Any(foodGroup => foodGroup.Identifier.HasValue && foodGroup.Identifier.Value == foodItemGroup.FoodGroupIdentifier.Value) == false);
            }

            if (PrimaryFoodGroup == null || PrimaryFoodGroup.Identifier.HasValue == false)
            {
                _dataProvider = (IFoodWasteDataProvider)dataProvider;
                return;
            }

            FoodItemGroupProxy primaryFoodItemGroup = foodItemGroups.SingleOrDefault(foodItemGroup => foodItemGroup.FoodGroupIdentifier.HasValue && foodItemGroup.FoodGroupIdentifier.Value == PrimaryFoodGroup.Identifier.Value);

            if (primaryFoodItemGroup != null && primaryFoodItemGroup.IsPrimary == false)
            {
                primaryFoodItemGroup.IsPrimary = true;
                using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone())
                {
                    foodItemGroups.Remove(primaryFoodItemGroup);
                    foodItemGroups.Add(subDataProvider.Save(primaryFoodItemGroup));
                }
            }

            FoodItemGroupProxy nonPrimaryFoodItemGroup = foodItemGroups.Where(foodItemGroup => foodItemGroup.FoodGroupIdentifier.HasValue && foodItemGroup.FoodGroupIdentifier.Value != PrimaryFoodGroup.Identifier.Value).SingleOrDefault(foodItemGroup => foodItemGroup.IsPrimary);

            while (nonPrimaryFoodItemGroup != null)
            {
                nonPrimaryFoodItemGroup.IsPrimary = false;
                using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone())
                {
                    foodItemGroups.Remove(nonPrimaryFoodItemGroup);
                    foodItemGroups.Add(subDataProvider.Save(nonPrimaryFoodItemGroup));
                }
                nonPrimaryFoodItemGroup = foodItemGroups.Where(foodItemGroup => foodItemGroup.FoodGroupIdentifier.HasValue && foodItemGroup.FoodGroupIdentifier.Value != PrimaryFoodGroup.Identifier.Value).SingleOrDefault(foodItemGroup => foodItemGroup.IsPrimary);
            }

            _dataProvider = (IFoodWasteDataProvider)dataProvider;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Maps relations.
        /// </summary>
        /// <param name="dataProvider">Implementation of the data provider used to access data.</param>
        public virtual void MapRelations(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider)
        {
            ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider));

            if (Identifier.HasValue == false)
            {
                throw new IntranetRepositoryException(Resource.GetExceptionMessage(ExceptionMessage.IllegalValue, Identifier, "Identifier"));
            }

            IList <FoodItemGroupProxy> foodItemGroupProxyCollection = new List <FoodItemGroupProxy>(FoodItemGroupProxy.GetFoodItemGroups(dataProvider, Identifier.Value));

            PrimaryFoodGroup = foodItemGroupProxyCollection.Single(m => m.IsPrimary).FoodGroup;
            FoodGroups       = new List <IFoodGroup>(foodItemGroupProxyCollection.Select(m => m.FoodGroup));
            Translations     = new List <ITranslation>(TranslationProxy.GetDomainObjectTranslations(dataProvider, Identifier.Value));
            ForeignKeys      = new List <IForeignKey>(ForeignKeyProxy.GetDomainObjectForeignKeys(dataProvider, Identifier.Value));

            _dataProvider = (IFoodWasteDataProvider)dataProvider;
        }