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