001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.cal;
016    
017    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
018    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.text.Format;
025    
026    import java.util.Calendar;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class RecurrenceSerializer {
032    
033            public static String toCronText(Recurrence recurrence) {
034                    Calendar dtStart = recurrence.getDtStart();
035    
036                    int frequency = recurrence.getFrequency();
037                    int interval = recurrence.getInterval();
038    
039                    DayAndPosition[] byDay = recurrence.getByDay();
040                    int[] byMonthDay = recurrence.getByMonthDay();
041                    int[] byMonth = recurrence.getByMonth();
042    
043                    String startDateSecond = String.valueOf(dtStart.get(Calendar.SECOND));
044                    String startDateMinute = String.valueOf(dtStart.get(Calendar.MINUTE));
045    
046                    int startDateHour = dtStart.get(Calendar.HOUR);
047    
048                    if (dtStart.get(Calendar.AM_PM) == Calendar.PM) {
049                            startDateHour += 12;
050                    }
051    
052                    String dayOfMonth = String.valueOf(dtStart.get(Calendar.DAY_OF_MONTH));
053                    String month = String.valueOf(dtStart.get(Calendar.MONTH) + 1);
054                    String dayOfWeek = String.valueOf(dtStart.get(Calendar.DAY_OF_WEEK));
055                    String year = String.valueOf(dtStart.get(Calendar.YEAR));
056    
057                    if (frequency == Recurrence.NO_RECURRENCE) {
058                            dayOfWeek = StringPool.QUESTION;
059                    }
060                    else if (frequency == Recurrence.DAILY) {
061                            dayOfMonth = 1 + _getIntervalValue(interval, Recurrence.DAILY);
062                            month = StringPool.STAR;
063                            dayOfWeek = StringPool.QUESTION;
064                            year = StringPool.STAR;
065    
066                            if (byDay != null) {
067                                    dayOfMonth = StringPool.QUESTION;
068                                    dayOfWeek = StringPool.BLANK;
069    
070                                    for (int i = 0; i < byDay.length; i++) {
071                                            if (i > 0) {
072                                                    dayOfWeek += StringPool.COMMA;
073                                            }
074    
075                                            dayOfWeek += getDayOfWeek(byDay[i]);
076                                    }
077                            }
078                    }
079                    else if (frequency == Recurrence.WEEKLY) {
080                            dayOfMonth = StringPool.QUESTION;
081                            month = StringPool.STAR;
082                            year = StringPool.STAR;
083    
084                            if (byDay != null) {
085                                    dayOfWeek = StringPool.BLANK;
086    
087                                    for (int i = 0; i < byDay.length; i++) {
088                                            if (i > 0) {
089                                                    dayOfWeek += StringPool.COMMA;
090                                            }
091    
092                                            dayOfWeek += getDayOfWeek(byDay[i]);
093                                    }
094                            }
095    
096                            dayOfWeek += _getIntervalValue(interval, Recurrence.WEEKLY);
097                    }
098                    else if (frequency == Recurrence.MONTHLY) {
099                            dayOfMonth = StringPool.QUESTION;
100                            month = 1 + _getIntervalValue(interval, Recurrence.MONTHLY);
101                            dayOfWeek = StringPool.QUESTION;
102                            year = StringPool.STAR;
103    
104                            if ((byMonthDay != null) && (byMonthDay.length == 1)) {
105                                    dayOfMonth = String.valueOf(byMonthDay[0]);
106                            }
107                            else if ((byDay != null) && (byDay.length == 1)) {
108                                    String pos = String.valueOf(byDay[0].getDayPosition());
109    
110                                    if (pos.equals("-1")) {
111                                            dayOfWeek = getDayOfWeek(byDay[0]) + "L";
112                                    }
113                                    else {
114                                            dayOfWeek = getDayOfWeek(byDay[0]) + StringPool.POUND + pos;
115                                    }
116                            }
117                    }
118                    else if (frequency == Recurrence.YEARLY) {
119                            dayOfMonth = StringPool.QUESTION;
120                            dayOfWeek = StringPool.QUESTION;
121                            year += _getIntervalValue(interval, Recurrence.YEARLY);
122    
123                            if ((byMonth != null) && (byMonth.length == 1)) {
124                                    month = String.valueOf(byMonth[0] + 1);
125    
126                                    if ((byMonthDay != null) && (byMonthDay.length == 1)) {
127                                            dayOfMonth = String.valueOf(byMonthDay[0]);
128                                    }
129                                    else if ((byDay != null) && (byDay.length == 1)) {
130                                            String pos = String.valueOf(byDay[0].getDayPosition());
131    
132                                            if (pos.equals("-1")) {
133                                                    dayOfWeek = getDayOfWeek(byDay[0]) + "L";
134                                            }
135                                            else {
136                                                    dayOfWeek =
137                                                            getDayOfWeek(byDay[0]) + StringPool.POUND + pos;
138                                            }
139                                    }
140                            }
141                    }
142    
143                    StringBundler sb = new StringBundler(13);
144    
145                    sb.append(startDateSecond);
146                    sb.append(StringPool.SPACE);
147                    sb.append(startDateMinute);
148                    sb.append(StringPool.SPACE);
149                    sb.append(startDateHour);
150                    sb.append(StringPool.SPACE);
151                    sb.append(dayOfMonth);
152                    sb.append(StringPool.SPACE);
153                    sb.append(month);
154                    sb.append(StringPool.SPACE);
155                    sb.append(dayOfWeek);
156                    sb.append(StringPool.SPACE);
157                    sb.append(year);
158    
159                    return sb.toString();
160            }
161    
162            protected static String getDayOfWeek(DayAndPosition dayPos) {
163                    Calendar calendar = CalendarFactoryUtil.getCalendar();
164    
165                    calendar.set(Calendar.DAY_OF_WEEK, dayPos.getDayOfWeek());
166    
167                    Format format = FastDateFormatFactoryUtil.getSimpleDateFormat(
168                            "EEE", LocaleUtil.US);
169    
170                    return StringUtil.toUpperCase(format.format(calendar));
171            }
172    
173            private static String _getIntervalValue(int interval, int period) {
174                    if ((interval <= 0) && (period == Recurrence.WEEKLY)) {
175                            return StringPool.BLANK;
176                    }
177                    else if (interval <= 0) {
178                            return StringPool.FORWARD_SLASH + 1;
179                    }
180    
181                    return StringPool.FORWARD_SLASH + interval;
182            }
183    
184    }