001    /**
002     * Copyright (c) 2000-2010 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.Time;
020    
021    /**
022     * @author Shuyang Zhou
023     */
024    public class SchedulerEntryImpl implements SchedulerEntry {
025    
026            public String getDescription() {
027                    return _description;
028            }
029    
030            public MessageListener getEventListener() {
031                    return _eventListener;
032            }
033    
034            public String getEventListenerClass() {
035                    return _eventListenerClass;
036            }
037    
038            public String getPropertyKey() {
039                    return _propertyKey;
040            }
041    
042            public TimeUnit getTimeUnit() {
043                    return _timeUnit;
044            }
045    
046            public Trigger getTrigger() throws SchedulerException {
047                    if (_trigger != null) {
048                            return _trigger;
049                    }
050    
051                    if (_triggerType == TriggerType.CRON) {
052                            _trigger = new CronTrigger(
053                                    _eventListenerClass, _eventListenerClass, _triggerValue);
054                    }
055                    else if (_triggerType == TriggerType.SIMPLE) {
056                            long intervalTime = GetterUtil.getLong(_triggerValue);
057    
058                            if (_timeUnit.equals(TimeUnit.DAY)) {
059                                    intervalTime = intervalTime * Time.DAY;
060                            }
061                            else if (_timeUnit.equals(TimeUnit.HOUR)) {
062                                    intervalTime = intervalTime * Time.HOUR;
063                            }
064                            else if (_timeUnit.equals(TimeUnit.MINUTE)) {
065                                    intervalTime = intervalTime * Time.MINUTE;
066                            }
067                            else if (_timeUnit.equals(TimeUnit.WEEK)) {
068                                    intervalTime = intervalTime * Time.WEEK;
069                            }
070                            else {
071                                    intervalTime = intervalTime * Time.SECOND;
072                            }
073    
074                            _trigger = new IntervalTrigger(
075                                    _eventListenerClass, _eventListenerClass, intervalTime);
076                    }
077                    else {
078                            throw new SchedulerException(
079                                    "Unsupport trigger type " + _triggerType);
080                    }
081    
082                    return _trigger;
083            }
084    
085            public TriggerType getTriggerType() {
086                    return _triggerType;
087            }
088    
089            public String getTriggerValue() {
090                    return _triggerValue;
091            }
092    
093            public void setDescription(String description) {
094                    _description = description;
095            }
096    
097            public void setEventListener(MessageListener eventListener) {
098                    _eventListener = eventListener;
099            }
100    
101            public void setEventListenerClass(String eventListenerClass) {
102                    _eventListenerClass = eventListenerClass;
103            }
104    
105            public void setPropertyKey(String propertyKey) {
106                    _propertyKey = propertyKey;
107            }
108    
109            public void setTimeUnit(TimeUnit timeUnit) {
110                    _timeUnit = timeUnit;
111            }
112    
113            public void setTriggerType(TriggerType triggerType) {
114                    _triggerType = triggerType;
115            }
116    
117            public void setTriggerValue(int triggerValue) {
118                    _triggerValue = String.valueOf(triggerValue);
119            }
120    
121            public void setTriggerValue(long triggerValue) {
122                    _triggerValue = String.valueOf(triggerValue);
123            }
124    
125            public void setTriggerValue(String triggerValue) {
126                    _triggerValue = triggerValue;
127            }
128    
129            public String toString() {
130                    StringBuilder sb = new StringBuilder();
131    
132                    sb.append("{description=");
133                    sb.append(_description);
134                    sb.append(", eventListener=");
135                    sb.append(_eventListener);
136                    sb.append(", eventListenerClass=");
137                    sb.append(_eventListenerClass);
138                    sb.append(", propertyKey=");
139                    sb.append(_propertyKey);
140                    sb.append(", timeUnit=");
141                    sb.append(_timeUnit);
142                    sb.append(", trigger=");
143                    sb.append(_trigger);
144                    sb.append(", triggerType=");
145                    sb.append(_triggerType);
146                    sb.append(", triggerValue=");
147                    sb.append(_triggerValue);
148                    sb.append("}");
149    
150                    return sb.toString();
151            }
152    
153            private String _description;
154            private transient MessageListener _eventListener;
155            private String _eventListenerClass;
156            private String _propertyKey;
157            private TimeUnit _timeUnit;
158            private Trigger _trigger;
159            private TriggerType _triggerType;
160            private String _triggerValue;
161    
162    }