001    /**
002     * Copyright (c) 2000-2013 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.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    }