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
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
067        super(startDate, endDate, isWholeDay, endType, serialEndDate, occurrences, exceptions);
068
069    }
070
071    /**
072     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#getFirstDate()
073     */
074    @Override
075    protected Calendar getFirstDate() {
076
077        Calendar date = (Calendar)getStartDate().clone();
078        if (!isWorkingDay(date)) {
079            toNextDate(date);
080        }
081        return date;
082
083    }
084
085    /**
086     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#isAnyDatePossible()
087     */
088    @Override
089    protected boolean isAnyDatePossible() {
090
091        return true;
092    }
093
094    /**
095     * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#toNextDate(java.util.Calendar)
096     */
097    @Override
098    protected void toNextDate(Calendar date) {
099
100        do {
101            date.add(Calendar.DATE, 1);
102        } while (!isWorkingDay(date));
103
104    }
105
106    /**
107     * Check, if the day for the provided date is a working day (i.e., not Saturday or Sunday).
108     * @param date the date to check.
109     * @return <code>true</code> if the provided date represents a working day, <code>false</code> otherwise.
110     */
111    private boolean isWorkingDay(Calendar date) {
112
113        return !ArrayUtils.contains(NON_WORKING_DAYS, date.get(Calendar.DAY_OF_WEEK));
114    }
115
116}