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.ContentTypes;
38  import com.liferay.portal.kernel.util.ParamUtil;
39  import com.liferay.portal.model.Portlet;
40  import com.liferay.portal.security.auth.PrincipalException;
41  import com.liferay.portal.service.LayoutServiceUtil;
42  import com.liferay.portal.struts.ActionConstants;
43  import com.liferay.portal.theme.ThemeDisplay;
44  import com.liferay.portal.util.PortalUtil;
45  import com.liferay.portal.util.WebKeys;
46  import com.liferay.portlet.communities.util.StagingUtil;
47  import com.liferay.util.servlet.ServletResponseUtil;
48  
49  import java.io.File;
50  import java.io.FileInputStream;
51  
52  import java.util.Calendar;
53  import java.util.Date;
54  
55  import javax.portlet.ActionRequest;
56  import javax.portlet.ActionResponse;
57  import javax.portlet.PortletConfig;
58  import javax.portlet.RenderRequest;
59  import javax.portlet.RenderResponse;
60  
61  import javax.servlet.http.HttpServletResponse;
62  
63  import org.apache.struts.action.ActionForm;
64  import org.apache.struts.action.ActionForward;
65  import org.apache.struts.action.ActionMapping;
66  
67  /**
68   * <a href="ExportImportAction.java.html"><b><i>View Source</i></b></a>
69   *
70   * @author Jorge Ferrer
71   */
72  public class ExportImportAction extends EditConfigurationAction {
73  
74      public void processAction(
75              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
76              ActionRequest actionRequest, ActionResponse actionResponse)
77          throws Exception {
78  
79          Portlet portlet = null;
80  
81          try {
82              portlet = getPortlet(actionRequest);
83          }
84          catch (PrincipalException pe) {
85              SessionErrors.add(
86                  actionRequest, PrincipalException.class.getName());
87  
88              setForward(actionRequest, "portlet.portlet_configuration.error");
89          }
90  
91          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
92  
93          try {
94              if (cmd.equals("copy_from_live")) {
95                  StagingUtil.copyFromLive(actionRequest, portlet);
96  
97                  sendRedirect(actionRequest, actionResponse);
98              }
99              else if (cmd.equals("export")) {
100                 exportData(actionRequest, actionResponse, portlet);
101             }
102             else if (cmd.equals("import")) {
103                 importData(actionRequest, portlet);
104 
105                 sendRedirect(actionRequest, actionResponse);
106             }
107             else if (cmd.equals("publish_to_live")) {
108                 StagingUtil.publishToLive(actionRequest, portlet);
109 
110                 sendRedirect(actionRequest, actionResponse);
111             }
112         }
113         catch (Exception e) {
114             if (e instanceof NoSuchLayoutException ||
115                 e instanceof PrincipalException) {
116 
117                 SessionErrors.add(actionRequest, e.getClass().getName());
118 
119                 setForward(
120                     actionRequest, "portlet.portlet_configuration.error");
121             }
122             else {
123                 throw e;
124             }
125         }
126     }
127 
128     public ActionForward render(
129             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
130             RenderRequest renderRequest, RenderResponse renderResponse)
131         throws Exception {
132 
133         Portlet portlet = null;
134 
135         try {
136             portlet = getPortlet(renderRequest);
137         }
138         catch (PrincipalException pe) {
139             SessionErrors.add(
140                 renderRequest, PrincipalException.class.getName());
141 
142             return mapping.findForward("portlet.portlet_configuration.error");
143         }
144 
145         renderResponse.setTitle(getTitle(portlet, renderRequest));
146 
147         return mapping.findForward(getForward(
148             renderRequest, "portlet.portlet_configuration.export_import"));
149     }
150 
151     protected void exportData(
152             ActionRequest actionRequest, ActionResponse actionResponse,
153             Portlet portlet)
154         throws Exception {
155 
156         try {
157             ThemeDisplay themeDisplay =
158                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
159 
160             long plid = ParamUtil.getLong(actionRequest, "plid");
161             long groupId = ParamUtil.getLong(actionRequest, "groupId");
162             String fileName = ParamUtil.getString(
163                 actionRequest, "exportFileName");
164             boolean dateRange = ParamUtil.getBoolean(
165                 actionRequest, "dateRange");
166             Date startDate = null;
167             Date endDate = null;
168 
169             if (dateRange) {
170                 int startDateMonth = ParamUtil.getInteger(
171                     actionRequest, "startDateMonth");
172                 int startDateDay = ParamUtil.getInteger(
173                     actionRequest, "startDateDay");
174                 int startDateYear = ParamUtil.getInteger(
175                     actionRequest, "startDateYear");
176                 int startDateHour = ParamUtil.getInteger(
177                     actionRequest, "startDateHour");
178                 int startDateMinute = ParamUtil.getInteger(
179                     actionRequest, "startDateMinute");
180                 int startDateAmPm = ParamUtil.getInteger(
181                     actionRequest, "startDateAmPm");
182 
183                 if (startDateAmPm == Calendar.PM) {
184                     startDateHour += 12;
185                 }
186 
187                 startDate = PortalUtil.getDate(
188                     startDateMonth, startDateDay, startDateYear, startDateHour,
189                     startDateMinute, themeDisplay.getTimeZone(),
190                     new PortalException());
191 
192                 int endDateMonth = ParamUtil.getInteger(
193                     actionRequest, "endDateMonth");
194                 int endDateDay = ParamUtil.getInteger(
195                     actionRequest, "endDateDay");
196                 int endDateYear = ParamUtil.getInteger(
197                     actionRequest, "endDateYear");
198                 int endDateHour = ParamUtil.getInteger(
199                     actionRequest, "endDateHour");
200                 int endDateMinute = ParamUtil.getInteger(
201                     actionRequest, "endDateMinute");
202                 int endDateAmPm = ParamUtil.getInteger(
203                     actionRequest, "endDateAmPm");
204 
205                 if (endDateAmPm == Calendar.PM) {
206                     endDateHour += 12;
207                 }
208 
209                 endDate = PortalUtil.getDate(
210                     endDateMonth, endDateDay, endDateYear, endDateHour,
211                     endDateMinute, themeDisplay.getTimeZone(),
212                     new PortalException());
213             }
214 
215             File file = LayoutServiceUtil.exportPortletInfoAsFile(
216                 plid, groupId, portlet.getPortletId(),
217                 actionRequest.getParameterMap(), startDate, endDate);
218 
219             HttpServletResponse response = PortalUtil.getHttpServletResponse(
220                 actionResponse);
221 
222             ServletResponseUtil.sendFile(
223                 response, fileName, new FileInputStream(file),
224                 ContentTypes.APPLICATION_ZIP);
225 
226             setForward(actionRequest, ActionConstants.COMMON_NULL);
227         }
228         catch (Exception e) {
229             _log.error(e, e);
230         }
231     }
232 
233     protected void importData(ActionRequest actionRequest, Portlet portlet)
234         throws Exception {
235 
236         try {
237             UploadPortletRequest uploadRequest =
238                 PortalUtil.getUploadPortletRequest(actionRequest);
239 
240             long plid = ParamUtil.getLong(uploadRequest, "plid");
241             long groupId = ParamUtil.getLong(uploadRequest, "groupId");
242             File file = uploadRequest.getFile("importFileName");
243 
244             if (!file.exists()) {
245                 throw new LARFileException("Import file does not exist");
246             }
247 
248             LayoutServiceUtil.importPortletInfo(
249                 plid, groupId, portlet.getPortletId(),
250                 actionRequest.getParameterMap(), file);
251 
252             SessionMessages.add(actionRequest, "request_processed");
253         }
254         catch (Exception e) {
255             if ((e instanceof LARFileException) ||
256                 (e instanceof LARTypeException) ||
257                 (e instanceof PortletIdException)) {
258 
259                 SessionErrors.add(actionRequest, e.getClass().getName());
260             }
261             else {
262                 _log.error(e, e);
263 
264                 SessionErrors.add(
265                     actionRequest, LayoutImportException.class.getName());
266             }
267         }
268     }
269 
270     private static Log _log = LogFactoryUtil.getLog(ExportImportAction.class);
271 
272 }