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.portlet.exportimport.backgroundtask;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskStatus;
018    import com.liferay.portal.kernel.messaging.Message;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LongWrapper;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.util.HashMap;
025    
026    /**
027     * @author Daniel Kocsis
028     */
029    public class PortletStagingBackgroundTaskStatusMessageTranslator
030            extends DefaultExportImportBackgroundTaskStatusMessageTranslator {
031    
032            protected long getAllModelAdditionCountersTotal(
033                    BackgroundTaskStatus backgroundTaskStatus) {
034    
035                    long allModelAdditionCountersTotal = GetterUtil.getLong(
036                            backgroundTaskStatus.getAttribute("allModelAdditionCountersTotal"));
037                    long currentModelAdditionCountersTotal = GetterUtil.getLong(
038                            backgroundTaskStatus.getAttribute(
039                                    "currentModelAdditionCountersTotal"));
040    
041                    return allModelAdditionCountersTotal +
042                            currentModelAdditionCountersTotal;
043            }
044    
045            @Override
046            protected synchronized void translatePortletMessage(
047                    BackgroundTaskStatus backgroundTaskStatus, Message message) {
048    
049                    String phase = GetterUtil.getString(
050                            backgroundTaskStatus.getAttribute("phase"));
051    
052                    if (Validator.isNull(phase)) {
053                            clearBackgroundTaskStatus(backgroundTaskStatus);
054    
055                            phase = Constants.EXPORT;
056                    }
057                    else {
058                            phase = Constants.IMPORT;
059                    }
060    
061                    backgroundTaskStatus.setAttribute("phase", phase);
062    
063                    if (phase.equals(Constants.EXPORT)) {
064                            long portletModelAdditionCountersTotal = GetterUtil.getLong(
065                                    message.get("portletModelAdditionCountersTotal"));
066    
067                            backgroundTaskStatus.setAttribute(
068                                    "allModelAdditionCountersTotal",
069                                    portletModelAdditionCountersTotal);
070                    }
071                    else {
072                            backgroundTaskStatus.setAttribute(
073                                    "allModelAdditionCountersTotal",
074                                    getAllModelAdditionCountersTotal(backgroundTaskStatus));
075                            backgroundTaskStatus.setAttribute(
076                                    "allPortletModelAdditionCounters",
077                                    new HashMap<String, LongWrapper>());
078                            backgroundTaskStatus.setAttribute(
079                                    "currentPortletModelAdditionCounters",
080                                    new HashMap<String, LongWrapper>());
081                    }
082    
083                    super.translatePortletMessage(backgroundTaskStatus, message);
084            }
085    
086    }