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