001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://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: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://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        super(startDate, endDate, isWholeDay, endType, serialEndDate, occurrences, exceptions);
081        m_interval = interval;
082        m_weeksOfMonth = weeksOfMonth;
083        m_weekDay = weekDay;
084    }
085
086    /**
087     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#getFirstDate()
088     */
089    @Override
090    protected Calendar getFirstDate() {
091
092        m_weekOfMonthIterator = m_weeksOfMonth.iterator();
093        Calendar date = (Calendar)getStartDate().clone();
094        toCorrectDateWithDay(date, m_weekOfMonthIterator.next());
095        while (date.getTimeInMillis() < getStartDate().getTimeInMillis()) {
096            toNextDate(date, 1);
097        }
098        return date;
099
100    }
101
102    /**
103     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#isAnyDatePossible()
104     */
105    @Override
106    protected boolean isAnyDatePossible() {
107
108        return m_weeksOfMonth.size() > 0;
109    }
110
111    /**
112     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#toNextDate(java.util.Calendar)
113     */
114    @Override
115    protected void toNextDate(Calendar date) {
116
117        toNextDate(date, m_interval);
118    }
119
120    /**
121     * Sets the day of the month that matches the condition, i.e., the day of month of the 2nd Saturday.
122     * If the day does not exist in the current month, the last possible date is set, i.e.,
123     * instead of the fifth Saturday, the fourth is chosen.
124     *
125     * @param date date that has the correct year and month already set.
126     * @param week the number of the week to choose.
127     */
128    private void toCorrectDateWithDay(Calendar date, WeekOfMonth week) {
129
130        date.set(Calendar.DAY_OF_MONTH, 1);
131        int daysToFirstWeekDayMatch = ((m_weekDay.toInt() + I_CmsSerialDateValue.NUM_OF_WEEKDAYS)
132            - (date.get(Calendar.DAY_OF_WEEK))) % I_CmsSerialDateValue.NUM_OF_WEEKDAYS;
133        date.add(Calendar.DAY_OF_MONTH, daysToFirstWeekDayMatch);
134        int wouldBeDayOfMonth = date.get(Calendar.DAY_OF_MONTH)
135            + ((week.ordinal()) * I_CmsSerialDateValue.NUM_OF_WEEKDAYS);
136        if (wouldBeDayOfMonth > date.getActualMaximum(Calendar.DAY_OF_MONTH)) {
137            date.set(Calendar.DAY_OF_MONTH, wouldBeDayOfMonth - I_CmsSerialDateValue.NUM_OF_WEEKDAYS);
138        } else {
139            date.set(Calendar.DAY_OF_MONTH, wouldBeDayOfMonth);
140        }
141
142    }
143
144    /**
145     * Calculates the next date, starting from the provided date.
146     *
147     * @param date the current date.
148     * @param interval the number of month to add when moving to the next month.
149     */
150    private void toNextDate(Calendar date, int interval) {
151
152        long previousDate = date.getTimeInMillis();
153        if (!m_weekOfMonthIterator.hasNext()) {
154            date.add(Calendar.MONTH, interval);
155            m_weekOfMonthIterator = m_weeksOfMonth.iterator();
156        }
157        toCorrectDateWithDay(date, m_weekOfMonthIterator.next());
158        if (previousDate == date.getTimeInMillis()) { // this can happen if the fourth and the last week are checked.
159            toNextDate(date);
160        }
161
162    }
163
164}