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.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.LongWrapper;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class DefaultExportImportBackgroundTaskStatusMessageTranslator
030            implements BackgroundTaskStatusMessageTranslator {
031    
032            @Override
033            public void translate(
034                    BackgroundTaskStatus backgroundTaskStatus, Message message) {
035    
036                    String messageType = message.getString("messageType");
037    
038                    if (messageType.equals("layout")) {
039                            translateLayoutMessage(backgroundTaskStatus, message);
040                    }
041                    else if (messageType.equals("portlet")) {
042                            translatePortletMessage(backgroundTaskStatus, message);
043                    }
044                    else if (messageType.equals("stagedModel")) {
045                            translateStagedModelMessage(backgroundTaskStatus, message);
046                    }
047            }
048    
049            protected long getTotal(Map<String, LongWrapper> modelCounters) {
050                    if (modelCounters == null) {
051                            return 0;
052                    }
053    
054                    long total = 0;
055    
056                    for (Map.Entry<String, LongWrapper> entry : modelCounters.entrySet()) {
057                            LongWrapper longWrapper = entry.getValue();
058    
059                            total += longWrapper.getValue();
060                    }
061    
062                    return total;
063            }
064    
065            protected synchronized void translateLayoutMessage(
066                    BackgroundTaskStatus backgroundTaskStatus, Message message) {
067    
068                    Map<String, LongWrapper> modelAdditionCounters =
069                            (Map<String, LongWrapper>)message.get("modelAdditionCounters");
070    
071                    backgroundTaskStatus.setAttribute(
072                            "allModelAdditionCounters",
073                            new HashMap<String, LongWrapper>(modelAdditionCounters));
074                    backgroundTaskStatus.setAttribute(
075                            "allModelAdditionCountersTotal", getTotal(modelAdditionCounters));
076    
077                    Map<String, LongWrapper> modelDeletionCounters =
078                            (Map<String, LongWrapper>)message.get("modelDeletionCounters");
079    
080                    backgroundTaskStatus.setAttribute(
081                            "allModelDeletionCounters",
082                            new HashMap<String, LongWrapper>(modelDeletionCounters));
083                    backgroundTaskStatus.setAttribute(
084                            "allModelDeletionCountersTotal", getTotal(modelDeletionCounters));
085            }
086    
087            protected synchronized void translatePortletMessage(
088                    BackgroundTaskStatus backgroundTaskStatus, Message message) {
089    
090                    backgroundTaskStatus.clearAttributes();
091    
092                    Map<String, LongWrapper> modelAdditionCounters =
093                            (Map<String, LongWrapper>)message.get("modelAdditionCounters");
094    
095                    backgroundTaskStatus.setAttribute(
096                            "allModelAdditionCounters",
097                            new HashMap<String, LongWrapper>(modelAdditionCounters));
098                    backgroundTaskStatus.setAttribute(
099                            "allModelAdditionCountersTotal", getTotal(modelAdditionCounters));
100    
101                    Map<String, LongWrapper> modelDeletionCounters =
102                            (Map<String, LongWrapper>)message.get("modelDeletionCounters");
103    
104                    backgroundTaskStatus.setAttribute(
105                            "allModelDeletionCounters",
106                            new HashMap<String, LongWrapper>(modelDeletionCounters));
107                    backgroundTaskStatus.setAttribute(
108                            "allModelDeletionCountersTotal", getTotal(modelDeletionCounters));
109    
110                    String portletId = message.getString("portletId");
111    
112                    backgroundTaskStatus.setAttribute("portletId", portletId);
113            }
114    
115            protected synchronized void translateStagedModelMessage(
116                    BackgroundTaskStatus backgroundTaskStatus, Message message) {
117    
118                    String portletId = (String)backgroundTaskStatus.getAttribute(
119                            "portletId");
120    
121                    if (Validator.isNull(portletId)) {
122                            return;
123                    }
124    
125                    Map<String, LongWrapper> modelAdditionCounters =
126                            (Map<String, LongWrapper>)message.get("modelAdditionCounters");
127    
128                    backgroundTaskStatus.setAttribute(
129                            "currentModelAdditionCounters",
130                            new HashMap<String, LongWrapper>(modelAdditionCounters));
131                    backgroundTaskStatus.setAttribute(
132                            "currentModelAdditionCountersTotal",
133                            getTotal(modelAdditionCounters));
134    
135                    Map<String, LongWrapper> modelDeletionCounters =
136                            (Map<String, LongWrapper>)message.get("modelDeletionCounters");
137    
138                    backgroundTaskStatus.setAttribute(
139                            "currentModelDeletionCounters",
140                            new HashMap<String, LongWrapper>(modelDeletionCounters));
141                    backgroundTaskStatus.setAttribute(
142                            "currentModelDeletionCountersTotal",
143                            getTotal(modelDeletionCounters));
144    
145                    String stagedModelName = message.getString("stagedModelName");
146    
147                    backgroundTaskStatus.setAttribute("stagedModelName", stagedModelName);
148    
149                    String stagedModelType = message.getString("stagedModelType");
150    
151                    backgroundTaskStatus.setAttribute("stagedModelType", stagedModelType);
152    
153                    String uuid = message.getString("uuid");
154    
155                    backgroundTaskStatus.setAttribute("uuid", uuid);
156            }
157    
158    }