Refactoring code moving to methods in PersonalTimesheetsPeriodicityEnum

FEA: ItEr77S07PersonalTimesheetsPeriodictyConfiguration
This commit is contained in:
Manuel Rego Casasnovas 2012-08-28 09:34:38 +02:00
parent 57763f233f
commit 33de9de661
2 changed files with 99 additions and 77 deletions

View file

@ -21,6 +21,10 @@ package org.libreplan.business.common.entities;
import static org.libreplan.business.i18n.I18nHelper._; import static org.libreplan.business.i18n.I18nHelper._;
import org.joda.time.LocalDate;
import org.joda.time.Months;
import org.joda.time.Weeks;
/** /**
* Different values for personal timesheets periodicity. * Different values for personal timesheets periodicity.
* *
@ -28,9 +32,86 @@ import static org.libreplan.business.i18n.I18nHelper._;
*/ */
public enum PersonalTimesheetsPeriodicityEnum { public enum PersonalTimesheetsPeriodicityEnum {
MONTHLY(_("Monthly")), MONTHLY(_("Monthly")) {
TWICE_MONTHLY(_("Twice-monthly")), @Override
WEEKLY(_("Weekly")); public LocalDate getStart(LocalDate date) {
return date.dayOfMonth().withMinimumValue();
}
@Override
public LocalDate getEnd(LocalDate date) {
return date.dayOfMonth().withMaximumValue();
}
@Override
public int getItemsBetween(LocalDate start, LocalDate end) {
return Months.monthsBetween(start, end).getMonths();
}
@Override
public LocalDate getDateForItemFromDate(int item, LocalDate fromDate) {
return fromDate.plusMonths(item);
}
},
TWICE_MONTHLY(_("Twice-monthly")) {
@Override
public LocalDate getStart(LocalDate date) {
if (date.getDayOfMonth() <= 15) {
return date.dayOfMonth().withMinimumValue();
} else {
return date.dayOfMonth().withMinimumValue().plusDays(15);
}
}
@Override
public LocalDate getEnd(LocalDate date) {
if (date.getDayOfMonth() <= 15) {
return date.dayOfMonth().withMinimumValue().plusDays(14);
} else {
return date.dayOfMonth().withMaximumValue();
}
}
@Override
public int getItemsBetween(LocalDate start, LocalDate end) {
return Months.monthsBetween(start, end).getMonths() * 2;
}
@Override
public LocalDate getDateForItemFromDate(int item, LocalDate fromDate) {
int months = (item % 2 == 0) ? (item / 2) : ((item - 1) / 2);
LocalDate date = fromDate.plusMonths(months);
if (item % 2 != 0) {
if (date.getDayOfMonth() <= 15) {
date = date.dayOfMonth().withMinimumValue().plusDays(15);
} else {
date = date.plusMonths(1).dayOfMonth().withMinimumValue();
}
}
return date;
}
},
WEEKLY(_("Weekly")) {
@Override
public LocalDate getStart(LocalDate date) {
return date.dayOfWeek().withMinimumValue();
}
@Override
public LocalDate getEnd(LocalDate date) {
return date.dayOfWeek().withMaximumValue();
}
@Override
public int getItemsBetween(LocalDate start, LocalDate end) {
return Weeks.weeksBetween(start, end).getWeeks();
}
@Override
public LocalDate getDateForItemFromDate(int item, LocalDate fromDate) {
return fromDate.plusWeeks(item);
}
};
private String name; private String name;
@ -42,4 +123,13 @@ public enum PersonalTimesheetsPeriodicityEnum {
return name; return name;
} }
public abstract LocalDate getStart(LocalDate date);
public abstract LocalDate getEnd(LocalDate date);
public abstract int getItemsBetween(LocalDate start, LocalDate end);
public abstract LocalDate getDateForItemFromDate(int item,
LocalDate fromDate);
} }

View file

@ -24,8 +24,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.joda.time.Months;
import org.joda.time.Weeks;
import org.libreplan.business.common.daos.IConfigurationDAO; import org.libreplan.business.common.daos.IConfigurationDAO;
import org.libreplan.business.common.entities.PersonalTimesheetsPeriodicityEnum; import org.libreplan.business.common.entities.PersonalTimesheetsPeriodicityEnum;
import org.libreplan.business.orders.entities.OrderElement; import org.libreplan.business.orders.entities.OrderElement;
@ -81,62 +79,16 @@ public class MonthlyTimesheetsAreaModel implements IMonthlyTimesheetsAreaModel {
private List<MonthlyTimesheetDTO> getMonthlyTimesheets(Resource resource, private List<MonthlyTimesheetDTO> getMonthlyTimesheets(Resource resource,
LocalDate start, LocalDate end, LocalDate start, LocalDate end,
PersonalTimesheetsPeriodicityEnum periodicity) { PersonalTimesheetsPeriodicityEnum periodicity) {
int items; start = periodicity.getStart(start);
switch (periodicity) { end = periodicity.getEnd(end);
case WEEKLY: int items = periodicity.getItemsBetween(start, end);
start = start.dayOfWeek().withMinimumValue();
end = end.dayOfWeek().withMaximumValue();
items = Weeks.weeksBetween(start, end).getWeeks();
break;
case TWICE_MONTHLY:
if (start.getDayOfMonth() <= 15) {
start = start.dayOfMonth().withMinimumValue();
} else {
start = start.dayOfMonth().withMinimumValue().plusDays(15);
}
if (end.getDayOfMonth() <= 15) {
end = end.dayOfMonth().withMinimumValue().plusDays(14);
} else {
end = end.dayOfMonth().withMaximumValue();
}
items = Months.monthsBetween(start, end).getMonths() * 2;
break;
case MONTHLY:
default:
start = start.dayOfMonth().withMinimumValue();
end = end.dayOfMonth().withMaximumValue();
items = Months.monthsBetween(start, end).getMonths();
break;
}
List<MonthlyTimesheetDTO> result = new ArrayList<MonthlyTimesheetDTO>(); List<MonthlyTimesheetDTO> result = new ArrayList<MonthlyTimesheetDTO>();
// In decreasing order to provide a list sorted with the more recent // In decreasing order to provide a list sorted with the more recent
// monthly timesheets at the beginning // monthly timesheets at the beginning
for (int i = items; i >= 0; i--) { for (int i = items; i >= 0; i--) {
LocalDate date; LocalDate date = periodicity.getDateForItemFromDate(i, start);
switch (periodicity) {
case WEEKLY:
date = start.plusWeeks(i);
break;
case TWICE_MONTHLY:
int months = (i % 2 == 0) ? (i / 2) : ((i - 1) / 2);
date = start.plusMonths(months);
if (i % 2 != 0) {
if (date.getDayOfMonth() <= 15) {
date = date.dayOfMonth().withMinimumValue()
.plusDays(15);
} else {
date = date.plusMonths(1).dayOfMonth()
.withMinimumValue();
}
}
break;
case MONTHLY:
default:
date = start.plusMonths(i);
break;
}
WorkReport workReport = getWorkReport(resource, date); WorkReport workReport = getWorkReport(resource, date);
@ -164,28 +116,8 @@ public class MonthlyTimesheetsAreaModel implements IMonthlyTimesheetsAreaModel {
private EffortDuration getResourceCapcity(Resource resource, private EffortDuration getResourceCapcity(Resource resource,
LocalDate date, PersonalTimesheetsPeriodicityEnum periodicity) { LocalDate date, PersonalTimesheetsPeriodicityEnum periodicity) {
LocalDate start; LocalDate start = periodicity.getStart(date);
LocalDate end; LocalDate end = periodicity.getEnd(date);
switch (periodicity) {
case WEEKLY:
start = date.dayOfWeek().withMinimumValue();
end = date.dayOfWeek().withMaximumValue();
break;
case TWICE_MONTHLY:
if (date.getDayOfMonth() <= 15) {
start = date.dayOfMonth().withMinimumValue();
end = start.plusDays(14);
} else {
start = date.dayOfMonth().withMinimumValue().plusDays(15);
end = date.dayOfMonth().withMaximumValue();
}
break;
default:
case MONTHLY:
start = date.dayOfMonth().withMinimumValue();
end = date.dayOfMonth().withMaximumValue();
break;
}
EffortDuration capacity = EffortDuration.zero(); EffortDuration capacity = EffortDuration.zero();
for (LocalDate day = start; day.compareTo(end) <= 0; day = day for (LocalDate day = start; day.compareTo(end) <= 0; day = day