Ejemplo n.º 1
0
        public static List <TaskHistory> GenerateDiff(string action, List <DiffInfo> diffInfo, int updateUser, int taskID)
        {
            List <TaskHistory> res  = new List <TaskHistory>();
            Worktask           task = WorktaskService.GetInstance().GetTask(taskID);

            foreach (var item in diffInfo)
            {
                string displayTitle = item.Title;
                switch (item.DisplayType)
                {
                case DisplayTypeE.UserId:
                    item.OldValue = (item.OldValue == null ? String.Empty : AccountHelper.GetUserById((int)item.OldValue).DisplayName2);
                    item.NewValue = (item.NewValue == null ? String.Empty : AccountHelper.GetUserById((int)item.NewValue).DisplayName2);
                    break;

                case DisplayTypeE.BoardId:
                    item.OldValue = (item.OldValue == null ? String.Empty : BoardService.GetInstance().GetBoard((int)item.OldValue).Title);
                    item.NewValue = (item.NewValue == null ? String.Empty : BoardService.GetInstance().GetBoard((int)item.NewValue).Title);
                    break;

                case DisplayTypeE.WorkflowState:
                    int id = (int)(task.WorkflowInstance.WorkflowID == null ? 0 : task.WorkflowInstance.WorkflowID);
                    item.OldValue = (item.OldValue == null ? String.Empty : WorkflowService.GetInstance().GetState(id, (int)item.OldValue).Name);
                    item.NewValue = (item.NewValue == null ? String.Empty : WorkflowService.GetInstance().GetState(id, (int)item.NewValue).Name);
                    break;

                case DisplayTypeE.CategoryPriority:
                    item.OldValue = (item.OldValue == null ? String.Empty : CategoryService.GetInstance().GetCategoryName((int)item.OldValue, Contain.CatType.Priority));
                    item.NewValue = (item.NewValue == null ? String.Empty : CategoryService.GetInstance().GetCategoryName((int)item.NewValue, Contain.CatType.Priority));
                    break;

                case DisplayTypeE.CategoryTaskType:
                    item.OldValue = (item.OldValue == null ? String.Empty : CategoryService.GetInstance().GetCategoryName((int)item.OldValue, Contain.CatType.Category));
                    item.NewValue = (item.NewValue == null ? String.Empty : CategoryService.GetInstance().GetCategoryName((int)item.NewValue, Contain.CatType.Category));
                    break;

                case DisplayTypeE.CategoryCompany:
                    item.OldValue = (item.OldValue == null ? String.Empty : CategoryService.GetInstance().GetCategoryName((int)item.OldValue, Contain.CatType.Company));
                    item.NewValue = (item.NewValue == null ? String.Empty : CategoryService.GetInstance().GetCategoryName((int)item.NewValue, Contain.CatType.Company));
                    break;

                case DisplayTypeE.Date:
                    item.OldValue = (item.OldValue == null ? String.Empty : ((DateTime)item.OldValue).ToString("dd/MM/yyyy"));
                    item.NewValue = (item.NewValue == null ? String.Empty : ((DateTime)item.NewValue).ToString("dd/MM/yyyy"));
                    break;
                }
                res.Add(CompareObjExtensions.GenerateDiff(action, displayTitle, (item.NewValue == null ? String.Empty : item.NewValue.ToString()), (item.OldValue == null ? String.Empty : item.OldValue.ToString()), updateUser, taskID));
            }
            return(res);
        }
Ejemplo n.º 2
0
        public ActionResult Edit(WorktaskViewModel worktask)
        {
            int currentUID = AccountHelper.GetCurrentUser(User.Identity.Name).UID;

            if (ModelState.IsValid)
            {
                //Compare data of new and old object for saving to history
                var oldObject = repository.Worktasks.Where(x => x.WorktaskID == worktask.WorktaskID).FirstOrDefault();
                oldObject = repository.Detail(oldObject.WorktaskID);
                if (oldObject != null)
                {
                    oldObject.loadMetaInfo(repository, accRepository);
                    worktask = (WorktaskViewModel)WorktaskService.GetInstance().loadMetaInfoFromRequest(worktask, Request);
                    worktask.loadMetaInfo(repository, accRepository);
                    List <DiffInfo> diffInfos = worktask.Compare(oldObject);
                    var             diff      = CompareObjExtensions.GenerateDiff("Cập nhật", diffInfos, currentUID, oldObject.WorktaskID);
                    if (repository.SaveWorktask(worktask))
                    {
                        if (repository.SaveHistory(diff))
                        {
                            TempData["message"] = string.Format("Thông tin công việc {0} lưu thành công!", worktask.Identify);
                            return(Json(new
                            {
                                taskId = worktask.WorktaskID,
                                success = true,
                                message = TempData["message"],
                                redirectUrl = Url.Action("Detail", new { taskcode = worktask.Identify })
                            }, JsonRequestBehavior.AllowGet));
                        }
                    }
                }

                TempData["message"] = string.Format("Lưu không thành công");
                return(Json(new { taskId = 0, success = false, message = TempData["message"] }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                TempData["message"] = string.Format(GetModelErrorMessages(ModelState));
                return(Json(new { taskId = 0, success = false, message = TempData["message"] }, JsonRequestBehavior.AllowGet));
            }
        }
Ejemplo n.º 3
0
        //objA : old object
        //objB : new object
        public static List <TaskHistory> DetailedCompare <T>(this T objA, T objB, int updateUser, int taskID, bool isNew)
        {
            var list   = new List <TaskHistory>();
            var action = new List <string> {
                "Cập nhật", "Tạo mới"
            };

            //insert
            if (objA == null)
            {
                //attachment
                if (IsList(objB))
                {
                    var collection = (IList)objB;
                    foreach (var item in collection)
                    {
                        //not default
                        if (!string.IsNullOrEmpty(GetValueProps(item, "AttachmentCode")))
                        {
                            var name          = GetValueProps(item, "Name");
                            var updated       = "Thêm mới tài liệu: " + name;
                            var newValue      = name;
                            var originalValue = string.Empty;
                            var result        = GenerateDiff(action[0], updated, newValue, originalValue, updateUser, taskID);
                            list.Add(result);
                        }
                    }
                }
                //worktask
                else
                {
                    //insert new worktask
                    if (isNew)
                    {
                        var identity      = GetValueProps(objB, "Identify");
                        var updated       = "Tạo mới task: " + identity;
                        var newValue      = identity;
                        var originalValue = "";
                        var result        = GenerateDiff(action[1], updated, newValue, originalValue, updateUser, taskID);
                        list.Add(result);
                    }
                }
            }
            //delete
            else if (objB == null)
            {
                var name          = GetValueProps(objA, "Name");
                var updated       = "Xóa tài liệu: " + name;
                var newValue      = string.Empty;
                var originalValue = name;
                var result        = GenerateDiff(action[0], updated, newValue, originalValue, updateUser, taskID);
                list.Add(result);
            }
            //edit
            else
            {
                var props = compareFieldTitle.Keys;
                foreach (var item in props)
                {
                    //get value by props name

                    var old    = GetValueProps(objA, item);
                    var newObj = GetValueProps(objB, item);
                    if (!Equals(old, newObj))
                    {
                        String oldName = WorktaskService.GetInstance().GetTaskDisplayValue(taskID, old, item);
                        String newName = WorktaskService.GetInstance().GetTaskDisplayValue(taskID, newObj, item);
                        var    result  = GenerateDiff(action[0], item, newName, oldName, updateUser, taskID);
                        list.Add(result);
                    }
                }
            }
            return(list);
        }
Ejemplo n.º 4
0
        public ActionResult InsertTask(Worktask worktask)
        {
            int currentUID = AccountHelper.GetCurrentUser(User.Identity.Name).UID;
            ElasticIndexService eService  = new ElasticIndexService();
            Worktask            oldObject = null;

            if (ModelState.IsValid)
            {
                var isNew = false;

                // Assign Current user AS Owner, Assignee, Reporter
                if (worktask.Owner == 0)
                {
                    worktask.Owner = currentUID;
                }
                if (worktask.Assignee == 0)
                {
                    worktask.Assignee = currentUID;
                }
                if (worktask.Reporter == null || worktask.Reporter == 0)
                {
                    worktask.Reporter = currentUID;
                }
                if (worktask.TaskType == 0)
                {
                    worktask.TaskType = int.Parse(CategoryService.GetInstance().GetCategoryList(Contain.CatType.Category).ElementAt(1).Value);
                }
                if (worktask.Priority == 0)
                {
                    worktask.Priority = int.Parse(CategoryService.GetInstance().GetCategoryList(Contain.CatType.Priority).ElementAt(1).Value);
                }

                if (worktask.WorktaskID == 0)
                {
                    isNew              = true;
                    worktask.BoardID   = this.GetLastViewedBoardId(User.Identity.Name);
                    worktask.TaskMetas = new List <WorkTaskMetas>();
                    worktask           = WorktaskService.GetInstance().loadMetaInfoFromRequest(worktask, Request);
                    worktask.loadMetaInfo(repository, accRepository);
                    // create workflow instance
                    Board            board      = this.boardRepository.Get(worktask.BoardID);
                    Workflow         wf         = board.Workflow;
                    State            firstState = wf.States.Where(s => s.Type == (int)Contain.StateType.Init).FirstOrDefault();
                    WorkflowInstance wfi        = new WorkflowInstance()
                    {
                        WorkflowID     = wf.ID,
                        CurrentStateID = (firstState != null ? firstState.ID : 0)
                    };
                    worktask.WorkflowInstance = wfi;
                    worktask.Status           = firstState.ID;
                }
                else
                {
                    worktask = WorktaskService.GetInstance().loadMetaInfoFromRequest(worktask, Request);
                    worktask.loadMetaInfo(repository, accRepository);
                    oldObject = repository.Worktasks.Where(x => x.WorktaskID == worktask.WorktaskID).FirstOrDefault();
                    oldObject = repository.Detail(oldObject.WorktaskID);
                    oldObject.loadMetaInfo(repository, accRepository);
                }
                var diff = new List <TaskHistory>();
                if (repository.SaveWorktask(worktask))
                {
                    //save history worktask when create new
                    string action = "Cập nhật";
                    if (isNew)
                    {
                        // Save data path for the new task
                        string dataPathRoot = HostingEnvironment.MapPath(WebConfigurationManager.AppSettings["DataDirectoryRoot"]);
                        string taskDataPath = Path.Combine(dataPathRoot, worktask.WorktaskID.ToString());
                        worktask.DataPath = taskDataPath;
                        Directory.CreateDirectory(taskDataPath);
                        action = "Tạo mới";
                    }
                    List <DiffInfo> diffInfos = worktask.Compare(oldObject);
                    diff = CompareObjExtensions.GenerateDiff(action, diffInfos, currentUID, worktask.WorktaskID);
                    repository.SaveHistory(diff);

                    foreach (var item in worktask.Attachment)
                    {
                        var text = String.Empty;
                        if (System.IO.File.Exists(item.StoredPath))
                        {
                            text = new TextExtractor().Extract(item.StoredPath).Text;
                        }
                        item.Metadata = text.Replace("\r", string.Empty).Replace("\n", string.Empty);
                    }
                    if (repository.SaveAttachmentFile(worktask))
                    {
                        //save history
                        diff = CompareObjExtensions.DetailedCompare(null, worktask.Attachment, currentUID, worktask.WorktaskID, isNew);
                        if (repository.SaveHistory(diff))
                        {
                            // Save Index in Elastic

                            eService.CreateSingleIndex(repository.Detail(worktask.WorktaskID));

                            TempData["message"] = string.Format("Thông tin công việc {0} lưu thành công!", worktask.Identify);
                            return(Json(new
                            {
                                taskId = worktask.WorktaskID,
                                attachment = worktask.Attachment.Select(x => new { AttachmentID = x.AttachmentID, Name = x.Name }),
                                success = true,
                                redirectUrl = Url.Action("Detail", new { taskcode = worktask.Identify })
                            }, JsonRequestBehavior.AllowGet));
                        }
                    }

                    // Save Index in Elastic
                    eService.CreateSingleIndex(repository.Detail(worktask.WorktaskID));

                    TempData["message"] = string.Format("Thông tin công việc {0} lưu thành công!", worktask.Identify);
                    return(Json(new
                    {
                        taskId = worktask.WorktaskID,
                        success = true,
                        redirectUrl = Url.Action("Detail", new { taskcode = worktask.Identify })
                    }, JsonRequestBehavior.AllowGet));
                }
                TempData["message"] = string.Format("Lưu không thành công");
                return(Json(new { taskId = 0, success = false, message = TempData["message"] }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                TempData["message"] = string.Format(GetModelErrorMessages(ModelState));
                return(Json(new { taskId = 0, success = false, message = TempData["message"] }, JsonRequestBehavior.AllowGet));
            }
        }