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