Example #1
0
        private Project DuplicateProject(Project Origin, Project Parent = null, Dictionary <ProjectTask, ProjectTask> TasksMap = null, int?Diff = null)
        {
            DateTime date   = DateTime.Now;
            int      userID = db.CurrentUser.ID;
            User     user   = db.CurrentUser;

            int     diff    = Diff ?? (date - Origin.StartDate).Days;
            Project project = Origin.Duplicate();

            project.Name          = string.Format("Копия - {0}", project.Name);
            project.CreatorID     = userID;
            project.ChangerID     = userID;
            project.ParentProject = Parent;

            db.Projects.AddObject(project);

            EntityJs.Client.Events.EntityEventArgs args = new EntityJs.Client.Events.EntityEventArgs(db, "Projects", "Project", project, EntityJs.Client.Events.ActionsEnum.Insert);
            project.OnInserted(args);

            TasksMap = TasksMap ?? new Dictionary <ProjectTask, ProjectTask>();
            foreach (ProjectTask oldTask in Origin.ProjectTasks)
            {
                ProjectTask task = oldTask.Duplicate();
                task.Creator = user.Login;
                task.Project = project;
                //if (task.DateBegin.HasValue)
                //    task.DateBegin = task.DateBegin.Value.AddDays(diff);
                //if (task.DateEndPlan.HasValue)
                //    task.DateEndPlan = task.DateEndPlan.Value.AddDays(diff);
                db.ProjectTasks.AddObject(task);

                args = new EntityJs.Client.Events.EntityEventArgs(db, "ProjectTasks", "ProjectTask", task, EntityJs.Client.Events.ActionsEnum.Insert);
                task.OnInserted(args);

                if (!oldTask.PreviousID.HasValue)
                {
                    task.DateBegin = date;
                }
                if (task.Type != null && task.Type.Duration.HasValue && task.DateBegin.HasValue)
                {
                    task.DateEndPlan = task.DateBegin.Value.AddDays(task.Type.Duration.Value);
                }

                TasksMap[oldTask] = task;
            }

            foreach (Project child in Origin.ChildProjects)
            {
                DuplicateProject(child, project, TasksMap, diff);
            }

            foreach (KeyValuePair <ProjectTask, ProjectTask> pair in TasksMap)
            {
                ProjectTask task;
                if (pair.Key.PreviousTask != null && pair.Value.PreviousTask == null && TasksMap.TryGetValue(pair.Key.PreviousTask, out task))
                {
                    pair.Value.PreviousTask = task;
                    task.NextTasks.Add(pair.Value);
                }
            }

            //if (Parent == null)
            //{
            //    foreach (ProjectTask task in TasksMap.Values)
            //    {
            //        if (task.PreviousID.HasValue)
            //            continue;
            //        task.UpdateNextTasks();
            //    }
            //}
            return(project);
        }