Beispiel #1
0
        public ActionResult Edit(Guid? Id)
        {
            DocumentModel model = null;

            if(Id.HasValue) //用户单击某个文档的编辑界面
            {
                var d = DocumentHelper.Get(Id.Value);
                if(d != null)
                {
                    CreateWorkflowIfNotExists(Id.Value);

                    var h = DocumentHelper.GetHistory(Id.Value);
                    model = new DocumentModel()
                               {
                                   Id = d.Id,
                                   AuthorId = d.AuthorId,
                                   AuthorName = d.Employee1.Name,
                                   Comment = d.Comment,
                                   EmloyeeControlerId = d.EmloyeeControlerId,
                                   EmloyeeControlerName =
                                       d.EmloyeeControlerId.HasValue ? d.Employee.Name : string.Empty,
                                   Name = d.Name,
                                   Number = d.Number,
                                   StateName = d.State,
                                   Sum = d.Sum,
                                   Commands = GetCommands(Id.Value),
                                   AvailiableStates = GetStates(Id.Value),
                                   HistoryModel = new DocumentHistoryModel{Items = h} // 关联文档流转历史记录
                               };
                }

            }
            else //新建文档的用户界面
            {
               Guid userId = CurrentUserSettings.GetCurrentUser();
                model = new DocumentModel()
                        {
                            AuthorId = userId,
                            AuthorName = EmployeeHelper.GetNameById(userId),
                            StateName = "Draft",
                            Commands = new Dictionary<string, string>{},
                            AvailiableStates = new Dictionary<string, string>()
                        };
            }

            return View(model);
        }
        private void ExecuteCommand(Guid id, string commandName, DocumentModel document)
        {
            var currentUser = CurrentUserSettings.GetCurrentUser().ToString("N");
            
            if (commandName.Equals("SetState", StringComparison.InvariantCultureIgnoreCase))
            {
                if (string.IsNullOrEmpty(document.StateNameToSet))
                    return;

                WorkflowInit.Runtime.SetState(id, currentUser, currentUser, document.StateNameToSet, new Dictionary<string, object> { { "Comment", document.Comment } });
                return;
            }

            var commands = WorkflowInit.Runtime.GetAvailableCommands(id, currentUser);

            var command =
                commands.FirstOrDefault(
                    c => c.CommandName.Equals(commandName, StringComparison.CurrentCultureIgnoreCase));

            if (command == null)
                return;

            if (command.Parameters.Count(p => p.ParameterName == "Comment") == 1)
                command.Parameters.Single(p => p.ParameterName == "Comment").Value = document.Comment ?? string.Empty;

            WorkflowInit.Runtime.ExecuteCommand(id, currentUser, currentUser, command);
        }
        public ActionResult Edit(Guid? Id, DocumentModel model, string button)
        {
            using (var context = new DataModelDataContext())
            {
                if (!ModelState.IsValid)
                {
                    return View(model);
                }

                Document target = null;
                if (model.Id != Guid.Empty)
                {
                    target = context.Documents.SingleOrDefault(d=>d.Id == model.Id);
                    if (target == null)
                    {
                        ModelState.AddModelError("", "Row not found!");
                        return View(model);
                    }
                }
                else
                {
                    target = new Document();
                    target.Id = Guid.NewGuid();
                    target.AuthorId = model.AuthorId;
                    target.StateName = model.StateName;
                    context.Documents.InsertOnSubmit(target);
                }

                target.Name = model.Name;
                target.EmloyeeControlerId = model.EmloyeeControlerId;
                target.Comment = model.Comment;
                target.Sum = model.Sum;

                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    var sb = new StringBuilder("Ошибка сохранения. " + ex.Message);
                    if (ex.InnerException != null)
                        sb.AppendLine(ex.InnerException.Message);
                    ModelState.AddModelError("", sb.ToString());
                    return View(model);
                }

                if (button == "SaveAndExit")
                    return RedirectToAction("Index");
                if (button != "Save")
                {
                    ExecuteCommand(target.Id, button, model);
                }
                return RedirectToAction("Edit", new {target.Id});
            }
            return View(model);
        }