/// <summary> /// Adds a relationship between the relatedEntity and the entity /// </summary> /// <typeparam name="TT">The type of the t.</typeparam> /// <param name="entityId">The entity identifier.</param> /// <param name="relatedEntity">The related entity.</param> /// <param name="purposeKey">The purpose key.</param> /// <param name="qualifierValue">The qualifier value.</param> public void AddRelatedToSourceEntity <TT>(int entityId, TT relatedEntity, string purposeKey, string qualifierValue) where TT : IEntity { var relatedEntityService = new Rock.Model.RelatedEntityService(this.Context as RockContext); var relatedEntityTypeId = EntityTypeCache.GetId <TT>(); var sourceEntityTypeId = EntityTypeCache.GetId <T>(); relatedEntityService.Add( new RelatedEntity { SourceEntityTypeId = sourceEntityTypeId.Value, SourceEntityId = entityId, TargetEntityTypeId = relatedEntityTypeId.Value, TargetEntityId = relatedEntity.Id, PurposeKey = purposeKey, QualifierValue = qualifierValue } ); }
/// <summary> /// Adds a relationship between the relatedEntity and the entity /// </summary> /// <typeparam name="TT">The type of the t.</typeparam> /// <param name="entityId">The entity identifier.</param> /// <param name="relatedEntity">The related entity.</param> /// <param name="purposeKey">The purpose key.</param> public void AddRelatedToSourceEntity <TT>(int entityId, TT relatedEntity, string purposeKey) where TT : IEntity { var relatedEntityTypeId = EntityTypeCache.GetId <TT>(); var sourceEntityTypeId = EntityTypeCache.GetId <T>(); var relatedEntityRecord = new RelatedEntity(); relatedEntityRecord.SourceEntityTypeId = sourceEntityTypeId.Value; relatedEntityRecord.SourceEntityId = entityId; relatedEntityRecord.TargetEntityTypeId = relatedEntityTypeId.Value; relatedEntityRecord.TargetEntityId = relatedEntity.Id; relatedEntityRecord.PurposeKey = purposeKey; var relatedEntityService = new Rock.Model.RelatedEntityService(this.Context as RockContext); relatedEntityService.Add(relatedEntityRecord); }
/// <summary> /// Sets the related target entities for the given source entity type, purpose key, and qualifier value. /// </summary> /// <typeparam name="TT">The type of the t.</typeparam> /// <param name="entityId">The entity identifier.</param> /// <param name="relatedEntities">The related entities.</param> /// <param name="purposeKey">The purpose key.</param> /// <param name="qualifierValue">The qualifier value.</param> public void SetRelatedToSourceEntity <TT>(int entityId, List <TT> relatedEntities, string purposeKey, string qualifierValue) where TT : IEntity { var relatedEntityTypeId = EntityTypeCache.GetId <TT>(); var sourceEntityTypeId = EntityTypeCache.GetId <T>(); var currentRelatedEntities = GetRelatedToSourceEntityQualifier <TT>(entityId, purposeKey, qualifierValue).ToList(); var relatedEntityService = new Rock.Model.RelatedEntityService(this.Context as RockContext); var relatedEntityIds = relatedEntities.Select(a => a.Id).ToList(); // delete related entities that are no longer in the list foreach (var currentRelatedEntity in currentRelatedEntities.Where(a => !relatedEntityIds.Contains(a.Id))) { // get related entity record(s) that need to be deleted since the relatedEntity is no longer in the list var relatedEntityToDelete = relatedEntityService .Queryable() .Where(a => a.SourceEntityTypeId == sourceEntityTypeId.Value && a.TargetEntityTypeId == relatedEntityTypeId.Value && a.SourceEntityId == entityId && a.TargetEntityId == currentRelatedEntity.Id && a.PurposeKey == purposeKey && a.QualifierValue == qualifierValue) .ToList(); relatedEntityService.DeleteRange(relatedEntityToDelete); } // add related entity record for related entities that that don't have a related entity record foreach (var relatedEntityId in relatedEntityIds.Where(a => !currentRelatedEntities.Any(r => r.Id == a))) { var relatedEntityRecord = new RelatedEntity { SourceEntityTypeId = sourceEntityTypeId.Value, SourceEntityId = entityId, TargetEntityTypeId = relatedEntityTypeId.Value, TargetEntityId = relatedEntityId, PurposeKey = purposeKey, QualifierValue = qualifierValue }; relatedEntityService.Add(relatedEntityRecord); } }