private EventItem ResolveEventSettingOrThrow(RockContext rockContext, string eventSettingValue)
        {
            var eventItemService = new EventItemService(rockContext);

            EventItem eventItem = null;

            // Verify that an Event reference has been provided.
            if (string.IsNullOrWhiteSpace(eventSettingValue))
            {
                throw new Exception($"An Event reference must be specified.");
            }

            // Get by ID.
            var eventId = eventSettingValue.AsIntegerOrNull();

            if (eventId != null)
            {
                eventItem = eventItemService.Get(eventId.Value);
            }

            // Get by Guid.
            if (eventItem == null)
            {
                var eventGuid = eventSettingValue.AsGuidOrNull();

                if (eventGuid != null)
                {
                    eventItem = eventItemService.Get(eventGuid.Value);
                }
            }

            // Get By Name.
            if (eventItem == null)
            {
                var eventName = eventSettingValue.ToString();

                if (!string.IsNullOrWhiteSpace(eventName))
                {
                    eventItem = eventItemService.Queryable()
                                .Where(x => x.Name != null && x.Name.Equals(eventName, StringComparison.OrdinalIgnoreCase))
                                .FirstOrDefault();
                }
            }

            if (eventItem == null)
            {
                throw new Exception($"Cannot find an Event matching the reference \"{ eventSettingValue }\".");
            }

            return(eventItem);
        }
        /// <summary>
        /// Handles the Click event of the DeleteEventCalendarItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs" /> instance containing the event data.</param>
        protected void DeleteEventCalendarItem_Click(object sender, Rock.Web.UI.Controls.RowEventArgs e)
        {
            using (RockContext rockContext = new RockContext())
            {
                EventItemService eventItemService = new EventItemService(rockContext);
                EventItem        eventItem        = eventItemService.Get(e.RowKeyId);
                if (eventItem != null)
                {
                    if (_canEdit)
                    {
                        string errorMessage;
                        if (!eventItemService.CanDelete(eventItem, out errorMessage))
                        {
                            mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                            return;
                        }

                        eventItemService.Delete(eventItem);
                        rockContext.SaveChanges();
                    }
                    else
                    {
                        mdGridWarning.Show("You are not authorized to delete this calendar item", ModalAlertType.Warning);
                    }
                }
            }

            BindEventCalendarItemsGrid();
        }
Example #3
0
        /// <summary>
        /// Handles the Click event of the btnDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                EventItemService eventItemService = new EventItemService(rockContext);
                EventItem        eventItem        = eventItemService.Get(int.Parse(hfEventItemId.Value));

                if (eventItem != null)
                {
                    string errorMessage;
                    if (!eventItemService.CanDelete(eventItem, out errorMessage))
                    {
                        mdDeleteWarning.Show(errorMessage, ModalAlertType.Information);
                        return;
                    }

                    eventItemService.Delete(eventItem);

                    rockContext.SaveChanges();
                }
            }

            var qryParams = new Dictionary <string, string>();

            if (_calendarId.HasValue)
            {
                qryParams.Add("EventCalendarId", _calendarId.Value.ToString());
            }
            NavigateToParentPage(qryParams);
        }
Example #4
0
        private EventItemOccurrence EventItemOccurrenceAddOrUpdateInstance(RockContext rockContext, Guid scheduleGuid, bool deleteExistingInstance)
        {
            // Get existing schedules.
            var scheduleService = new ScheduleService(rockContext);
            var schedule        = scheduleService.Get(scheduleGuid);

            // Get Event "Rock Solid Finances".
            var eventItemService           = new EventItemService(rockContext);
            var eventItemOccurrenceService = new EventItemOccurrenceService(rockContext);

            var financeEvent = eventItemService.Get(EventFinancesClassGuid.AsGuid());

            // Add a new occurrence of this event.
            var financeEvent1 = eventItemOccurrenceService.Get(FinancesClassOccurrenceTestGuid.AsGuid());

            if (financeEvent1 != null && deleteExistingInstance)
            {
                eventItemOccurrenceService.Delete(financeEvent1);
                rockContext.SaveChanges();
            }
            financeEvent1 = new EventItemOccurrence();

            var mainCampusId = CampusCache.GetId(MainCampusGuidString.AsGuid());

            financeEvent1.Location   = "Meeting Room 1";
            financeEvent1.ForeignKey = TestDataForeignKey;
            financeEvent1.ScheduleId = schedule.Id;
            financeEvent1.Guid       = FinancesClassOccurrenceTestGuid.AsGuid();
            financeEvent1.CampusId   = mainCampusId;

            financeEvent.EventItemOccurrences.Add(financeEvent1);

            return(financeEvent1);
        }
Example #5
0
        public void EventItemOccurrence_UpdatedWithInactiveEvent_HasNullNextDate()
        {
            var rockContext = new RockContext();

            // Get Event "Rock Solid Finances".
            var eventItemService = new EventItemService(rockContext);
            var financeEvent     = eventItemService.Get(EventFinancesClassGuid.AsGuid());

            // Get existing schedules.
            var scheduleService = new ScheduleService(rockContext);
            var scheduleSat1630 = scheduleService.Get(ScheduleSat1630Guid.AsGuid());

            // Set the event to inactive.
            financeEvent.IsActive = false;
            rockContext.SaveChanges();

            // Add an event occurrence for the inactive event.
            var financeEvent1 = EventItemOccurrenceAddOrUpdateInstance(rockContext, ScheduleSat1630Guid.AsGuid(), deleteExistingInstance: true);

            rockContext.SaveChanges();

            // Verify that the NextDateTime is correctly set.
            Assert.IsNull(financeEvent1.NextStartDateTime);

            // Set the event to active.
            financeEvent.IsActive = true;
            rockContext.SaveChanges();
        }
Example #6
0
        public void InheritedAttributes_ParentEntityAttributeValues_IncludesChildEntityAttributeValues()
        {
            var rockContext      = new RockContext();
            var eventItemService = new EventItemService(rockContext);

            // Verify the values of the test Attribute for Event A and Event B.
            // These Attributes are defined per-Calendar and so are directly linked to the EventItemCalendar entity,
            // but they should also appear as inherited attributes of the EventItem.
            var eventA = eventItemService.Get(EventAGuid.AsGuid());

            eventA.LoadAttributes();
            Assert.IsTrue(eventA.AttributeValues[InternalCalendarAttribute1Key].Value == "Event A Internal");
            Assert.IsTrue(eventA.AttributeValues[PublicCalendarAttribute1Key].Value == "Event A Public");

            var eventB = eventItemService.Get(EventBGuid.AsGuid());

            eventB.LoadAttributes();
            Assert.IsTrue(eventB.AttributeValues[PublicCalendarAttribute1Key].Value == "Event B Public");
            Assert.IsTrue(eventB.AttributeValues[InternalCalendarAttribute1Key].Value == null, $"Unexpected Attribute Value: {InternalCalendarAttribute1Key}.");
        }
 /// <summary>
 /// Handles the Edit event of the gEventCalendarItems control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param>
 protected void gEventCalendarItems_Edit(object sender, RowEventArgs e)
 {
     using (RockContext rockContext = new RockContext())
     {
         EventItemService eventItemService = new EventItemService(rockContext);
         EventItem        eventItem        = eventItemService.Get(e.RowKeyId);
         if (eventItem != null)
         {
             var qryParams = new Dictionary <string, string>();
             qryParams.Add("EventCalendarId", _eventCalendar.Id.ToString());
             qryParams.Add("EventItemId", eventItem.Id.ToString());
             NavigateToLinkedPage("DetailPage", qryParams);
         }
     }
 }
Example #8
0
        /// <summary>
        /// Handles the Click event of the btnDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                EventItemService eventItemService = new EventItemService(rockContext);
                EventItem        eventItem        = eventItemService.Get(int.Parse(hfEventItemId.Value));

                if (eventItem != null)
                {
                    string errorMessage;
                    if (!eventItemService.CanDelete(eventItem, out errorMessage))
                    {
                        mdDeleteWarning.Show(errorMessage, ModalAlertType.Information);
                        return;
                    }

                    eventItemService.Delete(eventItem);

                    rockContext.SaveChanges();
                }
            }

            NavigateToParentPage();
        }
Example #9
0
        public void InheritedAttributes_SetAttributeOnParentEntity_SetsValueForChildEntity()
        {
            var rockContext          = new RockContext();
            var eventItemService     = new EventItemService(rockContext);
            var eventCalendarService = new EventCalendarItemService(rockContext);

            // Setting the value of an inherited Attribute should persist the value for the child entity.
            var eventA = eventItemService.Get(EventAGuid.AsGuid());

            eventA.LoadAttributes(rockContext);
            eventA.SetAttributeValue(InternalCalendarAttribute1Key, "xyzzy");

            // TODO: SaveAttributeValues does not correctly save inherited values.
            // It wrongly attributes them to the entity on which they are set rather than the inherited entity.
            eventA.SaveAttributeValues(rockContext);

            var calendarItemInternal = eventCalendarService.Get(EventACalendarInternalGuid.AsGuid());
            var calendarItemPublic   = eventCalendarService.Get(EventACalendarPublicGuid.AsGuid());

            calendarItemInternal.LoadAttributes();
            Assert.IsTrue(calendarItemInternal.AttributeValues[InternalCalendarAttribute1Key].Value == "xyzzy");
            calendarItemPublic.LoadAttributes();
            Assert.IsTrue(calendarItemPublic.AttributeValues[InternalCalendarAttribute1Key].Value == "xyzzy");
        }
        public void RockCleanup_Execute_ShouldUpdateEventItemOccurrences()
        {
            var referenceDate = new DateTime(2020, 1, 1);

            // Get the sample data schedule for Saturday 4:30pm.
            var rockContext = new RockContext();

            var scheduleService = new ScheduleService(rockContext);
            var schedule        = scheduleService.Get(TestGuids.Schedules.ScheduleSat1630Guid.AsGuid());

            // Create a new inactive schedule.
            var scheduleInactive = scheduleService.Get(testScheduleGuid);

            if (scheduleInactive == null)
            {
                scheduleInactive = new Schedule();
                scheduleService.Add(scheduleInactive);
            }
            scheduleInactive.Name     = "Test Schedule";
            scheduleInactive.Guid     = testScheduleGuid;
            scheduleInactive.IsActive = false;

            rockContext.SaveChanges();

            // Create the Test Events.
            var eventItemService = new EventItemService(rockContext);

            // Test Event 1 (active)
            var testEvent1 = eventItemService.Get(testEvent1Guid);

            if (testEvent1 != null)
            {
                eventItemService.Delete(testEvent1);
                rockContext.SaveChanges();
            }

            testEvent1      = new EventItem();
            testEvent1.Guid = testEvent1Guid;
            testEvent1.Name = "Test Event 1";
            eventItemService.Add(testEvent1);

            // Add an occurrence with a future schedule and no NextDateTime value.
            // When the cleanup task executes, this should be updated to the next occurrence after the reference date.
            var testOccurrence11 = new EventItemOccurrence();

            testOccurrence11.ScheduleId        = schedule.Id;
            testOccurrence11.Guid              = testEventOccurrence11Guid;
            testOccurrence11.NextStartDateTime = null;
            testEvent1.EventItemOccurrences.Add(testOccurrence11);

            // Add an occurrence with a NextDateTime that is prior to the reference date.
            // When the cleanup task executes, this should be updated to the next occurrence after the reference date.
            var testOccurrence12 = new EventItemOccurrence();

            testOccurrence12.ScheduleId        = schedule.Id;
            testOccurrence12.Guid              = testEventOccurrence12Guid;
            testOccurrence12.NextStartDateTime = referenceDate.AddDays(-1);
            testEvent1.EventItemOccurrences.Add(testOccurrence12);

            // Add an occurrence with a NextDateTime and an inactive Schedule.
            // When the cleanup task executes, the NextDateTime should be set to null.
            var testOccurrence13 = new EventItemOccurrence();

            testOccurrence13.ScheduleId        = scheduleInactive.Id;
            testOccurrence13.Guid              = testEventOccurrence13Guid;
            testOccurrence13.NextStartDateTime = referenceDate.AddDays(7);
            testEvent1.EventItemOccurrences.Add(testOccurrence13);

            // Test Event 2 (inactive)
            var testEvent2 = eventItemService.Get(testEvent2Guid);

            if (testEvent2 != null)
            {
                eventItemService.Delete(testEvent2);
                rockContext.SaveChanges();
            }

            testEvent2          = new EventItem();
            testEvent2.Guid     = testEvent2Guid;
            testEvent2.Name     = "Test Event 2";
            testEvent2.IsActive = false;
            eventItemService.Add(testEvent2);

            // Add an occurrence with a future schedule and a NextDateTime value.
            // When the cleanup task executes, the NextDateTime should be set to null.
            var testOccurrence21 = new EventItemOccurrence();

            testOccurrence21.ScheduleId        = schedule.Id;
            testOccurrence21.Guid              = testEventOccurrence21Guid;
            testOccurrence21.NextStartDateTime = referenceDate;
            testEvent2.EventItemOccurrences.Add(testOccurrence21);

            // Save changes without triggering the pre-save, to avoid updating the NextEventDate field.
            rockContext.SaveChanges(new SaveChangesArgs {
                DisablePrePostProcessing = true
            });

            // Run the cleanup task to verify the results for the reference date.
            RunRockCleanupTaskUpdateEventNextOccurrenceDatesAndVerify(referenceDate);

            // Re-run the task to verify that the results are adjusted for the current date.
            RunRockCleanupTaskUpdateEventNextOccurrenceDatesAndVerify(RockDateTime.Now);
        }
Example #11
0
        /// <summary>
        /// Handles the Click event of the btnDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnDelete_Click( object sender, EventArgs e )
        {
            using ( var rockContext = new RockContext() )
            {
                EventItemService eventItemService = new EventItemService( rockContext );
                EventItem eventItem = eventItemService.Get( int.Parse( hfEventItemId.Value ) );

                if ( eventItem != null )
                {
                    string errorMessage;
                    if ( !eventItemService.CanDelete( eventItem, out errorMessage ) )
                    {
                        mdDeleteWarning.Show( errorMessage, ModalAlertType.Information );
                        return;
                    }

                    eventItemService.Delete( eventItem );

                    rockContext.SaveChanges();
                }
            }

            var qryParams = new Dictionary<string, string>();
            if ( _calendarId.HasValue )
            {
                qryParams.Add( "EventCalendarId", _calendarId.Value.ToString() );
            }
            NavigateToParentPage( qryParams );
        }
 /// <summary>
 /// Handles the Edit event of the gEventCalendarItems control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param>
 protected void gEventCalendarItems_Edit( object sender, RowEventArgs e )
 {
     using ( RockContext rockContext = new RockContext() )
     {
         EventItemService eventItemService = new EventItemService( rockContext );
         EventItem eventItem = eventItemService.Get( e.RowKeyId );
         if ( eventItem != null )
         {
             var qryParams = new Dictionary<string, string>();
             qryParams.Add( "EventCalendarId", _eventCalendar.Id.ToString() );
             qryParams.Add( "EventItemId", eventItem.Id.ToString() );
             NavigateToLinkedPage( "DetailPage", qryParams );
         }
     }
 }
        /// <summary>
        /// Handles the Click event of the DeleteEventCalendarItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs" /> instance containing the event data.</param>
        protected void DeleteEventCalendarItem_Click( object sender, Rock.Web.UI.Controls.RowEventArgs e )
        {
            using ( RockContext rockContext = new RockContext() )
            {
                EventItemService eventItemService = new EventItemService( rockContext );
                EventItem eventItem = eventItemService.Get( e.RowKeyId );
                if ( eventItem != null )
                {
                    if ( _canEdit )
                    {
                        string errorMessage;
                        if ( !eventItemService.CanDelete( eventItem, out errorMessage ) )
                        {
                            mdGridWarning.Show( errorMessage, ModalAlertType.Information );
                            return;
                        }

                        eventItemService.Delete( eventItem );
                        rockContext.SaveChanges();
                    }
                    else
                    {
                        mdGridWarning.Show( "You are not authorized to delete this calendar item", ModalAlertType.Warning );
                    }
                }
            }

            BindEventCalendarItemsGrid();
        }
Example #14
0
        /// <summary>
        /// Handles the Click event of the btnDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnDelete_Click( object sender, EventArgs e )
        {
            using ( var rockContext = new RockContext() )
            {
                EventItemService eventItemService = new EventItemService( rockContext );
                EventItem eventItem = eventItemService.Get( int.Parse( hfEventItemId.Value ) );

                if ( eventItem != null )
                {
                    string errorMessage;
                    if ( !eventItemService.CanDelete( eventItem, out errorMessage ) )
                    {
                        mdDeleteWarning.Show( errorMessage, ModalAlertType.Information );
                        return;
                    }

                    eventItemService.Delete( eventItem );

                    rockContext.SaveChanges();
                }
            }

            NavigateToParentPage();
        }
        /// <summary>
        /// Modifies the Rock Solid Finances Class to add multiple schedules and campuses.
        /// </summary>
        private static void InitializeEventRockSolidFinancesClassTestData()
        {
            var rockContext = new RockContext();

            // Add a new campus
            var campusService = new CampusService(rockContext);

            var campus2 = campusService.Get(SecondaryCampusGuidString.AsGuid());

            if (campus2 == null)
            {
                campus2 = new Campus();

                campusService.Add(campus2);
            }

            campus2.Name = "Stepping Stone";
            campus2.Guid = SecondaryCampusGuidString.AsGuid();

            rockContext.SaveChanges();

            // Get existing schedules.
            var scheduleService = new ScheduleService(rockContext);

            var scheduleSat1630Id = scheduleService.GetId(ScheduleSat1630Guid.AsGuid());
            var scheduleSat1800Id = scheduleService.GetId(ScheduleSun1200Guid.AsGuid());

            // Get Event "Rock Solid Finances".
            var eventItemService           = new EventItemService(rockContext);
            var eventItemOccurrenceService = new EventItemOccurrenceService(rockContext);

            var financeEvent = eventItemService.Get(EventFinancesClassGuid.AsGuid());

            // Add an occurrence of this event for each Schedule.
            var financeEvent1 = eventItemOccurrenceService.Get(FinancesClassOccurrenceSat1630Guid.AsGuid());

            if (financeEvent1 == null)
            {
                financeEvent1 = new EventItemOccurrence();
            }

            var mainCampusId   = CampusCache.GetId(MainCampusGuidString.AsGuid());
            var secondCampusId = CampusCache.GetId(SecondaryCampusGuidString.AsGuid());

            financeEvent1.Location   = "Meeting Room 1";
            financeEvent1.ForeignKey = TestDataForeignKey;
            financeEvent1.ScheduleId = scheduleSat1630Id;
            financeEvent1.Guid       = FinancesClassOccurrenceSat1630Guid.AsGuid();
            financeEvent1.CampusId   = mainCampusId;

            financeEvent.EventItemOccurrences.Add(financeEvent1);

            var financeEvent2 = eventItemOccurrenceService.Get(FinancesClassOccurrenceSun1200Guid.AsGuid());

            if (financeEvent2 == null)
            {
                financeEvent2 = new EventItemOccurrence();
            }

            financeEvent2.Location   = "Meeting Room 2";
            financeEvent2.ForeignKey = TestDataForeignKey;
            financeEvent2.ScheduleId = scheduleSat1800Id;
            financeEvent2.Guid       = FinancesClassOccurrenceSun1200Guid.AsGuid();
            financeEvent2.CampusId   = secondCampusId;

            financeEvent.EventItemOccurrences.Add(financeEvent2);

            rockContext.SaveChanges();
        }
Example #16
0
        /// <summary>
        /// Adds test data for Events that can be used to test inherited attributes.
        /// </summary>
        private static void InitializeInheritedAttributesTestData()
        {
            var rockContext = new RockContext();

            var EventCalendarPublicId   = EventCalendarCache.All().First(x => x.Name == "Public").Id;
            var EventCalendarInternalId = EventCalendarCache.All().First(x => x.Name == "Internal").Id;

            // Add Attributes for Calendars.
            // EventItem Attributes are defined per Calendar, so they are directly associated with the EventCalendarItem entity.
            // The Attributes collection for the EventItem shows the collection of attributes inherited from the EventCalendarItems associated with the event.
            var attributeService = new AttributeService(rockContext);
            var textFieldTypeId  = FieldTypeCache.GetId(SystemGuid.FieldType.TEXT.AsGuid()).GetValueOrDefault();

            // Add Attribute A for Internal Calendar.
            var attributeAInternal = attributeService.Get(InternalCalendarAttribute1Guid.AsGuid());

            if (attributeAInternal != null)
            {
                attributeService.Delete(attributeAInternal);
                rockContext.SaveChanges();
            }
            attributeAInternal = new Rock.Model.Attribute();
            attributeAInternal.EntityTypeId = EntityTypeCache.GetId(typeof(EventCalendarItem));
            attributeAInternal.EntityTypeQualifierColumn = "EventCalendarId";
            attributeAInternal.EntityTypeQualifierValue  = EventCalendarInternalId.ToString();
            attributeAInternal.Name        = InternalCalendarAttribute1Key;
            attributeAInternal.Key         = InternalCalendarAttribute1Key;
            attributeAInternal.Guid        = InternalCalendarAttribute1Guid.AsGuid();
            attributeAInternal.FieldTypeId = textFieldTypeId;

            attributeService.Add(attributeAInternal);
            rockContext.SaveChanges();

            // Add Attribute A for Public Calendar.
            var attributeAPublic = attributeService.Get(PublicCalendarAttribute1Guid.AsGuid());

            if (attributeAPublic != null)
            {
                attributeService.Delete(attributeAPublic);
                rockContext.SaveChanges();
            }
            attributeAPublic = new Rock.Model.Attribute();
            attributeAPublic.EntityTypeId = EntityTypeCache.GetId(typeof(EventCalendarItem));
            attributeAPublic.EntityTypeQualifierColumn = "EventCalendarId";
            attributeAPublic.EntityTypeQualifierValue  = EventCalendarPublicId.ToString();
            attributeAPublic.Name        = PublicCalendarAttribute1Key;
            attributeAPublic.Key         = PublicCalendarAttribute1Key;
            attributeAPublic.Guid        = PublicCalendarAttribute1Guid.AsGuid();
            attributeAPublic.FieldTypeId = textFieldTypeId;

            attributeService.Add(attributeAPublic);
            rockContext.SaveChanges();

            // Create a new Event: "Event A".
            // This event exists in the Internal and Public calendars.
            var eventItemService = new EventItemService(rockContext);

            var eventA = eventItemService.Get(EventAGuid.AsGuid());

            if (eventA != null)
            {
                eventItemService.Delete(eventA);
                rockContext.SaveChanges();
            }
            eventA      = new EventItem();
            eventA.Name = "Event A";
            eventA.Guid = EventAGuid.AsGuid();
            var eventACalendarInternal = new EventCalendarItem {
                EventCalendarId = EventCalendarInternalId, Guid = EventACalendarInternalGuid.AsGuid()
            };
            var eventACalendarPublic = new EventCalendarItem {
                EventCalendarId = EventCalendarPublicId, Guid = EventACalendarPublicGuid.AsGuid()
            };

            eventA.EventCalendarItems.Add(eventACalendarInternal);
            eventA.EventCalendarItems.Add(eventACalendarPublic);

            eventItemService.Add(eventA);

            rockContext.SaveChanges();

            // Create a new Event: "Event B".
            // This event exists in the Internal calendar only.
            // This event is created manually, to set the ID of Event B to match an Event Calendar Item associated with Event A.
            // The purpose is to create an EventItem and an EventCalendarItem that have an identical ID,
            // so we can verify that Attribute values for parent and child entities are correctly differentiated
            // by Entity Type when they are collated for inherited attributes.
            var eventB = eventItemService.Get(EventBGuid.AsGuid());

            if (eventB != null)
            {
                eventItemService.Delete(eventB);
                rockContext.SaveChanges();
            }

            var matchedID = eventACalendarInternal.Id;

            var sql = @"
SET IDENTITY_INSERT [EventItem] ON
;
INSERT INTO [EventItem] (Id, Name, Guid, IsActive)
VALUES (@p0, @p1, @p2, 1)
;
SET IDENTITY_INSERT [EventItem] OFF
;
";

            rockContext.Database.ExecuteSqlCommand(sql, matchedID, "Event B", EventBGuid.AsGuid());

            eventB = eventItemService.Get(EventBGuid.AsGuid());
            var eventBCalendarInternal = new EventCalendarItem {
                EventCalendarId = EventCalendarInternalId, Guid = EventBCalendarInternalGuid.AsGuid()
            };
            var eventBCalendarPublic = new EventCalendarItem {
                EventCalendarId = EventCalendarPublicId, Guid = EventBCalendarPublicGuid.AsGuid()
            };

            eventB.EventCalendarItems.Add(eventBCalendarInternal);
            eventB.EventCalendarItems.Add(eventBCalendarPublic);

            rockContext.SaveChanges();

            // Set Attribute Values.
            rockContext = new RockContext();
            AttributeCache.Clear();

            // Set Attribute Values for Event B.
            eventBCalendarPublic.LoadAttributes(rockContext);
            eventBCalendarPublic.SetAttributeValue(PublicCalendarAttribute1Key, "Event B Public");
            eventBCalendarPublic.SaveAttributeValues(rockContext);

            rockContext.SaveChanges();

            // Set Attribute Values for Event A
            eventACalendarInternal.LoadAttributes(rockContext);
            eventACalendarInternal.SetAttributeValue(InternalCalendarAttribute1Key, "Event A Internal");
            eventACalendarInternal.SaveAttributeValues(rockContext);

            eventACalendarPublic.LoadAttributes(rockContext);
            eventACalendarPublic.SetAttributeValue(PublicCalendarAttribute1Key, "Event A Public");
            eventACalendarPublic.SaveAttributeValues(rockContext);

            rockContext.SaveChanges();
        }