/// <summary>
 /// Inserts an identifiable domain object in the repository.
 /// </summary>
 /// <typeparam name="TIdentifiable">Type of the identifiable domain object.</typeparam>
 /// <typeparam name="TDataProxy">Type of the data proxy for the identifiable domain object.</typeparam>
 /// <param name="identifiable">Identifiable domain object to insert.</param>
 /// <returns>The inserted identifiable domain object.</returns>
 protected virtual TIdentifiable Insert <TIdentifiable, TDataProxy>(TIdentifiable identifiable) where TIdentifiable : IIdentifiable where TDataProxy : class, TIdentifiable, IMySqlDataProxy
 {
     if (Equals(identifiable, null))
     {
         throw new ArgumentNullException(nameof(identifiable));
     }
     try
     {
         if (identifiable.Identifier.HasValue == false)
         {
             identifiable.Identifier = Guid.NewGuid();
         }
         var dataProxy = _foodWasteObjectMapper.Map <TIdentifiable, TDataProxy>(identifiable);
         return(_foodWasteDataProvider.Add(dataProxy));
     }
     catch (IntranetRepositoryException)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw new IntranetRepositoryException(Resource.GetExceptionMessage(ExceptionMessage.RepositoryError, "Insert", ex.Message), ex);
     }
 }
Example #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"));
            }

            IEnumerable <IHouseholdMember> householdMemberCollection = base.HouseholdMembers.ToArray(); // Using base.HouseholdMembers will not force the proxy to reload the household member collection.
            IHouseholdMember householdMemberWithoutIdentifier        = householdMemberCollection.FirstOrDefault(householdMember => householdMember.Identifier.HasValue == false);

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

            IList <MemberOfHouseholdProxy> existingMemberOfHouseholdCollection = new List <MemberOfHouseholdProxy>(MemberOfHouseholdProxy.GetMemberOfHouseholds(dataProvider, this));

            foreach (IHouseholdMember householdMember in householdMemberCollection.Where(m => m.Identifier.HasValue))
            {
                if (existingMemberOfHouseholdCollection.Any(existingMemberOfHousehold => existingMemberOfHousehold.HouseholdMemberIdentifier == householdMember.Identifier))
                {
                    continue;
                }

                using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone())
                {
                    MemberOfHouseholdProxy memberOfHouseholdProxy = new MemberOfHouseholdProxy(householdMember, this)
                    {
                        Identifier = Guid.NewGuid()
                    };
                    existingMemberOfHouseholdCollection.Add(subDataProvider.Add(memberOfHouseholdProxy));
                }
            }

            while (_removedHouseholdMemberCollection.Count > 0)
            {
                IHouseholdMember householdMemberToRemove = _removedHouseholdMemberCollection.First();
                if (householdMemberToRemove.Identifier.HasValue == false)
                {
                    _removedHouseholdMemberCollection.Remove(householdMemberToRemove);
                    continue;
                }

                MemberOfHouseholdProxy memberOfHouseholdProxyToRemove = existingMemberOfHouseholdCollection.SingleOrDefault(existingMemberOfHousehold => existingMemberOfHousehold.HouseholdMemberIdentifier == householdMemberToRemove.Identifier);
                if (memberOfHouseholdProxyToRemove == null)
                {
                    _removedHouseholdMemberCollection.Remove(householdMemberToRemove);
                    continue;
                }

                using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone())
                {
                    subDataProvider.Delete(memberOfHouseholdProxyToRemove);
                }
                HandleAffectedHouseholdMember(dataProvider, memberOfHouseholdProxyToRemove.HouseholdMember as IHouseholdMemberProxy);
                _removedHouseholdMemberCollection.Remove(householdMemberToRemove);
            }

            _dataProvider = (IFoodWasteDataProvider)dataProvider;
        }
Example #3
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;
        }