1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.communities.action;
16  
17  import com.liferay.portal.LARFileException;
18  import com.liferay.portal.LARTypeException;
19  import com.liferay.portal.LayoutImportException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.upload.UploadPortletRequest;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.service.LayoutServiceUtil;
26  import com.liferay.portal.util.PortalUtil;
27  
28  import java.io.File;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="ImportPagesAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Alexander Chow
41   * @author Raymond Augé
42   */
43  public class ImportPagesAction extends EditPagesAction {
44  
45      public void processAction(
46              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
47              ActionRequest actionRequest, ActionResponse actionResponse)
48          throws Exception {
49  
50          try {
51              UploadPortletRequest uploadRequest =
52                  PortalUtil.getUploadPortletRequest(actionRequest);
53  
54              long groupId = ParamUtil.getLong(uploadRequest, "groupId");
55              boolean privateLayout = ParamUtil.getBoolean(
56                  uploadRequest, "privateLayout");
57              File file = uploadRequest.getFile("importFileName");
58  
59              if (!file.exists()) {
60                  throw new LARFileException("Import file does not exist");
61              }
62  
63              LayoutServiceUtil.importLayouts(
64                  groupId, privateLayout, actionRequest.getParameterMap(), file);
65  
66              addSuccessMessage(actionRequest, actionResponse);
67          }
68          catch (Exception e) {
69              if ((e instanceof LARFileException) ||
70                  (e instanceof LARTypeException)) {
71  
72                  SessionErrors.add(actionRequest, e.getClass().getName());
73              }
74              else {
75                  _log.error(e, e);
76  
77                  SessionErrors.add(
78                      actionRequest, LayoutImportException.class.getName());
79              }
80          }
81      }
82  
83      private static Log _log = LogFactoryUtil.getLog(ImportPagesAction.class);
84  
85  }