예제 #1
0
        public JsonResult CreateOneToOne(OneToOneViewModel model)
        {
            //remove Id from modelstate if tehre. 
            ModelState.Remove("Id");
            ModelState.Remove("CreatedById");
            ModelState.Remove("CreatedBy");

            if (ModelState.IsValid)
            {
                using (var orgSession = OrgStore.OpenSession())
                {
                    var target = orgSession.Load<User>(model.InviteeId);
                    var invitee = Invitee.FromUser(target);
                    var invitees = new List<Invitee> {invitee};

                    var meeting = new Meeting
                                      {
                                          CreatedBy = OrgUser.ToSimpleUser(),
                                          AssignedUsers = new List<SimpleUser> { OrgUser.ToSimpleUser() },
                                          DueDate = model.EventDate,
                                          ConsolidatedCompletionDate = model.EventDate,
                                          DurationMinutes = model.DurationMinutes,
                                          Title = model.Title,
                                          Description = model.Description,
                                          Invitees = invitees,
                                          Id = IlluminateDatabase.GenerateId<Meeting>()

                                      };

                    orgSession.Store(meeting);
                    meeting.UpdateAssignees(orgSession);

                    var notification = meeting.ToNotification();
                    orgSession.Store(notification);

                    orgSession.SaveChanges();

                    return JsonSuccess(meeting.ToTimelineItems(), "One To One created");
                }
            }
            return JsonError("Invalid data");
        }
        /// <summary>
        /// perform an action on the leave process
        /// </summary>
        /// <param name="documentStore">The RavenDB document store to be used</param>
        /// <param name="triggerprocess">The initiator process - e.g. leave or sickness</param>
        /// <param name="action">The action to be performed</param>
        public override void PerformAction(IDocumentStore documentStore,
                                            WorkflowInitiator triggerprocess,
                                           WorkflowHandlerAction action)
        {
            //we know its a Leave so cast it to the correct type
            var leave = (Leave) triggerprocess;

            switch (action.Action) //depending on the type of action do different things
            {
                case WorkflowHandlerAction.Actions.AddTask: //add a task
                    //what am I creating?
                    var actionmodel = action.DeserialiseObject<CreateProcessTaskActionModel>();

                    //need to convert the actionmodel into an event.
                    var newtask = new Task
                                      {
                                          Id = IlluminateDatabase.GenerateId<Task>(),
                                          Type = new TaskType {Name = actionmodel.TaskName},
                                          AssignedRoles =
                                              (from role in actionmodel.AssignedRoles select Role.GetRole(role)).ToList(),
                                          DueDate = actionmodel.DueDate,
                                          ConsolidatedCompletionDate = actionmodel.DueDate,
                                          CreatedBy = leave.UserCreatedProcess,
                                          Title = actionmodel.Title,
                                          Description = actionmodel.Description,
                                          ParentItemId = leave.Id
                                      };
                    using (var session = documentStore.OpenSession())
                    {
                        session.Store(newtask);
                        newtask.UpdateAssignees(session, leave.Subject.UserId);
                        session.SaveChanges();
                    }

                    break;
                case WorkflowHandlerAction.Actions.AddMeeting: //add a meeting

                    var meetingactionmodel = action.DeserialiseObject<CreateProcessMeetingActionModel>();

                    //need to convert the actionmodel into an event. but problem is looking up user?
                    var newMeeting = new Meeting
                                         {
                                             Id = IlluminateDatabase.GenerateId<Meeting>(),
                                             Type = new TaskType {Name = meetingactionmodel.TaskName},
                                             AssignedRoles =
                                                 (from role in meetingactionmodel.AssignedRoles
                                                  select Role.GetRole(role)).ToList(),
                                             Invitees = new List<Invitee> {Invitee.FromSimpleUser(leave.Subject)},
                                             DueDate = meetingactionmodel.DueDate,
                                             ConsolidatedCompletionDate = meetingactionmodel.DueDate,
                                             CreatedBy = leave.UserCreatedProcess,
                                             Title = meetingactionmodel.Title,
                                             Description = meetingactionmodel.Description,
                                             ParentItemId = leave.Id,
                                             DurationMinutes = 30
                                         };
                    //now save the new mmeeting
                    using (var session = documentStore.OpenSession())
                    {
                        session.Store(newMeeting);
                        newMeeting.UpdateAssignees(session, leave.Subject.UserId);
                        session.SaveChanges();
                    }

                    break;
                case WorkflowHandlerAction.Actions.DeleteTask: //delete scheduled a task

                    using (var session = documentStore.OpenSession())
                    {
                        var task = session.Load<Task>(action.Value);
                        session.Delete(task);
                        session.SaveChanges();
                    }
                    break;
                case WorkflowHandlerAction.Actions.DeleteMeeting: //delete a scheduled meeting
                    using (var session = documentStore.OpenSession())
                    {
                        var meeting = session.Load<Meeting>(action.Value);
                        session.Delete(meeting);
                        session.SaveChanges();
                    }
                    break;
            }
        }
        /// <summary>
        /// Perform an action on the default sickness workflow
        /// </summary>
        /// <param name="documentStore">The document store to use</param>
        /// <param name="triggerprocess">The workflow initiator that triggered this</param>
        /// <param name="action">The action to be performed</param>
        public override void PerformAction(IDocumentStore documentStore, WorkflowInitiator triggerprocess, WorkflowHandlerAction action)
        {
            var sickness = (Sickness) triggerprocess; //we know its a sickness so cast to it

            switch (action.Action) //depending on the action type act accordingly
            {
                case WorkflowHandlerAction.Actions.AddTask:
                    //what am I creating? - deserialise the contents of the action
                    var actionModel = action.DeserialiseObject<CreateProcessTaskActionModel>();

                    //need to convert the actionmodel into an event - in this case a task
                    var newtask = new Task
                                        {
                                            Id = IlluminateDatabase.GenerateId<Task>(),
                                            Type = new TaskType {Name = actionModel.TaskName},
                                            AssignedRoles = (from role in actionModel.AssignedRoles select Role.GetRole(role)).ToList(),
                                            DueDate = actionModel.DueDate,
                                            ConsolidatedCompletionDate = actionModel.DueDate>=DateTime.Now ? actionModel.DueDate : DateTime.Now,
                                            CreatedBy = sickness.UserCreatedProcess,
                                            Title =string.Format( actionModel.Title,sickness.Subject.FullName),
                                            Description = string.Format( actionModel.Description,sickness.Subject.FullName),
                                            ParentItemId = sickness.Id
                                        };
                    //store the task in the db
                    using (var session = documentStore.OpenSession())
                    {
                        session.Store(newtask);
                        newtask.UpdateAssignees(session, sickness.Subject.UserId);
                        session.SaveChanges();
                    }
                    
                    break;
                case WorkflowHandlerAction.Actions.AddMeeting:
   
                    var meetingactionmodel = action.DeserialiseObject<CreateProcessMeetingActionModel>();

                    //need to convert the actionmodel into an event. but problem is looking up user?
                    var newmeeting = new Meeting
                    {
                        Id = IlluminateDatabase.GenerateId<Meeting>(),
                        Type = new TaskType { Name = meetingactionmodel.TaskName },
                        AssignedRoles = (from role in meetingactionmodel.AssignedRoles select Role.GetRole(role)).ToList(),
                        Invitees = new List<Invitee> { Invitee.FromSimpleUser(sickness.Subject)},
                        DueDate = meetingactionmodel.DueDate,
                        ConsolidatedCompletionDate = meetingactionmodel.DueDate >= DateTime.Now ? meetingactionmodel.DueDate : DateTime.Now,
                        CreatedBy = sickness.UserCreatedProcess,
                        Title = meetingactionmodel.Title,
                        Description = meetingactionmodel.Description,
                        ParentItemId = sickness.Id
                    };
                    //save the meeting in the DB
                    using (var session = documentStore.OpenSession())
                    {
                        session.Store(newmeeting);
                        newmeeting.UpdateAssignees(session, sickness.Subject.UserId);
                        session.SaveChanges();
                    }
       
                    break;
                case WorkflowHandlerAction.Actions.DeleteTask:
                    //delete the task from the DB
                    using (var session = documentStore.OpenSession())
                    {
                        var task = session.Load<Task>(action.Value);
                        session.Delete(task);
                        session.SaveChanges();
                    }
                    break;
                case WorkflowHandlerAction.Actions.DeleteMeeting:
                    //delete the meeting from the DB
                    using (var session = documentStore.OpenSession())
                    {
                        var meeting = session.Load<Meeting>(action.Value);
                        session.Delete(meeting);
                        session.SaveChanges();
                    }
                    break; 
                case WorkflowHandlerAction.Actions.NotifyActorsOfCreation:
                    using (var session = documentStore.OpenSession())
                    {
                        
                        var managerCreatedSickness=sickness.UserCreatedProcess.UserId==sickness.CurrentProcessOwner.UserId;
                        //if the manager created the sickness notify the employee 
                        Notification notification;
                        if (managerCreatedSickness)
                        {
                            notification = new Notification
                                               {
                                                   
                                                   NotificationRecipients = new[]
                                                                                {
                                                                                    new NotificationRecipient
                                                                                        {
                                                                                            NotificationDeliveryTypes =
                                                                                                NotificationDeliveryTypes
                                                                                                    .Email |
                                                                                                NotificationDeliveryTypes
                                                                                                    .Toast,
                                                                                            Users = new[]
                                                                                                        {
                                                                                                            sickness
                                                                                                                .Subject
                                                                                                        }
                                                                                        }
                                                                                },
                                                   Body =
                                                       string.Format(
                                                           "You have been registered as off sick for {0} by {1}",
                                                           sickness.SicknessReason.Name,
                                                           sickness.CurrentProcessOwner.FullName)

                                               };
                        }
                        else
                        {
                            notification = new Notification
                            {

                                NotificationRecipients = new[]
                                                                                {
                                                                                    new NotificationRecipient
                                                                                        {
                                                                                            NotificationDeliveryTypes =
                                                                                                NotificationDeliveryTypes
                                                                                                    .Email |
                                                                                                NotificationDeliveryTypes
                                                                                                    .Toast|
                                                                                                    NotificationDeliveryTypes.Sms,
                                                                                            Users = new[]
                                                                                                        {
                                                                                                            sickness.CurrentProcessOwner
                                                                                                        }
                                                                                        }
                                                                                },
                                Body =
                                    string.Format(
                                        "{0} has registered themselves as off sick for {1}.",
                                        sickness.Subject.FullName,
                                        sickness.SicknessReason.Name)

                            };
                        }
                        notification.Title = string.Format("Sickness for {0}", sickness.Subject.FullName);
                        notification.About = sickness.Subject;
                        notification.Id = IlluminateDatabase.GenerateId<Notification>();
                        notification.SendDate = DateTime.Now;

                        session.Store(notification);
                        session.SaveChanges();
                    }
                    break;

            }
        }