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