CalendarWeekFactory implements Serializable { public List<CalendarWeek> createWeeks(Integer firstDayOfWeek, Integer userId, Calendar thisMonth) { List<CalendarWeek> calendarWeeks = new ArrayList<>(); thisMonth.setFirstDayOfWeek(firstDayOfWeek); List<LocalDate> bookedDays = getOverviewTimesheet().getBookedDaysMonthOverview(userId, thisMonth); thisMonth.set(Calendar.DAY_OF_MONTH, 1); LocalDate thisMonthLocal = new LocalDate(thisMonth.getTime()); int currentMonth = thisMonth.get(Calendar.MONTH); CalendarWeek week = new CalendarWeek(ElementLocation.FIRST); int weekOfYear = thisMonth.get(Calendar.WEEK_OF_YEAR); week.setWeek(weekOfYear); if (weekOfYear > 51 && currentMonth == Calendar.JANUARY) { week.setYear(thisMonth.get(Calendar.YEAR) - 1); } else { week.setYear(thisMonth.get(Calendar.YEAR)); } week.setWeekStart((Calendar) thisMonth.clone()); DateUtil.dayOfWeekFix(week.getWeekStart()); week.getWeekStart().set(Calendar.DAY_OF_WEEK, firstDayOfWeek); int previousWeek = -1; do { int dayInMonth = thisMonth.get(Calendar.DAY_OF_MONTH); int dayInWeek = thisMonth.get(Calendar.DAY_OF_WEEK); boolean isBooked = bookedDays.contains(thisMonthLocal.withDayOfMonth(dayInMonth)); CalendarDay day = new CalendarDay(dayInMonth, isBooked); week.addDayInWeek(dayInWeek, day); thisMonth.add(Calendar.DAY_OF_MONTH, 1); if (thisMonth.get(Calendar.DAY_OF_WEEK) == firstDayOfWeek) { calendarWeeks.add(week); week = new CalendarWeek(ElementLocation.MIDDLE); week.setWeek(thisMonth.get(Calendar.WEEK_OF_YEAR)); week.setWeekStart((Calendar) thisMonth.clone()); if (previousWeek != -1 && previousWeek > thisMonth.get(Calendar.WEEK_OF_YEAR)) { week.setYear(thisMonth.get(Calendar.YEAR) + 1); } else { week.setYear(thisMonth.get(Calendar.YEAR)); } previousWeek = thisMonth.get(Calendar.WEEK_OF_YEAR); } } while (thisMonth.get(Calendar.MONTH) == currentMonth); if (thisMonth.get(Calendar.DAY_OF_WEEK) != firstDayOfWeek) { calendarWeeks.add(week); } calendarWeeks.get(calendarWeeks.size() - 1).setLocation(ElementLocation.LAST); return calendarWeeks; } CalendarWeekFactory(); protected  CalendarWeekFactory(IOverviewTimesheet overviewTimesheet); List<CalendarWeek> createWeeks(Integer firstDayOfWeek, Integer userId, Calendar thisMonth);  }
@Test public void should_mark_first_and_last_rows() { List<CalendarWeek> weeks = factory.createWeeks(Calendar.SUNDAY, 1, GregorianCalendar.getInstance()); assertEquals(ElementLocation.FIRST, weeks.get(0).getLocation()); assertEquals(ElementLocation.MIDDLE, weeks.get(1).getLocation()); assertEquals(ElementLocation.LAST, weeks.get(weeks.size() - 1).getLocation()); }
@Test public void should_create_weeks_for_january_2012() { List<CalendarWeek> weeks = factory.createWeeks(Calendar.SUNDAY, 1, new GregorianCalendar(2012, Calendar.JANUARY, 1)); CalendarWeek calendarWeek = weeks.get(0); assertEquals(1, calendarWeek.getWeek()); assertEquals(1, calendarWeek.getWeekStart().get(Calendar.DAY_OF_MONTH)); assertEquals(5, weeks.size()); }
@Test public void should_create_weeks_for_february_2013() { GregorianCalendar requestedMonth = new GregorianCalendar(2013, Calendar.FEBRUARY, 1); LocalDate bookedDay = new LocalDate(2013, DateTimeConstants.FEBRUARY, 3); when(overviewTimesheet.getBookedDaysMonthOverview(1, requestedMonth)).thenReturn(Arrays.asList(bookedDay)); List<CalendarWeek> weeks = factory.createWeeks(Calendar.MONDAY, 1, requestedMonth); CalendarWeek calendarWeek = weeks.get(0); assertEquals(5, calendarWeek.getWeek()); assertEquals(28, calendarWeek.getWeekStart().get(Calendar.DAY_OF_MONTH)); assertEquals(5, weeks.size()); assertEquals(3, calendarWeek.getDay(Calendar.SUNDAY).getMonthDay()); assertTrue(calendarWeek.getDay(Calendar.SUNDAY).isBooked()); }