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.portlet.LiferayPortletConfig;
029    import com.liferay.portal.kernel.servlet.SessionErrors;
030    import com.liferay.portal.kernel.servlet.SessionMessages;
031    import com.liferay.portal.kernel.staging.StagingUtil;
032    import com.liferay.portal.kernel.util.Constants;
033    import com.liferay.portal.kernel.util.DateRange;
034    import com.liferay.portal.kernel.util.ParamUtil;
035    import com.liferay.portal.kernel.util.Validator;
036    import com.liferay.portal.model.Portlet;
037    import com.liferay.portal.security.auth.PrincipalException;
038    import com.liferay.portal.service.LayoutServiceUtil;
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                    actionRequest = ActionUtil.getWrappedActionRequest(actionRequest, null);
075    
076                    Portlet portlet = null;
077    
078                    try {
079                            portlet = ActionUtil.getPortlet(actionRequest);
080                    }
081                    catch (PrincipalException pe) {
082                            SessionErrors.add(
083                                    actionRequest, PrincipalException.class.getName());
084    
085                            setForward(actionRequest, "portlet.portlet_configuration.error");
086                    }
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                                            exportData(actionRequest, actionResponse, portlet);
119    
120                                            sendRedirect(actionRequest, actionResponse, redirect);
121                                    }
122                                    else if (cmd.equals(Constants.IMPORT)) {
123                                            importData(
124                                                    actionRequest, actionResponse,
125                                                    ExportImportHelper.TEMP_FOLDER_NAME +
126                                                            portlet.getPortletId());
127    
128                                            LiferayPortletConfig liferayPortletConfig =
129                                                    (LiferayPortletConfig)portletConfig;
130    
131                                            SessionMessages.add(
132                                                    actionRequest,
133                                                    liferayPortletConfig.getPortletId() +
134                                                            SessionMessages.KEY_SUFFIX_CLOSE_REFRESH_PORTLET,
135                                                    portlet.getPortletId());
136    
137                                            sendRedirect(actionRequest, actionResponse, redirect);
138                                    }
139                                    else if (cmd.equals("publish_to_live")) {
140                                            StagingUtil.publishToLive(actionRequest, portlet);
141    
142                                            sendRedirect(actionRequest, actionResponse);
143                                    }
144                            }
145                            else {
146                                    long plid = ParamUtil.getLong(actionRequest, "plid");
147    
148                                    DateRange dateRange = ExportImportHelperUtil.getDateRange(
149                                            actionRequest, 0, false, plid, portlet.getPortletId());
150    
151                                    Date startDate = dateRange.getStartDate();
152    
153                                    if (startDate != null) {
154                                            actionResponse.setRenderParameter(
155                                                    "startDate", String.valueOf(startDate.getTime()));
156                                    }
157    
158                                    Date endDate = dateRange.getEndDate();
159    
160                                    if (endDate != null) {
161                                            actionResponse.setRenderParameter(
162                                                    "endDate", String.valueOf(endDate.getTime()));
163                                    }
164                            }
165                    }
166                    catch (Exception e) {
167                            if (cmd.equals(Constants.ADD_TEMP) ||
168                                    cmd.equals(Constants.DELETE_TEMP)) {
169    
170                                    handleUploadException(
171                                            portletConfig, actionRequest, actionResponse,
172                                            ExportImportHelper.TEMP_FOLDER_NAME +
173                                                    portlet.getPortletId(),
174                                            e);
175                            }
176                            else {
177                                    if ((e instanceof LARFileException) ||
178                                            (e instanceof LARFileSizeException) ||
179                                            (e instanceof LARTypeException) ||
180                                            (e instanceof LocaleException) ||
181                                            (e instanceof NoSuchLayoutException) ||
182                                            (e instanceof PortletIdException) ||
183                                            (e instanceof PrincipalException) ||
184                                            (e instanceof StructureDuplicateStructureKeyException) ||
185                                            (e instanceof RecordSetDuplicateRecordSetKeyException)) {
186    
187                                            SessionErrors.add(actionRequest, e.getClass());
188                                    }
189                                    else {
190                                            _log.error(e, e);
191    
192                                            SessionErrors.add(
193                                                    actionRequest, ExportImportAction.class.getName());
194                                    }
195                            }
196                    }
197            }
198    
199            @Override
200            public ActionForward render(
201                            ActionMapping actionMapping, ActionForm actionForm,
202                            PortletConfig portletConfig, RenderRequest renderRequest,
203                            RenderResponse renderResponse)
204                    throws Exception {
205    
206                    Portlet portlet = null;
207    
208                    try {
209                            portlet = ActionUtil.getPortlet(renderRequest);
210                    }
211                    catch (PrincipalException pe) {
212                            SessionErrors.add(
213                                    renderRequest, PrincipalException.class.getName());
214    
215                            return actionMapping.findForward(
216                                    "portlet.portlet_configuration.error");
217                    }
218    
219                    renderResponse.setTitle(ActionUtil.getTitle(portlet, renderRequest));
220    
221                    renderRequest = ActionUtil.getWrappedRenderRequest(renderRequest, null);
222    
223                    return actionMapping.findForward(
224                            getForward(
225                                    renderRequest, "portlet.portlet_configuration.export_import"));
226            }
227    
228            @Override
229            public void serveResource(
230                            ActionMapping actionMapping, ActionForm actionForm,
231                            PortletConfig portletConfig, ResourceRequest resourceRequest,
232                            ResourceResponse resourceResponse)
233                    throws Exception {
234    
235                    String cmd = ParamUtil.getString(resourceRequest, Constants.CMD);
236    
237                    PortletContext portletContext = portletConfig.getPortletContext();
238    
239                    PortletRequestDispatcher portletRequestDispatcher = null;
240    
241                    if (cmd.equals(Constants.EXPORT)) {
242                            portletRequestDispatcher = portletContext.getRequestDispatcher(
243                                    "/html/portlet/portlet_configuration/" +
244                                            "export_portlet_processes.jsp");
245                    }
246                    else if (cmd.equals(Constants.IMPORT)) {
247                            portletRequestDispatcher = portletContext.getRequestDispatcher(
248                                    "/html/portlet/portlet_configuration/" +
249                                            "import_portlet_processes.jsp");
250                    }
251                    else if (cmd.equals(Constants.PUBLISH)) {
252                            portletRequestDispatcher = portletContext.getRequestDispatcher(
253                                    "/html/portlet/portlet_configuration/" +
254                                            "publish_portlet_processes.jsp");
255                    }
256                    else {
257                            portletRequestDispatcher = portletContext.getRequestDispatcher(
258                                    "/html/portlet/portlet_configuration/" +
259                                            "import_portlet_resources.jsp");
260                    }
261    
262                    resourceRequest = ActionUtil.getWrappedResourceRequest(
263                            resourceRequest, null);
264    
265                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
266            }
267    
268            protected void exportData(
269                            ActionRequest actionRequest, ActionResponse actionResponse,
270                            Portlet portlet)
271                    throws Exception {
272    
273                    try {
274                            long plid = ParamUtil.getLong(actionRequest, "plid");
275                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
276                            String fileName = ParamUtil.getString(
277                                    actionRequest, "exportFileName");
278    
279                            DateRange dateRange = ExportImportHelperUtil.getDateRange(
280                                    actionRequest, groupId, false, plid, portlet.getPortletId());
281    
282                            LayoutServiceUtil.exportPortletInfoAsFileInBackground(
283                                    portlet.getPortletId(), plid, groupId, portlet.getPortletId(),
284                                    actionRequest.getParameterMap(), dateRange.getStartDate(),
285                                    dateRange.getEndDate(), fileName);
286                    }
287                    catch (Exception e) {
288                            if (_log.isDebugEnabled()) {
289                                    _log.debug(e, e);
290                            }
291    
292                            SessionErrors.add(actionRequest, e.getClass(), e);
293                    }
294            }
295    
296            @Override
297            protected void importData(
298                            ActionRequest actionRequest, String fileName,
299                            InputStream inputStream)
300                    throws Exception {
301    
302                    long plid = ParamUtil.getLong(actionRequest, "plid");
303                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
304    
305                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
306    
307                    LayoutServiceUtil.importPortletInfoInBackground(
308                            portlet.getPortletId(), plid, groupId, portlet.getPortletId(),
309                            actionRequest.getParameterMap(), inputStream);
310            }
311    
312            @Override
313            protected MissingReferences validateFile(
314                            ActionRequest actionRequest, InputStream inputStream)
315                    throws Exception {
316    
317                    long plid = ParamUtil.getLong(actionRequest, "plid");
318                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
319    
320                    Portlet portlet = ActionUtil.getPortlet(actionRequest);
321    
322                    return LayoutServiceUtil.validateImportPortletInfo(
323                            plid, groupId, portlet.getPortletId(),
324                            actionRequest.getParameterMap(), inputStream);
325            }
326    
327            private static Log _log = LogFactoryUtil.getLog(ExportImportAction.class);
328    
329    }