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.portal.kernel.lar.exportimportconfiguration;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
019    import com.liferay.portal.kernel.staging.StagingUtil;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.MapUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.UnicodeProperties;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.portal.theme.ThemeDisplay;
029    
030    import java.io.Serializable;
031    
032    import java.util.Date;
033    import java.util.HashMap;
034    import java.util.Locale;
035    import java.util.Map;
036    import java.util.TimeZone;
037    
038    import javax.portlet.PortletRequest;
039    
040    /**
041     * @author Daniel Kocsis
042     */
043    public class ExportImportConfigurationSettingsMapFactory {
044    
045            public static Map<String, Serializable> buildSettingsMap(
046                    long userId, long groupId, boolean privateLayout, long[] layoutIds,
047                    Map<String, String[]> parameterMap, Date startDate, Date endDate,
048                    Locale locale, TimeZone timeZone) {
049    
050                    return buildSettingsMap(
051                            userId, groupId, 0, privateLayout, layoutIds, parameterMap,
052                            startDate, endDate, locale, timeZone);
053            }
054    
055            public static Map<String, Serializable> buildSettingsMap(
056                    long userId, long sourceGroupId, boolean privateLayout,
057                    Map<Long, Boolean> layoutIdMap, Map<String, String[]> parameterMap,
058                    String remoteAddress, int remotePort, String remotePathContext,
059                    boolean secureConnection, long remoteGroupId,
060                    boolean remotePrivateLayout, Date startDate, Date endDate,
061                    Locale locale, TimeZone timeZone) {
062    
063                    Map<String, Serializable> settingsMap = buildSettingsMap(
064                            userId, sourceGroupId, privateLayout, null, parameterMap, startDate,
065                            endDate, locale, timeZone);
066    
067                    if (MapUtil.isNotEmpty(layoutIdMap)) {
068                            HashMap<Long, Boolean> serializableLayoutIdMap =
069                                    new HashMap<Long, Boolean>(layoutIdMap);
070    
071                            settingsMap.put("layoutIdMap", serializableLayoutIdMap);
072                    }
073    
074                    settingsMap.put("remoteAddress", remoteAddress);
075                    settingsMap.put("remoteGroupId", remoteGroupId);
076                    settingsMap.put("remotePathContext", remotePathContext);
077                    settingsMap.put("remotePort", remotePort);
078                    settingsMap.put("remotePrivateLayout", remotePrivateLayout);
079                    settingsMap.put("secureConnection", secureConnection);
080    
081                    return settingsMap;
082            }
083    
084            public static Map<String, Serializable> buildSettingsMap(
085                    long userId, long sourceGroupId, long targetGroupId,
086                    boolean privateLayout, long[] layoutIds,
087                    Map<String, String[]> parameterMap, Date startDate, Date endDate,
088                    Locale locale, TimeZone timeZone) {
089    
090                    Map<String, Serializable> settingsMap =
091                            new HashMap<String, Serializable>();
092    
093                    if (endDate != null) {
094                            settingsMap.put("endDate", endDate);
095                    }
096    
097                    if (ArrayUtil.isNotEmpty(layoutIds)) {
098                            settingsMap.put("layoutIds", layoutIds);
099                    }
100    
101                    settingsMap.put("locale", locale);
102    
103                    if (parameterMap != null) {
104                            HashMap<String, String[]> serializableParameterMap =
105                                    new HashMap<String, String[]>(parameterMap);
106    
107                            settingsMap.put("parameterMap", serializableParameterMap);
108                    }
109    
110                    settingsMap.put("privateLayout", privateLayout);
111                    settingsMap.put("sourceGroupId", sourceGroupId);
112    
113                    if (startDate != null) {
114                            settingsMap.put("startDate", startDate);
115                    }
116    
117                    if (targetGroupId > 0) {
118                            settingsMap.put("targetGroupId", targetGroupId);
119                    }
120    
121                    settingsMap.put("timezone", timeZone);
122                    settingsMap.put("userId", userId);
123    
124                    return settingsMap;
125            }
126    
127            public static Map<String, Serializable> buildSettingsMap(
128                            PortletRequest portletRequest, long groupId, int type)
129                    throws PortalException {
130    
131                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
132                            WebKeys.THEME_DISPLAY);
133    
134                    boolean privateLayout = ParamUtil.getBoolean(
135                            portletRequest, "privateLayout");
136    
137                    Map<Long, Boolean> layoutIdMap = ExportImportHelperUtil.getLayoutIdMap(
138                            portletRequest);
139    
140                    if (type == ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT) {
141                            long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layoutIdMap);
142    
143                            return buildSettingsMap(
144                                    themeDisplay.getUserId(), groupId, privateLayout, layoutIds,
145                                    portletRequest.getParameterMap(), null, null,
146                                    themeDisplay.getLocale(), themeDisplay.getTimeZone());
147                    }
148    
149                    Group stagingGroup = GroupLocalServiceUtil.getGroup(groupId);
150    
151                    Group liveGroup = stagingGroup.getLiveGroup();
152    
153                    Map<String, String[]> parameterMap = StagingUtil.getStagingParameters(
154                            portletRequest);
155    
156                    if (liveGroup != null) {
157                            long[] layoutIds = ExportImportHelperUtil.getLayoutIds(
158                                    layoutIdMap, liveGroup.getGroupId());
159    
160                            return buildSettingsMap(
161                                    themeDisplay.getUserId(), stagingGroup.getGroupId(),
162                                    liveGroup.getGroupId(), privateLayout, layoutIds, parameterMap,
163                                    null, null, themeDisplay.getLocale(),
164                                    themeDisplay.getTimeZone());
165                    }
166    
167                    UnicodeProperties groupTypeSettingsProperties =
168                            stagingGroup.getTypeSettingsProperties();
169    
170                    String remoteAddress = ParamUtil.getString(
171                            portletRequest, "remoteAddress",
172                            groupTypeSettingsProperties.getProperty("remoteAddress"));
173    
174                    remoteAddress = StagingUtil.stripProtocolFromRemoteAddress(
175                            remoteAddress);
176    
177                    int remotePort = ParamUtil.getInteger(
178                            portletRequest, "remotePort",
179                            GetterUtil.getInteger(
180                                    groupTypeSettingsProperties.getProperty("remotePort")));
181                    String remotePathContext = ParamUtil.getString(
182                            portletRequest, "remotePathContext",
183                            groupTypeSettingsProperties.getProperty("remotePathContext"));
184                    boolean secureConnection = ParamUtil.getBoolean(
185                            portletRequest, "secureConnection",
186                            GetterUtil.getBoolean(
187                                    groupTypeSettingsProperties.getProperty("secureConnection")));
188                    long remoteGroupId = ParamUtil.getLong(
189                            portletRequest, "remoteGroupId",
190                            GetterUtil.getLong(
191                                    groupTypeSettingsProperties.getProperty("remoteGroupId")));
192                    boolean remotePrivateLayout = ParamUtil.getBoolean(
193                            portletRequest, "remotePrivateLayout");
194    
195                    StagingUtil.validateRemote(
196                            groupId, remoteAddress, remotePort, remotePathContext,
197                            secureConnection, remoteGroupId);
198    
199                    return buildSettingsMap(
200                            themeDisplay.getUserId(), groupId, privateLayout, layoutIdMap,
201                            parameterMap, remoteAddress, remotePort, remotePathContext,
202                            secureConnection, remoteGroupId, remotePrivateLayout, null, null,
203                            themeDisplay.getLocale(), themeDisplay.getTimeZone());
204            }
205    
206    }