ItEr60S19TimeUnitDataType: Introduce TaskDate in TaskElement.

Change some query methods so they keep working.
This commit is contained in:
Óscar González Fernández 2010-08-17 13:17:44 +02:00
parent 5b53e3187a
commit 794e6b5f1f
8 changed files with 99 additions and 40 deletions

View file

@ -46,12 +46,13 @@ public interface IResourceAllocationDAO extends
List<Resource> resources);
List<ResourceAllocation<?>> findAllocationsRelatedToAnyOf(
List<Resource> resources, Date intervalFilterStartDate, Date intervalFilterEndDate);
List<Resource> resources, LocalDate intervalFilterStartDate,
LocalDate intervalFilterEndDate);
List<ResourceAllocation<?>> findAllocationsRelatedTo(Resource resource);
List<ResourceAllocation<?>> findAllocationsRelatedTo(Resource resource,
Date intervalFilterStartDate, Date intervalFilterEndDate);
LocalDate intervalFilterStartDate, LocalDate intervalFilterEndDate);
Map<Criterion, List<GenericResourceAllocation>> findGenericAllocationsByCriterion();

View file

@ -66,7 +66,8 @@ public class ResourceAllocationDAO extends
@Override
public List<ResourceAllocation<?>> findAllocationsRelatedToAnyOf(
List<Resource> resources, Date intervalFilterStartDate, Date intervalFilterEndDate) {
List<Resource> resources, LocalDate intervalFilterStartDate,
LocalDate intervalFilterEndDate) {
List<ResourceAllocation<?>> result = new ArrayList<ResourceAllocation<?>>();
result.addAll(findSpecificAllocationsRelatedTo(resources, intervalFilterStartDate, intervalFilterEndDate));
result.addAll(findGenericAllocationsFor(resources, intervalFilterStartDate, intervalFilterEndDate));
@ -74,7 +75,9 @@ public class ResourceAllocationDAO extends
}
@SuppressWarnings("unchecked")
private List<GenericResourceAllocation> findGenericAllocationsFor(List<Resource> resources, Date intervalFilterStartDate, Date intervalFilterEndDate) {
private List<GenericResourceAllocation> findGenericAllocationsFor(
List<Resource> resources, LocalDate intervalFilterStartDate,
LocalDate intervalFilterEndDate) {
if(resources.isEmpty()) {
return new ArrayList<GenericResourceAllocation>();
}
@ -92,7 +95,8 @@ public class ResourceAllocationDAO extends
@SuppressWarnings("unchecked")
private List<SpecificResourceAllocation> findSpecificAllocationsRelatedTo(
List<Resource> resources, Date intervalFilterStartDate, Date intervalFilterEndDate) {
List<Resource> resources, LocalDate intervalFilterStartDate,
LocalDate intervalFilterEndDate) {
if(resources.isEmpty()) {
return new ArrayList<SpecificResourceAllocation>();
}
@ -106,14 +110,16 @@ public class ResourceAllocationDAO extends
}
private void filterByDatesIfApplyable(Criteria criteria,
Date intervalFilterStartDate, Date intervalFilterEndDate) {
LocalDate intervalFilterStartDate, LocalDate intervalFilterEndDate) {
if(intervalFilterStartDate != null || intervalFilterEndDate != null) {
Criteria dateCriteria = criteria.createCriteria("task");
if(intervalFilterEndDate != null) {
dateCriteria.add(Restrictions.le("startDate", intervalFilterEndDate));
dateCriteria.add(Restrictions.le("startDate.date",
intervalFilterEndDate));
}
if(intervalFilterStartDate != null) {
dateCriteria.add(Restrictions.ge("endDate", intervalFilterStartDate));
dateCriteria.add(Restrictions.ge("endDate.date",
intervalFilterStartDate));
}
}
}
@ -127,7 +133,8 @@ public class ResourceAllocationDAO extends
@Override
public List<ResourceAllocation<?>> findAllocationsRelatedTo(
Resource resource, Date intervalFilterStartDate, Date intervalFilterEndDate) {
Resource resource, LocalDate intervalFilterStartDate,
LocalDate intervalFilterEndDate) {
return stripAllocationsWithoutAssignations(findAllocationsRelatedToAnyOf(Arrays
.asList(resource), intervalFilterStartDate, intervalFilterEndDate));
}

View file

@ -25,6 +25,7 @@ import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.joda.time.LocalDate;
import org.navalplanner.business.common.daos.GenericDAOHibernate;
import org.navalplanner.business.planner.entities.GenericDayAssignment;
import org.navalplanner.business.planner.entities.SpecificDayAssignment;
@ -66,10 +67,12 @@ public class TaskElementDAO extends GenericDAOHibernate<TaskElement, Long>
public List<TaskElement> listFilteredByDate(Date start, Date end) {
Criteria criteria = getSession().createCriteria(TaskElement.class);
if(start != null) {
criteria.add(Restrictions.ge("endDate", start));
criteria.add(Restrictions.ge("endDate.date",
LocalDate.fromDateFields(start)));
}
if(end != null) {
criteria.add(Restrictions.le("startDate", end));
criteria.add(Restrictions.le("startDate.date",
LocalDate.fromDateFields(end)));
}
return criteria.list();
}

View file

@ -34,6 +34,7 @@ import java.util.TreeMap;
import org.apache.commons.lang.Validate;
import org.hibernate.validator.NotNull;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.navalplanner.business.calendars.entities.BaseCalendar;
import org.navalplanner.business.common.BaseEntity;
@ -43,7 +44,9 @@ import org.navalplanner.business.planner.entities.Dependency.Type;
import org.navalplanner.business.scenarios.entities.Scenario;
import org.navalplanner.business.util.deepcopy.OnCopy;
import org.navalplanner.business.util.deepcopy.Strategy;
import org.navalplanner.business.workingday.EffortDuration;
import org.navalplanner.business.workingday.ResourcesPerDay;
import org.navalplanner.business.workingday.TaskDate;
/**
* @author Óscar González Fernández <ogonzalez@igalia.com>
@ -112,9 +115,9 @@ public abstract class TaskElement extends BaseEntity {
@OnCopy(Strategy.SHARE)
private IDatesInterceptor datesInterceptor = EMPTY_INTERCEPTOR;
private Date startDate;
private TaskDate startDate;
private Date endDate;
private TaskDate endDate;
private LocalDate deadline;
@ -163,7 +166,7 @@ public abstract class TaskElement extends BaseEntity {
protected void copyPropertiesFrom(TaskElement task) {
this.name = task.getName();
this.notes = task.getNotes();
this.startDate = task.getStartDate();
this.startDate = task.startDate;
this.taskSource = task.getTaskSource();
}
@ -226,16 +229,26 @@ public abstract class TaskElement extends BaseEntity {
@NotNull
public Date getStartDate() {
return startDate != null ? new Date(startDate.getTime()) : null;
return startDate != null ? startDate.getDate().toDateTimeAtStartOfDay()
.toDate() : null;
}
public LocalDate getStartAsLocalDate() {
return startDate == null ? null : startDate.getDate();
}
public LocalDate getEndAsLocalDate() {
return endDate == null ? null : endDate.getDate();
}
public void setStartDate(Date startDate) {
Date previousDate = this.startDate;
Date previousDate = getStartDate();
long previousLenghtMilliseconds = getLengthMilliseconds();
this.startDate = startDate != null ? new Date(startDate.getTime())
this.startDate = startDate != null ? TaskDate.create(
LocalDate.fromDateFields(startDate), EffortDuration.zero())
: null;
datesInterceptor.setStartDate(previousDate, previousLenghtMilliseconds,
this.startDate);
getStartDate());
}
/**
@ -246,29 +259,31 @@ public abstract class TaskElement extends BaseEntity {
if (newStartDate == null) {
return;
}
final boolean sameDay = areSameDay(newStartDate, startDate);
final boolean sameDay = this.startDate.areSameDay(newStartDate);
long durationMilliseconds = getLengthMilliseconds();
setStartDate(newStartDate);
this.endDate = new Date(this.startDate.getTime() + durationMilliseconds);
DateTime newEnd = this.startDate.toDateTimeAtStartOfDay().plus(
durationMilliseconds);
this.endDate = TaskDate.create(newEnd.toLocalDate(),
EffortDuration.zero());
if (!sameDay) {
moveAllocations(scenario);
}
}
private boolean areSameDay(Date one, Date other) {
return new LocalDate(one).equals(new LocalDate(other));
}
protected abstract void moveAllocations(Scenario scenario);
@NotNull
public Date getEndDate() {
return endDate != null ? new Date(endDate.getTime()) : endDate;
return endDate != null ? endDate.toDateTimeAtStartOfDay().toDate()
: null;
}
public void setEndDate(Date endDate) {
long previousLength = getLengthMilliseconds();
this.endDate = endDate != null ? new Date(endDate.getTime()) : null;
this.endDate = endDate != null ? TaskDate.create(
LocalDate.fromDateFields(endDate), EffortDuration.zero())
: null;
datesInterceptor.setLengthMilliseconds(previousLength,
getLengthMilliseconds());
}
@ -277,7 +292,7 @@ public abstract class TaskElement extends BaseEntity {
if (!canBeResized()) {
return;
}
boolean sameDay = areSameDay(this.endDate, endDate);
boolean sameDay = this.endDate.areSameDay(endDate);
setEndDate(endDate);
if (!sameDay) {
moveAllocations(scenario);

View file

@ -11,8 +11,19 @@
<version name="version" access="property" type="long" />
<property name="name" />
<property name="notes"/>
<property name="startDate" type="timestamp" not-null="true"/>
<property name="endDate" type="timestamp" not-null="true"/>
<component name="startDate"
class="org.navalplanner.business.workingday.TaskDate">
<property name="date" column="startDate" not-null="true"
type="org.joda.time.contrib.hibernate.PersistentLocalDate"/>
<property name="effortDuration" column="startDayDuration"
type="org.navalplanner.business.workingday.hibernate.EffortDurationType"/>
</component>
<component name="endDate" class="org.navalplanner.business.workingday.TaskDate">
<property name="date" column="endDate" not-null="true"
type="org.joda.time.contrib.hibernate.PersistentLocalDate" />
<property name="effortDuration" column="endDayDuration"
type="org.navalplanner.business.workingday.hibernate.EffortDurationType"/>
</component>
<property name="deadline" type="org.joda.time.contrib.hibernate.PersistentLocalDate" />
<property name="advancePercentage" column="ADVANCE_PERCENTAGE" />

View file

@ -32,6 +32,7 @@ import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.joda.time.LocalDate;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -265,19 +266,21 @@ public class ResourceAllocationDAOTest {
ResourceAllocation<?> resourceAllocation1 = createValidSpecificResourceAllocation();
resourceAllocationDAO.save(resourceAllocation1);
Date intervalInitDate = resourceAllocation1.getTask().getStartDate();
Date intervalEndDate = resourceAllocation1.getTask().getEndDate();
LocalDate intervalInitDate = resourceAllocation1.getTask()
.getStartAsLocalDate();
LocalDate intervalEndDate = resourceAllocation1.getTask()
.getEndAsLocalDate();
List<Resource> resources = resourceAllocation1.getAssociatedResources();
assertTrue(resourceAllocationDAO.findAllocationsRelatedToAnyOf(resources,
intervalInitDate, intervalEndDate).contains(resourceAllocation1));
intervalEndDate.setDate(intervalInitDate.getDate());
intervalInitDate.setMonth(intervalInitDate.getMonth()-1);
intervalEndDate = intervalInitDate;
intervalInitDate = intervalInitDate.minusMonths(1);
assertTrue(resourceAllocationDAO.findAllocationsRelatedToAnyOf(resources,
intervalInitDate, intervalEndDate).contains(resourceAllocation1));
intervalEndDate.setMonth(intervalEndDate.getMonth()-1);
intervalEndDate = intervalEndDate.minusMonths(1);
assertFalse(resourceAllocationDAO.findAllocationsRelatedToAnyOf(resources,
intervalInitDate, intervalEndDate).contains(resourceAllocation1));

View file

@ -53,13 +53,13 @@ import org.navalplanner.business.orders.entities.OrderLineGroup;
import org.navalplanner.business.orders.entities.SchedulingDataForVersion;
import org.navalplanner.business.orders.entities.TaskSource;
import org.navalplanner.business.planner.entities.Dependency;
import org.navalplanner.business.planner.entities.Dependency.Type;
import org.navalplanner.business.planner.entities.StartConstraintType;
import org.navalplanner.business.planner.entities.Task;
import org.navalplanner.business.planner.entities.TaskElement;
import org.navalplanner.business.planner.entities.TaskGroup;
import org.navalplanner.business.planner.entities.TaskMilestone;
import org.navalplanner.business.planner.entities.TaskStartConstraint;
import org.navalplanner.business.planner.entities.Dependency.Type;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@ -107,12 +107,17 @@ public class TaskElementTest {
}
@Test
public void taskElementHasStartDateProperty() {
public void taskElementHasStartDatePropertyAndItIsRoundedToTheStartOfTheDay() {
Date now = new Date();
task.setStartDate(now);
assertThat(task.getStartDate(), equalTo(now));
assertThat(task.getStartDate(), equalTo(toStartOfDay(now)));
task.setEndDate(now);
assertThat(task.getEndDate(), equalTo(now));
assertThat(task.getEndDate(), equalTo(toStartOfDay(now)));
}
private static Date toStartOfDay(Date date) {
return LocalDate.fromDateFields(date)
.toDateTimeAtStartOfDay().toDate();
}
@Test

View file

@ -32,8 +32,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Set;
import org.joda.time.LocalDate;
import org.navalplanner.business.calendars.daos.IBaseCalendarDAO;
@ -574,7 +574,9 @@ public class ResourceLoadModel implements IResourceLoadModel {
private LoadTimeLine buildGroup(Resource resource) {
List<ResourceAllocation<?>> sortedByStartDate = ResourceAllocation
.sortedByStartDate(resourceAllocationDAO
.findAllocationsRelatedTo(resource, initDateFilter, endDateFilter));
.findAllocationsRelatedTo(resource,
asLocalDate(initDateFilter),
asLocalDate(endDateFilter)));
TimeLineRole<BaseEntity> role = getCurrentTimeLineRole(resource);
LoadTimeLine result = new LoadTimeLine(buildTimeLine(resource, resource
.getName(), sortedByStartDate, "resource", role),
@ -583,6 +585,18 @@ public class ResourceLoadModel implements IResourceLoadModel {
}
/**
* @param date
* the date to extract the {@link LocalDate} from
* @return <code>null</code> if date is null
*/
private static LocalDate asLocalDate(Date date) {
if (date == null) {
return null;
}
return LocalDate.fromDateFields(date);
}
private List<LoadTimeLine> buildSecondLevel(Resource resource,
List<ResourceAllocation<?>> sortedByStartDate) {
List<LoadTimeLine> result = new ArrayList<LoadTimeLine>();