public static void RemoveConsolidator(this Person person, Person newConsolidator)
        {
            var rockContext        = new RockContext();
            var groupMemberService = new GroupMemberService(rockContext);

            if (!person.HasConsolidator())
            {
                throw new Exception(person.FullName + " doesn't have a consolidator");
            }
            var consolidatedBy = new GroupTypeRoleService(rockContext).Get(SystemGuid.GroupTypeRole.CONSOLIDATED_BY.AsGuid());

            groupMemberService.DeleteKnownRelationship(person.Id, newConsolidator.Id, consolidatedBy.Id);
            rockContext.SaveChanges();
        }
Esempio n. 2
0
        public void DeleteKnownRelationship(int personId, int relatedPersonId, int relationshipRoleId)
        {
            SetProxyCreation(true);
            var rockContext   = this.Service.Context as RockContext;
            var personService = new PersonService(rockContext);
            var person        = personService.Get(personId);
            var relatedPerson = personService.Get(relatedPersonId);

            CheckCanEdit(person);
            CheckCanEdit(relatedPerson);

            System.Web.HttpContext.Current.Items.Add("CurrentPerson", GetPerson());

            var groupMemberService = new GroupMemberService(rockContext);

            groupMemberService.DeleteKnownRelationship(personId, relatedPersonId, relationshipRoleId);
        }
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            var personAliasService = new PersonAliasService(rockContext);

            #region Get attributes
            // 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);
                }
            }
            #endregion

            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} doesn't exist between {1} and {2}", relationshipType.Name, person.FullName, relatedPerson.FullName));
                return(false);
            }

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

            // Remove inverse relationship if it exists.
            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.DeleteKnownRelationship(relatedPerson.Id, person.Id, inverseRelationshipType.Id);
                    }
                }
            }


            return(true);
        }