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.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
072        super(startDate, endDate, isWholeDay, endType, serialEndDate, occurrences, exceptions);
073        m_interval = interval;
074        m_dayOfMonth = dayOfMonth;
075    }
076
077    /**
078     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#getFirstDate()
079     */
080    @Override
081    protected Calendar getFirstDate() {
082
083        Calendar date = (Calendar)getStartDate().clone();
084        if (date.get(Calendar.DAY_OF_MONTH) > m_dayOfMonth) {
085            date.set(Calendar.DAY_OF_MONTH, 1);
086            date.add(Calendar.MONTH, 1);
087        }
088        setCorrectDay(date);
089        return date;
090
091    }
092
093    /**
094     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#isAnyDatePossible()
095     */
096    @Override
097    protected boolean isAnyDatePossible() {
098
099        return true;
100    }
101
102    /**
103     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#toNextDate(java.util.Calendar)
104     */
105    @Override
106    protected void toNextDate(Calendar date) {
107
108        date.set(Calendar.DAY_OF_MONTH, 1); // To not get in trouble when we are at the 31st or a like.
109        date.add(Calendar.MONTH, m_interval);
110        setCorrectDay(date);
111    }
112
113    /**
114     * Checks, if the the current month does have less than the searched day.
115     * @param date the date where year and month are set correctly
116     * @return <code>true</code> if the month has less days than the day searched for, <code>false</code> otherwise.
117     */
118    private boolean monthHasNotDay(Calendar date) {
119
120        return date.getActualMaximum(Calendar.DAY_OF_MONTH) < m_dayOfMonth;
121    }
122
123    /**
124     * Set the correct day for the date with year and month already fixed.
125     * @param date the date, where year and month are already correct.
126     */
127    private void setCorrectDay(Calendar date) {
128
129        if (monthHasNotDay(date)) {
130            date.set(Calendar.DAY_OF_MONTH, date.getActualMaximum(Calendar.DAY_OF_MONTH));
131        } else {
132            date.set(Calendar.DAY_OF_MONTH, m_dayOfMonth);
133        }
134    }
135
136}