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.GetterUtil;
020    import com.liferay.portal.kernel.util.MapUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.model.User;
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     * Provides a way to build a settings map for an {@link
044     * com.liferay.portlet.exportimport.model.ExportImportConfiguration}, which can
045     * be used to start and control an export, import, or staging process.
046     *
047     * @author Daniel Kocsis
048     * @author Akos Thurzo
049     * @since  7.0
050     */
051    public class ExportImportConfigurationSettingsMapFactory {
052    
053            public static Map<String, Serializable> buildExportLayoutSettingsMap(
054                    long userId, long groupId, boolean privateLayout, long[] layoutIds,
055                    Map<String, String[]> parameterMap, Locale locale, TimeZone timeZone) {
056    
057                    return buildPublishLayoutLocalSettingsMap(
058                            userId, groupId, 0, privateLayout, layoutIds, parameterMap, locale,
059                            timeZone);
060            }
061    
062            public static Map<String, Serializable> buildExportLayoutSettingsMap(
063                    User user, long groupId, boolean privateLayout, long[] layoutIds,
064                    Map<String, String[]> parameterMap) {
065    
066                    return buildExportLayoutSettingsMap(
067                            user.getUserId(), groupId, privateLayout, layoutIds, parameterMap,
068                            user.getLocale(), user.getTimeZone());
069            }
070    
071            public static Map<String, Serializable> buildExportPortletSettingsMap(
072                    long userId, long sourcePlid, long sourceGroupId, String portletId,
073                    Map<String, String[]> parameterMap, Locale locale, TimeZone timeZone,
074                    String fileName) {
075    
076                    return buildSettingsMap(
077                            userId, sourceGroupId, sourcePlid, 0, 0, portletId, null, null,
078                            null, parameterMap, StringPool.BLANK, 0, StringPool.BLANK, null,
079                            null, locale, timeZone, fileName);
080            }
081    
082            public static Map<String, Serializable> buildExportPortletSettingsMap(
083                    User user, long sourcePlid, long sourceGroupId, String portletId,
084                    Map<String, String[]> parameterMap, String fileName) {
085    
086                    return buildExportPortletSettingsMap(
087                            user.getUserId(), sourcePlid, sourceGroupId, portletId,
088                            parameterMap, user.getLocale(), user.getTimeZone(), fileName);
089            }
090    
091            public static Map<String, Serializable> buildImportLayoutSettingsMap(
092                    long userId, long targetGroupId, boolean privateLayout,
093                    long[] layoutIds, Map<String, String[]> parameterMap, Locale locale,
094                    TimeZone timeZone) {
095    
096                    return buildSettingsMap(
097                            userId, 0, 0, targetGroupId, 0, StringPool.BLANK, privateLayout,
098                            null, layoutIds, parameterMap, StringPool.BLANK, 0,
099                            StringPool.BLANK, null, null, locale, timeZone, StringPool.BLANK);
100            }
101    
102            public static Map<String, Serializable> buildImportLayoutSettingsMap(
103                    User user, long targetGroupId, boolean privateLayout, long[] layoutIds,
104                    Map<String, String[]> parameterMap) {
105    
106                    return buildImportLayoutSettingsMap(
107                            user.getUserId(), targetGroupId, privateLayout, layoutIds,
108                            parameterMap, user.getLocale(), user.getTimeZone());
109            }
110    
111            public static Map<String, Serializable> buildImportPortletSettingsMap(
112                    long userId, long targetPlid, long targetGroupId, String portletId,
113                    Map<String, String[]> parameterMap, Locale locale, TimeZone timeZone) {
114    
115                    return buildSettingsMap(
116                            userId, 0, 0, targetGroupId, targetPlid, portletId, null, null,
117                            null, parameterMap, StringPool.BLANK, 0, StringPool.BLANK, null,
118                            null, locale, timeZone, StringPool.BLANK);
119            }
120    
121            public static Map<String, Serializable> buildImportPortletSettingsMap(
122                    User user, long targetPlid, long targetGroupId, String portletId,
123                    Map<String, String[]> parameterMap) {
124    
125                    return buildImportPortletSettingsMap(
126                            user.getUserId(), targetPlid, targetGroupId, portletId,
127                            parameterMap, user.getLocale(), user.getTimeZone());
128            }
129    
130            public static Map<String, Serializable> buildPublishLayoutLocalSettingsMap(
131                    long userId, long sourceGroupId, long targetGroupId,
132                    boolean privateLayout, long[] layoutIds,
133                    Map<String, String[]> parameterMap, Locale locale, TimeZone timeZone) {
134    
135                    return buildSettingsMap(
136                            userId, sourceGroupId, 0, targetGroupId, 0, StringPool.BLANK,
137                            privateLayout, null, layoutIds, parameterMap, StringPool.BLANK, 0,
138                            StringPool.BLANK, null, null, locale, timeZone, StringPool.BLANK);
139            }
140    
141            public static Map<String, Serializable> buildPublishLayoutLocalSettingsMap(
142                    User user, long sourceGroupId, long targetGroupId,
143                    boolean privateLayout, long[] layoutIds,
144                    Map<String, String[]> parameterMap) {
145    
146                    return buildPublishLayoutLocalSettingsMap(
147                            user.getUserId(), sourceGroupId, targetGroupId, privateLayout,
148                            layoutIds, parameterMap, user.getLocale(), user.getTimeZone());
149            }
150    
151            public static Map<String, Serializable> buildPublishLayoutRemoteSettingsMap(
152                    long userId, long sourceGroupId, boolean privateLayout,
153                    Map<Long, Boolean> layoutIdMap, Map<String, String[]> parameterMap,
154                    String remoteAddress, int remotePort, String remotePathContext,
155                    boolean secureConnection, long remoteGroupId,
156                    boolean remotePrivateLayout, Locale locale, TimeZone timeZone) {
157    
158                    return buildSettingsMap(
159                            userId, sourceGroupId, 0, remoteGroupId, 0, StringPool.BLANK,
160                            privateLayout, layoutIdMap, null, parameterMap, remoteAddress,
161                            remotePort, remotePathContext, secureConnection,
162                            remotePrivateLayout, locale, timeZone, StringPool.BLANK);
163            }
164    
165            public static Map<String, Serializable> buildPublishLayoutRemoteSettingsMap(
166                    User user, long sourceGroupId, boolean privateLayout,
167                    Map<Long, Boolean> layoutIdMap, Map<String, String[]> parameterMap,
168                    String remoteAddress, int remotePort, String remotePathContext,
169                    boolean secureConnection, long remoteGroupId,
170                    boolean remotePrivateLayout) {
171    
172                    return buildPublishLayoutRemoteSettingsMap(
173                            user.getUserId(), sourceGroupId, privateLayout, layoutIdMap,
174                            parameterMap, remoteAddress, remotePort, remotePathContext,
175                            secureConnection, remoteGroupId, remotePrivateLayout,
176                            user.getLocale(), user.getTimeZone());
177            }
178    
179            public static Map<String, Serializable> buildPublishPortletSettingsMap(
180                    long userId, long sourceGroupId, long sourcePlid, long targetGroupId,
181                    long targetPlid, String portletId, Map<String, String[]> parameterMap,
182                    Locale locale, TimeZone timeZone) {
183    
184                    return buildSettingsMap(
185                            userId, sourceGroupId, sourcePlid, targetGroupId, targetPlid,
186                            portletId, null, null, null, parameterMap, StringPool.BLANK, 0,
187                            StringPool.BLANK, null, null, locale, timeZone, null);
188            }
189    
190            public static Map<String, Serializable> buildPublishPortletSettingsMap(
191                    User user, long sourceGroupId, long sourcePlid, long targetGroupId,
192                    long targetPlid, String portletId, Map<String, String[]> parameterMap) {
193    
194                    return buildPublishPortletSettingsMap(
195                            user.getUserId(), sourceGroupId, sourcePlid, targetGroupId,
196                            targetPlid, portletId, parameterMap, user.getLocale(),
197                            user.getTimeZone());
198            }
199    
200            /**
201             * Returns an export layout settings map if the type is {@link
202             * ExportImportConfigurationConstants#TYPE_EXPORT_LAYOUT}; otherwise,
203             * returns either a local or remote publish layout settings map, depending
204             * on the staging type.
205             *
206             * @param  portletRequest the portlet request
207             * @param  groupId the primary key of the group
208             * @param  type the export/import option type
209             * @return an export layout settings map if the type is an export layout;
210             *         otherwise, returns either a local or remote publish layout
211             *         settings map, depending on the staging type
212             */
213            public static Map<String, Serializable> buildSettingsMap(
214                            PortletRequest portletRequest, long groupId, int type)
215                    throws PortalException {
216    
217                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
218                            WebKeys.THEME_DISPLAY);
219    
220                    boolean privateLayout = ParamUtil.getBoolean(
221                            portletRequest, "privateLayout");
222    
223                    Map<Long, Boolean> layoutIdMap = ExportImportHelperUtil.getLayoutIdMap(
224                            portletRequest);
225    
226                    if (type == ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT) {
227                            long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layoutIdMap);
228    
229                            return buildExportLayoutSettingsMap(
230                                    themeDisplay.getUserId(), groupId, privateLayout, layoutIds,
231                                    portletRequest.getParameterMap(), themeDisplay.getLocale(),
232                                    themeDisplay.getTimeZone());
233                    }
234    
235                    Group stagingGroup = GroupLocalServiceUtil.getGroup(groupId);
236    
237                    Group liveGroup = stagingGroup.getLiveGroup();
238    
239                    Map<String, String[]> parameterMap =
240                            ExportImportConfigurationParameterMapFactory.buildParameterMap(
241                                    portletRequest);
242    
243                    if (liveGroup != null) {
244                            long[] layoutIds = ExportImportHelperUtil.getLayoutIds(
245                                    layoutIdMap, liveGroup.getGroupId());
246    
247                            return buildPublishLayoutLocalSettingsMap(
248                                    themeDisplay.getUserId(), stagingGroup.getGroupId(),
249                                    liveGroup.getGroupId(), privateLayout, layoutIds, parameterMap,
250                                    themeDisplay.getLocale(), themeDisplay.getTimeZone());
251                    }
252    
253                    UnicodeProperties groupTypeSettingsProperties =
254                            stagingGroup.getTypeSettingsProperties();
255    
256                    String remoteAddress = ParamUtil.getString(
257                            portletRequest, "remoteAddress",
258                            groupTypeSettingsProperties.getProperty("remoteAddress"));
259    
260                    remoteAddress = StagingUtil.stripProtocolFromRemoteAddress(
261                            remoteAddress);
262    
263                    int remotePort = ParamUtil.getInteger(
264                            portletRequest, "remotePort",
265                            GetterUtil.getInteger(
266                                    groupTypeSettingsProperties.getProperty("remotePort")));
267                    String remotePathContext = ParamUtil.getString(
268                            portletRequest, "remotePathContext",
269                            groupTypeSettingsProperties.getProperty("remotePathContext"));
270                    boolean secureConnection = ParamUtil.getBoolean(
271                            portletRequest, "secureConnection",
272                            GetterUtil.getBoolean(
273                                    groupTypeSettingsProperties.getProperty("secureConnection")));
274                    long remoteGroupId = ParamUtil.getLong(
275                            portletRequest, "remoteGroupId",
276                            GetterUtil.getLong(
277                                    groupTypeSettingsProperties.getProperty("remoteGroupId")));
278                    boolean remotePrivateLayout = ParamUtil.getBoolean(
279                            portletRequest, "remotePrivateLayout");
280    
281                    StagingUtil.validateRemote(
282                            groupId, remoteAddress, remotePort, remotePathContext,
283                            secureConnection, remoteGroupId);
284    
285                    return buildPublishLayoutRemoteSettingsMap(
286                            themeDisplay.getUserId(), groupId, privateLayout, layoutIdMap,
287                            parameterMap, remoteAddress, remotePort, remotePathContext,
288                            secureConnection, remoteGroupId, remotePrivateLayout,
289                            themeDisplay.getLocale(), themeDisplay.getTimeZone());
290            }
291    
292            protected static Map<String, Serializable> buildSettingsMap(
293                    long userId, long sourceGroupId, long sourcePlid, long targetGroupId,
294                    long targetPlid, String portletId, Boolean privateLayout,
295                    Map<Long, Boolean> layoutIdMap, long[] layoutIds,
296                    Map<String, String[]> parameterMap, String remoteAddress,
297                    int remotePort, String remotePathContext, Boolean secureConnection,
298                    Boolean remotePrivateLayout, Locale locale, TimeZone timeZone,
299                    String fileName) {
300    
301                    Map<String, Serializable> settingsMap = new HashMap<>();
302    
303                    if (Validator.isNotNull(fileName)) {
304                            settingsMap.put("fileName", fileName);
305                    }
306    
307                    if (MapUtil.isNotEmpty(layoutIdMap)) {
308                            HashMap<Long, Boolean> serializableLayoutIdMap = new HashMap<>(
309                                    layoutIdMap);
310    
311                            settingsMap.put("layoutIdMap", serializableLayoutIdMap);
312                    }
313    
314                    if (ArrayUtil.isNotEmpty(layoutIds)) {
315                            settingsMap.put("layoutIds", layoutIds);
316                    }
317    
318                    if (locale != null) {
319                            settingsMap.put("locale", locale);
320                    }
321    
322                    if (parameterMap != null) {
323                            HashMap<String, String[]> serializableParameterMap = new HashMap<>(
324                                    parameterMap);
325    
326                            settingsMap.put("parameterMap", serializableParameterMap);
327                    }
328    
329                    if (Validator.isNotNull(portletId)) {
330                            settingsMap.put("portletId", portletId);
331                    }
332    
333                    if (privateLayout != null) {
334                            settingsMap.put("privateLayout", privateLayout);
335                    }
336    
337                    if (Validator.isNotNull(remoteAddress)) {
338                            settingsMap.put("remoteAddress", remoteAddress);
339                    }
340    
341                    if (Validator.isNotNull(remotePathContext)) {
342                            settingsMap.put("remotePathContext", remotePathContext);
343                    }
344    
345                    if (remotePort > 0) {
346                            settingsMap.put("remotePort", remotePort);
347                    }
348    
349                    if (remotePrivateLayout != null) {
350                            settingsMap.put("remotePrivateLayout", remotePrivateLayout);
351                    }
352    
353                    if (secureConnection != null) {
354                            settingsMap.put("secureConnection", secureConnection);
355                    }
356    
357                    if (sourceGroupId > 0) {
358                            settingsMap.put("sourceGroupId", sourceGroupId);
359                    }
360    
361                    if (sourcePlid > 0) {
362                            settingsMap.put("sourcePlid", sourcePlid);
363                    }
364    
365                    if (targetGroupId > 0) {
366                            settingsMap.put("targetGroupId", targetGroupId);
367                    }
368    
369                    if (targetPlid > 0) {
370                            settingsMap.put("targetPlid", targetPlid);
371                    }
372    
373                    if (timeZone != null) {
374                            settingsMap.put("timezone", timeZone);
375                    }
376    
377                    settingsMap.put("userId", userId);
378    
379                    return settingsMap;
380            }
381    
382    }