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.EndType;
031
032import java.util.Calendar;
033import java.util.Date;
034import java.util.SortedSet;
035
036/**
037 * Implementation of @{link org.opencms.widgets.serialdate.I_CmsSerialDateBean}
038 * that handles series' specified on a monthly base.
039 */
040public class CmsSerialDateBeanMonthly extends A_CmsSerialDateBean {
041
042    /** The number of months till the next event. */
043    private int m_interval;
044    /** The number of the day or week day of the month the event should occur. */
045    private int m_dayOfMonth;
046
047    /**
048     * Constructs the bean with all the information provided by the {@link org.opencms.widgets.CmsSerialDateWidget}.
049     *
050     * @param startDate the start date of the series as provided by the serial date widget.
051     * @param endDate the end date of the series as provided by the serial date widget.
052     * @param isWholeDay flag, indicating if the event lasts the whole day
053     * @param endType the end type of the series as provided by the serial date widget.
054     * @param serialEndDate the end date of the series as provided by the serial date widget.
055     * @param occurrences the maximal number of occurrences of the event as provided by the serial date widget.
056     * @param exceptions dates where the event does not take place, even if it is in the series.
057     * @param interval the number of month to the next events.
058     * @param dayOfMonth the day of the month the event should occur, if the month does not have that particular day
059     *   the event takes place at the last day of the month.
060     */
061    public CmsSerialDateBeanMonthly(
062        Date startDate,
063        Date endDate,
064        boolean isWholeDay,
065        EndType endType,
066        Date serialEndDate,
067        int occurrences,
068        SortedSet<Date> exceptions,
069        int interval,
070        int dayOfMonth) {
071        super(startDate, endDate, isWholeDay, endType, serialEndDate, occurrences, exceptions);
072        m_interval = interval;
073        m_dayOfMonth = dayOfMonth;
074    }
075
076    /**
077     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#getFirstDate()
078     */
079    @Override
080    protected Calendar getFirstDate() {
081
082        Calendar date = (Calendar)getStartDate().clone();
083        if (date.get(Calendar.DAY_OF_MONTH) > m_dayOfMonth) {
084            date.set(Calendar.DAY_OF_MONTH, 1);
085            date.add(Calendar.MONTH, 1);
086        }
087        setCorrectDay(date);
088        return date;
089
090    }
091
092    /**
093     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#isAnyDatePossible()
094     */
095    @Override
096    protected boolean isAnyDatePossible() {
097
098        return true;
099    }
100
101    /**
102     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#toNextDate(java.util.Calendar)
103     */
104    @Override
105    protected void toNextDate(Calendar date) {
106
107        date.set(Calendar.DAY_OF_MONTH, 1); // To not get in trouble when we are at the 31st or a like.
108        date.add(Calendar.MONTH, m_interval);
109        setCorrectDay(date);
110    }
111
112    /**
113     * Checks, if the the current month does have less than the searched day.
114     * @param date the date where year and month are set correctly
115     * @return <code>true</code> if the month has less days than the day searched for, <code>false</code> otherwise.
116     */
117    private boolean monthHasNotDay(Calendar date) {
118
119        return date.getActualMaximum(Calendar.DAY_OF_MONTH) < m_dayOfMonth;
120    }
121
122    /**
123     * Set the correct day for the date with year and month already fixed.
124     * @param date the date, where year and month are already correct.
125     */
126    private void setCorrectDay(Calendar date) {
127
128        if (monthHasNotDay(date)) {
129            date.set(Calendar.DAY_OF_MONTH, date.getActualMaximum(Calendar.DAY_OF_MONTH));
130        } else {
131            date.set(Calendar.DAY_OF_MONTH, m_dayOfMonth);
132        }
133    }
134
135}