From e6fbd02e4b8829cf9f22bcde3b9d018c1e89d889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Gonz=C3=A1lez=20Fern=C3=A1ndez?= Date: Wed, 28 Oct 2009 12:51:35 +0100 Subject: [PATCH] Last methods extracting and reordering of methods to improve readability --- .../planner/daos/DayAssignmentDAO.java | 100 ++++++++++-------- 1 file changed, 54 insertions(+), 46 deletions(-) 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 7bb7d8288..4c39e18e2 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 @@ -24,7 +24,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; @@ -62,59 +61,33 @@ public class DayAssignmentDAO extends GenericDAOHibernate @Transactional(readOnly = true) public SortedMap getHoursAssignedByDayFor( TaskElement taskElement) { + return calculateHoursAssignedByDayFor(taskElement + .getResourceAllocations()); + } + + private SortedMap calculateHoursAssignedByDayFor( + Collection> resourceAllocations) { SortedMap result = new TreeMap(); + addResultsFromGeneric(result, resourceAllocations); + addResultsFromSpecific(result, resourceAllocations); + return result; + } - Set> resourceAllocations = taskElement - .getResourceAllocations(); - + private void addResultsFromGeneric( + SortedMap result, + Collection> resourceAllocations) { List genericResourceAllocations = withId(getOfType( GenericResourceAllocation.class, resourceAllocations)); + addToResult(result, queryHoursByDay(GenericDayAssignment.class, + "genericResourceAllocation", genericResourceAllocations)); + } + private void addResultsFromSpecific(SortedMap result, + Collection> resourceAllocations) { List specificResourceAllocations = withId(getOfType( SpecificResourceAllocation.class, resourceAllocations)); - - addHoursByDate(result, queryHoursByDay(GenericDayAssignment.class, - "genericResourceAllocation", genericResourceAllocations)); - addHoursByDate(result, queryHoursByDay(SpecificDayAssignment.class, + addToResult(result, queryHoursByDay(SpecificDayAssignment.class, "specificResourceAllocation", specificResourceAllocations)); - - return result; - } - - @SuppressWarnings("unchecked") - private > List queryHoursByDay( - Class classBeingSearched, String allocationRelationship, - List allocations) { - if (allocations.isEmpty()) { - return Collections.emptyList(); - } - Criteria criteria = getSession().createCriteria(classBeingSearched); - criteria.add(Restrictions.in(allocationRelationship, allocations)); - - criteria.setProjection(Projections.projectionList().add( - Property.forName("day").group()).add(Projections.sum("hours"))); - List list = criteria.list(); - return list; - } - - private void addHoursByDate(SortedMap result, - List list) { - for (Object[] object : list) { - LocalDate date = (LocalDate) object[0]; - Integer hours = (Integer) object[1]; - int current = result.get(date) != null ? result.get(date) : 0; - result.put(date, current + hours); - } - } - - 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, @@ -128,4 +101,39 @@ 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; + } + + @SuppressWarnings("unchecked") + private > List queryHoursByDay( + Class classBeingSearched, String allocationRelationship, + List allocations) { + if (allocations.isEmpty()) { + return Collections.emptyList(); + } + Criteria criteria = getSession().createCriteria(classBeingSearched); + criteria.add(Restrictions.in(allocationRelationship, allocations)); + criteria.setProjection(Projections.projectionList().add( + Property.forName("day").group()).add(Projections.sum("hours"))); + List list = criteria.list(); + return list; + } + + private void addToResult(SortedMap result, + List list) { + for (Object[] object : list) { + LocalDate date = (LocalDate) object[0]; + Integer hours = (Integer) object[1]; + int current = result.get(date) != null ? result.get(date) : 0; + result.put(date, current + hours); + } + } + }