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 com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.UnicodeProperties;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.util.WebKeys;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.service.GroupLocalServiceUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portlet.exportimport.lar.ExportImportHelperUtil;
031    import com.liferay.portlet.exportimport.staging.StagingUtil;
032    
033    import java.io.Serializable;
034    
035    import java.util.HashMap;
036    import java.util.Locale;
037    import java.util.Map;
038    import java.util.TimeZone;
039    
040    import javax.portlet.PortletRequest;
041    
042    /**
043     * @author Daniel Kocsis
044     * @author Akos Thurzo
045     */
046    public class ExportImportConfigurationSettingsMapFactory {
047    
048            public static Map<String, Serializable> buildExportSettingsMap(
049                    long userId, long sourcePlid, long sourceGroupId, String portletId,
050                    Map<String, String[]> parameterMap, String cmd, Locale locale,
051                    TimeZone timeZone, String fileName) {
052    
053                    return buildSettingsMap(
054                            userId, sourceGroupId, sourcePlid, 0, 0, portletId, null, null,
055                            null, parameterMap, StringPool.BLANK, 0, StringPool.BLANK, null, 0,
056                            null, cmd, locale, timeZone, fileName);
057            }
058    
059            public static Map<String, Serializable> buildImportSettingsMap(
060                    long userId, long targetGroupId, boolean privateLayout,
061                    long[] layoutIds, Map<String, String[]> parameterMap, String cmd,
062                    Locale locale, TimeZone timeZone, String fileName) {
063    
064                    return buildSettingsMap(
065                            userId, 0, 0, targetGroupId, 0, StringPool.BLANK, privateLayout,
066                            null, layoutIds, parameterMap, StringPool.BLANK, 0,
067                            StringPool.BLANK, null, 0, null, cmd, locale, timeZone, fileName);
068            }
069    
070            public static Map<String, Serializable> buildImportSettingsMap(
071                    long userId, long targetPlid, long targetGroupId, String portletId,
072                    Map<String, String[]> parameterMap, String cmd, Locale locale,
073                    TimeZone timeZone, String fileName) {
074    
075                    return buildSettingsMap(
076                            userId, 0, 0, targetGroupId, targetPlid, portletId, null, null,
077                            null, parameterMap, StringPool.BLANK, 0, StringPool.BLANK, null, 0,
078                            null, cmd, locale, timeZone, fileName);
079            }
080    
081            public static Map<String, Serializable> buildSettingsMap(
082                    long userId, long groupId, boolean privateLayout, long[] layoutIds,
083                    Map<String, String[]> parameterMap, Locale locale, TimeZone timeZone) {
084    
085                    return buildSettingsMap(
086                            userId, groupId, 0, privateLayout, layoutIds, parameterMap, locale,
087                            timeZone);
088            }
089    
090            public static Map<String, Serializable> buildSettingsMap(
091                    long userId, long sourceGroupId, boolean privateLayout,
092                    Map<Long, Boolean> layoutIdMap, Map<String, String[]> parameterMap,
093                    String remoteAddress, int remotePort, String remotePathContext,
094                    boolean secureConnection, long remoteGroupId,
095                    boolean remotePrivateLayout, Locale locale, TimeZone timeZone) {
096    
097                    return buildSettingsMap(
098                            userId, sourceGroupId, 0, 0, 0, StringPool.BLANK, privateLayout,
099                            layoutIdMap, null, parameterMap, remoteAddress, remotePort,
100                            remotePathContext, secureConnection, remoteGroupId,
101                            remotePrivateLayout, StringPool.BLANK, locale, timeZone,
102                            StringPool.BLANK);
103            }
104    
105            public static Map<String, Serializable> buildSettingsMap(
106                    long userId, long sourceGroupId, long targetGroupId,
107                    boolean privateLayout, long[] layoutIds,
108                    Map<String, String[]> parameterMap, Locale locale, TimeZone timeZone) {
109    
110                    return buildSettingsMap(
111                            userId, sourceGroupId, 0, targetGroupId, 0, StringPool.BLANK,
112                            privateLayout, null, layoutIds, parameterMap, StringPool.BLANK, 0,
113                            StringPool.BLANK, null, 0, null, StringPool.BLANK, locale, timeZone,
114                            StringPool.BLANK);
115            }
116    
117            public static Map<String, Serializable> buildSettingsMap(
118                    long userId, long sourceGroupId, long sourcePlid, long targetGroupId,
119                    long targetPlid, String portletId, Map<String, String[]> parameterMap,
120                    String cmd, Locale locale, TimeZone timeZone) {
121    
122                    return buildSettingsMap(
123                            userId, sourceGroupId, sourcePlid, targetGroupId, targetPlid,
124                            portletId, null, null, null, parameterMap, StringPool.BLANK, 0,
125                            StringPool.BLANK, null, 0, null, cmd, locale, timeZone, null);
126            }
127    
128            public static Map<String, Serializable> buildSettingsMap(
129                            PortletRequest portletRequest, long groupId, int type)
130                    throws PortalException {
131    
132                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
133                            WebKeys.THEME_DISPLAY);
134    
135                    boolean privateLayout = ParamUtil.getBoolean(
136                            portletRequest, "privateLayout");
137    
138                    Map<Long, Boolean> layoutIdMap = ExportImportHelperUtil.getLayoutIdMap(
139                            portletRequest);
140    
141                    if (type == ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT) {
142                            long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layoutIdMap);
143    
144                            return buildSettingsMap(
145                                    themeDisplay.getUserId(), groupId, privateLayout, layoutIds,
146                                    portletRequest.getParameterMap(), themeDisplay.getLocale(),
147                                    themeDisplay.getTimeZone());
148                    }
149    
150                    Group stagingGroup = GroupLocalServiceUtil.getGroup(groupId);
151    
152                    Group liveGroup = stagingGroup.getLiveGroup();
153    
154                    Map<String, String[]> parameterMap =
155                            ExportImportConfigurationParameterMapFactory.buildParameterMap(
156                                    portletRequest);
157    
158                    if (liveGroup != null) {
159                            long[] layoutIds = ExportImportHelperUtil.getLayoutIds(
160                                    layoutIdMap, liveGroup.getGroupId());
161    
162                            return buildSettingsMap(
163                                    themeDisplay.getUserId(), stagingGroup.getGroupId(),
164                                    liveGroup.getGroupId(), privateLayout, layoutIds, parameterMap,
165                                    themeDisplay.getLocale(), themeDisplay.getTimeZone());
166                    }
167    
168                    UnicodeProperties groupTypeSettingsProperties =
169                            stagingGroup.getTypeSettingsProperties();
170    
171                    String remoteAddress = ParamUtil.getString(
172                            portletRequest, "remoteAddress",
173                            groupTypeSettingsProperties.getProperty("remoteAddress"));
174    
175                    remoteAddress = StagingUtil.stripProtocolFromRemoteAddress(
176                            remoteAddress);
177    
178                    int remotePort = ParamUtil.getInteger(
179                            portletRequest, "remotePort",
180                            GetterUtil.getInteger(
181                                    groupTypeSettingsProperties.getProperty("remotePort")));
182                    String remotePathContext = ParamUtil.getString(
183                            portletRequest, "remotePathContext",
184                            groupTypeSettingsProperties.getProperty("remotePathContext"));
185                    boolean secureConnection = ParamUtil.getBoolean(
186                            portletRequest, "secureConnection",
187                            GetterUtil.getBoolean(
188                                    groupTypeSettingsProperties.getProperty("secureConnection")));
189                    long remoteGroupId = ParamUtil.getLong(
190                            portletRequest, "remoteGroupId",
191                            GetterUtil.getLong(
192                                    groupTypeSettingsProperties.getProperty("remoteGroupId")));
193                    boolean remotePrivateLayout = ParamUtil.getBoolean(
194                            portletRequest, "remotePrivateLayout");
195    
196                    StagingUtil.validateRemote(
197                            groupId, remoteAddress, remotePort, remotePathContext,
198                            secureConnection, remoteGroupId);
199    
200                    return buildSettingsMap(
201                            themeDisplay.getUserId(), groupId, privateLayout, layoutIdMap,
202                            parameterMap, remoteAddress, remotePort, remotePathContext,
203                            secureConnection, remoteGroupId, remotePrivateLayout,
204                            themeDisplay.getLocale(), themeDisplay.getTimeZone());
205            }
206    
207            protected static Map<String, Serializable> buildSettingsMap(
208                    long userId, long sourceGroupId, long sourcePlid, long targetGroupId,
209                    long targetPlid, String portletId, Boolean privateLayout,
210                    Map<Long, Boolean> layoutIdMap, long[] layoutIds,
211                    Map<String, String[]> parameterMap, String remoteAddress,
212                    int remotePort, String remotePathContext, Boolean secureConnection,
213                    long remoteGroupId, Boolean remotePrivateLayout, String cmd,
214                    Locale locale, TimeZone timeZone, String fileName) {
215    
216                    Map<String, Serializable> settingsMap = new HashMap<>();
217    
218                    if (Validator.isNotNull(cmd)) {
219                            settingsMap.put(Constants.CMD, cmd);
220                    }
221    
222                    if (Validator.isNotNull(fileName)) {
223                            settingsMap.put("fileName", fileName);
224                    }
225    
226                    if (MapUtil.isNotEmpty(layoutIdMap)) {
227                            HashMap<Long, Boolean> serializableLayoutIdMap = new HashMap<>(
228                                    layoutIdMap);
229    
230                            settingsMap.put("layoutIdMap", serializableLayoutIdMap);
231                    }
232    
233                    if (ArrayUtil.isNotEmpty(layoutIds)) {
234                            settingsMap.put("layoutIds", layoutIds);
235                    }
236    
237                    if (locale != null) {
238                            settingsMap.put("locale", locale);
239                    }
240    
241                    if (parameterMap != null) {
242                            HashMap<String, String[]> serializableParameterMap = new HashMap<>(
243                                    parameterMap);
244    
245                            settingsMap.put("parameterMap", serializableParameterMap);
246                    }
247    
248                    if (Validator.isNotNull(portletId)) {
249                            settingsMap.put("portletId", portletId);
250                    }
251    
252                    if (privateLayout != null) {
253                            settingsMap.put("privateLayout", privateLayout);
254                    }
255    
256                    if (Validator.isNotNull(remoteAddress)) {
257                            settingsMap.put("remoteAddress", remoteAddress);
258                    }
259    
260                    if (remoteGroupId > 0) {
261                            settingsMap.put("remoteGroupId", remoteGroupId);
262                    }
263    
264                    if (Validator.isNotNull(remotePathContext)) {
265                            settingsMap.put("remotePathContext", remotePathContext);
266                    }
267    
268                    if (remotePort > 0) {
269                            settingsMap.put("remotePort", remotePort);
270                    }
271    
272                    if (remotePrivateLayout != null) {
273                            settingsMap.put("remotePrivateLayout", remotePrivateLayout);
274                    }
275    
276                    if (secureConnection != null) {
277                            settingsMap.put("secureConnection", secureConnection);
278                    }
279    
280                    if (sourceGroupId > 0) {
281                            settingsMap.put("sourceGroupId", sourceGroupId);
282                    }
283    
284                    if (sourcePlid > 0) {
285                            settingsMap.put("sourcePlid", sourcePlid);
286                    }
287    
288                    if (targetGroupId > 0) {
289                            settingsMap.put("targetGroupId", targetGroupId);
290                    }
291    
292                    if (targetPlid > 0) {
293                            settingsMap.put("targetPlid", targetPlid);
294                    }
295    
296                    if (timeZone != null) {
297                            settingsMap.put("timezone", timeZone);
298                    }
299    
300                    settingsMap.put("userId", userId);
301    
302                    return settingsMap;
303            }
304    
305    }