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.Month; 033import org.opencms.acacia.shared.I_CmsSerialDateValue.WeekDay; 034import org.opencms.acacia.shared.I_CmsSerialDateValue.WeekOfMonth; 035 036import java.util.Calendar; 037import java.util.Date; 038import java.util.SortedSet; 039 040/** 041 * Implementation of @{link org.opencms.widgets.serialdate.I_CmsSerialDateBean} 042 * that handles series' specified on a yearly base. 043 */ 044public class CmsSerialDateBeanYearlyWeekday extends A_CmsSerialDateBean { 045 046 /** The number of the week of the month the event should occur. */ 047 private WeekOfMonth m_weekOfMonth; 048 /** The month in which the event should occur. */ 049 private Month m_month; 050 /** The weekday the event should occur. Can be <code>null</code> if the weekday does not matter. */ 051 private WeekDay m_weekDay; 052 053 /** 054 * Constructs the bean with all the information provided by the {@link org.opencms.widgets.CmsSerialDateWidget}. 055 * 056 * @param startDate the start date of the series as provided by the serial date widget. 057 * @param endDate the end date of the series as provided by the serial date widget. 058 * @param isWholeDay flag, indicating if the event lasts the whole day 059 * @param endType the end type of the series as provided by the serial date widget. 060 * @param serialEndDate the end date of the series as provided by the serial date widget. 061 * @param occurrences the maximal number of occurrences of the event as provided by the serial date widget. 062 * @param exceptions dates where the event does not take place, even if it is in the series. 063 * @param weekOfMonth 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. 064 * @param month the month in which the event should occur 065 * @param weekDay the weekday on which the event should occur 066 */ 067 public CmsSerialDateBeanYearlyWeekday( 068 Date startDate, 069 Date endDate, 070 boolean isWholeDay, 071 EndType endType, 072 Date serialEndDate, 073 int occurrences, 074 SortedSet<Date> exceptions, 075 WeekOfMonth weekOfMonth, 076 Month month, 077 WeekDay weekDay) { 078 079 super(startDate, endDate, isWholeDay, endType, serialEndDate, occurrences, exceptions); 080 m_weekOfMonth = weekOfMonth; 081 m_month = month; 082 m_weekDay = weekDay; 083 } 084 085 /** 086 * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#getFirstDate() 087 */ 088 @Override 089 protected Calendar getFirstDate() { 090 091 Calendar date = (Calendar)getStartDate().clone(); 092 int month = date.get(Calendar.MONTH); 093 Calendar firstPossibleMatch = (Calendar)date.clone(); 094 setFittingWeekDay(firstPossibleMatch); 095 if ((month > m_month.ordinal()) 096 || ((month == m_month.ordinal()) && (date.getTimeInMillis() > firstPossibleMatch.getTimeInMillis()))) { 097 date.set(Calendar.MONTH, 0); 098 date.set(Calendar.DAY_OF_MONTH, 1); 099 date.add(Calendar.YEAR, 1); 100 } 101 date.set(Calendar.MONTH, m_month.ordinal()); 102 setFittingWeekDay(date); 103 return date; 104 105 } 106 107 /** 108 * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#isAnyDatePossible() 109 */ 110 @Override 111 protected boolean isAnyDatePossible() { 112 113 return true; 114 115 } 116 117 /** 118 * @see org.opencms.widgets.serialdate.A_CmsSerialDateBean#toNextDate(java.util.Calendar) 119 */ 120 @Override 121 protected void toNextDate(Calendar date) { 122 123 date.set(Calendar.DAY_OF_MONTH, 1); 124 date.add(Calendar.YEAR, 1); 125 setFittingWeekDay(date); 126 } 127 128 /** 129 * Adjusts the day in the provided month, that it fits the specified week day. 130 * If there's no match for that provided month, the next possible month is checked. 131 * 132 * @param date the date to adjust, with the correct year and month already set. 133 */ 134 private void setFittingWeekDay(Calendar date) { 135 136 date.set(Calendar.DAY_OF_MONTH, 1); 137 int weekDayFirst = date.get(Calendar.DAY_OF_WEEK); 138 int firstFittingWeekDay = (((m_weekDay.toInt() + I_CmsSerialDateValue.NUM_OF_WEEKDAYS) - weekDayFirst) 139 % I_CmsSerialDateValue.NUM_OF_WEEKDAYS) + 1; 140 int fittingWeekDay = firstFittingWeekDay + (I_CmsSerialDateValue.NUM_OF_WEEKDAYS * m_weekOfMonth.ordinal()); 141 if (fittingWeekDay > date.getActualMaximum(Calendar.DAY_OF_MONTH)) { 142 fittingWeekDay -= I_CmsSerialDateValue.NUM_OF_WEEKDAYS; 143 } 144 date.set(Calendar.DAY_OF_MONTH, fittingWeekDay); 145 } 146}