diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/planner/daos/DayAssignmentDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/planner/daos/DayAssignmentDAO.java index 15a1aba9f..e8b33b8f1 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/planner/daos/DayAssignmentDAO.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/planner/daos/DayAssignmentDAO.java @@ -20,7 +20,8 @@ package org.navalplanner.business.planner.daos; -import java.util.HashSet; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Set; import java.util.SortedMap; @@ -31,6 +32,7 @@ import org.hibernate.criterion.Projections; import org.hibernate.criterion.Property; import org.hibernate.criterion.Restrictions; import org.joda.time.LocalDate; +import org.navalplanner.business.common.BaseEntity; import org.navalplanner.business.common.daos.GenericDAOHibernate; import org.navalplanner.business.planner.entities.DayAssignment; import org.navalplanner.business.planner.entities.GenericDayAssignment; @@ -64,24 +66,10 @@ public class DayAssignmentDAO extends GenericDAOHibernate Set> resourceAllocations = taskElement .getResourceAllocations(); - Set genericResourceAllocations = new HashSet(); - Set specificResourceAllocations = new HashSet(); - - for (ResourceAllocation resourceAllocation : resourceAllocations) { - if (resourceAllocation instanceof GenericResourceAllocation) { - Long id = resourceAllocation.getId(); - if (id != null) { - genericResourceAllocations - .add((GenericResourceAllocation) resourceAllocation); - } - } else if (resourceAllocation instanceof SpecificResourceAllocation) { - Long id = resourceAllocation.getId(); - if (id != null) { - specificResourceAllocations - .add((SpecificResourceAllocation) resourceAllocation); - } - } - } + List genericResourceAllocations = withId(getOfType( + GenericResourceAllocation.class, resourceAllocations)); + List specificResourceAllocations = withId(getOfType( + SpecificResourceAllocation.class, resourceAllocations)); if (!genericResourceAllocations.isEmpty()) { Criteria criteria = getSession().createCriteria( @@ -129,4 +117,25 @@ public class DayAssignmentDAO extends GenericDAOHibernate return result; } + private List withId(List elements) { + List result = new ArrayList(); + for (T element : elements) { + if (element.getId() != null) { + result.add(element); + } + } + return result; + } + + private > List getOfType(Class type, + Collection> resourceAllocations) { + List result = new ArrayList(); + for (ResourceAllocation allocation : resourceAllocations) { + if (type.isInstance(allocation)) { + result.add(type.cast(allocation)); + } + } + return result; + } + }