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.exportimport.kernel.configuration;
016    
017    import com.liferay.exportimport.kernel.lar.ExportImportDateUtil;
018    import com.liferay.exportimport.kernel.lar.ExportImportHelperUtil;
019    import com.liferay.exportimport.kernel.lar.PortletDataHandlerKeys;
020    import com.liferay.exportimport.kernel.model.ExportImportConfiguration;
021    import com.liferay.exportimport.kernel.service.ExportImportConfigurationLocalServiceUtil;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.model.User;
024    import com.liferay.portal.kernel.service.ServiceContext;
025    import com.liferay.portal.kernel.theme.ThemeDisplay;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.WebKeys;
028    
029    import java.io.Serializable;
030    
031    import java.util.Map;
032    
033    import javax.portlet.PortletRequest;
034    
035    /**
036     * @author Levente Hud??k
037     */
038    public class ExportImportConfigurationFactory {
039    
040            public static ExportImportConfiguration
041                            buildDefaultLocalPublishingExportImportConfiguration(
042                                    PortletRequest portletRequest)
043                    throws PortalException {
044    
045                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
046                            WebKeys.THEME_DISPLAY);
047    
048                    long sourceGroupId = ParamUtil.getLong(portletRequest, "sourceGroupId");
049                    long targetGroupId = ParamUtil.getLong(portletRequest, "targetGroupId");
050                    boolean privateLayout = ParamUtil.getBoolean(
051                            portletRequest, "privateLayout");
052    
053                    Map<String, String[]> parameterMap = getDefaultPublishingParameters(
054                            portletRequest);
055    
056                    return buildDefaultLocalPublishingExportImportConfiguration(
057                            themeDisplay.getUser(), sourceGroupId, targetGroupId, privateLayout,
058                            parameterMap);
059            }
060    
061            public static ExportImportConfiguration
062                            buildDefaultLocalPublishingExportImportConfiguration(
063                                    User user, long sourceGroupId, long targetGroupId,
064                                    boolean privateLayout)
065                    throws PortalException {
066    
067                    return buildDefaultLocalPublishingExportImportConfiguration(
068                            user, sourceGroupId, targetGroupId, privateLayout,
069                            getDefaultPublishingParameters());
070            }
071    
072            public static ExportImportConfiguration
073                            buildDefaultLocalPublishingExportImportConfiguration(
074                                    User user, long sourceGroupId, long targetGroupId,
075                                    boolean privateLayout, Map<String, String[]> parameterMap)
076                    throws PortalException {
077    
078                    Map<String, Serializable> publishLayoutLocalSettingsMap =
079                            ExportImportConfigurationSettingsMapFactory.
080                                    buildPublishLayoutLocalSettingsMap(
081                                            user, sourceGroupId, targetGroupId, privateLayout,
082                                            ExportImportHelperUtil.getAllLayoutIds(
083                                                    sourceGroupId, privateLayout),
084                                            parameterMap);
085    
086                    return ExportImportConfigurationLocalServiceUtil.
087                            addDraftExportImportConfiguration(
088                                    user.getUserId(),
089                                    ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_LOCAL,
090                                    publishLayoutLocalSettingsMap);
091            }
092    
093            public static ExportImportConfiguration
094                            buildDefaultRemotePublishingExportImportConfiguration(
095                                    PortletRequest portletRequest)
096                    throws PortalException {
097    
098                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
099                            WebKeys.THEME_DISPLAY);
100    
101                    long sourceGroupId = ParamUtil.getLong(portletRequest, "sourceGroupId");
102                    boolean privateLayout = ParamUtil.getBoolean(
103                            portletRequest, "privateLayout");
104                    String remoteAddress = ParamUtil.getString(
105                            portletRequest, "remoteAddress");
106                    int remotePort = ParamUtil.getInteger(portletRequest, "remotePort");
107                    String remotePathContext = ParamUtil.getString(
108                            portletRequest, "remotePathContext");
109                    boolean secureConnection = ParamUtil.getBoolean(
110                            portletRequest, "secureConnection");
111                    long remoteGroupId = ParamUtil.getLong(portletRequest, "remoteGroupId");
112    
113                    Map<String, String[]> parameterMap = getDefaultPublishingParameters(
114                            portletRequest);
115    
116                    return buildDefaultRemotePublishingExportImportConfiguration(
117                            themeDisplay.getUser(), sourceGroupId, privateLayout, remoteAddress,
118                            remotePort, remotePathContext, secureConnection, remoteGroupId,
119                            parameterMap);
120            }
121    
122            public static ExportImportConfiguration
123                            buildDefaultRemotePublishingExportImportConfiguration(
124                                    User user, long sourceGroupId, boolean privateLayout,
125                                    String remoteAddress, int remotePort, String remotePathContext,
126                                    boolean secureConnection, long remoteGroupId)
127                    throws PortalException {
128    
129                    return buildDefaultRemotePublishingExportImportConfiguration(
130                            user, sourceGroupId, privateLayout, remoteAddress, remotePort,
131                            remotePathContext, secureConnection, remoteGroupId,
132                            getDefaultPublishingParameters());
133            }
134    
135            public static ExportImportConfiguration cloneExportImportConfiguration(
136                            ExportImportConfiguration exportImportConfiguration)
137                    throws PortalException {
138    
139                    if (exportImportConfiguration == null) {
140                            return null;
141                    }
142    
143                    return ExportImportConfigurationLocalServiceUtil.
144                            addExportImportConfiguration(
145                                    exportImportConfiguration.getUserId(),
146                                    exportImportConfiguration.getGroupId(),
147                                    exportImportConfiguration.getName(),
148                                    exportImportConfiguration.getDescription(),
149                                    exportImportConfiguration.getType(),
150                                    exportImportConfiguration.getSettingsMap(),
151                                    exportImportConfiguration.getStatus(), new ServiceContext());
152            }
153    
154            public static Map<String, String[]> getDefaultPublishingParameters(
155                    PortletRequest portletRequest) {
156    
157                    Map<String, String[]> parameterMap =
158                            ExportImportConfigurationParameterMapFactory.buildParameterMap(
159                                    portletRequest);
160    
161                    return addDefaultPublishingParameters(parameterMap);
162            }
163    
164            protected static Map<String, String[]> addDefaultPublishingParameters(
165                    Map<String, String[]> parameterMap) {
166    
167                    parameterMap.put(
168                            PortletDataHandlerKeys.DELETIONS,
169                            new String[] {Boolean.TRUE.toString()});
170                    parameterMap.put(
171                            PortletDataHandlerKeys.IGNORE_LAST_PUBLISH_DATE,
172                            new String[] {Boolean.FALSE.toString()});
173                    parameterMap.put(
174                            PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
175                            new String[] {Boolean.TRUE.toString()});
176                    parameterMap.put(
177                            PortletDataHandlerKeys.LOGO,
178                            new String[] {Boolean.TRUE.toString()});
179                    parameterMap.put(
180                            PortletDataHandlerKeys.PERMISSIONS,
181                            new String[] {Boolean.TRUE.toString()});
182                    parameterMap.put(
183                            PortletDataHandlerKeys.PORTLET_CONFIGURATION,
184                            new String[] {Boolean.TRUE.toString()});
185                    parameterMap.put(
186                            PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
187                            new String[] {Boolean.TRUE.toString()});
188                    parameterMap.put(
189                            PortletDataHandlerKeys.PORTLET_DATA,
190                            new String[] {Boolean.TRUE.toString()});
191                    parameterMap.put(
192                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
193                            new String[] {Boolean.TRUE.toString()});
194                    parameterMap.put(
195                            PortletDataHandlerKeys.PORTLET_SETUP_ALL,
196                            new String[] {Boolean.TRUE.toString()});
197                    parameterMap.put(
198                            ExportImportDateUtil.RANGE,
199                            new String[] {ExportImportDateUtil.RANGE_FROM_LAST_PUBLISH_DATE});
200    
201                    return parameterMap;
202            }
203    
204            protected static ExportImportConfiguration
205                            buildDefaultRemotePublishingExportImportConfiguration(
206                                    User user, long sourceGroupId, boolean privateLayout,
207                                    String remoteAddress, int remotePort, String remotePathContext,
208                                    boolean secureConnection, long remoteGroupId,
209                                    Map<String, String[]> parameterMap)
210                    throws PortalException {
211    
212                    Map<String, Serializable> publishLayoutRemoteSettingsMap =
213                            ExportImportConfigurationSettingsMapFactory.
214                                    buildPublishLayoutRemoteSettingsMap(
215                                            user.getUserId(), sourceGroupId, privateLayout,
216                                            ExportImportHelperUtil.getAllLayoutIdsMap(
217                                                    sourceGroupId, privateLayout),
218                                            parameterMap, remoteAddress, remotePort, remotePathContext,
219                                            secureConnection, remoteGroupId, privateLayout,
220                                            user.getLocale(), user.getTimeZone());
221    
222                    return ExportImportConfigurationLocalServiceUtil.
223                            addDraftExportImportConfiguration(
224                                    user.getUserId(),
225                                    ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_REMOTE,
226                                    publishLayoutRemoteSettingsMap);
227            }
228    
229            protected static Map<String, String[]> getDefaultPublishingParameters() {
230                    Map<String, String[]> parameterMap =
231                            ExportImportConfigurationParameterMapFactory.buildParameterMap();
232    
233                    return addDefaultPublishingParameters(parameterMap);
234            }
235    
236    }