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   */
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             long groupId = ParamUtil.getLong(actionRequest, "groupId");
161             String fileName = ParamUtil.getString(
162                 actionRequest, "exportFileName");
163             boolean dateRange = ParamUtil.getBoolean(
164                 actionRequest, "dateRange");
165             Date startDate = null;
166             Date endDate = null;
167 
168             if (dateRange) {
169                 int startDateMonth = ParamUtil.getInteger(
170                     actionRequest, "startDateMonth");
171                 int startDateDay = ParamUtil.getInteger(
172                     actionRequest, "startDateDay");
173                 int startDateYear = ParamUtil.getInteger(
174                     actionRequest, "startDateYear");
175                 int startDateHour = ParamUtil.getInteger(
176                     actionRequest, "startDateHour");
177                 int startDateMinute = ParamUtil.getInteger(
178                     actionRequest, "startDateMinute");
179                 int startDateAmPm = ParamUtil.getInteger(
180                     actionRequest, "startDateAmPm");
181 
182                 if (startDateAmPm == Calendar.PM) {
183                     startDateHour += 12;
184                 }
185 
186                 startDate = PortalUtil.getDate(
187                     startDateMonth, startDateDay, startDateYear, startDateHour,
188                     startDateMinute, themeDisplay.getTimeZone(),
189                     new PortalException());
190 
191                 int endDateMonth = ParamUtil.getInteger(
192                     actionRequest, "endDateMonth");
193                 int endDateDay = ParamUtil.getInteger(
194                     actionRequest, "endDateDay");
195                 int endDateYear = ParamUtil.getInteger(
196                     actionRequest, "endDateYear");
197                 int endDateHour = ParamUtil.getInteger(
198                     actionRequest, "endDateHour");
199                 int endDateMinute = ParamUtil.getInteger(
200                     actionRequest, "endDateMinute");
201                 int endDateAmPm = ParamUtil.getInteger(
202                     actionRequest, "endDateAmPm");
203 
204                 if (endDateAmPm == Calendar.PM) {
205                     endDateHour += 12;
206                 }
207 
208                 endDate = PortalUtil.getDate(
209                     endDateMonth, endDateDay, endDateYear, endDateHour,
210                     endDateMinute, themeDisplay.getTimeZone(),
211                     new PortalException());
212             }
213 
214             byte[] bytes = LayoutServiceUtil.exportPortletInfo(
215                 plid, groupId, portlet.getPortletId(),
216                 actionRequest.getParameterMap(), startDate, endDate);
217 
218             HttpServletResponse response = PortalUtil.getHttpServletResponse(
219                 actionResponse);
220 
221             ServletResponseUtil.sendFile(response, fileName, bytes);
222 
223             setForward(actionRequest, ActionConstants.COMMON_NULL);
224         }
225         catch (Exception e) {
226             _log.error(e, e);
227         }
228     }
229 
230     protected void importData(ActionRequest actionRequest, Portlet portlet)
231         throws Exception {
232 
233         try {
234             UploadPortletRequest uploadRequest =
235                 PortalUtil.getUploadPortletRequest(actionRequest);
236 
237             long plid = ParamUtil.getLong(uploadRequest, "plid");
238             long groupId = ParamUtil.getLong(uploadRequest, "groupId");
239             File file = uploadRequest.getFile("importFileName");
240 
241             if (!file.exists()) {
242                 throw new LARFileException("Import file does not exist");
243             }
244 
245             LayoutServiceUtil.importPortletInfo(
246                 plid, groupId, portlet.getPortletId(),
247                 actionRequest.getParameterMap(), file);
248 
249             SessionMessages.add(actionRequest, "request_processed");
250         }
251         catch (Exception e) {
252             if ((e instanceof LARFileException) ||
253                 (e instanceof LARTypeException) ||
254                 (e instanceof PortletIdException)) {
255 
256                 SessionErrors.add(actionRequest, e.getClass().getName());
257             }
258             else {
259                 _log.error(e, e);
260 
261                 SessionErrors.add(
262                     actionRequest, LayoutImportException.class.getName());
263             }
264         }
265     }
266 
267     private static Log _log = LogFactoryUtil.getLog(ExportImportAction.class);
268 
269 }