Пример #1
0
        public HttpResponseMessage RemoveHoliday(string name, int holidayId, IEnumerable <string> dates)
        {
            try
            {
                //1. search if exist wf
                var workflows = TasksService.SearchWorkflows(new SearchWorkflowsRequest
                {
                    Domain       = string.Empty,
                    WorkflowCode = "HolidayWf",
                    Properties   = new PropertyInfos(new List <PropertyInfo>
                    {
                        new PropertyInfo
                        {
                            Name  = "UserName",
                            Value = name
                        },
                        new PropertyInfo
                        {
                            Name  = "HolidayId",
                            Value = holidayId.ToString()
                        }
                    })
                });

                if (workflows.WorkflowInfos.Any())
                {
                    //2. If workflow in progress terminate
                    if (workflows.WorkflowInfos[0].Status == WorkflowStatusType.InProgress.ToString())
                    {
                        var resp = TasksService.CancelWorkflow(new ControlWorkflowRequest
                        {
                            WorkflowOid = workflows.WorkflowInfos[0].WorkflowId
                        });
                    }
                    else
                    {
                        //3. else start a new wf or send a notification
                        TasksService.CreateNotification(new CreateNotificationRequest
                        {
                            NotificationInfo = new NotificationInfo
                            {
                                AssignedToUsers = "{r.HR}",
                                TaskOid         = Guid.NewGuid(),
                                WorkflowOid     = Guid.Parse(workflows.WorkflowInfos[0].WorkflowId),
                                Title           = string.Format("Holiday cancellation for user {0}", name),
                                Description     = string.Format("The holiday previously requested by user {0} has been cancelled. Holiday details: {1}", name, string.Join(",", dates))
                            }
                        });
                    }
                }

                var removeHoliday = TasksService.RemoveHoliday(new RemoveHolidayRequest
                {
                    HolidayId = holidayId,
                    User      = name
                });

                if (!removeHoliday.IsRemoved)
                {
                    var result = Request.CreateResponse(HttpStatusCode.InternalServerError);
                    result.Content = new StringContent("Could not remove workflow. Contact System Administrator.", Encoding.UTF8, "text/plain");
                    return(result);
                }

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }