1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.cal;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  import com.liferay.portal.kernel.util.StringPool;
19  
20  import java.util.Calendar;
21  
22  /**
23   * <a href="RecurrenceSerializer.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Brian Wing Shun Chan
26   */
27  public class RecurrenceSerializer {
28  
29      public static String toCronText(Recurrence recurrence) {
30          Calendar dtStart = recurrence.getDtStart();
31  
32          int frequency = recurrence.getFrequency();
33          int interval = recurrence.getInterval();
34  
35          DayAndPosition[] byDay = recurrence.getByDay();
36          int[] byMonthDay = recurrence.getByMonthDay();
37          int[] byMonth = recurrence.getByMonth();
38  
39          String startDateSecond = String.valueOf(dtStart.get(Calendar.SECOND));
40          String startDateMinute = String.valueOf(dtStart.get(Calendar.MINUTE));
41  
42          int startDateHour = dtStart.get(Calendar.HOUR);
43  
44          if (dtStart.get(Calendar.AM_PM) == Calendar.PM) {
45              startDateHour += 12;
46          }
47  
48          String dayOfMonth = String.valueOf(dtStart.get(Calendar.DAY_OF_MONTH));
49          String month = String.valueOf(dtStart.get(Calendar.MONTH) + 1);
50          String dayOfWeek = String.valueOf(dtStart.get(Calendar.DAY_OF_WEEK));
51          String year = String.valueOf(dtStart.get(Calendar.YEAR));
52  
53          if (frequency == Recurrence.NO_RECURRENCE) {
54              dayOfWeek = StringPool.QUESTION;
55          }
56          else if (frequency == Recurrence.DAILY) {
57              dayOfMonth += StringPool.FORWARD_SLASH + interval;
58              month = StringPool.STAR;
59              dayOfWeek = StringPool.QUESTION;
60              year = StringPool.STAR;
61  
62              if (byDay != null) {
63                  dayOfMonth = StringPool.QUESTION;
64                  dayOfWeek = StringPool.BLANK;
65  
66                  for (int i = 0; i < byDay.length; i++) {
67                      if (i > 0) {
68                          dayOfWeek += StringPool.COMMA;
69                      }
70  
71                      dayOfWeek += byDay[i].getDayOfWeek();
72                  }
73              }
74          }
75          else if (frequency == Recurrence.WEEKLY) {
76              dayOfMonth = StringPool.QUESTION;
77              month = StringPool.STAR;
78              year = StringPool.STAR;
79  
80              if (byDay != null) {
81                  dayOfWeek = StringPool.BLANK;
82  
83                  for (int i = 0; i < byDay.length; i++) {
84                      if (i > 0) {
85                          dayOfWeek += StringPool.COMMA;
86                      }
87  
88                      dayOfWeek += byDay[i].getDayOfWeek();
89                  }
90              }
91  
92              dayOfWeek += StringPool.FORWARD_SLASH + interval;
93          }
94          else if (frequency == Recurrence.MONTHLY) {
95              dayOfMonth = StringPool.QUESTION;
96              month += StringPool.FORWARD_SLASH + interval;
97              dayOfWeek = StringPool.QUESTION;
98              year = StringPool.STAR;
99  
100             if ((byMonthDay != null) && (byMonthDay.length == 1)) {
101                 dayOfMonth = String.valueOf(byMonthDay[0]);
102             }
103             else if ((byDay != null) && (byDay.length == 1)) {
104                 String pos = String.valueOf(byDay[0].getDayPosition());
105 
106                 if (pos.equals("-1")) {
107                     dayOfWeek = byDay[0].getDayOfWeek() + "L";
108                 }
109                 else {
110                     dayOfWeek =
111                         byDay[0].getDayOfWeek() + StringPool.POUND + pos;
112                 }
113             }
114         }
115         else if (frequency == Recurrence.YEARLY) {
116             dayOfMonth = StringPool.QUESTION;
117             dayOfWeek = StringPool.QUESTION;
118             year += StringPool.FORWARD_SLASH + interval;
119 
120             if ((byMonth != null) && (byMonth.length == 1)) {
121                 month = String.valueOf(byMonth[0] + 1);
122 
123                 if ((byMonthDay != null) && (byMonthDay.length == 1)) {
124                     dayOfMonth = String.valueOf(byMonthDay[0]);
125                 }
126                 else if ((byDay != null) && (byDay.length == 1)) {
127                     String pos = String.valueOf(byDay[0].getDayPosition());
128 
129                     if (pos.equals("-1")) {
130                         dayOfWeek = byDay[0].getDayOfWeek() + "L";
131                     }
132                     else {
133                         dayOfWeek =
134                             byDay[0].getDayOfWeek() + StringPool.POUND + pos;
135                     }
136                 }
137             }
138         }
139 
140         StringBundler sb = new StringBundler(13);
141 
142         sb.append(startDateSecond);
143         sb.append(StringPool.SPACE);
144         sb.append(startDateMinute);
145         sb.append(StringPool.SPACE);
146         sb.append(startDateHour);
147         sb.append(StringPool.SPACE);
148         sb.append(dayOfMonth);
149         sb.append(StringPool.SPACE);
150         sb.append(month);
151         sb.append(StringPool.SPACE);
152         sb.append(dayOfWeek);
153         sb.append(StringPool.SPACE);
154         sb.append(year);
155 
156         return sb.toString();
157     }
158 
159 }