1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.portletconfiguration.action;
16  
17  import com.liferay.portal.LARFileException;
18  import com.liferay.portal.LARTypeException;
19  import com.liferay.portal.LayoutImportException;
20  import com.liferay.portal.NoSuchLayoutException;
21  import com.liferay.portal.PortletIdException;
22  import com.liferay.portal.kernel.exception.PortalException;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.servlet.SessionMessages;
27  import com.liferay.portal.kernel.upload.UploadPortletRequest;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ContentTypes;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.service.LayoutServiceUtil;
34  import com.liferay.portal.struts.ActionConstants;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.portlet.communities.util.StagingUtil;
39  import com.liferay.util.servlet.ServletResponseUtil;
40  
41  import java.io.File;
42  import java.io.FileInputStream;
43  
44  import java.util.Calendar;
45  import java.util.Date;
46  
47  import javax.portlet.ActionRequest;
48  import javax.portlet.ActionResponse;
49  import javax.portlet.PortletConfig;
50  import javax.portlet.RenderRequest;
51  import javax.portlet.RenderResponse;
52  
53  import javax.servlet.http.HttpServletResponse;
54  
55  import org.apache.struts.action.ActionForm;
56  import org.apache.struts.action.ActionForward;
57  import org.apache.struts.action.ActionMapping;
58  
59  /**
60   * <a href="ExportImportAction.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Jorge Ferrer
63   */
64  public class ExportImportAction extends EditConfigurationAction {
65  
66      public void processAction(
67              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
68              ActionRequest actionRequest, ActionResponse actionResponse)
69          throws Exception {
70  
71          Portlet portlet = null;
72  
73          try {
74              portlet = getPortlet(actionRequest);
75          }
76          catch (PrincipalException pe) {
77              SessionErrors.add(
78                  actionRequest, PrincipalException.class.getName());
79  
80              setForward(actionRequest, "portlet.portlet_configuration.error");
81          }
82  
83          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
84  
85          try {
86              if (cmd.equals("copy_from_live")) {
87                  StagingUtil.copyFromLive(actionRequest, portlet);
88  
89                  sendRedirect(actionRequest, actionResponse);
90              }
91              else if (cmd.equals("export")) {
92                  exportData(actionRequest, actionResponse, portlet);
93              }
94              else if (cmd.equals("import")) {
95                  importData(actionRequest, portlet);
96  
97                  sendRedirect(actionRequest, actionResponse);
98              }
99              else if (cmd.equals("publish_to_live")) {
100                 StagingUtil.publishToLive(actionRequest, portlet);
101 
102                 sendRedirect(actionRequest, actionResponse);
103             }
104         }
105         catch (Exception e) {
106             if (e instanceof NoSuchLayoutException ||
107                 e instanceof PrincipalException) {
108 
109                 SessionErrors.add(actionRequest, e.getClass().getName());
110 
111                 setForward(
112                     actionRequest, "portlet.portlet_configuration.error");
113             }
114             else {
115                 throw e;
116             }
117         }
118     }
119 
120     public ActionForward render(
121             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
122             RenderRequest renderRequest, RenderResponse renderResponse)
123         throws Exception {
124 
125         Portlet portlet = null;
126 
127         try {
128             portlet = getPortlet(renderRequest);
129         }
130         catch (PrincipalException pe) {
131             SessionErrors.add(
132                 renderRequest, PrincipalException.class.getName());
133 
134             return mapping.findForward("portlet.portlet_configuration.error");
135         }
136 
137         renderResponse.setTitle(getTitle(portlet, renderRequest));
138 
139         return mapping.findForward(getForward(
140             renderRequest, "portlet.portlet_configuration.export_import"));
141     }
142 
143     protected void exportData(
144             ActionRequest actionRequest, ActionResponse actionResponse,
145             Portlet portlet)
146         throws Exception {
147 
148         try {
149             ThemeDisplay themeDisplay =
150                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
151 
152             long plid = ParamUtil.getLong(actionRequest, "plid");
153             long groupId = ParamUtil.getLong(actionRequest, "groupId");
154             String fileName = ParamUtil.getString(
155                 actionRequest, "exportFileName");
156             boolean dateRange = ParamUtil.getBoolean(
157                 actionRequest, "dateRange");
158             Date startDate = null;
159             Date endDate = null;
160 
161             if (dateRange) {
162                 int startDateMonth = ParamUtil.getInteger(
163                     actionRequest, "startDateMonth");
164                 int startDateDay = ParamUtil.getInteger(
165                     actionRequest, "startDateDay");
166                 int startDateYear = ParamUtil.getInteger(
167                     actionRequest, "startDateYear");
168                 int startDateHour = ParamUtil.getInteger(
169                     actionRequest, "startDateHour");
170                 int startDateMinute = ParamUtil.getInteger(
171                     actionRequest, "startDateMinute");
172                 int startDateAmPm = ParamUtil.getInteger(
173                     actionRequest, "startDateAmPm");
174 
175                 if (startDateAmPm == Calendar.PM) {
176                     startDateHour += 12;
177                 }
178 
179                 startDate = PortalUtil.getDate(
180                     startDateMonth, startDateDay, startDateYear, startDateHour,
181                     startDateMinute, themeDisplay.getTimeZone(),
182                     new PortalException());
183 
184                 int endDateMonth = ParamUtil.getInteger(
185                     actionRequest, "endDateMonth");
186                 int endDateDay = ParamUtil.getInteger(
187                     actionRequest, "endDateDay");
188                 int endDateYear = ParamUtil.getInteger(
189                     actionRequest, "endDateYear");
190                 int endDateHour = ParamUtil.getInteger(
191                     actionRequest, "endDateHour");
192                 int endDateMinute = ParamUtil.getInteger(
193                     actionRequest, "endDateMinute");
194                 int endDateAmPm = ParamUtil.getInteger(
195                     actionRequest, "endDateAmPm");
196 
197                 if (endDateAmPm == Calendar.PM) {
198                     endDateHour += 12;
199                 }
200 
201                 endDate = PortalUtil.getDate(
202                     endDateMonth, endDateDay, endDateYear, endDateHour,
203                     endDateMinute, themeDisplay.getTimeZone(),
204                     new PortalException());
205             }
206 
207             File file = LayoutServiceUtil.exportPortletInfoAsFile(
208                 plid, groupId, portlet.getPortletId(),
209                 actionRequest.getParameterMap(), startDate, endDate);
210 
211             HttpServletResponse response = PortalUtil.getHttpServletResponse(
212                 actionResponse);
213 
214             ServletResponseUtil.sendFile(
215                 response, fileName, new FileInputStream(file),
216                 ContentTypes.APPLICATION_ZIP);
217 
218             setForward(actionRequest, ActionConstants.COMMON_NULL);
219         }
220         catch (Exception e) {
221             _log.error(e, e);
222         }
223     }
224 
225     protected void importData(ActionRequest actionRequest, Portlet portlet)
226         throws Exception {
227 
228         try {
229             UploadPortletRequest uploadRequest =
230                 PortalUtil.getUploadPortletRequest(actionRequest);
231 
232             long plid = ParamUtil.getLong(uploadRequest, "plid");
233             long groupId = ParamUtil.getLong(uploadRequest, "groupId");
234             File file = uploadRequest.getFile("importFileName");
235 
236             if (!file.exists()) {
237                 throw new LARFileException("Import file does not exist");
238             }
239 
240             LayoutServiceUtil.importPortletInfo(
241                 plid, groupId, portlet.getPortletId(),
242                 actionRequest.getParameterMap(), file);
243 
244             SessionMessages.add(actionRequest, "request_processed");
245         }
246         catch (Exception e) {
247             if ((e instanceof LARFileException) ||
248                 (e instanceof LARTypeException) ||
249                 (e instanceof PortletIdException)) {
250 
251                 SessionErrors.add(actionRequest, e.getClass().getName());
252             }
253             else {
254                 _log.error(e, e);
255 
256                 SessionErrors.add(
257                     actionRequest, LayoutImportException.class.getName());
258             }
259         }
260     }
261 
262     private static Log _log = LogFactoryUtil.getLog(ExportImportAction.class);
263 
264 }