private void LoadEmployeesAndSubtasksForTask(Task tsk) { tsk.AssignedEmployees = Program.dbms.GetEmployeesOnTask(tsk.ID); List <Task> subtasks = Program.dbms.GetAllSubTasks(tsk.ID); tsk.SubTasks.Clear(); foreach (var subtsk in subtasks) { LoadEmployeesAndSubtasksForTask(subtsk); tsk.SubTasks.Add(subtsk); } }
private void SetEntryToEdit(Task tag) { txtTitle.Text = tag.Title; chkIsFinished.Checked = tag.IsFinished; chkIsMilestone.Checked = tag.IsMilestone; dtpStartDate.Value = tag.StartingDate; dtpDueDate.Value = tag.DueDate; numActualWorkingHours.Value = tag.ActualWorkingHours ?? 0; btnAddEdit.Text = "Edit Selected Task"; ReloadFreeEmployeesList(); LoadEmployeeListIntoListView(lstSelectedEmployees, tag.AssignedEmployees); currentMode = EntryMode.Edit; }
private void btnAddEdit_Click(object sender, EventArgs e) { if (chkIsMilestone.Checked) { dtpDueDate.Value = dtpStartDate.Value; } if (trvTasks.SelectedNode == null) { MessageBox.Show("Nothing is selected!", "Selection", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } txtTitle.Text = txtTitle.Text.Trim(); if (txtTitle.Text == "") { MessageBox.Show("Title of task cannot be empty!", "Invalid Data", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (dtpStartDate.Value > dtpDueDate.Value) { MessageBox.Show("Due Date of Project must be later than the start date", "Invalid Data", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (dtpStartDate.Value < ownerProject.StartingDate || dtpDueDate.Value > ownerProject.DueDate) { MessageBox.Show("Task schedule does not fit in the project's schedule.", "Invalid Data", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } TreeNode parent = null; if (currentMode == EntryMode.Add) { parent = trvTasks.SelectedNode; } else if (currentMode == EntryMode.Edit) { parent = trvTasks.SelectedNode.Parent; } if (parent.Tag != null) { Task p = (Task)parent.Tag; if (dtpStartDate.Value >= p.DueDate || dtpDueDate.Value <= p.StartingDate) { MessageBox.Show("Task does not fit in its parent's schedule.", "Invalid Data", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } if (currentMode == EntryMode.Add) { int?id = null; if (parent.Tag != null) { id = ((Task)parent.Tag).ID; } bool isFinished = chkIsFinished.Checked; int taskType = (int)(chkIsMilestone.Checked ? Task.TypeOfTask.Milestone : Task.TypeOfTask.Task); int? actualWork = numActualWorkingHours.Value == 0 ? null : (int?)numActualWorkingHours.Value; Task addedTask = new Task(id, taskType, ownerProject.ID, dtpStartDate.Value, dtpDueDate.Value, txtTitle.Text, actualWork, isFinished); try { int taskID = Program.dbms.AddTask(addedTask); List <int> lst = GetSelectedEmployees(); if (lst == null) { return; } Program.dbms.SetEmployeesOnTask(taskID, lst); ReloadTasksList(); } catch (Exception ex) { MessageBox.Show("Unable to add to database: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (currentMode == EntryMode.Edit) { try { Task cur = ((Task)trvTasks.SelectedNode.Tag); int taskID = cur.ID; cur.DueDate = dtpDueDate.Value; cur.StartingDate = dtpStartDate.Value; cur.Title = txtTitle.Text; cur.ActualWorkingHours = numActualWorkingHours.Value == 0 ? null : (int?)numActualWorkingHours.Value; cur.IsFinished = chkIsFinished.Checked; cur.TaskType = (chkIsMilestone.Checked ? Task.TypeOfTask.Milestone : Task.TypeOfTask.Task); Program.dbms.UpdateTask(cur); Program.dbms.UnselectAllEmployeesFromTask(taskID); List <int> lst = GetSelectedEmployees(); if (lst == null) { return; } Program.dbms.SetEmployeesOnTask(taskID, lst); ReloadTasksList(); } catch (Exception ex) { MessageBox.Show("Unable to add to database: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Something went wrong!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error); } }