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