001package biweekly.util; 002 003import java.util.ArrayList; 004import java.util.Iterator; 005import java.util.List; 006 007import biweekly.property.DateStart; 008import biweekly.util.Recurrence.ByDay; 009import biweekly.util.Recurrence.DayOfWeek; 010import biweekly.util.Recurrence.Frequency; 011 012import com.google.ical.values.DateTimeValue; 013import com.google.ical.values.DateTimeValueImpl; 014import com.google.ical.values.DateValue; 015import com.google.ical.values.RRule; 016import com.google.ical.values.Weekday; 017import com.google.ical.values.WeekdayNum; 018 019/* 020 Copyright (c) 2013-2015, Michael Angstadt 021 All rights reserved. 022 023 Redistribution and use in source and binary forms, with or without 024 modification, are permitted provided that the following conditions are met: 025 026 1. Redistributions of source code must retain the above copyright notice, this 027 list of conditions and the following disclaimer. 028 2. Redistributions in binary form must reproduce the above copyright notice, 029 this list of conditions and the following disclaimer in the documentation 030 and/or other materials provided with the distribution. 031 032 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 033 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 034 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 035 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 036 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 037 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 038 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 039 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 040 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 041 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 042 */ 043 044/** 045 * Contains utility methods related to the google-rfc-2445 project. 046 * @author Michael Angstadt 047 * @see <a href="https://code.google.com/p/google-rfc-2445/">google-rfc-2445</a> 048 */ 049public class Google2445Utils { 050 /** 051 * Converts a biweekly {@link DateStart} object to a google-rfc-2445 052 * {@link DateTimeValue} object. 053 * @param dtstart the biweekly object 054 * @return the google-rfc-2445 object 055 */ 056 public static DateTimeValue convert(DateStart dtstart) { 057 ICalDate value = dtstart.getValue(); 058 if (value == null) { 059 return null; 060 } 061 062 DateTimeComponents raw = value.getRawComponents(); 063 if (raw == null) { 064 raw = new DateTimeComponents(value); 065 } 066 return new DateTimeValueImpl(raw.getYear(), raw.getMonth(), raw.getDate(), raw.getHour(), raw.getMinute(), raw.getSecond()); 067 } 068 069 /** 070 * Converts a biweekly {@link Recurrence} object to a google-rfc-2445 071 * {@link RRule} object. 072 * @param recur the biweekly recurrence object 073 * @return the google-rfc-2445 object 074 */ 075 public static RRule convert(Recurrence recur) { 076 RRule googleRRule = new RRule(); 077 078 List<WeekdayNum> weekdayNums = new ArrayList<WeekdayNum>(); 079 for (ByDay byDay : recur.getByDay()) { 080 Integer prefix = byDay.getNum(); 081 if (prefix == null) { 082 prefix = 0; 083 } 084 085 weekdayNums.add(new WeekdayNum(prefix, convert(byDay.getDay()))); 086 } 087 googleRRule.setByDay(weekdayNums); 088 089 googleRRule.setByYearDay(toArray(recur.getByYearDay())); 090 googleRRule.setByMonth(toArray(recur.getByMonth())); 091 googleRRule.setByWeekNo(toArray(recur.getByWeekNo())); 092 googleRRule.setByMonthDay(toArray(recur.getByMonthDay())); 093 googleRRule.setByHour(toArray(recur.getByHour())); 094 googleRRule.setByMinute(toArray(recur.getByMinute())); 095 googleRRule.setBySecond(toArray(recur.getBySecond())); 096 googleRRule.setBySetPos(toArray(recur.getBySetPos())); 097 098 Integer count = recur.getCount(); 099 if (count != null) { 100 googleRRule.setCount(count); 101 } 102 103 Frequency freq = recur.getFrequency(); 104 if (freq != null) { 105 googleRRule.setFreq(convert(freq)); 106 } 107 108 Integer interval = recur.getInterval(); 109 if (interval != null) { 110 googleRRule.setInterval(interval); 111 } 112 113 ICalDate until = recur.getUntil(); 114 if (until != null) { 115 googleRRule.setUntil(convert(until)); 116 } 117 118 DayOfWeek workweekStarts = recur.getWorkweekStarts(); 119 if (workweekStarts != null) { 120 googleRRule.setWkSt(convert(workweekStarts)); 121 } 122 123 return googleRRule; 124 } 125 126 /** 127 * Converts a biweekly {@link DayOfWeek} object to a google-rfc-2445 128 * {@link Weekday} object. 129 * @param day the biweekly object 130 * @return the google-rfc-2445 object 131 */ 132 public static Weekday convert(DayOfWeek day) { 133 switch (day) { 134 case SUNDAY: 135 return Weekday.SU; 136 case MONDAY: 137 return Weekday.MO; 138 case TUESDAY: 139 return Weekday.TU; 140 case WEDNESDAY: 141 return Weekday.WE; 142 case THURSDAY: 143 return Weekday.TH; 144 case FRIDAY: 145 return Weekday.FR; 146 case SATURDAY: 147 return Weekday.SA; 148 default: 149 return null; 150 } 151 } 152 153 /** 154 * Converts a biweekly {@link Frequency} object to a google-rfc-2445 155 * {@link com.google.ical.values.Frequency Frequency} object. 156 * @param freq the biweekly object 157 * @return the google-rfc-2445 object 158 */ 159 public static com.google.ical.values.Frequency convert(Frequency freq) { 160 switch (freq) { 161 case YEARLY: 162 return com.google.ical.values.Frequency.YEARLY; 163 case MONTHLY: 164 return com.google.ical.values.Frequency.MONTHLY; 165 case WEEKLY: 166 return com.google.ical.values.Frequency.WEEKLY; 167 case DAILY: 168 return com.google.ical.values.Frequency.DAILY; 169 case HOURLY: 170 return com.google.ical.values.Frequency.HOURLY; 171 case MINUTELY: 172 return com.google.ical.values.Frequency.MINUTELY; 173 case SECONDLY: 174 return com.google.ical.values.Frequency.SECONDLY; 175 default: 176 return null; 177 } 178 } 179 180 /** 181 * Converts an {@link ICalDate} object to a google-rfc-2445 182 * {@link DateValue} object. 183 * @param date the Java date object 184 * @return the google-rfc-2445 object 185 */ 186 public static DateValue convert(ICalDate date) { 187 DateTimeComponents raw = date.getRawComponents(); 188 if (raw == null) { 189 raw = new DateTimeComponents(date); 190 } 191 192 //@formatter:off 193 return new DateTimeValueImpl( 194 raw.getYear(), 195 raw.getMonth(), 196 raw.getDate(), 197 raw.getHour(), 198 raw.getMinute(), 199 raw.getSecond() 200 ); 201 //@formatter:on 202 } 203 204 /** 205 * Converts an Integer list to an int array. 206 * @param list the Integer list 207 * @return the int array 208 */ 209 private static int[] toArray(List<Integer> list) { 210 int[] array = new int[list.size()]; 211 Iterator<Integer> it = list.iterator(); 212 int i = 0; 213 while (it.hasNext()) { 214 Integer next = it.next(); 215 array[i++] = (next == null) ? 0 : next; 216 } 217 return array; 218 } 219 220 private Google2445Utils() { 221 //hide 222 } 223}