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.lar;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskStatusMessage;
018    import com.liferay.portal.kernel.util.LongWrapper;
019    import com.liferay.portal.model.Portlet;
020    import com.liferay.portal.model.StagedModel;
021    import com.liferay.portal.service.PortletLocalServiceUtil;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    /**
027     * @author Andrew Betts
028     */
029    public class PortletDataHandlerBackgroundTaskStatusMessage
030            extends BackgroundTaskStatusMessage {
031    
032            public PortletDataHandlerBackgroundTaskStatusMessage(
033                    String messageType, String portletId, ManifestSummary manifestSummary) {
034    
035                    init(messageType, manifestSummary);
036    
037                    put("portletId", portletId);
038    
039                    Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
040    
041                    if (portlet != null) {
042                            PortletDataHandler portletDataHandler =
043                                    portlet.getPortletDataHandlerInstance();
044    
045                            long portletModelAdditionCountersTotal =
046                                    portletDataHandler.getExportModelCount(manifestSummary);
047    
048                            if (portletModelAdditionCountersTotal < 0) {
049                                    portletModelAdditionCountersTotal = 0;
050                            }
051    
052                            put(
053                                    "portletModelAdditionCountersTotal",
054                                    portletModelAdditionCountersTotal);
055                    }
056            }
057    
058            public PortletDataHandlerBackgroundTaskStatusMessage(
059                    String messageType, String[] portletIds,
060                    ManifestSummary manifestSummary) {
061    
062                    init(messageType, manifestSummary);
063    
064                    put("portletIds", portletIds);
065            }
066    
067            public <T extends StagedModel> PortletDataHandlerBackgroundTaskStatusMessage(
068                    String messageType, T stagedModel, ManifestSummary manifestSummary) {
069    
070                    init(messageType, manifestSummary);
071    
072                    StagedModelDataHandler<T> stagedModelDataHandler =
073                            (StagedModelDataHandler<T>)
074                                    StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
075                                            stagedModel.getModelClassName());
076    
077                    put(
078                            "stagedModelName",
079                            stagedModelDataHandler.getDisplayName(stagedModel));
080    
081                    put(
082                            "stagedModelType",
083                            String.valueOf(stagedModel.getStagedModelType()));
084    
085                    put("uuid", stagedModel.getUuid());
086            }
087    
088            protected void init(String messageType, ManifestSummary manifestSummary) {
089                    put("messageType", messageType);
090    
091                    Map<String, LongWrapper> modelAdditionCounters =
092                            manifestSummary.getModelAdditionCounters();
093    
094                    put("modelAdditionCounters", new HashMap<>(modelAdditionCounters));
095    
096                    Map<String, LongWrapper> modelDeletionCounters =
097                            manifestSummary.getModelDeletionCounters();
098    
099                    put("modelDeletionCounters", new HashMap<>(modelDeletionCounters));
100            }
101    
102    }