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.configuration;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys;
025    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
026    import com.liferay.portlet.exportimport.service.ExportImportConfigurationLocalServiceUtil;
027    
028    import java.io.Serializable;
029    
030    import java.util.Map;
031    
032    import javax.portlet.PortletRequest;
033    
034    /**
035     * @author Levente Hud??k
036     */
037    @ProviderType
038    public class ExportImportConfigurationHelper {
039    
040            public static ExportImportConfiguration
041                            addExportLayoutExportImportConfiguration(
042                                    PortletRequest portletRequest)
043                    throws PortalException {
044    
045                    return addExportImportConfiguration(
046                            portletRequest,
047                            ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT);
048            }
049    
050            public static ExportImportConfiguration
051                            addPublishLayoutLocalExportImportConfiguration(
052                                    PortletRequest portletRequest)
053                    throws PortalException {
054    
055                    return addExportImportConfiguration(
056                            portletRequest,
057                            ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_LOCAL);
058            }
059    
060            public static ExportImportConfiguration
061                            addPublishLayoutRemoteExportImportConfiguration(
062                                    PortletRequest portletRequest)
063                    throws PortalException {
064    
065                    return addExportImportConfiguration(
066                            portletRequest,
067                            ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_REMOTE);
068            }
069    
070            public static ExportImportConfiguration
071                            updateExportLayoutExportImportConfiguration(
072                                    PortletRequest portletRequest)
073                    throws PortalException {
074    
075                    return updateExportImportConfiguration(
076                            portletRequest,
077                            ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT);
078            }
079    
080            public static ExportImportConfiguration
081                            updatePublishLayoutLocalExportImportConfiguration(
082                                    PortletRequest portletRequest)
083                    throws PortalException {
084    
085                    return updateExportImportConfiguration(
086                            portletRequest,
087                            ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_LOCAL);
088            }
089    
090            public static ExportImportConfiguration
091                            updatePublishLayoutRemoteExportImportConfiguration(
092                                    PortletRequest portletRequest)
093                    throws PortalException {
094    
095                    return updateExportImportConfiguration(
096                            portletRequest,
097                            ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_REMOTE);
098            }
099    
100            protected static ExportImportConfiguration addExportImportConfiguration(
101                            PortletRequest portletRequest, int type)
102                    throws PortalException {
103    
104                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
105                            WebKeys.THEME_DISPLAY);
106    
107                    long groupId = ParamUtil.getLong(portletRequest, "groupId");
108    
109                    if (type == ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT) {
110                            groupId = ParamUtil.getLong(portletRequest, "liveGroupId");
111                    }
112    
113                    String name = ParamUtil.getString(portletRequest, "name");
114                    String description = ParamUtil.getString(portletRequest, "description");
115    
116                    Map<String, Serializable> settingsMap =
117                            ExportImportConfigurationSettingsMapFactory.buildSettingsMap(
118                                    portletRequest, groupId, type);
119    
120                    Map<String, String[]> parameterMap =
121                            (Map<String, String[]>)settingsMap.get("parameterMap");
122    
123                    if ((parameterMap != null) &&
124                            (type ==
125                                    ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_LOCAL)) {
126    
127                            parameterMap.put(
128                                    PortletDataHandlerKeys.PERFORM_DIRECT_BINARY_IMPORT,
129                                    new String[] {Boolean.TRUE.toString()});
130                    }
131    
132                    return ExportImportConfigurationLocalServiceUtil.
133                            addExportImportConfiguration(
134                                    themeDisplay.getUserId(), groupId, name, description, type,
135                                    settingsMap, new ServiceContext());
136            }
137    
138            protected static ExportImportConfiguration updateExportImportConfiguration(
139                            PortletRequest portletRequest, int type)
140                    throws PortalException {
141    
142                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
143                            WebKeys.THEME_DISPLAY);
144    
145                    long exportImportConfigurationId = ParamUtil.getLong(
146                            portletRequest, "exportImportConfigurationId");
147    
148                    long groupId = ParamUtil.getLong(portletRequest, "groupId");
149                    String name = ParamUtil.getString(portletRequest, "name");
150                    String description = ParamUtil.getString(portletRequest, "description");
151    
152                    Map<String, Serializable> settingsMap =
153                            ExportImportConfigurationSettingsMapFactory.buildSettingsMap(
154                                    portletRequest, groupId, type);
155    
156                    return ExportImportConfigurationLocalServiceUtil.
157                            updateExportImportConfiguration(
158                                    themeDisplay.getUserId(), exportImportConfigurationId, name,
159                                    description, settingsMap, new ServiceContext());
160            }
161    
162    }