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.LayoutImportException;
26  import com.liferay.portal.NoSuchLayoutException;
27  import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
28  import com.liferay.portal.kernel.lar.UserIdStrategy;
29  import com.liferay.portal.kernel.util.Constants;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.model.Portlet;
34  import com.liferay.portal.security.auth.PrincipalException;
35  import com.liferay.portal.service.LayoutLocalServiceUtil;
36  import com.liferay.portal.service.LayoutServiceUtil;
37  import com.liferay.portal.struts.ActionConstants;
38  import com.liferay.portal.util.PortalUtil;
39  import com.liferay.portlet.ActionResponseImpl;
40  import com.liferay.util.servlet.ServletResponseUtil;
41  import com.liferay.util.servlet.SessionErrors;
42  import com.liferay.util.servlet.SessionMessages;
43  import com.liferay.util.servlet.UploadPortletRequest;
44  
45  import java.io.ByteArrayInputStream;
46  import java.io.File;
47  
48  import java.util.HashMap;
49  import java.util.Map;
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 config,
75              ActionRequest req, ActionResponse res)
76          throws Exception {
77  
78          Portlet portlet = null;
79  
80          try {
81              portlet = getPortlet(req);
82          }
83          catch (PrincipalException pe) {
84              SessionErrors.add(req, PrincipalException.class.getName());
85  
86              setForward(req, "portlet.portlet_configuration.error");
87          }
88  
89          String cmd = ParamUtil.getString(req, Constants.CMD);
90  
91          try {
92              if (cmd.equals("copy_from_live")) {
93                  copyFromLive(req, portlet);
94  
95                  sendRedirect(req, res);
96              }
97              else if (cmd.equals("export")) {
98                  exportData(req, res, portlet);
99              }
100             else if (cmd.equals("import")) {
101                 importData(req, portlet);
102 
103                 sendRedirect(req, res);
104             }
105             else if (cmd.equals("publish_to_live")) {
106                 publishToLive(req, portlet);
107 
108                 sendRedirect(req, res);
109             }
110         }
111         catch (Exception e) {
112             if (e instanceof NoSuchLayoutException ||
113                 e instanceof PrincipalException) {
114 
115                 SessionErrors.add(req, e.getClass().getName());
116 
117                 setForward(req, "portlet.portlet_configuration.error");
118             }
119             else {
120                 throw e;
121             }
122         }
123     }
124 
125     public ActionForward render(
126             ActionMapping mapping, ActionForm form, PortletConfig config,
127             RenderRequest req, RenderResponse res)
128         throws Exception {
129 
130         Portlet portlet = null;
131 
132         try {
133             portlet = getPortlet(req);
134         }
135         catch (PrincipalException pe) {
136             SessionErrors.add(req, PrincipalException.class.getName());
137 
138             return mapping.findForward("portlet.portlet_configuration.error");
139         }
140 
141         res.setTitle(getTitle(portlet, req));
142 
143         return mapping.findForward(getForward(
144             req, "portlet.portlet_configuration.export_import"));
145     }
146 
147     protected void copyFromLive(ActionRequest req, Portlet portlet)
148         throws Exception {
149 
150         long plid = ParamUtil.getLong(req, "plid");
151 
152         Layout targetLayout = LayoutLocalServiceUtil.getLayout(plid);
153 
154         Group stagingGroup = targetLayout.getGroup();
155         Group liveGroup = stagingGroup.getLiveGroup();
156 
157         Layout sourceLayout = LayoutLocalServiceUtil.getLayout(
158             liveGroup.getGroupId(), targetLayout.isPrivateLayout(),
159             targetLayout.getLayoutId());
160 
161         copyPortletInfo(
162             sourceLayout.getPlid(), targetLayout.getPlid(),
163             portlet.getPortletId(), req.getParameterMap());
164     }
165 
166     protected void copyPortletInfo(
167             long sourcePlid, long targetPlid, String portletId, Map parameters)
168         throws Exception {
169 
170         Map parameterMap = getStagingParameters(parameters);
171 
172         byte[] data = LayoutLocalServiceUtil.exportPortletInfo(
173             sourcePlid, portletId, parameterMap);
174 
175         ByteArrayInputStream bais = new ByteArrayInputStream(data);
176 
177         LayoutServiceUtil.importPortletInfo(
178             targetPlid, portletId, parameterMap, bais);
179     }
180 
181     private void exportData(
182             ActionRequest req, ActionResponse res, Portlet portlet)
183         throws Exception {
184 
185         try {
186             long plid = ParamUtil.getLong(req, "plid");
187             String fileName = ParamUtil.getString(req, "exportFileName");
188 
189             byte[] byteArray = LayoutServiceUtil.exportPortletInfo(
190                 plid, portlet.getPortletId(), req.getParameterMap());
191 
192             HttpServletResponse httpRes =
193                 ((ActionResponseImpl)res).getHttpServletResponse();
194 
195             ServletResponseUtil.sendFile(httpRes, fileName, byteArray);
196 
197             setForward(req, ActionConstants.COMMON_NULL);
198         }
199         catch (Exception e) {
200             _log.error(e, e);
201         }
202     }
203 
204     protected static Map getStagingParameters(Map parameters) {
205         Map parameterMap = new HashMap();
206 
207         parameterMap.putAll(parameters);
208 
209         parameterMap.put(
210             PortletDataHandlerKeys.PORTLET_DATA_ALL, Boolean.TRUE.toString());
211         parameterMap.put(
212             PortletDataHandlerKeys.THEME, Boolean.FALSE.toString());
213         parameterMap.put(
214             PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
215             Boolean.TRUE.toString());
216         parameterMap.put(
217             PortletDataHandlerKeys.DATA_STRATEGY,
218             PortletDataHandlerKeys.DATA_STRATEGY_MIRROR);
219         parameterMap.put(
220             PortletDataHandlerKeys.USER_ID_STRATEGY,
221             UserIdStrategy.CURRENT_USER_ID);
222 
223         return parameterMap;
224     }
225 
226     private void importData(ActionRequest req, Portlet portlet)
227         throws Exception {
228 
229         try {
230             UploadPortletRequest uploadReq =
231                 PortalUtil.getUploadPortletRequest(req);
232 
233             long plid = ParamUtil.getLong(uploadReq, "plid");
234             File file = uploadReq.getFile("importFileName");
235 
236             LayoutServiceUtil.importPortletInfo(
237                 plid, portlet.getPortletId(), req.getParameterMap(), file);
238 
239             SessionMessages.add(req, "request_processed");
240         }
241         catch (Exception e) {
242             _log.error(e, e);
243 
244             SessionErrors.add(req, LayoutImportException.class.getName());
245         }
246     }
247 
248     protected void publishToLive(ActionRequest req, Portlet portlet)
249         throws Exception {
250 
251         long plid = ParamUtil.getLong(req, "plid");
252 
253         Layout sourceLayout = LayoutLocalServiceUtil.getLayout(plid);
254 
255         Group stagingGroup = sourceLayout.getGroup();
256         Group liveGroup = stagingGroup.getLiveGroup();
257 
258         Layout targetLayout = LayoutLocalServiceUtil.getLayout(
259             liveGroup.getGroupId(), sourceLayout.isPrivateLayout(),
260             sourceLayout.getLayoutId());
261 
262         copyPortletInfo(
263             sourceLayout.getPlid(), targetLayout.getPlid(),
264             portlet.getPortletId(), req.getParameterMap());
265     }
266 
267     private static Log _log = LogFactory.getLog(ExportImportAction.class);
268 
269 }