Ejemplo n.º 1
0
        /// <summary>
        ///     Takes action on the specified workflow item, representing a
        ///     membership request.
        /// </summary>
        /// <param name="workflowId">The id of the workflow </param>
        /// <param name="action">The moderation action to be taken</param>
        /// <param name="userId">The unique id of the user under moderation.</param>
        /// <param name="communityId">The unique id of the community to which membership has been requested.</param>
        public void Moderate(string workflowId, string action, string userId, string communityId)
        {
            var membershipRequest   = GetMembershipRequest(userId, communityId);
            var populatedWorkflowId = WorkflowId.Create(workflowId);

            var requestReference = Reference.Create(CreateUri(membershipRequest.Group, membershipRequest.User));

            try
            {
                var transitionToken = _workflowService.BeginTransitionSession(populatedWorkflowId, requestReference);
                try
                {
                    // Retrieve the moderation workflow associated with
                    // the item to be acted upon.

                    var workflow = _workflowService.Get(populatedWorkflowId);

                    // Leverage the workflow to determine what the
                    // resulting state of the item will be upon taking
                    // the specified action.

                    //retrieve the current state of the workflow item once the begintransitionsession begins.
                    var filter = new WorkflowItemFilter {
                        Target = requestReference
                    };
                    var criteria = new Criteria <WorkflowItemFilter> {
                        Filter = filter
                    };
                    var workflowItem = _workflowItemService.Get(criteria).Results.Last();

                    // Example: Current State: "Pending", Action: "Approve" => Transitioned State: "Approved"
                    var transitionedState = workflow.Transition(workflowItem.State, new WorkflowAction(action));

                    var subsequentWorkflowItem = new WorkflowItem(
                        workflow.Id,
                        transitionedState,
                        requestReference
                        );

                    _workflowItemService.Add(subsequentWorkflowItem, membershipRequest, transitionToken);

                    // Perform any application logic given the item's
                    // new state.

                    if (IsApproved(subsequentWorkflowItem.State))
                    {
                        _memberRepository.Add(_memberAdapter.Adapt(membershipRequest));
                    }
                }
                finally
                {
                    _workflowService.EndTransitionSession(transitionToken);
                }
            }
            catch (SocialAuthenticationException ex)
            {
                throw new SocialRepositoryException("The application failed to authenticate with Episerver Social.",
                                                    ex);
            }
            catch (MaximumDataSizeExceededException ex)
            {
                throw new SocialRepositoryException(
                          "The application request was deemed too large for Episerver Social.", ex);
            }
            catch (SocialCommunicationException ex)
            {
                throw new SocialRepositoryException("The application failed to communicate with Episerver Social.", ex);
            }
            catch (SocialException ex)
            {
                throw new SocialRepositoryException("Episerver Social failed to process the application request.", ex);
            }
        }