001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.poller.comet;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.notifications.ChannelHubManagerUtil;
023    import com.liferay.portal.kernel.notifications.NotificationEvent;
024    import com.liferay.portal.kernel.notifications.UnknownChannelException;
025    import com.liferay.portal.kernel.poller.comet.CometRequest;
026    import com.liferay.portal.kernel.poller.comet.CometResponse;
027    import com.liferay.portal.kernel.poller.comet.CometSession;
028    
029    import java.util.List;
030    
031    /**
032     * @author Edward Han
033     */
034    public class PollerCometDelayedTask {
035    
036            public PollerCometDelayedTask(
037                    CometSession cometSession, JSONObject pollerResponseHeaderJSONObject) {
038    
039                    _cometSession = cometSession;
040                    _pollerResponseHeaderJSONObject = pollerResponseHeaderJSONObject;
041            }
042    
043            public void executeTask() throws Exception {
044                    CometRequest cometRequest = _cometSession.getCometRequest();
045    
046                    try {
047                            List<NotificationEvent> notificationEvents =
048                                    ChannelHubManagerUtil.getNotificationEvents(
049                                            cometRequest.getCompanyId(), cometRequest.getUserId(),
050                                            false);
051    
052                            JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
053    
054                            if (_pollerResponseHeaderJSONObject != null) {
055                                    jsonArray.put(_pollerResponseHeaderJSONObject);
056                            }
057    
058                            for (NotificationEvent notificationEvent : notificationEvents) {
059                                    jsonArray.put(notificationEvent.toJSONObject());
060                            }
061    
062                            CometResponse cometResponse = _cometSession.getCometResponse();
063    
064                            cometResponse.writeData(jsonArray.toString());
065    
066                            ChannelHubManagerUtil.removeTransientNotificationEvents(
067                                    cometRequest.getCompanyId(), cometRequest.getUserId(),
068                                    notificationEvents);
069                    }
070                    catch (UnknownChannelException uce) {
071                            if (_log.isDebugEnabled()) {
072                                    _log.debug(
073                                            "Unable to complete processing because user session ended",
074                                            uce);
075                            }
076                    }
077                    finally {
078                            _cometSession.close();
079                    }
080            }
081    
082            private static Log _log = LogFactoryUtil.getLog(
083                    PollerCometDelayedTask.class);
084    
085            private CometSession _cometSession;
086            private JSONObject _pollerResponseHeaderJSONObject;
087    
088    }