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