Exemplo n.º 1
0
        /// <summary>
        /// Gets the properties and attributes for the entity
        /// </summary>
        /// <param name="groupTypeId">The group type identifier.</param>
        /// <returns></returns>
        private List <EntityField> GetGroupMemberAttributes(int?groupTypeId)
        {
            List <EntityField> entityAttributeFields = new List <EntityField>();

            var fakeGroupMember = new Rock.Model.GroupMember();

            fakeGroupMember.Group = new Rock.Model.Group();
            if (groupTypeId.HasValue)
            {
                fakeGroupMember.Group.GroupTypeId = groupTypeId.Value;
            }
            else
            {
                //// if no GroupTypeId was specified, just set the GroupTypeId to 0
                //// NOTE: There could be GroupMember Attributes that are not specific to a GroupType
                fakeGroupMember.Group.GroupTypeId = 0;
            }

            Rock.Attribute.Helper.LoadAttributes(fakeGroupMember);
            var attributeList = fakeGroupMember.Attributes.Select(a => a.Value).ToList();

            EntityHelper.AddEntityFieldsForAttributeList(entityAttributeFields, attributeList);

            int index        = 0;
            var sortedFields = new List <EntityField>();

            foreach (var entityProperty in entityAttributeFields.OrderBy(p => p.TitleWithoutQualifier).ThenBy(p => p.Name))
            {
                entityProperty.Index = index;
                index++;
                sortedFields.Add(entityProperty);
            }

            return(sortedFields);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the inverse relationship.
        /// Returns the <see cref="Rock.Model.GroupMember" /> who has an inverse relationship to the provided <see cref="Rock.Model.GroupMember" />.
        /// </summary>
        /// <param name="groupMember">A <see cref="Rock.Model.GroupMember" /> representing the person to find the inverse relationship for.</param>
        /// <param name="createGroup">A <see cref="System.Boolean"/> flag indicating if a new <see cref="Rock.Model.Group"/> can be created
        /// for the person with the inverse relationship. </param>
        /// <param name="personAlias">The alias of the <see cref="Rock.Model.Person"/> who has the inverse relationship.</param>
        /// <returns>
        /// A <see cref="Rock.Model.GroupMember"/> representing the <see cref="Rock.Model.Person"/> with the inverse relationship.
        /// </returns>
        /// <remarks>
        /// In Rock, examples of inverse relationships include: Parent/Child, Can Check In/Check in By, Sibling/Sibling, Grandparent/Grandchild, etc.
        /// </remarks>
        public GroupMember GetInverseRelationship(GroupMember groupMember, bool createGroup, PersonAlias personAlias)
        {
            var groupRole = groupMember.GroupRole;

            if (groupRole == null)
            {
                groupRole = Queryable()
                            .Where(m => m.Id == groupMember.Id)
                            .Select(m => m.GroupRole)
                            .FirstOrDefault();
            }

            if (groupRole != null)
            {
                if (groupRole.Attributes == null)
                {
                    groupRole.LoadAttributes();
                }

                if (groupRole.Attributes.ContainsKey("InverseRelationship"))
                {
                    Guid ownerRoleGuid = new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_OWNER);

                    var memberInfo = Queryable()
                                     .Where(m =>
                                            m.GroupId == groupMember.GroupId &&
                                            m.GroupRole.Guid.Equals(ownerRoleGuid))
                                     .Select(m => new
                    {
                        PersonId = m.PersonId,
                        RoleId   = m.GroupRoleId
                    })
                                     .FirstOrDefault();

                    int?ownerPersonId = null;
                    int?ownerRoleId   = null;

                    if (memberInfo != null)
                    {
                        ownerPersonId = memberInfo.PersonId;
                        ownerRoleId   = memberInfo.RoleId;
                    }

                    if (ownerPersonId.HasValue && ownerRoleId.HasValue)
                    {
                        // Find related person's group
                        var inverseGroup = Queryable()
                                           .Where(m =>
                                                  m.PersonId == groupMember.PersonId &&
                                                  m.Group.GroupTypeId == groupRole.GroupTypeId &&
                                                  m.GroupRole.Guid.Equals(ownerRoleGuid))
                                           .Select(m => m.Group)
                                           .FirstOrDefault();

                        if (inverseGroup == null && createGroup)
                        {
                            var ownerGroupMember = new GroupMember();
                            ownerGroupMember.PersonId    = groupMember.PersonId;
                            ownerGroupMember.GroupRoleId = ownerRoleId.Value;

                            inverseGroup             = new Group();
                            inverseGroup.Name        = groupRole.GroupType.Name;
                            inverseGroup.GroupTypeId = groupRole.GroupTypeId.Value;
                            inverseGroup.Members.Add(ownerGroupMember);
                        }

                        if (inverseGroup != null)
                        {
                            Guid inverseRoleGuid = Guid.Empty;
                            if (Guid.TryParse(groupRole.GetAttributeValue("InverseRelationship"), out inverseRoleGuid))
                            {
                                var inverseGroupMember = Queryable()
                                                         .Where(m =>
                                                                m.PersonId == ownerPersonId &&
                                                                m.GroupId == inverseGroup.Id &&
                                                                m.GroupRole.Guid.Equals(inverseRoleGuid))
                                                         .FirstOrDefault();

                                if (inverseGroupMember == null)
                                {
                                    var inverseRole = new GroupTypeRoleService((RockContext)Context).Get(inverseRoleGuid);
                                    if (inverseRole != null)
                                    {
                                        inverseGroupMember             = new GroupMember();
                                        inverseGroupMember.PersonId    = ownerPersonId.Value;
                                        inverseGroupMember.Group       = inverseGroup;
                                        inverseGroupMember.GroupRoleId = inverseRole.Id;
                                        Add(inverseGroupMember);
                                    }
                                }

                                return(inverseGroupMember);
                            }
                        }
                    }
                }
            }

            return(null);
        }
        /// <summary>
        /// Creates the known relationship.
        /// </summary>
        /// <param name="personId">The person identifier.</param>
        /// <param name="relatedPersonId">The related person identifier.</param>
        /// <param name="relationshipRoleId">The relationship role identifier.</param>
        public void CreateKnownRelationship(int personId, int relatedPersonId, int relationshipRoleId)
        {
            var groupMemberService = this;
            var rockContext        = this.Context as RockContext;

            var knownRelationshipGroupType = GroupTypeCache.Read(Rock.SystemGuid.GroupType.GROUPTYPE_KNOWN_RELATIONSHIPS);
            var ownerRole        = knownRelationshipGroupType.Roles.FirstOrDefault(r => r.Guid.Equals(Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_OWNER.AsGuid()));
            var relationshipRole = knownRelationshipGroupType.Roles.FirstOrDefault(r => r.Id == relationshipRoleId);

            if (ownerRole == null)
            {
                throw new Exception("Unable to find known relationships owner role");
            }

            if (relationshipRole == null)
            {
                throw new Exception("Specified relationshipRoleId is not a known relationships role");
            }

            var knownRelationshipGroup = groupMemberService.Queryable()
                                         .Where(m =>
                                                m.PersonId == personId &&
                                                m.GroupRole.Guid.Equals(ownerRole.Guid))
                                         .Select(m => m.Group)
                                         .FirstOrDefault();

            // Create known relationship group if doesn't exist
            if (knownRelationshipGroup == null)
            {
                var groupMember = new GroupMember();
                groupMember.PersonId    = personId;
                groupMember.GroupRoleId = ownerRole.Id;

                knownRelationshipGroup             = new Group();
                knownRelationshipGroup.Name        = knownRelationshipGroupType.Name;
                knownRelationshipGroup.GroupTypeId = knownRelationshipGroupType.Id;
                knownRelationshipGroup.Members.Add(groupMember);

                new GroupService(rockContext).Add(knownRelationshipGroup);
                rockContext.SaveChanges();
            }

            // Add relationships
            var relationshipMember = groupMemberService.Queryable()
                                     .FirstOrDefault(m =>
                                                     m.GroupId == knownRelationshipGroup.Id &&
                                                     m.PersonId == relatedPersonId &&
                                                     m.GroupRoleId == relationshipRoleId);

            if (relationshipMember == null)
            {
                relationshipMember             = new GroupMember();
                relationshipMember.GroupId     = knownRelationshipGroup.Id;
                relationshipMember.PersonId    = relatedPersonId;
                relationshipMember.GroupRoleId = relationshipRoleId;
                groupMemberService.Add(relationshipMember);
                rockContext.SaveChanges();
            }

            var inverseGroupMember = groupMemberService.GetInverseRelationship(relationshipMember, true);

            if (inverseGroupMember != null)
            {
                groupMemberService.Add(inverseGroupMember);
                rockContext.SaveChanges();
            }
        }