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                    try {
159                            ThemeDisplay themeDisplay =
160                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
161    
162                            long plid = ParamUtil.getLong(actionRequest, "plid");
163                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
164                            String fileName = ParamUtil.getString(
165                                    actionRequest, "exportFileName");
166                            String range = ParamUtil.getString(actionRequest, "range");
167    
168                            Date startDate = null;
169                            Date endDate = null;
170    
171                            if (range.equals("dateRange")) {
172                                    int startDateMonth = ParamUtil.getInteger(
173                                            actionRequest, "startDateMonth");
174                                    int startDateDay = ParamUtil.getInteger(
175                                            actionRequest, "startDateDay");
176                                    int startDateYear = ParamUtil.getInteger(
177                                            actionRequest, "startDateYear");
178                                    int startDateHour = ParamUtil.getInteger(
179                                            actionRequest, "startDateHour");
180                                    int startDateMinute = ParamUtil.getInteger(
181                                            actionRequest, "startDateMinute");
182                                    int startDateAmPm = ParamUtil.getInteger(
183                                            actionRequest, "startDateAmPm");
184    
185                                    if (startDateAmPm == Calendar.PM) {
186                                            startDateHour += 12;
187                                    }
188    
189                                    startDate = PortalUtil.getDate(
190                                            startDateMonth, startDateDay, startDateYear, startDateHour,
191                                            startDateMinute, themeDisplay.getTimeZone(),
192                                            new PortalException());
193    
194                                    int endDateMonth = ParamUtil.getInteger(
195                                            actionRequest, "endDateMonth");
196                                    int endDateDay = ParamUtil.getInteger(
197                                            actionRequest, "endDateDay");
198                                    int endDateYear = ParamUtil.getInteger(
199                                            actionRequest, "endDateYear");
200                                    int endDateHour = ParamUtil.getInteger(
201                                            actionRequest, "endDateHour");
202                                    int endDateMinute = ParamUtil.getInteger(
203                                            actionRequest, "endDateMinute");
204                                    int endDateAmPm = ParamUtil.getInteger(
205                                            actionRequest, "endDateAmPm");
206    
207                                    if (endDateAmPm == Calendar.PM) {
208                                            endDateHour += 12;
209                                    }
210    
211                                    endDate = PortalUtil.getDate(
212                                            endDateMonth, endDateDay, endDateYear, endDateHour,
213                                            endDateMinute, themeDisplay.getTimeZone(),
214                                            new PortalException());
215                            }
216                            else if (range.equals("fromLastPublishDate")) {
217                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
218    
219                                    PortletPreferences preferences =
220                                            PortletPreferencesFactoryUtil.getPortletSetup(
221                                                    layout, portlet.getPortletId(), StringPool.BLANK);
222    
223                                    long lastPublishDate = GetterUtil.getLong(
224                                            preferences.getValue(
225                                                    "last-publish-date", StringPool.BLANK));
226    
227                                    if (lastPublishDate > 0) {
228                                            Calendar cal = Calendar.getInstance(
229                                                    themeDisplay.getTimeZone(), themeDisplay.getLocale());
230    
231                                            endDate = cal.getTime();
232    
233                                            cal.setTimeInMillis(lastPublishDate);
234    
235                                            startDate = cal.getTime();
236                                    }
237                            }
238    
239                            File file = LayoutServiceUtil.exportPortletInfoAsFile(
240                                    plid, groupId, portlet.getPortletId(),
241                                    actionRequest.getParameterMap(), startDate, endDate);
242    
243                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
244                                    actionRequest);
245                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
246                                    actionResponse);
247    
248                            ServletResponseUtil.sendFile(
249                                    request, response, fileName, new FileInputStream(file),
250                                    ContentTypes.APPLICATION_ZIP);
251    
252                            FileUtil.delete(file);
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());
262                    }
263            }
264    
265            protected void importData(
266                            ActionRequest actionRequest, ActionResponse actionResponse,
267                            Portlet portlet)
268                    throws Exception {
269    
270                    try {
271                            UploadPortletRequest uploadPortletRequest =
272                                    PortalUtil.getUploadPortletRequest(actionRequest);
273    
274                            long plid = ParamUtil.getLong(uploadPortletRequest, "plid");
275                            long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
276                            File file = uploadPortletRequest.getFile("importFileName");
277    
278                            if (!file.exists()) {
279                                    throw new LARFileException("Import file does not exist");
280                            }
281    
282                            LayoutServiceUtil.importPortletInfo(
283                                    plid, groupId, portlet.getPortletId(),
284                                    actionRequest.getParameterMap(), file);
285    
286                            addSuccessMessage(actionRequest, actionResponse);
287                    }
288                    catch (Exception e) {
289                            if ((e instanceof LARFileException) ||
290                                    (e instanceof LARTypeException) ||
291                                    (e instanceof PortletIdException)) {
292    
293                                    SessionErrors.add(actionRequest, e.getClass().getName());
294                            }
295                            else {
296                                    _log.error(e, e);
297    
298                                    SessionErrors.add(
299                                            actionRequest, LayoutImportException.class.getName());
300                            }
301                    }
302            }
303    
304            private static Log _log = LogFactoryUtil.getLog(ExportImportAction.class);
305    
306    }