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