1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
35  public class RecurrenceSerializer {
36  
37      public static String toCronText(Recurrence recurrence) {
38          Calendar dtStart = recurrence.getDtStart();
39  
40          int frequency = recurrence.getFrequency();
41          int interval = recurrence.getInterval();
42  
43          DayAndPosition[] byDay = recurrence.getByDay();
44          int[] byMonthDay = recurrence.getByMonthDay();
45          int[] byMonth = recurrence.getByMonth();
46  
47          String startDateSecond = String.valueOf(dtStart.get(Calendar.SECOND));
48          String startDateMinute = String.valueOf(dtStart.get(Calendar.MINUTE));
49  
50          int startDateHour = dtStart.get(Calendar.HOUR);
51  
52          if (dtStart.get(Calendar.AM_PM) == Calendar.PM) {
53              startDateHour += 12;
54          }
55  
56          String dayOfMonth = String.valueOf(dtStart.get(Calendar.DAY_OF_MONTH));
57          String month = String.valueOf(dtStart.get(Calendar.MONTH) + 1);
58          String dayOfWeek = String.valueOf(dtStart.get(Calendar.DAY_OF_WEEK));
59          String year = String.valueOf(dtStart.get(Calendar.YEAR));
60  
61          if (frequency == Recurrence.NO_RECURRENCE) {
62              dayOfWeek = StringPool.QUESTION;
63          }
64          else if (frequency == Recurrence.DAILY) {
65              dayOfMonth += StringPool.FORWARD_SLASH + interval;
66              month = StringPool.STAR;
67              dayOfWeek = StringPool.QUESTION;
68              year = StringPool.STAR;
69  
70              if (byDay != null) {
71                  dayOfMonth = StringPool.QUESTION;
72                  dayOfWeek = StringPool.BLANK;
73  
74                  for (int i = 0; i < byDay.length; i++) {
75                      if (i > 0) {
76                          dayOfWeek += StringPool.COMMA;
77                      }
78  
79                      dayOfWeek += byDay[i].getDayOfWeek();
80                  }
81              }
82          }
83          else if (frequency == Recurrence.WEEKLY) {
84              dayOfMonth = StringPool.QUESTION;
85              month = StringPool.STAR;
86              year = StringPool.STAR;
87  
88              if (byDay != null) {
89                  dayOfWeek = StringPool.BLANK;
90  
91                  for (int i = 0; i < byDay.length; i++) {
92                      if (i > 0) {
93                          dayOfWeek += StringPool.COMMA;
94                      }
95  
96                      dayOfWeek += byDay[i].getDayOfWeek();
97                  }
98              }
99  
100             dayOfWeek += StringPool.FORWARD_SLASH + interval;
101         }
102         else if (frequency == Recurrence.MONTHLY) {
103             dayOfMonth = StringPool.QUESTION;
104             month += StringPool.FORWARD_SLASH + interval;
105             dayOfWeek = StringPool.QUESTION;
106             year = StringPool.STAR;
107 
108             if ((byMonthDay != null) && (byMonthDay.length == 1)) {
109                 dayOfMonth = String.valueOf(byMonthDay[0]);
110             }
111             else if ((byDay != null) && (byDay.length == 1)) {
112                 String pos = String.valueOf(byDay[0].getDayPosition());
113 
114                 if (pos.equals("-1")) {
115                     dayOfWeek = byDay[0].getDayOfWeek() + "L";
116                 }
117                 else {
118                     dayOfWeek =
119                         byDay[0].getDayOfWeek() + StringPool.POUND + pos;
120                 }
121             }
122         }
123         else if (frequency == Recurrence.YEARLY) {
124             dayOfMonth = StringPool.QUESTION;
125             dayOfWeek = StringPool.QUESTION;
126             year += StringPool.FORWARD_SLASH + interval;
127 
128             if ((byMonth != null) && (byMonth.length == 1)) {
129                 month = String.valueOf(byMonth[0] + 1);
130 
131                 if ((byMonthDay != null) && (byMonthDay.length == 1)) {
132                     dayOfMonth = String.valueOf(byMonthDay[0]);
133                 }
134                 else if ((byDay != null) && (byDay.length == 1)) {
135                     String pos = String.valueOf(byDay[0].getDayPosition());
136 
137                     if (pos.equals("-1")) {
138                         dayOfWeek = byDay[0].getDayOfWeek() + "L";
139                     }
140                     else {
141                         dayOfWeek =
142                             byDay[0].getDayOfWeek() + StringPool.POUND + pos;
143                     }
144                 }
145             }
146         }
147 
148         StringBuilder sb = new StringBuilder();
149 
150         sb.append(startDateSecond);
151         sb.append(StringPool.SPACE);
152         sb.append(startDateMinute);
153         sb.append(StringPool.SPACE);
154         sb.append(startDateHour);
155         sb.append(StringPool.SPACE);
156         sb.append(dayOfMonth);
157         sb.append(StringPool.SPACE);
158         sb.append(month);
159         sb.append(StringPool.SPACE);
160         sb.append(dayOfWeek);
161         sb.append(StringPool.SPACE);
162         sb.append(year);
163 
164         return sb.toString();
165     }
166 
167 }