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