Esempio n. 1
0
        /// <summary>
        /// Ensures the tracked attributes exists.
        /// </summary>
        /// <param name="trackedAttributeKey">The tracked attribute key.</param>
        /// <param name="trackedAttributeName">Name of the tracked attribute.</param>
        /// <param name="fieldTypeGuid">The field type unique identifier.</param>
        private void EnsureTrackedAttributeExists(string trackedAttributeKey, string trackedAttributeName, Guid fieldTypeGuid)
        {
            var entityTypeIdPerson = EntityTypeCache.GetId <Rock.Model.Person>();
            var fieldTypeId        = FieldTypeCache.GetId(fieldTypeGuid);

            if (!entityTypeIdPerson.HasValue || !fieldTypeId.HasValue)
            {
                // shouldn't happen, but just in case
                return;
            }

            var rockContext = new RockContext();

            var attributeService = new AttributeService(rockContext);

            var trackedAttribute = attributeService.GetByEntityTypeId(entityTypeIdPerson.Value).Where(a => a.Key == trackedAttributeKey).FirstOrDefault();

            if (trackedAttribute == null)
            {
                trackedAttribute = new Rock.Model.Attribute();
                trackedAttribute.EntityTypeId = entityTypeIdPerson;
                trackedAttribute.Key          = trackedAttributeKey;
                trackedAttribute.Name         = trackedAttributeName;
                trackedAttribute.FieldTypeId  = fieldTypeId.Value;
                attributeService.Add(trackedAttribute);
                rockContext.SaveChanges();
            }
            else
            {
                if (trackedAttribute.FieldTypeId != fieldTypeId.Value)
                {
                    trackedAttribute.FieldTypeId = fieldTypeId.Value;
                    rockContext.SaveChanges();
                }
            }
        }
Esempio n. 2
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();
        }