001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.cal;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    
020    import java.util.Calendar;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class RecurrenceSerializer {
026    
027            public static String toCronText(Recurrence recurrence) {
028                    Calendar dtStart = recurrence.getDtStart();
029    
030                    int frequency = recurrence.getFrequency();
031                    int interval = recurrence.getInterval();
032    
033                    DayAndPosition[] byDay = recurrence.getByDay();
034                    int[] byMonthDay = recurrence.getByMonthDay();
035                    int[] byMonth = recurrence.getByMonth();
036    
037                    String startDateSecond = String.valueOf(dtStart.get(Calendar.SECOND));
038                    String startDateMinute = String.valueOf(dtStart.get(Calendar.MINUTE));
039    
040                    int startDateHour = dtStart.get(Calendar.HOUR);
041    
042                    if (dtStart.get(Calendar.AM_PM) == Calendar.PM) {
043                            startDateHour += 12;
044                    }
045    
046                    String dayOfMonth = String.valueOf(dtStart.get(Calendar.DAY_OF_MONTH));
047                    String month = String.valueOf(dtStart.get(Calendar.MONTH) + 1);
048                    String dayOfWeek = String.valueOf(dtStart.get(Calendar.DAY_OF_WEEK));
049                    String year = String.valueOf(dtStart.get(Calendar.YEAR));
050    
051                    if (frequency == Recurrence.NO_RECURRENCE) {
052                            dayOfWeek = StringPool.QUESTION;
053                    }
054                    else if (frequency == Recurrence.DAILY) {
055                            dayOfMonth += StringPool.FORWARD_SLASH + interval;
056                            month = StringPool.STAR;
057                            dayOfWeek = StringPool.QUESTION;
058                            year = StringPool.STAR;
059    
060                            if (byDay != null) {
061                                    dayOfMonth = StringPool.QUESTION;
062                                    dayOfWeek = StringPool.BLANK;
063    
064                                    for (int i = 0; i < byDay.length; i++) {
065                                            if (i > 0) {
066                                                    dayOfWeek += StringPool.COMMA;
067                                            }
068    
069                                            dayOfWeek += byDay[i].getDayOfWeek();
070                                    }
071                            }
072                    }
073                    else if (frequency == Recurrence.WEEKLY) {
074                            dayOfMonth = StringPool.QUESTION;
075                            month = StringPool.STAR;
076                            year = StringPool.STAR;
077    
078                            if (byDay != null) {
079                                    dayOfWeek = StringPool.BLANK;
080    
081                                    for (int i = 0; i < byDay.length; i++) {
082                                            if (i > 0) {
083                                                    dayOfWeek += StringPool.COMMA;
084                                            }
085    
086                                            dayOfWeek += byDay[i].getDayOfWeek();
087                                    }
088                            }
089    
090                            dayOfWeek += StringPool.FORWARD_SLASH + interval;
091                    }
092                    else if (frequency == Recurrence.MONTHLY) {
093                            dayOfMonth = StringPool.QUESTION;
094                            month += StringPool.FORWARD_SLASH + interval;
095                            dayOfWeek = StringPool.QUESTION;
096                            year = StringPool.STAR;
097    
098                            if ((byMonthDay != null) && (byMonthDay.length == 1)) {
099                                    dayOfMonth = String.valueOf(byMonthDay[0]);
100                            }
101                            else if ((byDay != null) && (byDay.length == 1)) {
102                                    String pos = String.valueOf(byDay[0].getDayPosition());
103    
104                                    if (pos.equals("-1")) {
105                                            dayOfWeek = byDay[0].getDayOfWeek() + "L";
106                                    }
107                                    else {
108                                            dayOfWeek =
109                                                    byDay[0].getDayOfWeek() + StringPool.POUND + pos;
110                                    }
111                            }
112                    }
113                    else if (frequency == Recurrence.YEARLY) {
114                            dayOfMonth = StringPool.QUESTION;
115                            dayOfWeek = StringPool.QUESTION;
116                            year += StringPool.FORWARD_SLASH + interval;
117    
118                            if ((byMonth != null) && (byMonth.length == 1)) {
119                                    month = String.valueOf(byMonth[0] + 1);
120    
121                                    if ((byMonthDay != null) && (byMonthDay.length == 1)) {
122                                            dayOfMonth = String.valueOf(byMonthDay[0]);
123                                    }
124                                    else if ((byDay != null) && (byDay.length == 1)) {
125                                            String pos = String.valueOf(byDay[0].getDayPosition());
126    
127                                            if (pos.equals("-1")) {
128                                                    dayOfWeek = byDay[0].getDayOfWeek() + "L";
129                                            }
130                                            else {
131                                                    dayOfWeek =
132                                                            byDay[0].getDayOfWeek() + StringPool.POUND + pos;
133                                            }
134                                    }
135                            }
136                    }
137    
138                    StringBundler sb = new StringBundler(13);
139    
140                    sb.append(startDateSecond);
141                    sb.append(StringPool.SPACE);
142                    sb.append(startDateMinute);
143                    sb.append(StringPool.SPACE);
144                    sb.append(startDateHour);
145                    sb.append(StringPool.SPACE);
146                    sb.append(dayOfMonth);
147                    sb.append(StringPool.SPACE);
148                    sb.append(month);
149                    sb.append(StringPool.SPACE);
150                    sb.append(dayOfWeek);
151                    sb.append(StringPool.SPACE);
152                    sb.append(year);
153    
154                    return sb.toString();
155            }
156    
157    }