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.communities.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.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.servlet.SessionErrors;
31  import com.liferay.portal.kernel.servlet.SessionMessages;
32  import com.liferay.portal.kernel.upload.UploadPortletRequest;
33  import com.liferay.portal.kernel.util.ParamUtil;
34  import com.liferay.portal.service.LayoutServiceUtil;
35  import com.liferay.portal.util.PortalUtil;
36  
37  import java.io.File;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionMapping;
45  
46  /**
47   * <a href="ImportPagesAction.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Alexander Chow
50   * @author Raymond Aug�
51   *
52   */
53  public class ImportPagesAction extends EditPagesAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          try {
61              UploadPortletRequest uploadRequest =
62                  PortalUtil.getUploadPortletRequest(actionRequest);
63  
64              long groupId = ParamUtil.getLong(uploadRequest, "groupId");
65              boolean privateLayout = ParamUtil.getBoolean(
66                  uploadRequest, "privateLayout");
67              File file = uploadRequest.getFile("importFileName");
68  
69              if (!file.exists()) {
70                  throw new LARFileException("Import file does not exist");
71              }
72  
73              LayoutServiceUtil.importLayouts(
74                  groupId, privateLayout, actionRequest.getParameterMap(), file);
75  
76              SessionMessages.add(actionRequest, "request_processed");
77          }
78          catch (Exception e) {
79              if ((e instanceof LARFileException) ||
80                  (e instanceof LARTypeException)) {
81  
82                  SessionErrors.add(actionRequest, e.getClass().getName());
83              }
84              else {
85                  _log.error(e, e);
86  
87                  SessionErrors.add(
88                      actionRequest, LayoutImportException.class.getName());
89              }
90          }
91      }
92  
93      private static Log _log = LogFactoryUtil.getLog(ImportPagesAction.class);
94  
95  }