From b0ff416c65fefe0e24051e00fea3c0a3a2aa2888 Mon Sep 17 00:00:00 2001 From: Manuel Rego Casasnovas Date: Tue, 1 Dec 2009 12:43:00 +0100 Subject: [PATCH] ItEr37S17CUCalculoValorGanadoItEr36S19: Added method "calculatedValueForEveryDay" to ChartFiller class. --- .../web/planner/chart/ChartFiller.java | 51 +++++++++++ .../web/planner/chart/ChartFillerTest.java | 89 +++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 navalplanner-webapp/src/test/java/org/navalplanner/web/planner/chart/ChartFillerTest.java diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/chart/ChartFiller.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/chart/ChartFiller.java index cac5682b3..9d203a908 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/chart/ChartFiller.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/planner/chart/ChartFiller.java @@ -23,6 +23,7 @@ package org.navalplanner.web.planner.chart; import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.Collection; import java.util.Collections; import java.util.Date; @@ -38,6 +39,7 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.joda.time.Days; import org.joda.time.LocalDate; import org.navalplanner.business.calendars.entities.ResourceCalendar; import org.navalplanner.business.calendars.entities.SameWorkHoursEveryDay; @@ -480,4 +482,53 @@ public abstract class ChartFiller implements IChartFiller { return result; } + protected SortedMap calculatedValueForEveryDay( + SortedMap map, Date start, Date finish) { + return calculatedValueForEveryDay(map, new LocalDate(start), + new LocalDate(finish)); + } + + protected SortedMap calculatedValueForEveryDay( + SortedMap map, LocalDate start, + LocalDate finish) { + SortedMap result = new TreeMap(); + + LocalDate previousDay = start; + BigDecimal previousValue = BigDecimal.ZERO; + + for (LocalDate day : map.keySet()) { + BigDecimal value = map.get(day); + fillValues(result, previousDay, day, previousValue, value); + + previousDay = day; + previousValue = value; + } + + if (previousDay.compareTo(finish) < 0) { + fillValues(result, previousDay, finish, previousValue, + previousValue); + } + + return result; + } + + private void fillValues(SortedMap map, + LocalDate firstDay, LocalDate lastDay, BigDecimal firstValue, + BigDecimal lastValue) { + + Integer days = Days.daysBetween(firstDay, lastDay).getDays(); + if (days > 0) { + BigDecimal ammount = lastValue.subtract(firstValue); + BigDecimal ammountPerDay = ammount.setScale(2).divide( + new BigDecimal(days), RoundingMode.DOWN); + + BigDecimal value = firstValue.setScale(2); + for (LocalDate day = firstDay; day.compareTo(lastDay) <= 0; day = day + .plusDays(1)) { + map.put(day, value); + value = value.add(ammountPerDay); + } + } + } + } diff --git a/navalplanner-webapp/src/test/java/org/navalplanner/web/planner/chart/ChartFillerTest.java b/navalplanner-webapp/src/test/java/org/navalplanner/web/planner/chart/ChartFillerTest.java new file mode 100644 index 000000000..73153529b --- /dev/null +++ b/navalplanner-webapp/src/test/java/org/navalplanner/web/planner/chart/ChartFillerTest.java @@ -0,0 +1,89 @@ +/* + * This file is part of ###PROJECT_NAME### + * + * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e + * Desenvolvemento Tecnolóxico de Galicia + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.navalplanner.web.planner.chart; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.math.BigDecimal; +import java.util.SortedMap; +import java.util.TreeMap; + +import org.joda.time.LocalDate; +import org.junit.Test; +import org.zkforge.timeplot.Timeplot; +import org.zkoss.ganttz.util.Interval; + +/** + * Tests for {@link ChartFiller} + * + * @author Manuel Rego Casasnovas + */ +public class ChartFillerTest { + + private ChartFiller chartFiller = new ChartFiller() { + + @Override + public void fillChart(Timeplot chart, Interval interval, Integer size) { + } + + }; + + private static final LocalDate START_DAY = new LocalDate(2009, 12, 1); + private static final LocalDate FIRST_DAY = new LocalDate(2009, 12, 5); + private static final LocalDate LAST_DAY = new LocalDate(2009, 12, 15); + private static final LocalDate FINISH_DAY = new LocalDate(2009, 12, 30); + + private SortedMap givenExampleMap() { + SortedMap result = new TreeMap(); + + result.put(FIRST_DAY, new BigDecimal(100)); + result.put(LAST_DAY, new BigDecimal(150)); + + return result; + } + + @Test + public void testCalculatedValueForEveryDay() { + SortedMap result = chartFiller + .calculatedValueForEveryDay(givenExampleMap(), START_DAY, + FINISH_DAY); + + assertThat(result.get(START_DAY), equalTo(BigDecimal.ZERO.setScale(2))); + assertThat(result.get(START_DAY.plusDays(1)), + equalTo(new BigDecimal(25).setScale(2))); + + assertThat(result.get(FIRST_DAY), equalTo(new BigDecimal(100) + .setScale(2))); + assertThat(result.get(FIRST_DAY.plusDays(1)), equalTo(new BigDecimal( + 105).setScale(2))); + + assertThat(result.get(LAST_DAY), equalTo(new BigDecimal(150) + .setScale(2))); + assertThat(result.get(LAST_DAY.plusDays(1)), + equalTo(new BigDecimal(150).setScale(2))); + + assertThat(result.get(FINISH_DAY), equalTo(new BigDecimal(150) + .setScale(2))); + System.out.println("HERE"); + } + +}