1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }