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