001//
002//  Copyright 2013 Cloud Software Group, Inc. ALL RIGHTS RESERVED. 
003//  Cloud Software Group, Inc. Confidential Information
004//
005//  $Revision: 1.5 $ $Date: 2013/07/01 22:45:59 $
006//
007package com.tibco.xp.runtime;
008
009import com.kabira.platform.ManagedObject;
010import com.kabira.platform.annotation.ByValue;
011import com.kabira.platform.annotation.Managed;
012import com.tibco.cep.runtime.model.event.impl.SimpleEventImpl;
013import com.tibco.cep.runtime.model.event.impl.TimeEventImpl;
014
015/**
016 * Timed event base class
017 */
018@Managed
019public abstract class Timed extends Event 
020{
021        /**
022         * Java constructor
023         */
024        public Timed() 
025        {
026        }
027        
028        /**
029         * Rule or Rule Function constructor
030         * @param id Unique identifier
031         */
032        public Timed(long id) 
033        {
034            register(id);
035        }
036        
037        @Override
038        public String toString()
039        {
040                StringBuilder value = new StringBuilder(super.toString());
041                
042                value.append("@closure=");
043                value.append(getClosure());
044                value.append("@scheduledTime=");
045                value.append(getScheduledTime());
046                value.append("@interval=");
047                value.append(getInterval());
048                value.append(" ");
049                value.append(getIntervalUnits());
050                value.append("&fired=");
051                value.append(getFired());
052                return value.toString();
053        }
054        
055        /**
056         * Delete this event.
057         * <p>
058         * If this event currently has a TTL expiration timer
059         * scheduled, the timer is canceled.
060         */
061        @Override
062        public final void delete()
063        {
064                if (getTimerIdentifier().equals(_INVALID_TIMER_IDENTIFIER) == false)
065                {
066                        assert TransactionService.getInstance() != null : this;
067                        TransactionService.getInstance().cancelExpirationTimer(this);
068                }
069                
070                //
071                //      Delete the event
072                //
073                ManagedObject.delete(this);
074        }
075        
076        @Override
077        public final Boolean getRetryOnException() 
078        {
079                return m_retryOnException;
080        }
081
082        @Override
083        public final Long getTimeToLive() 
084        {
085                return m_timeToLive;
086        }
087
088        @Override
089        public final String getTimeToLiveUnits() 
090        {
091                return m_timeToLiveUnits;
092        }
093        
094        /**
095         * Update user properties from shared memory
096         * @param eventImpl Event handle to update
097         */
098        protected abstract void updateUserProperties(SimpleEventImpl eventImpl);
099
100        @Override
101        protected final void setRetryOnException(Boolean retryOnException) 
102        {
103                m_retryOnException = retryOnException;
104        }
105
106        @Override
107        public final PayLoad getPayLoad() 
108        {
109                return m_payload;
110        }
111        
112        @Override
113        public final void setDestination(final String destination) 
114        {
115                enforceDispatchState();
116                
117                m_destination = destination;
118        }
119        
120        @Override
121        public final String getDestination() 
122        {
123                return m_destination;
124        }
125        
126        @Override
127        protected final void updatePayLoad(PayLoad payload) 
128        {
129                m_payload = payload;
130        }
131
132        @Override
133        protected final void setTimeToLive(Long timeToLive) 
134        {
135                m_timeToLive = timeToLive;
136        }
137
138        @Override
139        protected final void setTimeToLiveUnits(String timeToLiveUnits) 
140        {
141                m_timeToLiveUnits = timeToLiveUnits;
142        }
143
144        @Override
145        protected final Boolean getDispatched() 
146        {
147                return m_dispatched;
148        }
149
150        @Override
151        protected final void setDispatched() 
152        {
153                m_dispatched = true;
154        }
155
156        /**
157         * Get the closure for this timed event
158         * @return Closure value
159         */
160        public final String getClosure()
161        {
162                return m_closure;
163        }
164        
165        /**
166         * Set the closure for this timed event
167         * @param closure Closure value
168         */
169        final void setClosure(final String closure)
170        {
171                m_closure = closure;
172        }
173        
174        /**
175         * Get the next scheduled expiration time
176         * @return Next expiration time
177         */
178        final long getScheduledTime()
179        {
180                return m_scheduledTime;
181        }
182        
183        /**
184         * Set the next scheduled expiration time
185         * @param next Next expiration time
186         */
187        final void setScheduledTime(final long next)
188        {
189                m_scheduledTime = next;
190        }
191        
192        /**
193         * Get the repeat interval
194         * @return Repeat interval in milliseconds
195         */
196        final long getInterval()
197        {
198                return m_interval;
199        }
200        
201        /**
202         * Set the repeat interval
203         * @param interval Repeat interval
204         */
205        protected final void setInterval(final long interval)
206        {
207                m_interval = interval;
208        }
209        
210        /**
211         * Get the repeat interval units
212         * @return Repeat interval units
213         */
214        final String getIntervalUnits()
215        {
216                return m_intervalUnits;
217        }
218        
219        /**
220         * Set the expiration interval units
221         * @param units Repeat interval units
222         */
223        protected final void setIntervalUnits(final String units)
224        {
225                m_intervalUnits = units;
226        }
227        
228        /**
229         * Get the fired value
230         * @return Fired value
231         */
232        public final boolean getFired()
233        {
234                return m_fired;
235        }
236        
237        /**
238         * Set the fired value
239         * @param fired Fired flag
240         */
241        final void setFired(final boolean fired)
242        {
243                m_fired = fired;
244        }
245        
246        @Override
247        protected final void updateProperties()
248        {
249                assert EntityProxy.getProxy(this) instanceof TimedProxy : this;
250        assert getHandle(false) instanceof TimeEventImpl : this;
251        
252        TimedProxy proxy = (TimedProxy)EntityProxy.getProxy(this);
253        TimeEventImpl handle = (TimeEventImpl) getHandle(false);
254
255                proxy.updateMetaProperties(handle);
256        }
257        
258        @Override
259        final String getTimerIdentifier()
260        {
261                return m_timerIdentifier;
262        }
263        
264        @Override
265        final void setTimerIdentifier(final String identifier)
266        {
267                assert identifier != null : this;
268                m_timerIdentifier = identifier;
269        }
270                
271        private boolean m_dispatched = false;
272        private boolean m_retryOnException = _DEFAULT_RETRY_ON_EXCEPTION;
273        private long    m_timeToLive = _DEFAULT_TIME_TO_LIVE;
274        private String  m_timeToLiveUnits = _TIME_TO_LIVE_UNITS_DEFAULT;
275        @ByValue
276        private PayLoad m_payload = null;
277        private String  m_destination = null;
278        private String m_closure = null;
279        private long m_scheduledTime = 0;
280        private long m_interval = 0;
281        private String  m_intervalUnits = _TIME_TO_LIVE_UNITS_DEFAULT;
282        private boolean m_fired = false;
283        private String m_timerIdentifier = _INVALID_TIMER_IDENTIFIER;
284        private static final long serialVersionUID = 3L;
285}