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.portletconfiguration.action;
016    
017    import com.liferay.portal.LARFileException;
018    import com.liferay.portal.LARFileSizeException;
019    import com.liferay.portal.LARTypeException;
020    import com.liferay.portal.LocaleException;
021    import com.liferay.portal.NoSuchLayoutException;
022    import com.liferay.portal.PortletIdException;
023    import com.liferay.portal.kernel.lar.ExportImportDateUtil;
024    import com.liferay.portal.kernel.lar.ExportImportHelper;
025    import com.liferay.portal.kernel.lar.MissingReferences;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.servlet.SessionErrors;
029    import com.liferay.portal.kernel.servlet.SessionMessages;
030    import com.liferay.portal.kernel.staging.StagingUtil;
031    import com.liferay.portal.kernel.util.Constants;
032    import com.liferay.portal.kernel.util.DateRange;
033    import com.liferay.portal.kernel.util.ParamUtil;
034    import com.liferay.portal.kernel.util.Validator;
035    import com.liferay.portal.model.Portlet;
036    import com.liferay.portal.security.auth.PrincipalException;
037    import com.liferay.portal.service.LayoutServiceUtil;
038    import com.liferay.portal.util.PortalUtil;
039    import com.liferay.portlet.dynamicdatalists.RecordSetDuplicateRecordSetKeyException;
040    import com.liferay.portlet.dynamicdatamapping.StructureDuplicateStructureKeyException;
041    import com.liferay.portlet.layoutsadmin.action.ImportLayoutsAction;
042    
043    import java.io.InputStream;
044    
045    import javax.portlet.ActionRequest;
046    import javax.portlet.ActionResponse;
047    import javax.portlet.PortletConfig;
048    import javax.portlet.PortletContext;
049    import javax.portlet.PortletRequestDispatcher;
050    import javax.portlet.RenderRequest;
051    import javax.portlet.RenderResponse;
052    import javax.portlet.ResourceRequest;
053    import javax.portlet.ResourceResponse;
054    
055    import org.apache.struts.action.ActionForm;
056    import org.apache.struts.action.ActionForward;
057    import org.apache.struts.action.ActionMapping;
058    
059    /**
060     * @author Jorge Ferrer
061     * @author Raymond Aug??
062     */
063    public class ExportImportAction extends ImportLayoutsAction {
064    
065            @Override
066            public void processAction(
067                            ActionMapping actionMapping, ActionForm actionForm,
068                            PortletConfig portletConfig, ActionRequest actionRequest,
069                            ActionResponse actionResponse)
070                    throws Exception {
071    
072                    Portlet portlet = null;
073    
074                    try {
075                            portlet = ActionUtil.getPortlet(actionRequest);
076                    }
077                    catch (PrincipalException pe) {
078                            SessionErrors.add(
079                                    actionRequest, PrincipalException.class.getName());
080    
081                            setForward(actionRequest, "portlet.portlet_configuration.error");
082                    }
083    
084                    actionRequest = ActionUtil.getWrappedActionRequest(actionRequest, null);
085    
086                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
087    
088                    try {
089                            if (Validator.isNotNull(cmd)) {
090                                    String redirect = ParamUtil.getString(
091                                            actionRequest, "redirect");
092    
093                                    if (cmd.equals(Constants.ADD_TEMP)) {
094                                            addTempFileEntry(
095                                                    actionRequest, actionResponse,
096                                                    ExportImportHelper.TEMP_FOLDER_NAME +
097                                                            portlet.getPortletId());
098    
099                                            validateFile(
100                                                    actionRequest, actionResponse,
101                                                    ExportImportHelper.TEMP_FOLDER_NAME +
102                                                            portlet.getPortletId());
103                                    }
104                                    else if (cmd.equals("copy_from_live")) {
105                                            StagingUtil.copyFromLive(actionRequest, portlet);
106    
107                                            sendRedirect(actionRequest, actionResponse);
108                                    }
109                                    else if (cmd.equals(Constants.DELETE_TEMP)) {
110                                            deleteTempFileEntry(
111                                                    actionRequest, actionResponse,
112                                                    ExportImportHelper.TEMP_FOLDER_NAME +
113                                                            portlet.getPortletId());
114                                    }
115                                    else if (cmd.equals(Constants.EXPORT)) {
116                                            hideDefaultSuccessMessage(actionRequest);
117    
118                                            exportData(actionRequest, actionResponse, portlet);
119    
120                                            sendRedirect(actionRequest, actionResponse, redirect);
121                                    }
122                                    else if (cmd.equals(Constants.IMPORT)) {
123                                            hideDefaultSuccessMessage(actionRequest);
124    
125                                            importData(
126                                                    actionRequest, actionResponse,
127                                                    ExportImportHelper.TEMP_FOLDER_NAME +
128                                                            portlet.getPortletId());
129    
130                                            SessionMessages.add(
131                                                    actionRequest,
132                                                    PortalUtil.getPortletId(actionRequest) +
133                                                            SessionMessages.KEY_SUFFIX_CLOSE_REFRESH_PORTLET,
134                                                    portlet.getPortletId());
135    
136                                            sendRedirect(actionRequest, actionResponse, redirect);
137                                    }
138                                    else if (cmd.equals(Constants.PUBLISH_TO_LIVE)) {
139                                            hideDefaultSuccessMessage(actionRequest);
140    
141                                            StagingUtil.publishToLive(actionRequest, portlet);
142    
143                                            sendRedirect(actionRequest, actionResponse);
144                                    }
145                            }
146                    }
147                    catch (Exception e) {
148                            if (cmd.equals(Constants.ADD_TEMP) ||
149                                    cmd.equals(Constants.DELETE_TEMP)) {
150    
151                                    handleUploadException(
152                                            portletConfig, actionRequest, actionResponse,
153                                            ExportImportHelper.TEMP_FOLDER_NAME +
154                                                    portlet.getPortletId(),
155                                            e);
156                            }
157                            else {
158                                    if ((e instanceof LARFileException) ||
159                                            (e instanceof LARFileSizeException) ||
160                                            (e instanceof LARTypeException) ||
161                                            (e instanceof LocaleException) ||
162                                            (e instanceof NoSuchLayoutException) ||
163                                            (e instanceof PortletIdException) ||
164                                            (e instanceof PrincipalException) ||
165                                            (e instanceof StructureDuplicateStructureKeyException) ||
166                                            (e instanceof RecordSetDuplicateRecordSetKeyException)) {
167    
168                                            SessionErrors.add(actionRequest, e.getClass());
169                                    }
170                                    else {
171                                            _log.error(e, e);
172    
173                                            SessionErrors.add(
174                                                    actionRequest, ExportImportAction.class.getName());
175                                    }
176                            }
177                    }
178            }
179    
180            @Override
181            public ActionForward render(
182                            ActionMapping actionMapping, ActionForm actionForm,
183                            PortletConfig portletConfig, RenderRequest renderRequest,
184                            RenderResponse renderResponse)
185                    throws Exception {
186    
187                    Portlet portlet = null;
188    
189                    try {
190                            portlet = ActionUtil.getPortlet(renderRequest);
191                    }
192                    catch (PrincipalException pe) {
193                            SessionErrors.add(
194                                    renderRequest, PrincipalException.class.getName());
195    
196                            return actionMapping.findForward(
197                                    "portlet.portlet_configuration.error");
198                    }
199    
200                    renderResponse.setTitle(ActionUtil.getTitle(portlet, renderRequest));
201    
202                    renderRequest = ActionUtil.getWrappedRenderRequest(renderRequest, null);
203    
204                    String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
205    
206                    String forward = "portlet.portlet_configuration.export_import";
207    
208                    if (cmd.equals(Constants.PUBLISH_TO_LIVE)) {
209                            forward = "portlet.portlet_configuration.staging";
210                    }
211    
212                    return actionMapping.findForward(getForward(renderRequest, forward));
213            }
214    
215            @Override
216            public void serveResource(
217                            ActionMapping actionMapping, ActionForm actionForm,
218                            PortletConfig portletConfig, ResourceRequest resourceRequest,
219                            ResourceResponse resourceResponse)
220                    throws Exception {
221    
222                    String cmd = ParamUtil.getString(resourceRequest, Constants.CMD);
223    
224                    PortletContext portletContext = portletConfig.getPortletContext();
225    
226                    PortletRequestDispatcher portletRequestDispatcher = null;
227    
228                    if (cmd.equals(Constants.EXPORT)) {
229                            portletRequestDispatcher = portletContext.getRequestDispatcher(
230                                    "/html/portlet/portlet_configuration/" +
231                                            "export_portlet_processes.jsp");
232                    }
233                    else if (cmd.equals(Constants.IMPORT)) {
234                            portletRequestDispatcher = portletContext.getRequestDispatcher(
235                                    "/html/portlet/portlet_configuration/" +
236                                            "import_portlet_processes.jsp");
237                    }
238                    else if (cmd.equals(Constants.PUBLISH)) {
239                            portletRequestDispatcher = portletContext.getRequestDispatcher(
240                                    "/html/portlet/portlet_configuration/" +
241                                            "publish_portlet_processes.jsp");
242                    }
243                    else {
244                            portletRequestDispatcher = portletContext.getRequestDispatcher(
245                                    "/html/portlet/portlet_configuration/" +
246                                            "import_portlet_resources.jsp");
247                    }
248    
249                    resourceRequest = ActionUtil.getWrappedResourceRequest(
250                            resourceRequest, null);
251    
252                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
253            }
254    
255            protected void exportData(
256                            ActionRequest actionRequest, ActionResponse actionResponse,
257                            Portlet portlet)
258                    throws Exception {
259    
260                    try {
261                            long plid = ParamUtil.getLong(actionRequest, "plid");
262                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
263                            String fileName = ParamUtil.getString(
264                                    actionRequest, "exportFileName");
265    
266                            DateRange dateRange = ExportImportDateUtil.getDateRange(
267                                    actionRequest, groupId, false, plid, portlet.getPortletId(),
268                                    ExportImportDateUtil.RANGE_ALL);
269    
270                            LayoutServiceUtil.exportPortletInfoAsFileInBackground(
271                                    portlet.getPortletId(), plid, groupId, portlet.getPortletId(),
272                                    actionRequest.getParameterMap(), dateRange.getStartDate(),
273                                    dateRange.getEndDate(), fileName);
274                    }
275                    catch (Exception e) {
276                            if (_log.isDebugEnabled()) {
277                                    _log.debug(e, e);
278                            }
279    
280                            SessionErrors.add(actionRequest, e.getClass(), e);
281                    }
282            }
283    
284            @Override
285            protected void importData(
286                            ActionRequest actionRequest, String fileName,
287                            InputStream inputStream)
288                    throws Exception {
289    
290                    long plid = ParamUtil.getLong(actionRequest, "plid");
291                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
292    
293                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
294    
295                    LayoutServiceUtil.importPortletInfoInBackground(
296                            portlet.getPortletId(), plid, groupId, portlet.getPortletId(),
297                            actionRequest.getParameterMap(), inputStream);
298            }
299    
300            @Override
301            protected MissingReferences validateFile(
302                            ActionRequest actionRequest, InputStream inputStream)
303                    throws Exception {
304    
305                    long plid = ParamUtil.getLong(actionRequest, "plid");
306                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
307    
308                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
309    
310                    return LayoutServiceUtil.validateImportPortletInfo(
311                            plid, groupId, portlet.getPortletId(),
312                            actionRequest.getParameterMap(), inputStream);
313            }
314    
315            private static final Log _log = LogFactoryUtil.getLog(
316                    ExportImportAction.class);
317    
318    }