ItEr10S10CUCreacionDeOrganizacionsDeTraballo:: OneToMany relationships on TaskWork and ProjectWork mapped as lists, since order is important.
This commit is contained in:
parent
18c6864aa8
commit
d8d9b0d4fd
5 changed files with 90 additions and 21 deletions
|
|
@ -2,9 +2,7 @@ package org.navalplanner.business.workorders.entities;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.hibernate.validator.NotEmpty;
|
import org.hibernate.validator.NotEmpty;
|
||||||
import org.hibernate.validator.NotNull;
|
import org.hibernate.validator.NotNull;
|
||||||
|
|
@ -38,7 +36,7 @@ public class ProjectWork {
|
||||||
// TODO turn into a many to one relationship when Customer entity is defined
|
// TODO turn into a many to one relationship when Customer entity is defined
|
||||||
private String customer;
|
private String customer;
|
||||||
|
|
||||||
private Set<TaskWork> taskWorks = new HashSet<TaskWork>();
|
private List<TaskWork> taskWorks = new ArrayList<TaskWork>();
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
package org.navalplanner.business.workorders.entities;
|
package org.navalplanner.business.workorders.entities;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class TaskWorkContainer extends TaskWork {
|
public class TaskWorkContainer extends TaskWork {
|
||||||
|
|
||||||
private Set<TaskWork> children = new HashSet<TaskWork>();
|
private List<TaskWork> children = new ArrayList<TaskWork>();
|
||||||
|
|
||||||
public List<TaskWork> getChildren() {
|
public List<TaskWork> getChildren() {
|
||||||
return new ArrayList<TaskWork>(children);
|
return new ArrayList<TaskWork>(children);
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,11 @@
|
||||||
<property name="description" access="field"></property>
|
<property name="description" access="field"></property>
|
||||||
<property name="responsible" access="field"></property>
|
<property name="responsible" access="field"></property>
|
||||||
<property name="customer" access="field"></property>
|
<property name="customer" access="field"></property>
|
||||||
<set name="taskWorks" access="field" cascade="all-delete-orphan">
|
<list name="taskWorks" access="field" cascade="all-delete-orphan">
|
||||||
<key column="projectId" not-null="false"></key>
|
<key column="projectId" not-null="false"></key>
|
||||||
|
<index column="positionInProject"></index>
|
||||||
<one-to-many class="TaskWork" />
|
<one-to-many class="TaskWork" />
|
||||||
</set>
|
</list>
|
||||||
</class>
|
</class>
|
||||||
|
|
||||||
<class name="TaskWork" abstract="true">
|
<class name="TaskWork" abstract="true">
|
||||||
|
|
@ -33,10 +34,11 @@
|
||||||
|
|
||||||
<joined-subclass name="TaskWorkContainer">
|
<joined-subclass name="TaskWorkContainer">
|
||||||
<key column="TASKWORKID"></key>
|
<key column="TASKWORKID"></key>
|
||||||
<set name="children" access="field" cascade="all-delete-orphan">
|
<list name="children" access="field" cascade="all-delete-orphan">
|
||||||
<key column="parent" not-null="false"></key>
|
<key column="parent" not-null="false"></key>
|
||||||
|
<index column="positionInContainer"></index>
|
||||||
<one-to-many class="TaskWork" />
|
<one-to-many class="TaskWork" />
|
||||||
</set>
|
</list>
|
||||||
</joined-subclass>
|
</joined-subclass>
|
||||||
|
|
||||||
<joined-subclass name="TaskWorkLeaf">
|
<joined-subclass name="TaskWorkLeaf">
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
package org.navalplanner.business.test.workorders.entities;
|
package org.navalplanner.business.test.workorders.entities;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.navalplanner.business.workorders.entities.ProjectWork;
|
import org.navalplanner.business.workorders.entities.ProjectWork;
|
||||||
|
import org.navalplanner.business.workorders.entities.TaskWork;
|
||||||
import org.navalplanner.business.workorders.entities.TaskWorkContainer;
|
import org.navalplanner.business.workorders.entities.TaskWorkContainer;
|
||||||
import org.navalplanner.business.workorders.entities.TaskWorkLeaf;
|
import org.navalplanner.business.workorders.entities.TaskWorkLeaf;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link ProjectWork}. <br />
|
* Tests for {@link ProjectWork}. <br />
|
||||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||||
|
|
@ -23,4 +24,19 @@ public class ProjectWorkTest {
|
||||||
projectWork.add(container);
|
projectWork.add(container);
|
||||||
assertThat(projectWork.getTaskWorks().size(), equalTo(1));
|
assertThat(projectWork.getTaskWorks().size(), equalTo(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPreservesOrder() throws Exception {
|
||||||
|
TaskWorkContainer container = new TaskWorkContainer();
|
||||||
|
|
||||||
|
TaskWorkLeaf[] created = new TaskWorkLeaf[100];
|
||||||
|
for (int i = 0; i < created.length; i++) {
|
||||||
|
created[i] = new TaskWorkLeaf();
|
||||||
|
container.addTask(created[i]);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < created.length; i++) {
|
||||||
|
assertThat(container.getChildren().get(i),
|
||||||
|
equalTo((TaskWork) created[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,5 @@
|
||||||
package org.navalplanner.business.test.workorders.services;
|
package org.navalplanner.business.test.workorders.services;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
|
||||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
|
|
||||||
import static org.navalplanner.business.test.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_TEST_FILE;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -28,6 +20,14 @@ import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
|
||||||
|
import static org.navalplanner.business.test.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_TEST_FILE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link ProjectWork}. <br />
|
* Tests for {@link ProjectWork}. <br />
|
||||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||||
|
|
@ -90,6 +90,61 @@ public class ProjectWorkServiceTest {
|
||||||
assertThat(projectWorkService.find(projectWork.getId()), notNullValue());
|
assertThat(projectWorkService.find(projectWork.getId()), notNullValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@NotTransactional
|
||||||
|
public void testOrderPreserved() throws ValidationException,
|
||||||
|
InstanceNotFoundException {
|
||||||
|
final ProjectWork projectWork = createValidProjectWork();
|
||||||
|
final TaskWork[] containers = new TaskWorkContainer[10];
|
||||||
|
for (int i = 0; i < containers.length; i++) {
|
||||||
|
containers[i] = new TaskWorkContainer();
|
||||||
|
containers[i].setName("bla");
|
||||||
|
projectWork.add(containers[i]);
|
||||||
|
}
|
||||||
|
TaskWorkContainer container = (TaskWorkContainer) containers[0];
|
||||||
|
container.setName("container");
|
||||||
|
final TaskWork[] tasks = new TaskWork[10];
|
||||||
|
for (int i = 0; i < tasks.length; i++) {
|
||||||
|
TaskWorkLeaf leaf = createValidLeaf("bla");
|
||||||
|
tasks[i] = leaf;
|
||||||
|
container.addTask(leaf);
|
||||||
|
}
|
||||||
|
projectWorkService.save(projectWork);
|
||||||
|
projectWorkService.onTransaction(new OnTransaction<Void>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void execute() {
|
||||||
|
try {
|
||||||
|
ProjectWork reloaded = projectWorkService.find(projectWork
|
||||||
|
.getId());
|
||||||
|
List<TaskWork> taskWorks = reloaded.getTaskWorks();
|
||||||
|
for (int i = 0; i < containers.length; i++) {
|
||||||
|
assertThat(taskWorks.get(i).getId(),
|
||||||
|
equalTo(containers[i].getId()));
|
||||||
|
}
|
||||||
|
TaskWorkContainer container = (TaskWorkContainer) reloaded
|
||||||
|
.getTaskWorks().iterator().next();
|
||||||
|
List<TaskWork> children = container.getChildren();
|
||||||
|
for (int i = 0; i < tasks.length; i++) {
|
||||||
|
assertThat(children.get(i).getId(), equalTo(tasks[i]
|
||||||
|
.getId()));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
projectWorkService.remove(projectWork);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TaskWorkLeaf createValidLeaf(String parameter) {
|
||||||
|
TaskWorkLeaf result = new TaskWorkLeaf();
|
||||||
|
result.setName(parameter);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@NotTransactional
|
@NotTransactional
|
||||||
public void testAddingTaskWork() throws Exception {
|
public void testAddingTaskWork() throws Exception {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue