001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.widgets.serialdate; 029 030import org.opencms.acacia.shared.I_CmsSerialDateValue; 031import org.opencms.acacia.shared.I_CmsSerialDateValue.EndType; 032import org.opencms.acacia.shared.I_CmsSerialDateValue.WeekDay; 033import org.opencms.acacia.shared.I_CmsSerialDateValue.WeekOfMonth; 034 035import java.util.Calendar; 036import java.util.Date; 037import java.util.Iterator; 038import java.util.SortedSet; 039 040/** 041 * Implementation of @{link org.opencms.widgets.serialdate.I_CmsSerialDateBean} 042 * that handles series' specified on a monthly base. 043 */ 044public class CmsSerialDateBeanMonthlyWeeks extends A_CmsSerialDateBean { 045 046 /** The number of months till the next event. */ 047 private int m_interval; 048 /** The number of the day or week day of the month the event should occur. */ 049 private SortedSet<WeekOfMonth> m_weeksOfMonth; 050 /** The weekday the event should occur. Can be <code>null</code> if the weekday does not matter. */ 051 private WeekDay m_weekDay; 052 /** The index of the current week from the weeks of the month */ 053 private Iterator<WeekOfMonth> m_weekOfMonthIterator; 054 055 /** 056 * Constructs the bean with all the information provided by the {@link org.opencms.widgets.CmsSerialDateWidget}. 057 * 058 * @param startDate the start date of the series as provided by the serial date widget. 059 * @param endDate the end date of the series as provided by the serial date widget. 060 * @param isWholeDay flag, indicating if the event lasts the whole day 061 * @param endType the end type of the series as provided by the serial date widget. 062 * @param serialEndDate the end date of the series as provided by the serial date widget. 063 * @param occurrences the maximal number of occurrences of the event as provided by the serial date widget. 064 * @param exceptions dates where the event does not take place, even if it is in the series. 065 * @param interval the number of month to the next events. 066 * @param weeksOfMonth the weeks in the month the event should take place, e.g., the first and the third week. 067 * @param weekDay the weekday on which the event should occur 068 */ 069 public CmsSerialDateBeanMonthlyWeeks( 070 Date startDate, 071 Date endDate, 072 boolean isWholeDay, 073 EndType endType, 074 Date serialEndDate, 075 int occurrences, 076 SortedSet<Date> exceptions, 077 int interval, 078 SortedSet<WeekOfMonth> weeksOfMonth, 079 WeekDay weekDay) { 080 081 super(startDate, endDate, isWholeDay, endType, serialEndDate, occurrences, exceptions); 082 m_interval = interval; 083 m_weeksOfMonth = weeksOfMonth; 084 m_weekDay = weekDay; 085 } 086 087 /** 088 * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#getFirstDate() 089 */ 090 @Override 091 protected Calendar getFirstDate() { 092 093 m_weekOfMonthIterator = m_weeksOfMonth.iterator(); 094 Calendar date = (Calendar)getStartDate().clone(); 095 toCorrectDateWithDay(date, m_weekOfMonthIterator.next()); 096 while (date.getTimeInMillis() < getStartDate().getTimeInMillis()) { 097 toNextDate(date, 1); 098 } 099 return date; 100 101 } 102 103 /** 104 * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#isAnyDatePossible() 105 */ 106 @Override 107 protected boolean isAnyDatePossible() { 108 109 return m_weeksOfMonth.size() > 0; 110 } 111 112 /** 113 * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#toNextDate(java.util.Calendar) 114 */ 115 @Override 116 protected void toNextDate(Calendar date) { 117 118 toNextDate(date, m_interval); 119 } 120 121 /** 122 * Sets the day of the month that matches the condition, i.e., the day of month of the 2nd Saturday. 123 * If the day does not exist in the current month, the last possible date is set, i.e., 124 * instead of the fifth Saturday, the fourth is chosen. 125 * 126 * @param date date that has the correct year and month already set. 127 * @param week the number of the week to choose. 128 */ 129 private void toCorrectDateWithDay(Calendar date, WeekOfMonth week) { 130 131 date.set(Calendar.DAY_OF_MONTH, 1); 132 int daysToFirstWeekDayMatch = ((m_weekDay.toInt() + I_CmsSerialDateValue.NUM_OF_WEEKDAYS) 133 - (date.get(Calendar.DAY_OF_WEEK))) % I_CmsSerialDateValue.NUM_OF_WEEKDAYS; 134 date.add(Calendar.DAY_OF_MONTH, daysToFirstWeekDayMatch); 135 int wouldBeDayOfMonth = date.get(Calendar.DAY_OF_MONTH) 136 + ((week.ordinal()) * I_CmsSerialDateValue.NUM_OF_WEEKDAYS); 137 if (wouldBeDayOfMonth > date.getActualMaximum(Calendar.DAY_OF_MONTH)) { 138 date.set(Calendar.DAY_OF_MONTH, wouldBeDayOfMonth - I_CmsSerialDateValue.NUM_OF_WEEKDAYS); 139 } else { 140 date.set(Calendar.DAY_OF_MONTH, wouldBeDayOfMonth); 141 } 142 143 } 144 145 /** 146 * Calculates the next date, starting from the provided date. 147 * 148 * @param date the current date. 149 * @param interval the number of month to add when moving to the next month. 150 */ 151 private void toNextDate(Calendar date, int interval) { 152 153 long previousDate = date.getTimeInMillis(); 154 if (!m_weekOfMonthIterator.hasNext()) { 155 date.add(Calendar.MONTH, interval); 156 m_weekOfMonthIterator = m_weeksOfMonth.iterator(); 157 } 158 toCorrectDateWithDay(date, m_weekOfMonthIterator.next()); 159 if (previousDate == date.getTimeInMillis()) { // this can happen if the fourth and the last week are checked. 160 toNextDate(date); 161 } 162 163 } 164 165}