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             * @throws PortalException if a portal exception occurred
213             */
214            public static Map<String, Serializable> buildSettingsMap(
215                            PortletRequest portletRequest, long groupId, int type)
216                    throws PortalException {
217    
218                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
219                            WebKeys.THEME_DISPLAY);
220    
221                    boolean privateLayout = ParamUtil.getBoolean(
222                            portletRequest, "privateLayout");
223    
224                    Map<Long, Boolean> layoutIdMap = ExportImportHelperUtil.getLayoutIdMap(
225                            portletRequest);
226    
227                    if (type == ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT) {
228                            long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layoutIdMap);
229    
230                            return buildExportLayoutSettingsMap(
231                                    themeDisplay.getUserId(), groupId, privateLayout, layoutIds,
232                                    portletRequest.getParameterMap(), themeDisplay.getLocale(),
233                                    themeDisplay.getTimeZone());
234                    }
235    
236                    Group stagingGroup = GroupLocalServiceUtil.getGroup(groupId);
237    
238                    Group liveGroup = stagingGroup.getLiveGroup();
239    
240                    Map<String, String[]> parameterMap =
241                            ExportImportConfigurationParameterMapFactory.buildParameterMap(
242                                    portletRequest);
243    
244                    if (liveGroup != null) {
245                            long[] layoutIds = ExportImportHelperUtil.getLayoutIds(
246                                    layoutIdMap, liveGroup.getGroupId());
247    
248                            return buildPublishLayoutLocalSettingsMap(
249                                    themeDisplay.getUserId(), stagingGroup.getGroupId(),
250                                    liveGroup.getGroupId(), privateLayout, layoutIds, parameterMap,
251                                    themeDisplay.getLocale(), themeDisplay.getTimeZone());
252                    }
253    
254                    UnicodeProperties groupTypeSettingsProperties =
255                            stagingGroup.getTypeSettingsProperties();
256    
257                    String remoteAddress = ParamUtil.getString(
258                            portletRequest, "remoteAddress",
259                            groupTypeSettingsProperties.getProperty("remoteAddress"));
260    
261                    remoteAddress = StagingUtil.stripProtocolFromRemoteAddress(
262                            remoteAddress);
263    
264                    int remotePort = ParamUtil.getInteger(
265                            portletRequest, "remotePort",
266                            GetterUtil.getInteger(
267                                    groupTypeSettingsProperties.getProperty("remotePort")));
268                    String remotePathContext = ParamUtil.getString(
269                            portletRequest, "remotePathContext",
270                            groupTypeSettingsProperties.getProperty("remotePathContext"));
271                    boolean secureConnection = ParamUtil.getBoolean(
272                            portletRequest, "secureConnection",
273                            GetterUtil.getBoolean(
274                                    groupTypeSettingsProperties.getProperty("secureConnection")));
275                    long remoteGroupId = ParamUtil.getLong(
276                            portletRequest, "remoteGroupId",
277                            GetterUtil.getLong(
278                                    groupTypeSettingsProperties.getProperty("remoteGroupId")));
279                    boolean remotePrivateLayout = ParamUtil.getBoolean(
280                            portletRequest, "remotePrivateLayout");
281    
282                    StagingUtil.validateRemote(
283                            groupId, remoteAddress, remotePort, remotePathContext,
284                            secureConnection, remoteGroupId);
285    
286                    return buildPublishLayoutRemoteSettingsMap(
287                            themeDisplay.getUserId(), groupId, privateLayout, layoutIdMap,
288                            parameterMap, remoteAddress, remotePort, remotePathContext,
289                            secureConnection, remoteGroupId, remotePrivateLayout,
290                            themeDisplay.getLocale(), themeDisplay.getTimeZone());
291            }
292    
293            protected static Map<String, Serializable> buildSettingsMap(
294                    long userId, long sourceGroupId, long sourcePlid, long targetGroupId,
295                    long targetPlid, String portletId, Boolean privateLayout,
296                    Map<Long, Boolean> layoutIdMap, long[] layoutIds,
297                    Map<String, String[]> parameterMap, String remoteAddress,
298                    int remotePort, String remotePathContext, Boolean secureConnection,
299                    Boolean remotePrivateLayout, Locale locale, TimeZone timeZone,
300                    String fileName) {
301    
302                    Map<String, Serializable> settingsMap = new HashMap<>();
303    
304                    if (Validator.isNotNull(fileName)) {
305                            settingsMap.put("fileName", fileName);
306                    }
307    
308                    if (MapUtil.isNotEmpty(layoutIdMap)) {
309                            HashMap<Long, Boolean> serializableLayoutIdMap = new HashMap<>(
310                                    layoutIdMap);
311    
312                            settingsMap.put("layoutIdMap", serializableLayoutIdMap);
313                    }
314    
315                    if (ArrayUtil.isNotEmpty(layoutIds)) {
316                            settingsMap.put("layoutIds", layoutIds);
317                    }
318    
319                    if (locale != null) {
320                            settingsMap.put("locale", locale);
321                    }
322    
323                    if (parameterMap != null) {
324                            HashMap<String, String[]> serializableParameterMap = new HashMap<>(
325                                    parameterMap);
326    
327                            settingsMap.put("parameterMap", serializableParameterMap);
328                    }
329    
330                    if (Validator.isNotNull(portletId)) {
331                            settingsMap.put("portletId", portletId);
332                    }
333    
334                    if (privateLayout != null) {
335                            settingsMap.put("privateLayout", privateLayout);
336                    }
337    
338                    if (Validator.isNotNull(remoteAddress)) {
339                            settingsMap.put("remoteAddress", remoteAddress);
340                    }
341    
342                    if (Validator.isNotNull(remotePathContext)) {
343                            settingsMap.put("remotePathContext", remotePathContext);
344                    }
345    
346                    if (remotePort > 0) {
347                            settingsMap.put("remotePort", remotePort);
348                    }
349    
350                    if (remotePrivateLayout != null) {
351                            settingsMap.put("remotePrivateLayout", remotePrivateLayout);
352                    }
353    
354                    if (secureConnection != null) {
355                            settingsMap.put("secureConnection", secureConnection);
356                    }
357    
358                    if (sourceGroupId > 0) {
359                            settingsMap.put("sourceGroupId", sourceGroupId);
360                    }
361    
362                    if (sourcePlid > 0) {
363                            settingsMap.put("sourcePlid", sourcePlid);
364                    }
365    
366                    if (targetGroupId > 0) {
367                            settingsMap.put("targetGroupId", targetGroupId);
368                    }
369    
370                    if (targetPlid > 0) {
371                            settingsMap.put("targetPlid", targetPlid);
372                    }
373    
374                    if (timeZone != null) {
375                            settingsMap.put("timezone", timeZone);
376                    }
377    
378                    settingsMap.put("userId", userId);
379    
380                    return settingsMap;
381            }
382    
383    }