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.lar;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskThreadLocal;
018    import com.liferay.portal.kernel.lar.ManifestSummary;
019    import com.liferay.portal.kernel.lar.PortletDataHandler;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerStatusMessageSender;
021    import com.liferay.portal.kernel.lar.StagedModelDataHandler;
022    import com.liferay.portal.kernel.lar.StagedModelDataHandlerRegistryUtil;
023    import com.liferay.portal.kernel.messaging.Message;
024    import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
025    import com.liferay.portal.kernel.util.LongWrapper;
026    import com.liferay.portal.model.Portlet;
027    import com.liferay.portal.model.StagedModel;
028    import com.liferay.portal.service.PortletLocalServiceUtil;
029    
030    import java.util.HashMap;
031    import java.util.Map;
032    
033    /**
034     * @author Michael C. Han
035     */
036    public class PortletDataHandlerStatusMessageSenderImpl
037            implements PortletDataHandlerStatusMessageSender {
038    
039            /**
040             * @deprecated As of 7.0.0, replaced by {@link #sendStatusMessage(String,
041             *             String[], ManifestSummary)}
042             */
043            @Deprecated
044            @Override
045            public void sendStatusMessage(
046                    String messageType, ManifestSummary manifestSummary) {
047    
048                    sendStatusMessage(messageType, (String[])null, manifestSummary);
049            }
050    
051            @Override
052            public void sendStatusMessage(
053                    String messageType, String portletId, ManifestSummary manifestSummary) {
054    
055                    if (!BackgroundTaskThreadLocal.hasBackgroundTask()) {
056                            return;
057                    }
058    
059                    Message message = createMessage(messageType, manifestSummary);
060    
061                    message.put("portletId", portletId);
062    
063                    Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
064    
065                    if (portlet != null) {
066                            PortletDataHandler portletDataHandler =
067                                    portlet.getPortletDataHandlerInstance();
068    
069                            long portletModelAdditionCountersTotal =
070                                    portletDataHandler.getExportModelCount(manifestSummary);
071    
072                            if (portletModelAdditionCountersTotal < 0) {
073                                    portletModelAdditionCountersTotal = 0;
074                            }
075    
076                            message.put(
077                                    "portletModelAdditionCountersTotal",
078                                    portletModelAdditionCountersTotal);
079                    }
080    
081                    _singleDestinationMessageSender.send(message);
082            }
083    
084            @Override
085            public void sendStatusMessage(
086                    String messageType, String[] portletIds,
087                    ManifestSummary manifestSummary) {
088    
089                    if (!BackgroundTaskThreadLocal.hasBackgroundTask()) {
090                            return;
091                    }
092    
093                    Message message = createMessage(messageType, manifestSummary);
094    
095                    message.put("portletIds", portletIds);
096    
097                    _singleDestinationMessageSender.send(message);
098            }
099    
100            @Override
101            public <T extends StagedModel> void sendStatusMessage(
102                    String messageType, T stagedModel, ManifestSummary manifestSummary) {
103    
104                    if (!BackgroundTaskThreadLocal.hasBackgroundTask()) {
105                            return;
106                    }
107    
108                    Message message = createMessage(messageType, manifestSummary);
109    
110                    StagedModelDataHandler<T> stagedModelDataHandler =
111                            (StagedModelDataHandler<T>)
112                                    StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
113                                            stagedModel.getModelClassName());
114    
115                    message.put(
116                            "stagedModelName",
117                            stagedModelDataHandler.getDisplayName(stagedModel));
118    
119                    message.put(
120                            "stagedModelType",
121                            String.valueOf(stagedModel.getStagedModelType()));
122                    message.put("uuid", stagedModel.getUuid());
123    
124                    _singleDestinationMessageSender.send(message);
125            }
126    
127            public void setSingleDestinationMessageSender(
128                    SingleDestinationMessageSender singleDestinationMessageSender) {
129    
130                    _singleDestinationMessageSender = singleDestinationMessageSender;
131            }
132    
133            protected Message createMessage(
134                    String messageType, ManifestSummary manifestSummary) {
135    
136                    Message message = new Message();
137    
138                    message.put(
139                            "backgroundTaskId",
140                            BackgroundTaskThreadLocal.getBackgroundTaskId());
141                    message.put("messageType", messageType);
142    
143                    Map<String, LongWrapper> modelAdditionCounters =
144                            manifestSummary.getModelAdditionCounters();
145    
146                    message.put(
147                            "modelAdditionCounters",
148                            new HashMap<String, LongWrapper>(modelAdditionCounters));
149    
150                    Map<String, LongWrapper> modelDeletionCounters =
151                            manifestSummary.getModelDeletionCounters();
152    
153                    message.put(
154                            "modelDeletionCounters",
155                            new HashMap<String, LongWrapper>(modelDeletionCounters));
156    
157                    return message;
158            }
159    
160            private SingleDestinationMessageSender _singleDestinationMessageSender;
161    
162    }