1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.portletconfiguration.action;
24  
25  import com.liferay.portal.LARFileException;
26  import com.liferay.portal.LARTypeException;
27  import com.liferay.portal.LayoutImportException;
28  import com.liferay.portal.NoSuchLayoutException;
29  import com.liferay.portal.PortalException;
30  import com.liferay.portal.PortletIdException;
31  import com.liferay.portal.kernel.log.Log;
32  import com.liferay.portal.kernel.log.LogFactoryUtil;
33  import com.liferay.portal.kernel.servlet.SessionErrors;
34  import com.liferay.portal.kernel.servlet.SessionMessages;
35  import com.liferay.portal.kernel.upload.UploadPortletRequest;
36  import com.liferay.portal.kernel.util.Constants;
37  import com.liferay.portal.kernel.util.ParamUtil;
38  import com.liferay.portal.model.Portlet;
39  import com.liferay.portal.security.auth.PrincipalException;
40  import com.liferay.portal.service.LayoutServiceUtil;
41  import com.liferay.portal.struts.ActionConstants;
42  import com.liferay.portal.theme.ThemeDisplay;
43  import com.liferay.portal.util.PortalUtil;
44  import com.liferay.portal.util.WebKeys;
45  import com.liferay.portlet.communities.util.StagingUtil;
46  import com.liferay.util.servlet.ServletResponseUtil;
47  
48  import java.io.File;
49  
50  import java.util.Calendar;
51  import java.util.Date;
52  
53  import javax.portlet.ActionRequest;
54  import javax.portlet.ActionResponse;
55  import javax.portlet.PortletConfig;
56  import javax.portlet.RenderRequest;
57  import javax.portlet.RenderResponse;
58  
59  import javax.servlet.http.HttpServletResponse;
60  
61  import org.apache.struts.action.ActionForm;
62  import org.apache.struts.action.ActionForward;
63  import org.apache.struts.action.ActionMapping;
64  
65  /**
66   * <a href="ExportImportAction.java.html"><b><i>View Source</i></b></a>
67   *
68   * @author Jorge Ferrer
69   */
70  public class ExportImportAction extends EditConfigurationAction {
71  
72      public void processAction(
73              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
74              ActionRequest actionRequest, ActionResponse actionResponse)
75          throws Exception {
76  
77          Portlet portlet = null;
78  
79          try {
80              portlet = getPortlet(actionRequest);
81          }
82          catch (PrincipalException pe) {
83              SessionErrors.add(
84                  actionRequest, PrincipalException.class.getName());
85  
86              setForward(actionRequest, "portlet.portlet_configuration.error");
87          }
88  
89          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
90  
91          try {
92              if (cmd.equals("copy_from_live")) {
93                  StagingUtil.copyFromLive(actionRequest, portlet);
94  
95                  sendRedirect(actionRequest, actionResponse);
96              }
97              else if (cmd.equals("export")) {
98                  exportData(actionRequest, actionResponse, portlet);
99              }
100             else if (cmd.equals("import")) {
101                 importData(actionRequest, portlet);
102 
103                 sendRedirect(actionRequest, actionResponse);
104             }
105             else if (cmd.equals("publish_to_live")) {
106                 StagingUtil.publishToLive(actionRequest, portlet);
107 
108                 sendRedirect(actionRequest, actionResponse);
109             }
110         }
111         catch (Exception e) {
112             if (e instanceof NoSuchLayoutException ||
113                 e instanceof PrincipalException) {
114 
115                 SessionErrors.add(actionRequest, e.getClass().getName());
116 
117                 setForward(
118                     actionRequest, "portlet.portlet_configuration.error");
119             }
120             else {
121                 throw e;
122             }
123         }
124     }
125 
126     public ActionForward render(
127             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
128             RenderRequest renderRequest, RenderResponse renderResponse)
129         throws Exception {
130 
131         Portlet portlet = null;
132 
133         try {
134             portlet = getPortlet(renderRequest);
135         }
136         catch (PrincipalException pe) {
137             SessionErrors.add(
138                 renderRequest, PrincipalException.class.getName());
139 
140             return mapping.findForward("portlet.portlet_configuration.error");
141         }
142 
143         renderResponse.setTitle(getTitle(portlet, renderRequest));
144 
145         return mapping.findForward(getForward(
146             renderRequest, "portlet.portlet_configuration.export_import"));
147     }
148 
149     protected void exportData(
150             ActionRequest actionRequest, ActionResponse actionResponse,
151             Portlet portlet)
152         throws Exception {
153 
154         try {
155             ThemeDisplay themeDisplay =
156                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
157 
158             long plid = ParamUtil.getLong(actionRequest, "plid");
159             long groupId = ParamUtil.getLong(actionRequest, "groupId");
160             String fileName = ParamUtil.getString(
161                 actionRequest, "exportFileName");
162             boolean dateRange = ParamUtil.getBoolean(
163                 actionRequest, "dateRange");
164             Date startDate = null;
165             Date endDate = null;
166 
167             if (dateRange) {
168                 int startDateMonth = ParamUtil.getInteger(
169                     actionRequest, "startDateMonth");
170                 int startDateDay = ParamUtil.getInteger(
171                     actionRequest, "startDateDay");
172                 int startDateYear = ParamUtil.getInteger(
173                     actionRequest, "startDateYear");
174                 int startDateHour = ParamUtil.getInteger(
175                     actionRequest, "startDateHour");
176                 int startDateMinute = ParamUtil.getInteger(
177                     actionRequest, "startDateMinute");
178                 int startDateAmPm = ParamUtil.getInteger(
179                     actionRequest, "startDateAmPm");
180 
181                 if (startDateAmPm == Calendar.PM) {
182                     startDateHour += 12;
183                 }
184 
185                 startDate = PortalUtil.getDate(
186                     startDateMonth, startDateDay, startDateYear, startDateHour,
187                     startDateMinute, themeDisplay.getTimeZone(),
188                     new PortalException());
189 
190                 int endDateMonth = ParamUtil.getInteger(
191                     actionRequest, "endDateMonth");
192                 int endDateDay = ParamUtil.getInteger(
193                     actionRequest, "endDateDay");
194                 int endDateYear = ParamUtil.getInteger(
195                     actionRequest, "endDateYear");
196                 int endDateHour = ParamUtil.getInteger(
197                     actionRequest, "endDateHour");
198                 int endDateMinute = ParamUtil.getInteger(
199                     actionRequest, "endDateMinute");
200                 int endDateAmPm = ParamUtil.getInteger(
201                     actionRequest, "endDateAmPm");
202 
203                 if (endDateAmPm == Calendar.PM) {
204                     endDateHour += 12;
205                 }
206 
207                 endDate = PortalUtil.getDate(
208                     endDateMonth, endDateDay, endDateYear, endDateHour,
209                     endDateMinute, themeDisplay.getTimeZone(),
210                     new PortalException());
211             }
212 
213             byte[] bytes = LayoutServiceUtil.exportPortletInfo(
214                 plid, groupId, portlet.getPortletId(),
215                 actionRequest.getParameterMap(), startDate, endDate);
216 
217             HttpServletResponse response = PortalUtil.getHttpServletResponse(
218                 actionResponse);
219 
220             ServletResponseUtil.sendFile(response, fileName, bytes);
221 
222             setForward(actionRequest, ActionConstants.COMMON_NULL);
223         }
224         catch (Exception e) {
225             _log.error(e, e);
226         }
227     }
228 
229     protected void importData(ActionRequest actionRequest, Portlet portlet)
230         throws Exception {
231 
232         try {
233             UploadPortletRequest uploadRequest =
234                 PortalUtil.getUploadPortletRequest(actionRequest);
235 
236             long plid = ParamUtil.getLong(uploadRequest, "plid");
237             long groupId = ParamUtil.getLong(uploadRequest, "groupId");
238             File file = uploadRequest.getFile("importFileName");
239 
240             if (!file.exists()) {
241                 throw new LARFileException("Import file does not exist");
242             }
243 
244             LayoutServiceUtil.importPortletInfo(
245                 plid, groupId, portlet.getPortletId(),
246                 actionRequest.getParameterMap(), file);
247 
248             SessionMessages.add(actionRequest, "request_processed");
249         }
250         catch (Exception e) {
251             if ((e instanceof LARFileException) ||
252                 (e instanceof LARTypeException) ||
253                 (e instanceof PortletIdException)) {
254 
255                 SessionErrors.add(actionRequest, e.getClass().getName());
256             }
257             else {
258                 _log.error(e, e);
259 
260                 SessionErrors.add(
261                     actionRequest, LayoutImportException.class.getName());
262             }
263         }
264     }
265 
266     private static Log _log = LogFactoryUtil.getLog(ExportImportAction.class);
267 
268 }