public ICanAction Reject(string comments = null)
        {
            IPAFTransition transition = getTransition(userContext);

            setDecision(transition, DecisionType.Rejected, comments);
            return(this);
        }
        public ICanAction Invalidate(string approver, string comments = null)
        {
            IPAFTransition transition = getTransition(approver);

            setDecision(transition, DecisionType.Invalidated, comments);
            return(this);
        }
        public ICanAction CheckIn()
        {
            IPAFTransition transition = getTransition(userContext);

            checkin(transition);
            return(this);
        }
        private void checkin(IPAFTransition transition)
        {
            if (transition == null)
            {
                throw new NullReferenceException("No transition found");
            }

            transition.ApproverCheckInDate = (DateTime?)DateTime.Now;

            editTransition((PAFTransition)transition);
        }
        public ICanAction LoadNotification(string approver, string[] usersToCC = null, string[] mailsToCC = null)
        {
            IPAFTransition transition = getTransition(approver);

            if (transition == null)
            {
                throw new NullReferenceException("No transition found");
            }

            string to;
            string from = null;
            string comments;

            if (transition.ApproverDecision == DecisionType.AwaitingDecision || transition.ApproverDecision == DecisionType.Invalidated)
            {
                to       = transition.ApproverID;
                comments = transition.RequesterComments;
                if (transition.ApproverID != transition.RequesterID)
                {
                    from = transition.RequesterID;
                }
            }
            else
            {
                to       = transition.RequesterID;
                comments = transition.ApproverComments;
                if (transition.ApproverID != transition.RequesterID)
                {
                    from = transition.ApproverID;
                }
            }

            if (pafNotifications == null)
            {
                pafNotifications = new List <PAFNotification>();
            }

            pafNotifications.Add(new PAFNotification
            {
                From         = from,
                To           = to,
                Comments     = comments,
                DecisionType = transition.ApproverDecision,
                UsersToCC    = usersToCC,
                MailsToCC    = mailsToCC,
            });

            return(this);
        }
        private void editTransition(PAFTransition pafT)
        {
            IPAFTransition transition = getTransition(pafT.ApproverID);

            if (transition == null)
            {
                throw new NullReferenceException("No transition found");
            }

            transition.Order               = pafT.Order;
            transition.RequesterID         = pafT.RequesterID;
            transition.RequestedDate       = pafT.RequestedDate;
            transition.ApproverID          = pafT.ApproverID;
            transition.ApproverRole        = pafT.ApproverRole;
            transition.ApproverDecision    = pafT.ApproverDecision;
            transition.RequesterComments   = pafT.RequesterComments;
            transition.ApproverComments    = pafT.ApproverComments;
            transition.AcknowledgementDate = pafT.AcknowledgementDate;
        }
        private void setDecision(IPAFTransition transition, DecisionType decision, string comments)
        {
            if (transition == null)
            {
                throw new NullReferenceException("No transition found");
            }

            transition.ApproverDecision = decision;

            if (transition.ApproverCheckInDate == null && decision != DecisionType.AwaitingDecision)
            {
                transition.ApproverCheckInDate = (DateTime?)DateTime.Now;
            }

            transition.AcknowledgementDate = decision == DecisionType.AwaitingDecision ? null : (DateTime?)DateTime.Now;
            transition.ApproverComments    = comments;

            editTransition((PAFTransition)transition);
        }
        private void createTransition(string requester, string approver, string role, string comments)
        {
            if (!approvalFlowEntity.Transitions.Any(t => t.ApproverID == approver))
            {
                int order         = !approvalFlowEntity.Transitions.Any() ? 1 : approvalFlowEntity.Transitions.Max(a => a.Order) + 1;
                var newTransition = new T();
                newTransition.Initalize(order, requester, approver, role, comments);
                approvalFlowEntity.Transitions.Add(newTransition);
            }

            if (approvalFlowEntity.Transitions.Any(t => t.ApproverID == approver && (t.ApproverDecision == DecisionType.Invalidated || t.ApproverDecision == DecisionType.AwaitingDecision)))
            {
                IPAFTransition transition = getTransition(approver);
                transition.ApproverRole = role;
                if (comments != null)
                {
                    transition.RequesterComments = comments;
                }
                setDecision(transition, DecisionType.AwaitingDecision, comments);
            }
        }