1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.scheduler;
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="CronText.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Thiago Moreira
26   */
27  public class CronText {
28  
29      public final static int DAILY_FREQUENCY = 3;
30  
31      public final static int MINUTELY_FREQUENCY = 2;
32  
33      public final static int MONTHLY_FREQUENCY = 4;
34  
35      public final static int NO_FREQUENCY = 1;
36  
37      public final static int WEEKLY_FREQUENCY = 5;
38  
39      public final static int YEARLY_FREQUENCY = 6;
40  
41      public CronText(Calendar startDate) {
42          this(startDate, CronText.NO_FREQUENCY, 0);
43      }
44  
45      public CronText(Calendar startDate, int frequency, int interval) {
46          _startDate = startDate;
47          setFrequency(frequency);
48          _interval = interval;
49      }
50  
51      public int getFrequency() {
52          return _frequency;
53      }
54  
55      public int getInterval() {
56          return _interval;
57      }
58  
59      public Calendar getStartDate() {
60          return _startDate;
61      }
62  
63      public void setFrequency(int frequency) {
64          if ((frequency != CronText.DAILY_FREQUENCY) &&
65              (frequency != CronText.MINUTELY_FREQUENCY) &&
66              (frequency != CronText.MONTHLY_FREQUENCY) &&
67              (frequency != CronText.NO_FREQUENCY) &&
68              (frequency != CronText.WEEKLY_FREQUENCY) &&
69              (frequency != CronText.YEARLY_FREQUENCY)) {
70  
71              throw new IllegalArgumentException(String.valueOf(frequency));
72          }
73  
74          _frequency = frequency;
75      }
76  
77      public void setInterval(int interval) {
78          _interval = interval;
79      }
80      public void setStartDate(Calendar startDate) {
81          _startDate = startDate;
82      }
83      public String toString() {
84          String second = String.valueOf(_startDate.get(Calendar.SECOND));
85          String minute = String.valueOf(_startDate.get(Calendar.MINUTE));
86          String hour = String.valueOf(_startDate.get(Calendar.HOUR_OF_DAY));
87          String dayOfMonth = String.valueOf(
88              _startDate.get(Calendar.DAY_OF_MONTH));
89          String month = String.valueOf(_startDate.get(Calendar.MONTH) + 1);
90          String dayOfWeek = String.valueOf(_startDate.get(Calendar.DAY_OF_WEEK));
91          String year = String.valueOf(_startDate.get(Calendar.YEAR));
92  
93          if (_frequency == CronText.NO_FREQUENCY) {
94              dayOfWeek = StringPool.QUESTION;
95          }
96          else if (_frequency == CronText.MINUTELY_FREQUENCY) {
97              minute = StringPool.STAR + StringPool.FORWARD_SLASH + _interval;
98              hour = StringPool.STAR;
99              dayOfMonth = StringPool.STAR;
100             month = StringPool.STAR;
101             dayOfWeek = StringPool.QUESTION;
102             year = StringPool.STAR;
103         }
104         else if (_frequency == CronText.DAILY_FREQUENCY) {
105             dayOfMonth += StringPool.FORWARD_SLASH + _interval;
106             month = StringPool.STAR;
107             dayOfWeek = StringPool.QUESTION;
108             year = StringPool.STAR;
109         }
110         else if (_frequency == CronText.WEEKLY_FREQUENCY) {
111             dayOfMonth += StringPool.FORWARD_SLASH + (_interval * 7);
112             month = StringPool.STAR;
113             dayOfWeek = StringPool.QUESTION;
114             year = StringPool.STAR;
115         }
116         else if (_frequency == CronText.MONTHLY_FREQUENCY) {
117             month += StringPool.FORWARD_SLASH + _interval;
118             dayOfWeek = StringPool.QUESTION;
119             year = StringPool.STAR;
120         }
121         else if (_frequency == CronText.YEARLY_FREQUENCY) {
122             dayOfWeek = StringPool.QUESTION;
123             year += StringPool.FORWARD_SLASH + _interval;
124         }
125 
126         StringBundler sb = new StringBundler(13);
127 
128         sb.append(second);
129         sb.append(StringPool.SPACE);
130         sb.append(minute);
131         sb.append(StringPool.SPACE);
132         sb.append(hour);
133         sb.append(StringPool.SPACE);
134         sb.append(dayOfMonth);
135         sb.append(StringPool.SPACE);
136         sb.append(month);
137         sb.append(StringPool.SPACE);
138         sb.append(dayOfWeek);
139         sb.append(StringPool.SPACE);
140         sb.append(year);
141 
142         return sb.toString();
143     }
144 
145     private int _frequency;
146     private int _interval;
147     private Calendar _startDate;
148 
149 }