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
036import org.apache.commons.lang3.ArrayUtils;
037
038/**
039 * Implementation of @{link org.opencms.widgets.serialdate.I_CmsSerialDateBean}
040 * that handles series' specified on a daily base.
041 */
042public class CmsSerialDateBeanWorkingDays extends A_CmsSerialDateBean {
043
044    /** The non-working days. */
045    private static final int[] NON_WORKING_DAYS = {Calendar.SATURDAY, Calendar.SUNDAY};
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     */
058    public CmsSerialDateBeanWorkingDays(
059        Date startDate,
060        Date endDate,
061        boolean isWholeDay,
062        EndType endType,
063        Date serialEndDate,
064        int occurrences,
065        SortedSet<Date> exceptions) {
066        super(startDate, endDate, isWholeDay, endType, serialEndDate, occurrences, exceptions);
067
068    }
069
070    /**
071     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#getFirstDate()
072     */
073    @Override
074    protected Calendar getFirstDate() {
075
076        Calendar date = (Calendar)getStartDate().clone();
077        if (!isWorkingDay(date)) {
078            toNextDate(date);
079        }
080        return date;
081
082    }
083
084    /**
085     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#isAnyDatePossible()
086     */
087    @Override
088    protected boolean isAnyDatePossible() {
089
090        return true;
091    }
092
093    /**
094     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#toNextDate(java.util.Calendar)
095     */
096    @Override
097    protected void toNextDate(Calendar date) {
098
099        do {
100            date.add(Calendar.DATE, 1);
101        } while (!isWorkingDay(date));
102
103    }
104
105    /**
106     * Check, if the day for the provided date is a working day (i.e., not Saturday or Sunday).
107     * @param date the date to check.
108     * @return <code>true</code> if the provided date represents a working day, <code>false</code> otherwise.
109     */
110    private boolean isWorkingDay(Calendar date) {
111
112        return !ArrayUtils.contains(NON_WORKING_DAYS, date.get(Calendar.DAY_OF_WEEK));
113    }
114
115}