001    /**
002     * Copyright (c) 2000-2013 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.ExportImportHelper;
024    import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
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,
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("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                    return actionMapping.findForward(
205                            getForward(
206                                    renderRequest, "portlet.portlet_configuration.export_import"));
207            }
208    
209            @Override
210            public void serveResource(
211                            ActionMapping actionMapping, ActionForm actionForm,
212                            PortletConfig portletConfig, ResourceRequest resourceRequest,
213                            ResourceResponse resourceResponse)
214                    throws Exception {
215    
216                    String cmd = ParamUtil.getString(resourceRequest, Constants.CMD);
217    
218                    PortletContext portletContext = portletConfig.getPortletContext();
219    
220                    PortletRequestDispatcher portletRequestDispatcher = null;
221    
222                    if (cmd.equals(Constants.EXPORT)) {
223                            portletRequestDispatcher = portletContext.getRequestDispatcher(
224                                    "/html/portlet/portlet_configuration/" +
225                                            "export_portlet_processes.jsp");
226                    }
227                    else if (cmd.equals(Constants.IMPORT)) {
228                            portletRequestDispatcher = portletContext.getRequestDispatcher(
229                                    "/html/portlet/portlet_configuration/" +
230                                            "import_portlet_processes.jsp");
231                    }
232                    else if (cmd.equals(Constants.PUBLISH)) {
233                            portletRequestDispatcher = portletContext.getRequestDispatcher(
234                                    "/html/portlet/portlet_configuration/" +
235                                            "publish_portlet_processes.jsp");
236                    }
237                    else {
238                            portletRequestDispatcher = portletContext.getRequestDispatcher(
239                                    "/html/portlet/portlet_configuration/" +
240                                            "import_portlet_resources.jsp");
241                    }
242    
243                    resourceRequest = ActionUtil.getWrappedResourceRequest(
244                            resourceRequest, null);
245    
246                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
247            }
248    
249            protected void exportData(
250                            ActionRequest actionRequest, ActionResponse actionResponse,
251                            Portlet portlet)
252                    throws Exception {
253    
254                    try {
255                            long plid = ParamUtil.getLong(actionRequest, "plid");
256                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
257                            String fileName = ParamUtil.getString(
258                                    actionRequest, "exportFileName");
259    
260                            DateRange dateRange = ExportImportHelperUtil.getDateRange(
261                                    actionRequest, groupId, false, plid, portlet.getPortletId(),
262                                    "all");
263    
264                            LayoutServiceUtil.exportPortletInfoAsFileInBackground(
265                                    portlet.getPortletId(), plid, groupId, portlet.getPortletId(),
266                                    actionRequest.getParameterMap(), dateRange.getStartDate(),
267                                    dateRange.getEndDate(), fileName);
268                    }
269                    catch (Exception e) {
270                            if (_log.isDebugEnabled()) {
271                                    _log.debug(e, e);
272                            }
273    
274                            SessionErrors.add(actionRequest, e.getClass(), e);
275                    }
276            }
277    
278            @Override
279            protected void importData(
280                            ActionRequest actionRequest, String fileName,
281                            InputStream inputStream)
282                    throws Exception {
283    
284                    long plid = ParamUtil.getLong(actionRequest, "plid");
285                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
286    
287                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
288    
289                    LayoutServiceUtil.importPortletInfoInBackground(
290                            portlet.getPortletId(), plid, groupId, portlet.getPortletId(),
291                            actionRequest.getParameterMap(), inputStream);
292            }
293    
294            @Override
295            protected MissingReferences validateFile(
296                            ActionRequest actionRequest, InputStream inputStream)
297                    throws Exception {
298    
299                    long plid = ParamUtil.getLong(actionRequest, "plid");
300                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
301    
302                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
303    
304                    return LayoutServiceUtil.validateImportPortletInfo(
305                            plid, groupId, portlet.getPortletId(),
306                            actionRequest.getParameterMap(), inputStream);
307            }
308    
309            private static Log _log = LogFactoryUtil.getLog(ExportImportAction.class);
310    
311    }