1   /**
2    * Copyright (c) 2000-2008 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.servlet.SessionErrors;
32  import com.liferay.portal.kernel.servlet.SessionMessages;
33  import com.liferay.portal.kernel.upload.UploadPortletRequest;
34  import com.liferay.portal.kernel.util.Constants;
35  import com.liferay.portal.kernel.util.ParamUtil;
36  import com.liferay.portal.model.Portlet;
37  import com.liferay.portal.security.auth.PrincipalException;
38  import com.liferay.portal.service.LayoutServiceUtil;
39  import com.liferay.portal.struts.ActionConstants;
40  import com.liferay.portal.theme.ThemeDisplay;
41  import com.liferay.portal.util.PortalUtil;
42  import com.liferay.portal.util.WebKeys;
43  import com.liferay.portlet.communities.util.StagingUtil;
44  import com.liferay.util.servlet.ServletResponseUtil;
45  
46  import java.io.File;
47  
48  import java.util.Calendar;
49  import java.util.Date;
50  
51  import javax.portlet.ActionRequest;
52  import javax.portlet.ActionResponse;
53  import javax.portlet.PortletConfig;
54  import javax.portlet.RenderRequest;
55  import javax.portlet.RenderResponse;
56  
57  import javax.servlet.http.HttpServletResponse;
58  
59  import org.apache.commons.logging.Log;
60  import org.apache.commons.logging.LogFactory;
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   */
71  public class ExportImportAction extends EditConfigurationAction {
72  
73      public void processAction(
74              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
75              ActionRequest actionRequest, ActionResponse actionResponse)
76          throws Exception {
77  
78          Portlet portlet = null;
79  
80          try {
81              portlet = getPortlet(actionRequest);
82          }
83          catch (PrincipalException pe) {
84              SessionErrors.add(
85                  actionRequest, PrincipalException.class.getName());
86  
87              setForward(actionRequest, "portlet.portlet_configuration.error");
88          }
89  
90          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
91  
92          try {
93              if (cmd.equals("copy_from_live")) {
94                  StagingUtil.copyFromLive(actionRequest, portlet);
95  
96                  sendRedirect(actionRequest, actionResponse);
97              }
98              else if (cmd.equals("export")) {
99                  exportData(actionRequest, actionResponse, portlet);
100             }
101             else if (cmd.equals("import")) {
102                 importData(actionRequest, portlet);
103 
104                 sendRedirect(actionRequest, actionResponse);
105             }
106             else if (cmd.equals("publish_to_live")) {
107                 StagingUtil.publishToLive(actionRequest, portlet);
108 
109                 sendRedirect(actionRequest, actionResponse);
110             }
111         }
112         catch (Exception e) {
113             if (e instanceof NoSuchLayoutException ||
114                 e instanceof PrincipalException) {
115 
116                 SessionErrors.add(actionRequest, e.getClass().getName());
117 
118                 setForward(
119                     actionRequest, "portlet.portlet_configuration.error");
120             }
121             else {
122                 throw e;
123             }
124         }
125     }
126 
127     public ActionForward render(
128             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
129             RenderRequest renderRequest, RenderResponse renderResponse)
130         throws Exception {
131 
132         Portlet portlet = null;
133 
134         try {
135             portlet = getPortlet(renderRequest);
136         }
137         catch (PrincipalException pe) {
138             SessionErrors.add(
139                 renderRequest, PrincipalException.class.getName());
140 
141             return mapping.findForward("portlet.portlet_configuration.error");
142         }
143 
144         renderResponse.setTitle(getTitle(portlet, renderRequest));
145 
146         return mapping.findForward(getForward(
147             renderRequest, "portlet.portlet_configuration.export_import"));
148     }
149 
150     protected void exportData(
151             ActionRequest actionRequest, ActionResponse actionResponse,
152             Portlet portlet)
153         throws Exception {
154 
155         try {
156             ThemeDisplay themeDisplay =
157                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
158 
159             long plid = ParamUtil.getLong(actionRequest, "plid");
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, portlet.getPortletId(), actionRequest.getParameterMap(),
215                 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             File file = uploadRequest.getFile("importFileName");
238 
239             if (!file.exists()) {
240                 throw new LARFileException("Import file does not exist");
241             }
242 
243             LayoutServiceUtil.importPortletInfo(
244                 plid, portlet.getPortletId(), actionRequest.getParameterMap(),
245                 file);
246 
247             SessionMessages.add(actionRequest, "request_processed");
248         }
249         catch (Exception e) {
250             if ((e instanceof LARFileException) ||
251                 (e instanceof LARTypeException) ||
252                 (e instanceof PortletIdException)) {
253 
254                 SessionErrors.add(actionRequest, e.getClass().getName());
255             }
256             else {
257                 _log.error(e, e);
258 
259                 SessionErrors.add(
260                     actionRequest, LayoutImportException.class.getName());
261             }
262         }
263     }
264 
265     private static Log _log = LogFactory.getLog(ExportImportAction.class);
266 
267 }