001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.scheduler;
016    
017    import com.liferay.portal.kernel.messaging.MessageListener;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Time;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class SchedulerEntryImpl implements SchedulerEntry {
027    
028            public String getDescription() {
029                    return _description;
030            }
031    
032            public MessageListener getEventListener() {
033                    return _eventListener;
034            }
035    
036            public String getEventListenerClass() {
037                    return _eventListenerClass;
038            }
039    
040            public String getPropertyKey() {
041                    return _propertyKey;
042            }
043    
044            public TimeUnit getTimeUnit() {
045                    return _timeUnit;
046            }
047    
048            public Trigger getTrigger() throws SchedulerException {
049                    if (_trigger != null) {
050                            return _trigger;
051                    }
052    
053                    if (_triggerType.equals(TriggerType.CRON)) {
054                            _trigger = new CronTrigger(
055                                    _eventListenerClass, _eventListenerClass, _triggerValue);
056                    }
057                    else if (_triggerType.equals(TriggerType.SIMPLE)) {
058                            long intervalTime = GetterUtil.getLong(_triggerValue);
059    
060                            if (_timeUnit.equals(TimeUnit.DAY)) {
061                                    intervalTime = intervalTime * Time.DAY;
062                            }
063                            else if (_timeUnit.equals(TimeUnit.HOUR)) {
064                                    intervalTime = intervalTime * Time.HOUR;
065                            }
066                            else if (_timeUnit.equals(TimeUnit.MINUTE)) {
067                                    intervalTime = intervalTime * Time.MINUTE;
068                            }
069                            else if (_timeUnit.equals(TimeUnit.WEEK)) {
070                                    intervalTime = intervalTime * Time.WEEK;
071                            }
072                            else {
073                                    intervalTime = intervalTime * Time.SECOND;
074                            }
075    
076                            _trigger = new IntervalTrigger(
077                                    _eventListenerClass, _eventListenerClass, intervalTime);
078                    }
079                    else {
080                            throw new SchedulerException(
081                                    "Unsupport trigger type " + _triggerType);
082                    }
083    
084                    return _trigger;
085            }
086    
087            public TriggerType getTriggerType() {
088                    return _triggerType;
089            }
090    
091            public String getTriggerValue() {
092                    return _triggerValue;
093            }
094    
095            public void setDescription(String description) {
096                    _description = description;
097            }
098    
099            public void setEventListener(MessageListener eventListener) {
100                    _eventListener = eventListener;
101            }
102    
103            public void setEventListenerClass(String eventListenerClass) {
104                    _eventListenerClass = eventListenerClass;
105            }
106    
107            public void setPropertyKey(String propertyKey) {
108                    _propertyKey = propertyKey;
109            }
110    
111            public void setTimeUnit(TimeUnit timeUnit) {
112                    _timeUnit = timeUnit;
113            }
114    
115            public void setTriggerType(TriggerType triggerType) {
116                    _triggerType = triggerType;
117            }
118    
119            public void setTriggerValue(int triggerValue) {
120                    _triggerValue = String.valueOf(triggerValue);
121            }
122    
123            public void setTriggerValue(long triggerValue) {
124                    _triggerValue = String.valueOf(triggerValue);
125            }
126    
127            public void setTriggerValue(String triggerValue) {
128                    _triggerValue = triggerValue;
129            }
130    
131            @Override
132            public String toString() {
133                    StringBundler sb = new StringBundler(17);
134    
135                    sb.append(", description=");
136                    sb.append(_description);
137                    sb.append(", eventListener=");
138                    sb.append(_eventListener);
139                    sb.append(", eventListenerClass=");
140                    sb.append(_eventListenerClass);
141                    sb.append(", propertyKey=");
142                    sb.append(_propertyKey);
143                    sb.append(", timeUnit=");
144                    sb.append(_timeUnit);
145                    sb.append(", trigger=");
146                    sb.append(_trigger);
147                    sb.append(", triggerType=");
148                    sb.append(_triggerType);
149                    sb.append(", triggerValue=");
150                    sb.append(_triggerValue);
151                    sb.append("}");
152    
153                    return sb.toString();
154            }
155    
156            private String _description = StringPool.BLANK;
157            private transient MessageListener _eventListener;
158            private String _eventListenerClass = StringPool.BLANK;
159            private String _propertyKey = StringPool.BLANK;
160            private TimeUnit _timeUnit;
161            private Trigger _trigger;
162            private TriggerType _triggerType;
163            private String _triggerValue = StringPool.BLANK;
164    
165    }