001    /**
002     * Copyright (c) 2000-present 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.backgroundtask;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskStatus;
018    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskStatusMessageTranslator;
019    import com.liferay.portal.kernel.messaging.Message;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LongWrapper;
022    import com.liferay.portal.kernel.util.MapUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    import java.util.HashMap;
027    import java.util.Map;
028    
029    /**
030     * @author Michael C. Han
031     * @author Daniel Kocsis
032     */
033    public class DefaultExportImportBackgroundTaskStatusMessageTranslator
034            implements BackgroundTaskStatusMessageTranslator {
035    
036            @Override
037            public void translate(
038                    BackgroundTaskStatus backgroundTaskStatus, Message message) {
039    
040                    String messageType = message.getString("messageType");
041    
042                    if (messageType.equals("layout")) {
043                            translateLayoutMessage(backgroundTaskStatus, message);
044                    }
045                    else if (messageType.equals("portlet")) {
046                            translatePortletMessage(backgroundTaskStatus, message);
047                    }
048                    else if (messageType.equals("stagedModel")) {
049                            translateStagedModelMessage(backgroundTaskStatus, message);
050                    }
051            }
052    
053            protected void clearBackgroundTaskStatus(
054                    BackgroundTaskStatus backgroundTaskStatus) {
055    
056                    backgroundTaskStatus.clearAttributes();
057    
058                    backgroundTaskStatus.setAttribute("allModelAdditionCountersTotal", 0L);
059                    backgroundTaskStatus.setAttribute("allPortletAdditionCounter", 0L);
060                    backgroundTaskStatus.setAttribute(
061                            "allPortletModelAdditionCounters",
062                            new HashMap<String, LongWrapper>());
063                    backgroundTaskStatus.setAttribute(
064                            "currentModelAdditionCountersTotal", 0L);
065                    backgroundTaskStatus.setAttribute("currentPortletAdditionCounter", 0L);
066                    backgroundTaskStatus.setAttribute(
067                            "currentPortletModelAdditionCounters",
068                            new HashMap<String, LongWrapper>());
069            }
070    
071            protected long getTotal(Map<String, LongWrapper> modelCounters) {
072                    if (modelCounters == null) {
073                            return 0;
074                    }
075    
076                    long total = 0;
077    
078                    for (Map.Entry<String, LongWrapper> entry : modelCounters.entrySet()) {
079                            LongWrapper longWrapper = entry.getValue();
080    
081                            total += longWrapper.getValue();
082                    }
083    
084                    return total;
085            }
086    
087            protected synchronized void translateLayoutMessage(
088                    BackgroundTaskStatus backgroundTaskStatus, Message message) {
089    
090                    Map<String, LongWrapper> modelAdditionCounters =
091                            (Map<String, LongWrapper>)message.get("modelAdditionCounters");
092    
093                    backgroundTaskStatus.setAttribute(
094                            "allModelAdditionCountersTotal", getTotal(modelAdditionCounters));
095    
096                    long allPortletAdditionCounter = 0;
097    
098                    String[] portletIds = (String[])message.get("portletIds");
099    
100                    if (portletIds != null) {
101                            allPortletAdditionCounter = portletIds.length;
102                    }
103    
104                    backgroundTaskStatus.setAttribute(
105                            "allPortletAdditionCounter", allPortletAdditionCounter);
106            }
107    
108            protected synchronized void translatePortletMessage(
109                    BackgroundTaskStatus backgroundTaskStatus, Message message) {
110    
111                    String portletId = message.getString("portletId");
112    
113                    HashMap<String, Long> allPortletModelAdditionCounters =
114                            (HashMap<String, Long>)backgroundTaskStatus.getAttribute(
115                                    "allPortletModelAdditionCounters");
116    
117                    long portletModelAdditionCountersTotal = GetterUtil.getLong(
118                            message.get("portletModelAdditionCountersTotal"));
119    
120                    allPortletModelAdditionCounters.put(
121                            portletId, portletModelAdditionCountersTotal);
122    
123                    backgroundTaskStatus.setAttribute(
124                            "allPortletModelAdditionCounters", allPortletModelAdditionCounters);
125    
126                    long allPortletAdditionCounter = GetterUtil.getLong(
127                            backgroundTaskStatus.getAttribute("allPortletAdditionCounter"));
128                    long currentPortletAdditionCounter = GetterUtil.getLong(
129                            backgroundTaskStatus.getAttribute("currentPortletAdditionCounter"));
130    
131                    if (currentPortletAdditionCounter < allPortletAdditionCounter) {
132                            backgroundTaskStatus.setAttribute(
133                                    "currentPortletAdditionCounter",
134                                    ++currentPortletAdditionCounter);
135                    }
136    
137                    HashMap<String, Long> currentPortletModelAdditionCounters =
138                            (HashMap<String, Long>)backgroundTaskStatus.getAttribute(
139                                    "currentPortletModelAdditionCounters");
140    
141                    currentPortletModelAdditionCounters.put(portletId, 0L);
142    
143                    backgroundTaskStatus.setAttribute(
144                            "currentPortletModelAdditionCounters",
145                            currentPortletModelAdditionCounters);
146    
147                    backgroundTaskStatus.setAttribute("portletId", portletId);
148                    backgroundTaskStatus.setAttribute("stagedModelName", StringPool.BLANK);
149                    backgroundTaskStatus.setAttribute("stagedModelType", StringPool.BLANK);
150                    backgroundTaskStatus.setAttribute("uuid", StringPool.BLANK);
151            }
152    
153            protected synchronized void translateStagedModelMessage(
154                    BackgroundTaskStatus backgroundTaskStatus, Message message) {
155    
156                    String portletId = (String)backgroundTaskStatus.getAttribute(
157                            "portletId");
158    
159                    if (Validator.isNull(portletId)) {
160                            return;
161                    }
162    
163                    long allModelAdditionCountersTotal = GetterUtil.getLong(
164                            backgroundTaskStatus.getAttribute("allModelAdditionCountersTotal"));
165                    long currentModelAdditionCountersTotal = GetterUtil.getLong(
166                            backgroundTaskStatus.getAttribute(
167                                    "currentModelAdditionCountersTotal"));
168    
169                    Map<String, Long> allPortletModelAdditionCounters =
170                            (HashMap<String, Long>)backgroundTaskStatus.getAttribute(
171                                    "allPortletModelAdditionCounters");
172    
173                    long allPortletModelAdditionCounter = MapUtil.getLong(
174                            allPortletModelAdditionCounters, portletId);
175    
176                    HashMap<String, Long> currentPortletModelAdditionCounters =
177                            (HashMap<String, Long>)backgroundTaskStatus.getAttribute(
178                                    "currentPortletModelAdditionCounters");
179    
180                    long currentPortletModelAdditionCounter = MapUtil.getLong(
181                            currentPortletModelAdditionCounters, portletId);
182    
183                    if ((allModelAdditionCountersTotal >
184                                    currentModelAdditionCountersTotal) &&
185                            (allPortletModelAdditionCounter >
186                                    currentPortletModelAdditionCounter)) {
187    
188                            backgroundTaskStatus.setAttribute(
189                                    "currentModelAdditionCountersTotal",
190                                    ++currentModelAdditionCountersTotal);
191    
192                            currentPortletModelAdditionCounters.put(
193                                    portletId, ++currentPortletModelAdditionCounter);
194    
195                            backgroundTaskStatus.setAttribute(
196                                    "currentPortletModelAdditionCounters",
197                                    currentPortletModelAdditionCounters);
198                    }
199    
200                    backgroundTaskStatus.setAttribute(
201                            "stagedModelName", message.getString("stagedModelName"));
202                    backgroundTaskStatus.setAttribute(
203                            "stagedModelType", message.getString("stagedModelType"));
204                    backgroundTaskStatus.setAttribute("uuid", message.getString("uuid"));
205            }
206    
207    }