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.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 LayoutStagingBackgroundTaskStatusMessageTranslator
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            protected long getAllPortletAdditionCounter(
046                    BackgroundTaskStatus backgroundTaskStatus) {
047    
048                    long allPortletAdditionCounter = GetterUtil.getLong(
049                            backgroundTaskStatus.getAttribute("allPortletAdditionCounter"));
050                    long currentPortletAdditionCounter = GetterUtil.getLong(
051                            backgroundTaskStatus.getAttribute("currentPortletAdditionCounter"));
052    
053                    return allPortletAdditionCounter + currentPortletAdditionCounter;
054            }
055    
056            @Override
057            protected synchronized void translateLayoutMessage(
058                    BackgroundTaskStatus backgroundTaskStatus, Message message) {
059    
060                    String phase = GetterUtil.getString(
061                            backgroundTaskStatus.getAttribute("phase"));
062    
063                    if (Validator.isNull(phase)) {
064                            clearBackgroundTaskStatus(backgroundTaskStatus);
065    
066                            phase = Constants.EXPORT;
067                    }
068                    else {
069                            phase = Constants.IMPORT;
070                    }
071    
072                    backgroundTaskStatus.setAttribute("phase", phase);
073    
074                    super.translateLayoutMessage(backgroundTaskStatus, message);
075    
076                    if (phase.equals(Constants.IMPORT)) {
077                            backgroundTaskStatus.setAttribute(
078                                    "allModelAdditionCountersTotal",
079                                    getAllModelAdditionCountersTotal(backgroundTaskStatus));
080                            backgroundTaskStatus.setAttribute(
081                                    "allPortletAdditionCounter",
082                                    getAllPortletAdditionCounter(backgroundTaskStatus));
083                            backgroundTaskStatus.setAttribute(
084                                    "allPortletModelAdditionCounters",
085                                    new HashMap<String, LongWrapper>());
086                            backgroundTaskStatus.setAttribute(
087                                    "currentPortletModelAdditionCounters",
088                                    new HashMap<String, LongWrapper>());
089                    }
090            }
091    
092    }