/// <summary> /// Deletes all the storages for a given household. /// </summary> /// <param name="dataProvider">Implementation of the data provider used to access data.</param> /// <param name="householdIdentifier">The identifier for the household on which to delete the storages.</param> /// <exception cref="ArgumentNullException">Thrown when <paramref name="dataProvider"/> is null.</exception> internal static void DeleteStorages(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider, Guid householdIdentifier) { ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider)); foreach (StorageProxy storageProxy in GetStorages(dataProvider, householdIdentifier)) { using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone()) { subDataProvider.Delete(storageProxy); } } }
/// <summary> /// Deletes foreign keys for a domain object in the food waste domain. /// </summary> /// <param name="dataProvider">Implementation of the data provider used to access data.</param> /// <param name="foreignKeyForIdentifier">Identifier for the given domain object on which to get the foreign keys.</param> internal static void DeleteDomainObjectForeignKeys(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider, Guid foreignKeyForIdentifier) { ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider)); foreach (IForeignKeyProxy foreignKeyProxy in GetDomainObjectForeignKeys(dataProvider, foreignKeyForIdentifier)) { using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone()) { subDataProvider.Delete(foreignKeyProxy); } } }
/// <summary> /// Delete all the payments made by a given stakeholder. /// </summary> /// <param name="dataProvider">Implementation of the data provider used to access data.</param> /// <param name="stakeholderIdentifier">Identifier for the stakeholder on which to delete payments.</param> internal static void DeletePayments(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider, Guid stakeholderIdentifier) { ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider)); foreach (PaymentProxy paymentProxy in GetPayments(dataProvider, stakeholderIdentifier)) { using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone()) { subDataProvider.Delete(paymentProxy); } } }
/// <summary> /// Deletes translations for a given translatable domain object in the food waste domain. /// </summary> /// <param name="dataProvider"></param> /// <param name="translationOfIdentifier">Identifier for the given domain object on which to delete the translations.</param> internal static void DeleteDomainObjectTranslations(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider, Guid translationOfIdentifier) { ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider)); foreach (ITranslationProxy translationProxy in GetDomainObjectTranslations(dataProvider, translationOfIdentifier)) { using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone()) { subDataProvider.Delete(translationProxy); } } }
/// <summary> /// Deletes relations between a given food group and it's food items in the food waste domain. /// </summary> /// <param name="dataProvider">Implementation of the data provider used to access data.</param> /// <param name="foodGroupProxy">Data proxy for the food group on which to delete the relations between the food group and it's food items.</param> internal static void DeleteFoodItemGroups(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider, IFoodGroupProxy foodGroupProxy) { ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider)) .NotNull(foodGroupProxy, nameof(foodGroupProxy)); foreach (IFoodItemGroupProxy foodItemGroupProxy in GetFoodItemGroups(dataProvider, foodGroupProxy)) { using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone()) { subDataProvider.Delete(foodItemGroupProxy); } } }
/// <summary> /// Handles an affected household. /// </summary> /// <param name="dataProvider">Implementation of the data provider used to access data.</param> /// <param name="affectedHousehold">Implementation of a data proxy to the affected household.</param> /// <exception cref="ArgumentNullException">Thrown when <paramref name="dataProvider"/> or <paramref name="affectedHousehold"/> is null.</exception> private static void HandleAffectedHousehold(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider, IHouseholdProxy affectedHousehold) { ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider)) .NotNull(affectedHousehold, nameof(affectedHousehold)); if (affectedHousehold.HouseholdMembers.Any()) { return; } using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone()) { subDataProvider.Delete(affectedHousehold); } }
/// <summary> /// Deletes relations between a given food item and it's food groups in the food waste domain. /// </summary> /// <param name="dataProvider">Implementation of the data provider used to access data.</param> /// <param name="foodItemProxy">Data proxy for the food item on which to delete the relations between the food item and it's food groups.</param> internal static void DeleteFoodItemGroups(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider, IFoodItemProxy foodItemProxy) { ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider)) .NotNull(foodItemProxy, nameof(foodItemProxy)); if (foodItemProxy.Identifier.HasValue == false) { throw new IntranetRepositoryException(Resource.GetExceptionMessage(ExceptionMessage.IllegalValue, foodItemProxy.Identifier, "Identifier")); } foreach (IFoodItemGroupProxy foodItemGroupProxy in GetFoodItemGroups(dataProvider, foodItemProxy.Identifier.Value)) { using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone()) { subDataProvider.Delete(foodItemGroupProxy); } } }
/// <summary> /// Deletes 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 delete.</param> protected virtual void Delete <TIdentifiable, TDataProxy>(TIdentifiable identifiable) where TIdentifiable : IIdentifiable where TDataProxy : class, TIdentifiable, IMySqlDataProxy { if (Equals(identifiable, null)) { throw new ArgumentNullException(nameof(identifiable)); } try { var dataProxy = _foodWasteObjectMapper.Map <TIdentifiable, TDataProxy>(identifiable); _foodWasteDataProvider.Delete(dataProxy); } catch (IntranetRepositoryException) { throw; } catch (Exception ex) { throw new IntranetRepositoryException(Resource.GetExceptionMessage(ExceptionMessage.RepositoryError, "Delete", ex.Message), ex); } }
/// <summary> /// Deletes the bindings which binds household members to households. /// </summary> /// <typeparam name="T">Type of the result which should be returned for the deleted binding.</typeparam> /// <param name="dataProvider">Implementation of the data provider used to access data.</param> /// <param name="memberOfHouseholdProxyCollectionGetter">The getter which gets the bindings to delete.</param> /// <param name="resultGetter">The getter which gets the result for each deleted binding.</param> /// <returns>Collection of the results for each deleted binding.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="dataProvider"/>, <paramref name="memberOfHouseholdProxyCollectionGetter"/> or <paramref name="resultGetter"/> is null.</exception> private static IEnumerable <T> DeleteMemberOfHouseholds <T>(IDataProviderBase <MySqlDataReader, MySqlCommand> dataProvider, Func <MemberOfHouseholdProxy[]> memberOfHouseholdProxyCollectionGetter, Func <MemberOfHouseholdProxy, T> resultGetter) where T : IMySqlDataProxy { ArgumentNullGuard.NotNull(dataProvider, nameof(dataProvider)) .NotNull(memberOfHouseholdProxyCollectionGetter, nameof(memberOfHouseholdProxyCollectionGetter)) .NotNull(resultGetter, nameof(resultGetter)); MemberOfHouseholdProxy[] memberOfHouseholdProxyCollection = memberOfHouseholdProxyCollectionGetter(); foreach (MemberOfHouseholdProxy memberOfHouseholdProxy in memberOfHouseholdProxyCollection) { using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone()) { subDataProvider.Delete(memberOfHouseholdProxy); } } return(memberOfHouseholdProxyCollection .Where(memberOfHouseholdProxy => memberOfHouseholdProxy != null && resultGetter(memberOfHouseholdProxy) != null) .Select(resultGetter) .ToList()); }
/// <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")); } foreach (IFoodGroupProxy foodGroupProxy in GetFoodGroupChildren(dataProvider, Identifier.Value)) { using (IFoodWasteDataProvider subDataProvider = (IFoodWasteDataProvider)dataProvider.Clone()) { subDataProvider.Delete(foodGroupProxy); } } FoodItemGroupProxy.DeleteFoodItemGroups(dataProvider, this); TranslationProxy.DeleteDomainObjectTranslations(dataProvider, Identifier.Value); ForeignKeyProxy.DeleteDomainObjectForeignKeys(dataProvider, Identifier.Value); _dataProvider = (IFoodWasteDataProvider)dataProvider; }
/// <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; }
/// <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; }