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