コード例 #1
0
 protected GroupTypeRole GetInverseRole(GroupTypeRole role, GroupTypeRoleService groupRoleServ)
 {
     role.LoadAttributes();
     return(groupRoleServ.Get(role.GetAttributeValue("InverseRelationship").AsGuid()));
 }
コード例 #2
0
 protected GroupTypeRole GetInverseRole( GroupTypeRole role, GroupTypeRoleService groupRoleServ )
 {
     role.LoadAttributes();
     return groupRoleServ.Get( role.GetAttributeValue( "InverseRelationship" ).AsGuid() );
 }
コード例 #3
0
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            var personAliasService = new PersonAliasService(rockContext);

            // get person
            Person person = null;
            var    guidPersonAttribute = GetAttributeValue(action, "Person").AsGuidOrNull();

            if (guidPersonAttribute.HasValue)
            {
                var attributePerson = AttributeCache.Read(guidPersonAttribute.Value, rockContext);
                if (attributePerson != null)
                {
                    var attributePersonValue = action.GetWorklowAttributeValue(guidPersonAttribute.Value).AsGuidOrNull();
                    if (attributePersonValue.HasValue)
                    {
                        person = personAliasService.GetPerson(attributePersonValue.Value);
                        if (person == null)
                        {
                            errorMessages.Add(string.Format("Person could not be found for selected value ('{0}')!", guidPersonAttribute));
                            return(false);
                        }
                    }
                }
            }

            Person relatedPerson = null;

            var guidRelatedPersonAttribute = GetAttributeValue(action, "RelationshipTo").AsGuidOrNull();

            if (guidRelatedPersonAttribute.HasValue)
            {
                var attributePerson = AttributeCache.Read(guidRelatedPersonAttribute.Value, rockContext);
                if (attributePerson != null)
                {
                    var attributePersonValue = action.GetWorklowAttributeValue(guidRelatedPersonAttribute.Value).AsGuidOrNull();
                    if (attributePersonValue.HasValue)
                    {
                        relatedPerson = personAliasService.GetPerson(attributePersonValue.Value);
                        if (relatedPerson == null)
                        {
                            errorMessages.Add(string.Format("Person could not be found for selected value ('{0}')!", guidPersonAttribute));
                            return(false);
                        }
                    }
                }
            }


            var groupTypeRoleService = new GroupTypeRoleService(rockContext);

            GroupTypeRole relationshipType     = null;
            var           relationshipRoleGuid = GetActionAttributeValue(action, "RelationshipType").AsGuidOrNull();

            if (relationshipRoleGuid.HasValue)
            {
                relationshipType = groupTypeRoleService.Get(relationshipRoleGuid.Value);
                if (relationshipType == null)
                {
                    errorMessages.Add(string.Format("GroupTypeRole (Relationship Type) could not be found for selected value ('{0}')!", relationshipRoleGuid));
                    return(false);
                }
            }

            var groupMemberService = new GroupMemberService(rockContext);

            // Check if relationship already exists
            if (
                groupMemberService.GetKnownRelationship(person.Id, relationshipType.Id)
                .Any(gm => gm.Person.Id == relatedPerson.Id))
            {
                errorMessages.Add(string.Format("Relationship of {0} already exists between {1} and {2}", relationshipType.Name, person.FullName, relatedPerson.FullName));
                return(false);
            }

            groupMemberService.CreateKnownRelationship(person.Id, relatedPerson.Id, relationshipType.Id);

            // Create inverse relationship if it doesn't exist.
            if (relationshipType.Attributes.ContainsKey("InverseRelationship"))
            {
                var inverseRelationshipTypeGuid =
                    relationshipType.GetAttributeValue("InverseRelationship").AsGuidOrNull();
                if (inverseRelationshipTypeGuid.HasValue)
                {
                    var inverseRelationshipType = groupTypeRoleService.Get(inverseRelationshipTypeGuid.Value);
                    // Ensure relationship doesn't already exist
                    if (!groupMemberService.GetKnownRelationship(relatedPerson.Id, inverseRelationshipType.Id)
                        .Any(gm => gm.Person.Id == person.Id))
                    {
                        groupMemberService.CreateKnownRelationship(relatedPerson.Id, person.Id, inverseRelationshipType.Id);
                    }
                }
            }


            return(true);
        }