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;
033
034import java.util.Calendar;
035import java.util.Date;
036import java.util.SortedSet;
037
038/**
039 * Implementation of @{link org.opencms.widgets.serialdate.I_CmsSerialDateBean}
040 * that handles series' specified on a weekly base.
041 */
042public class CmsSerialDateBeanWeekly extends A_CmsSerialDateBean {
043
044    /** The number of weeks till the next event occurs. */
045    private int m_interval;
046    /** The weekdays the event occurs. */
047    private SortedSet<WeekDay> m_weekDays;
048
049    /**
050     * Constructs the bean with all the information provided by the {@link org.opencms.widgets.CmsSerialDateWidget}.
051     *
052     * @param startDate the start date of the series as provided by the serial date widget.
053     * @param endDate the end date of the series as provided by the serial date widget.
054     * @param isWholeDay flag, indicating if the event lasts the whole day
055     * @param endType the end type of the series as provided by the serial date widget.
056     * @param serialEndDate the end date of the series as provided by the serial date widget.
057     * @param occurrences the maximal number of occurrences of the event as provided by the serial date widget.
058     * @param exceptions dates where the event does not take place, even if it is in the series.
059     * @param interval the number of weeks to the next event.
060     * @param weekDays the weekdays the event occurs.
061     */
062    public CmsSerialDateBeanWeekly(
063        Date startDate,
064        Date endDate,
065        boolean isWholeDay,
066        EndType endType,
067        Date serialEndDate,
068        int occurrences,
069        SortedSet<Date> exceptions,
070        int interval,
071        SortedSet<WeekDay> weekDays) {
072        super(startDate, endDate, isWholeDay, endType, serialEndDate, occurrences, exceptions);
073        m_interval = interval;
074        m_weekDays = weekDays;
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        WeekDay currentWeekDay = WeekDay.fromInt(date.get(Calendar.DAY_OF_WEEK));
085        if (!m_weekDays.contains(currentWeekDay)) {
086            toNextDate(date);
087        }
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 !m_weekDays.isEmpty();
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        WeekDay currentWeekDay = WeekDay.fromInt(date.get(Calendar.DAY_OF_WEEK));
108        int daysToNextMatch = getDaysToNextMatch(currentWeekDay);
109        date.add(Calendar.DATE, daysToNextMatch);
110
111    }
112
113    /**
114     * Returns the number of days from the given weekday to the next weekday the event should occur.
115     * @param weekDay the current weekday.
116     * @return the number of days to the next weekday an event could occur.
117     */
118    private int getDaysToNextMatch(WeekDay weekDay) {
119
120        for (WeekDay wd : m_weekDays) {
121            if (wd.compareTo(weekDay) > 0) {
122                return wd.toInt() - weekDay.toInt();
123            }
124        }
125        return (m_weekDays.iterator().next().toInt() + (m_interval * I_CmsSerialDateValue.NUM_OF_WEEKDAYS))
126            - weekDay.toInt();
127    }
128
129}