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.LayoutImportException;
021    import com.liferay.portal.LocaleException;
022    import com.liferay.portal.NoSuchLayoutException;
023    import com.liferay.portal.PortletIdException;
024    import com.liferay.portal.kernel.exception.PortalException;
025    import com.liferay.portal.kernel.log.Log;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
028    import com.liferay.portal.kernel.servlet.SessionErrors;
029    import com.liferay.portal.kernel.staging.StagingUtil;
030    import com.liferay.portal.kernel.upload.UploadException;
031    import com.liferay.portal.kernel.upload.UploadPortletRequest;
032    import com.liferay.portal.kernel.util.Constants;
033    import com.liferay.portal.kernel.util.ContentTypes;
034    import com.liferay.portal.kernel.util.FileUtil;
035    import com.liferay.portal.kernel.util.GetterUtil;
036    import com.liferay.portal.kernel.util.ParamUtil;
037    import com.liferay.portal.kernel.util.StringPool;
038    import com.liferay.portal.model.Layout;
039    import com.liferay.portal.model.Portlet;
040    import com.liferay.portal.security.auth.PrincipalException;
041    import com.liferay.portal.service.LayoutLocalServiceUtil;
042    import com.liferay.portal.service.LayoutServiceUtil;
043    import com.liferay.portal.struts.ActionConstants;
044    import com.liferay.portal.theme.ThemeDisplay;
045    import com.liferay.portal.util.PortalUtil;
046    import com.liferay.portal.util.WebKeys;
047    import com.liferay.portlet.PortletPreferencesFactoryUtil;
048    import com.liferay.portlet.dynamicdatalists.RecordSetDuplicateRecordSetKeyException;
049    import com.liferay.portlet.dynamicdatamapping.StructureDuplicateStructureKeyException;
050    
051    import java.io.File;
052    import java.io.FileInputStream;
053    
054    import java.util.Calendar;
055    import java.util.Date;
056    
057    import javax.portlet.ActionRequest;
058    import javax.portlet.ActionResponse;
059    import javax.portlet.PortletConfig;
060    import javax.portlet.PortletPreferences;
061    import javax.portlet.PortletRequest;
062    import javax.portlet.RenderRequest;
063    import javax.portlet.RenderResponse;
064    
065    import javax.servlet.http.HttpServletRequest;
066    import javax.servlet.http.HttpServletResponse;
067    
068    import org.apache.struts.action.ActionForm;
069    import org.apache.struts.action.ActionForward;
070    import org.apache.struts.action.ActionMapping;
071    
072    /**
073     * @author Jorge Ferrer
074     * @author Raymond Augé
075     */
076    public class ExportImportAction extends EditConfigurationAction {
077    
078            @Override
079            public void processAction(
080                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
081                            ActionRequest actionRequest, ActionResponse actionResponse)
082                    throws Exception {
083    
084                    Portlet portlet = null;
085    
086                    try {
087                            portlet = getPortlet(actionRequest);
088                    }
089                    catch (PrincipalException pe) {
090                            SessionErrors.add(
091                                    actionRequest, PrincipalException.class.getName());
092    
093                            setForward(actionRequest, "portlet.portlet_configuration.error");
094                    }
095    
096                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
097    
098                    try {
099                            if (cmd.equals("copy_from_live")) {
100                                    StagingUtil.copyFromLive(actionRequest, portlet);
101    
102                                    sendRedirect(actionRequest, actionResponse);
103                            }
104                            else if (cmd.equals(Constants.EXPORT)) {
105                                    exportData(actionRequest, actionResponse, portlet);
106    
107                                    sendRedirect(actionRequest, actionResponse);
108                            }
109                            else if (cmd.equals(Constants.IMPORT)) {
110                                    checkExceededSizeLimit(actionRequest);
111    
112                                    importData(actionRequest, actionResponse, portlet);
113    
114                                    sendRedirect(actionRequest, actionResponse);
115                            }
116                            else if (cmd.equals("publish_to_live")) {
117                                    StagingUtil.publishToLive(actionRequest, portlet);
118    
119                                    sendRedirect(actionRequest, actionResponse);
120                            }
121                    }
122                    catch (Exception e) {
123                            if ((e instanceof LARFileSizeException) ||
124                                    (e instanceof NoSuchLayoutException) ||
125                                    (e instanceof PrincipalException)) {
126    
127                                    SessionErrors.add(actionRequest, e.getClass());
128    
129                                    setForward(
130                                            actionRequest, "portlet.portlet_configuration.error");
131                            }
132                            else {
133                                    throw e;
134                            }
135                    }
136            }
137    
138            @Override
139            public ActionForward render(
140                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
141                            RenderRequest renderRequest, RenderResponse renderResponse)
142                    throws Exception {
143    
144                    Portlet portlet = null;
145    
146                    try {
147                            portlet = getPortlet(renderRequest);
148                    }
149                    catch (PrincipalException pe) {
150                            SessionErrors.add(
151                                    renderRequest, PrincipalException.class.getName());
152    
153                            return mapping.findForward("portlet.portlet_configuration.error");
154                    }
155    
156                    renderResponse.setTitle(getTitle(portlet, renderRequest));
157    
158                    return mapping.findForward(
159                            getForward(
160                                    renderRequest, "portlet.portlet_configuration.export_import"));
161            }
162    
163            protected void checkExceededSizeLimit(PortletRequest portletRequest)
164                    throws PortalException {
165    
166                    UploadException uploadException =
167                            (UploadException)portletRequest.getAttribute(
168                                    WebKeys.UPLOAD_EXCEPTION);
169    
170                    if (uploadException != null) {
171                            if (uploadException.isExceededSizeLimit()) {
172                                    throw new LARFileSizeException(uploadException.getCause());
173                            }
174    
175                            throw new PortalException(uploadException.getCause());
176                    }
177            }
178    
179            protected void exportData(
180                            ActionRequest actionRequest, ActionResponse actionResponse,
181                            Portlet portlet)
182                    throws Exception {
183    
184                    File file = null;
185    
186                    try {
187                            ThemeDisplay themeDisplay =
188                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
189    
190                            long plid = ParamUtil.getLong(actionRequest, "plid");
191                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
192                            String fileName = ParamUtil.getString(
193                                    actionRequest, "exportFileName");
194                            String range = ParamUtil.getString(actionRequest, "range");
195    
196                            Date startDate = null;
197                            Date endDate = null;
198    
199                            if (range.equals("dateRange")) {
200                                    int startDateMonth = ParamUtil.getInteger(
201                                            actionRequest, "startDateMonth");
202                                    int startDateDay = ParamUtil.getInteger(
203                                            actionRequest, "startDateDay");
204                                    int startDateYear = ParamUtil.getInteger(
205                                            actionRequest, "startDateYear");
206                                    int startDateHour = ParamUtil.getInteger(
207                                            actionRequest, "startDateHour");
208                                    int startDateMinute = ParamUtil.getInteger(
209                                            actionRequest, "startDateMinute");
210                                    int startDateAmPm = ParamUtil.getInteger(
211                                            actionRequest, "startDateAmPm");
212    
213                                    if (startDateAmPm == Calendar.PM) {
214                                            startDateHour += 12;
215                                    }
216    
217                                    startDate = PortalUtil.getDate(
218                                            startDateMonth, startDateDay, startDateYear, startDateHour,
219                                            startDateMinute, themeDisplay.getTimeZone(),
220                                            PortalException.class);
221    
222                                    int endDateMonth = ParamUtil.getInteger(
223                                            actionRequest, "endDateMonth");
224                                    int endDateDay = ParamUtil.getInteger(
225                                            actionRequest, "endDateDay");
226                                    int endDateYear = ParamUtil.getInteger(
227                                            actionRequest, "endDateYear");
228                                    int endDateHour = ParamUtil.getInteger(
229                                            actionRequest, "endDateHour");
230                                    int endDateMinute = ParamUtil.getInteger(
231                                            actionRequest, "endDateMinute");
232                                    int endDateAmPm = ParamUtil.getInteger(
233                                            actionRequest, "endDateAmPm");
234    
235                                    if (endDateAmPm == Calendar.PM) {
236                                            endDateHour += 12;
237                                    }
238    
239                                    endDate = PortalUtil.getDate(
240                                            endDateMonth, endDateDay, endDateYear, endDateHour,
241                                            endDateMinute, themeDisplay.getTimeZone(),
242                                            PortalException.class);
243                            }
244                            else if (range.equals("fromLastPublishDate")) {
245                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
246    
247                                    PortletPreferences preferences =
248                                            PortletPreferencesFactoryUtil.getPortletSetup(
249                                                    layout, portlet.getPortletId(), StringPool.BLANK);
250    
251                                    long lastPublishDate = GetterUtil.getLong(
252                                            preferences.getValue(
253                                                    "last-publish-date", StringPool.BLANK));
254    
255                                    if (lastPublishDate > 0) {
256                                            endDate = new Date();
257    
258                                            startDate = new Date(lastPublishDate);
259                                    }
260                            }
261    
262                            file = LayoutServiceUtil.exportPortletInfoAsFile(
263                                    plid, groupId, portlet.getPortletId(),
264                                    actionRequest.getParameterMap(), startDate, endDate);
265    
266                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
267                                    actionRequest);
268                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
269                                    actionResponse);
270    
271                            ServletResponseUtil.sendFile(
272                                    request, response, fileName, new FileInputStream(file),
273                                    ContentTypes.APPLICATION_ZIP);
274    
275                            setForward(actionRequest, ActionConstants.COMMON_NULL);
276                    }
277                    catch (Exception e) {
278                            if (_log.isDebugEnabled()) {
279                                    _log.debug(e, e);
280                            }
281    
282                            SessionErrors.add(actionRequest, e.getClass(), e);
283                    }
284                    finally {
285                            FileUtil.delete(file);
286                    }
287            }
288    
289            protected void importData(
290                            ActionRequest actionRequest, ActionResponse actionResponse,
291                            Portlet portlet)
292                    throws Exception {
293    
294                    try {
295                            UploadPortletRequest uploadPortletRequest =
296                                    PortalUtil.getUploadPortletRequest(actionRequest);
297    
298                            long plid = ParamUtil.getLong(uploadPortletRequest, "plid");
299                            long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
300                            File file = uploadPortletRequest.getFile("importFileName");
301    
302                            if (!file.exists()) {
303                                    throw new LARFileException("Import file does not exist");
304                            }
305    
306                            LayoutServiceUtil.importPortletInfo(
307                                    plid, groupId, portlet.getPortletId(),
308                                    actionRequest.getParameterMap(), file);
309    
310                            addSuccessMessage(actionRequest, actionResponse);
311                    }
312                    catch (Exception e) {
313                            if ((e instanceof LARFileException) ||
314                                    (e instanceof LARTypeException) ||
315                                    (e instanceof PortletIdException)) {
316    
317                                    SessionErrors.add(actionRequest, e.getClass());
318                            }
319                            else if ((e instanceof LocaleException) ||
320                                             (e instanceof RecordSetDuplicateRecordSetKeyException) ||
321                                             (e instanceof StructureDuplicateStructureKeyException)) {
322    
323                                    SessionErrors.add(actionRequest, e.getClass(), e);
324                            }
325                            else {
326                                    _log.error(e, e);
327    
328                                    SessionErrors.add(
329                                            actionRequest, LayoutImportException.class.getName());
330                            }
331                    }
332            }
333    
334            private static Log _log = LogFactoryUtil.getLog(ExportImportAction.class);
335    
336    }