public ProjectData AddAssignment(AssignmentId assignmentId, TaskId taskId, ResourceId resourceId) { Debug.Assert(!assignmentId.IsDefault); Debug.Assert(!taskId.IsDefault); Debug.Assert(!resourceId.IsDefault); var project = this; var numberOfExistingAssignments = project.GetAssignments(taskId).Count(); // Add assignment var assignment = AssignmentData.Create(assignmentId, taskId, resourceId); var taskAssignments = project.Get(TaskFields.Assignments, taskId).Add(assignmentId); var resourceAssignments = project.Get(ResourceFields.Assignments, resourceId).Add(assignmentId); project = project.WithAssignmentMap(_assignmentMap.Add(assignment.Id, assignment)) .Set(TaskFields.Assignments, taskId, taskAssignments) .Set(ResourceFields.Assignments, resourceId, resourceAssignments); // Initialize Assignment.Work and update Task.Work if (numberOfExistingAssignments == 0) { var work = project.Get(TaskFields.Work, taskId); if (work == TimeSpan.Zero) { work = project.Get(TaskFields.Duration, taskId); } project = project.SetRaw(TaskFields.Work, taskId, work) .SetRaw(AssignmentFields.Work, assignmentId, work); } else { var taskWork = project.Get(TaskFields.Work, taskId); var newTaskWorkHours = taskWork.TotalHours / numberOfExistingAssignments * (numberOfExistingAssignments + 1); var newAssignmentWorkHours = newTaskWorkHours - taskWork.TotalHours; var newTaskWork = TimeSpan.FromHours(newTaskWorkHours); var newAssignmentWork = TimeSpan.FromHours(newAssignmentWorkHours); project = project.SetRaw(TaskFields.Work, taskId, newTaskWork) .SetRaw(AssignmentFields.Work, assignmentId, newAssignmentWork); } // Initialize Assignment.TaskName and Assignment.ResourceName var taskName = project.Get(TaskFields.Name, taskId); var resourceName = project.Get(ResourceFields.Name, resourceId); project = project.SetRaw(AssignmentFields.TaskName, assignmentId, taskName) .SetRaw(AssignmentFields.ResourceName, assignmentId, resourceName); // Update Task.ResourceNames project = project.Reset(TaskFields.ResourceNames, taskId) .Reset(TaskFields.ResourceInitials, taskId); return(project); }
internal static AssignmentChanges Compute(AssignmentData oldAssignment, AssignmentData newAssignment) { Debug.Assert(oldAssignment != newAssignment); Debug.Assert(oldAssignment.Id == newAssignment.Id); var result = new List <FieldChange <AssignmentField> >(); foreach (var field in oldAssignment.SetFields) { var oldValue = oldAssignment.GetValue(field); if (!newAssignment.HasValue(field)) { result.Add(new FieldChange <AssignmentField>(field, oldValue, field.DefaultValue)); } else { var newValue = newAssignment.GetValue(field); if (!Equals(oldValue, newValue)) { result.Add(new FieldChange <AssignmentField>(field, oldValue, newValue)); } } } foreach (var field in newAssignment.SetFields) { var newValue = newAssignment.GetValue(field); if (!oldAssignment.HasValue(field)) { result.Add(new FieldChange <AssignmentField>(field, field.DefaultValue, newValue)); } } return(new AssignmentChanges(oldAssignment.Id, result.ToImmutableArray())); }
internal Assignment(Project project, AssignmentData data) { _project = project; _data = data; }
public ProjectData AddAssignment(AssignmentData assignment) { Debug.Assert(assignment != null); return(WithAssignmentMap(_assignmentMap.Add(assignment.Id, assignment))); }