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