ItEr19S08CUCreacionProxectoPlanificacionItEr18S08: Adding detach method. It severs the task from the other tasks.

This commit is contained in:
Óscar González Fernández 2009-07-24 20:05:00 +02:00 committed by Javier Moran Rua
parent d15ab1f3f7
commit 5f9c335563
4 changed files with 104 additions and 1 deletions

View file

@ -136,5 +136,4 @@ public class Task extends TaskElement {
}
return result;
}
}

View file

@ -148,7 +148,75 @@ public abstract class TaskElement {
public abstract List<TaskElement> getChildren();
protected void setParent(TaskGroup taskGroup) {
this.parent = taskGroup;
}
public void detach() {
detachDependencies();
detachFromParent();
}
private void detachFromParent() {
if (parent != null) {
parent.remove(this);
}
}
private void removeDependenciesWithOrigin(TaskElement t) {
List<Dependency> dependenciesToRemove = getDependenciesWithOrigin(t);
dependenciesWithThisDestination.removeAll(dependenciesToRemove);
}
private void removeDependenciesWithDestination(TaskElement t) {
List<Dependency> dependenciesToRemove = getDependenciesWithDestination(t);
dependenciesWithThisOrigin.removeAll(dependenciesToRemove);
}
private List<Dependency> getDependenciesWithDestination(TaskElement t) {
ArrayList<Dependency> result = new ArrayList<Dependency>();
for (Dependency dependency : dependenciesWithThisOrigin) {
if (dependency.getDestination().equals(t)) {
result.add(dependency);
}
}
return result;
}
private List<Dependency> getDependenciesWithOrigin(TaskElement t) {
ArrayList<Dependency> result = new ArrayList<Dependency>();
for (Dependency dependency : dependenciesWithThisDestination) {
if (dependency.getOrigin().equals(t)) {
result.add(dependency);
}
}
return result;
}
private void detachDependencies() {
detachOutcomingDependencies();
detachIncomingDependencies();
}
private void detachIncomingDependencies() {
Set<TaskElement> tasksToNotify = new HashSet<TaskElement>();
for (Dependency dependency : dependenciesWithThisDestination) {
tasksToNotify.add(dependency.getOrigin());
}
for (TaskElement taskElement : tasksToNotify) {
taskElement.removeDependenciesWithDestination(this);
}
}
private void detachOutcomingDependencies() {
Set<TaskElement> tasksToNotify = new HashSet<TaskElement>();
for (Dependency dependency : dependenciesWithThisOrigin) {
tasksToNotify.add(dependency.getDestination());
}
for (TaskElement taskElement : tasksToNotify) {
taskElement.removeDependenciesWithOrigin(this);
}
}
}

View file

@ -34,4 +34,8 @@ public class TaskGroup extends TaskElement {
return getOrderElement().getWorkHours();
}
public void remove(TaskElement taskElement) {
taskElements.remove(taskElement);
}
}

View file

@ -197,4 +197,36 @@ public class TaskElementTest {
}
}
}
@Test
public void detachRemovesDependenciesFromRelatedTasks() {
HoursGroup hoursGroup = new HoursGroup();
Task taskToDetach = Task.createTask(hoursGroup);
Task sourceDependencyTask = Task.createTask(new HoursGroup());
Task destinationDependencyTask = Task.createTask(new HoursGroup());
taskToDetach.setName("prueba");
taskToDetach.setNotes("blabla");
taskToDetach.setStartDate(new Date());
Dependency.createDependency(sourceDependencyTask, taskToDetach,
Type.END_START);
Dependency.createDependency(taskToDetach,
destinationDependencyTask, Type.END_START);
taskToDetach.detach();
assertThat(sourceDependencyTask.getDependenciesWithThisOrigin().size(),
equalTo(0));
assertThat(destinationDependencyTask
.getDependenciesWithThisDestination().size(), equalTo(0));
}
@Test
public void detachRemovesTaskFromParent() {
TaskGroup parent = new TaskGroup();
HoursGroup hoursGroup = new HoursGroup();
Task child = Task.createTask(hoursGroup);
Task anotherChild = Task.createTask(hoursGroup);
parent.addTaskElement(child);
parent.addTaskElement(anotherChild);
child.detach();
assertThat(parent.getChildren().size(), equalTo(1));
}
}