001package biweekly.property; 002 003import static biweekly.util.Google2445Utils.convert; 004 005import java.util.Date; 006import java.util.List; 007import java.util.NoSuchElementException; 008import java.util.TimeZone; 009 010import biweekly.ICalVersion; 011import biweekly.Warning; 012import biweekly.component.ICalComponent; 013import biweekly.util.ICalDate; 014import biweekly.util.Recurrence; 015import biweekly.util.Recurrence.Frequency; 016 017import com.google.ical.compat.javautil.DateIterator; 018import com.google.ical.compat.javautil.DateIteratorFactory; 019import com.google.ical.iter.RecurrenceIterator; 020import com.google.ical.iter.RecurrenceIteratorFactory; 021import com.google.ical.values.DateValue; 022import com.google.ical.values.RRule; 023 024/* 025 Copyright (c) 2013-2015, Michael Angstadt 026 All rights reserved. 027 028 Redistribution and use in source and binary forms, with or without 029 modification, are permitted provided that the following conditions are met: 030 031 1. Redistributions of source code must retain the above copyright notice, this 032 list of conditions and the following disclaimer. 033 2. Redistributions in binary form must reproduce the above copyright notice, 034 this list of conditions and the following disclaimer in the documentation 035 and/or other materials provided with the distribution. 036 037 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 038 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 039 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 040 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 041 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 042 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 043 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 044 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 045 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 046 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 047 */ 048 049/** 050 * Represents a property whose value is a recurrence rule. 051 * @author Michael Angstadt 052 */ 053public class RecurrenceProperty extends ValuedProperty<Recurrence> { 054 /** 055 * Creates a new recurrence property. 056 * @param recur the recurrence value 057 */ 058 public RecurrenceProperty(Recurrence recur) { 059 super(recur); 060 } 061 062 /** 063 * Gets the date values of this recurrence property. 064 * @param startDate the date that the recurrence starts (typically, the 065 * value of its accompanying {@link DateStart} property) 066 * @return an iterator containing the dates 067 * @see <a 068 * href="https://code.google.com/p/google-rfc-2445/">google-rfc-2445</a> 069 */ 070 public DateIterator getDateIterator(Date startDate) { 071 return getDateIterator(new ICalDate(startDate)); 072 } 073 074 /** 075 * Gets the date values of this recurrence property. 076 * @param startDate the date that the recurrence starts (typically, the 077 * value of its accompanying {@link DateStart} property) 078 * @return an iterator containing the dates 079 * @see <a 080 * href="https://code.google.com/p/google-rfc-2445/">google-rfc-2445</a> 081 */ 082 public DateIterator getDateIterator(ICalDate startDate) { 083 Recurrence recur = getValue(); 084 if (recur == null) { 085 return new EmptyDateIterator(); 086 } 087 088 RRule rruleValue = convert(recur); 089 090 //we need an ICalDate that *doesn't* have any raw date/time components to pass into "convert()" 091 //see: https://sourceforge.net/p/biweekly/discussion/help-and-support/thread/faa25306/ 092 ICalDate startDateCopy = (startDate.getRawComponents() == null) ? startDate : new ICalDate(startDate, null, startDate.hasTime()); 093 DateValue dtstartValue = convert(startDateCopy); 094 095 RecurrenceIterator it = RecurrenceIteratorFactory.createRecurrenceIterator(rruleValue, dtstartValue, TimeZone.getDefault()); 096 return DateIteratorFactory.createDateIterator(it); 097 } 098 099 @Override 100 protected void validate(List<ICalComponent> components, ICalVersion version, List<Warning> warnings) { 101 super.validate(components, version, warnings); 102 if (value == null) { 103 return; 104 } 105 106 if (value.getFrequency() == null) { 107 warnings.add(Warning.validate(30)); 108 } 109 110 if (value.getUntil() != null && value.getCount() != null) { 111 warnings.add(Warning.validate(31)); 112 } 113 114 switch (version) { 115 case V1_0: 116 if (!value.getXRules().isEmpty()) { 117 warnings.add(new Warning("X-Rules are not supported by vCal.")); 118 } 119 if (!value.getBySetPos().isEmpty()) { 120 warnings.add(new Warning("BYSETPOS is not supported by vCal.")); 121 } 122 if (value.getFrequency() == Frequency.SECONDLY) { 123 warnings.add(new Warning(Frequency.SECONDLY.name() + " frequency is not supported by vCal.")); 124 } 125 break; 126 127 case V2_0_DEPRECATED: 128 //empty 129 break; 130 131 case V2_0: 132 if (!value.getXRules().isEmpty()) { 133 warnings.add(Warning.validate(32)); 134 } 135 136 break; 137 } 138 } 139 140 private static class EmptyDateIterator implements DateIterator { 141 public boolean hasNext() { 142 return false; 143 } 144 145 public Date next() { 146 throw new NoSuchElementException(); 147 } 148 149 public void remove() { 150 throw new UnsupportedOperationException(); 151 } 152 153 public void advanceTo(Date newStartUtc) { 154 //empty 155 } 156 } 157}