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.wiki.action;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.upload.UploadPortletRequest;
20  import com.liferay.portal.kernel.util.NotificationThreadLocal;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.ProgressTracker;
23  import com.liferay.portal.kernel.util.ProgressTrackerThreadLocal;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portlet.wiki.NoSuchNodeException;
28  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
29  import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
30  import com.liferay.portlet.wiki.util.WikiCacheUtil;
31  
32  import java.io.File;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="ImportPagesAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   */
49  public class ImportPagesAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          try {
57              importPages(actionRequest, actionResponse);
58  
59              sendRedirect(actionRequest, actionResponse);
60          }
61          catch (Exception e) {
62              if (e instanceof NoSuchNodeException ||
63                  e instanceof PrincipalException) {
64  
65                  SessionErrors.add(actionRequest, e.getClass().getName());
66  
67                  setForward(actionRequest, "portlet.wiki.error");
68              }
69              else if (e instanceof PortalException) {
70                  SessionErrors.add(actionRequest, e.getClass().getName());
71              }
72              else {
73                  throw e;
74              }
75          }
76      }
77  
78      public ActionForward render(
79              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
80              RenderRequest renderRequest, RenderResponse renderResponse)
81          throws Exception {
82  
83          try {
84              ActionUtil.getNode(renderRequest);
85          }
86          catch (Exception e) {
87              if (e instanceof NoSuchNodeException ||
88                  e instanceof PrincipalException) {
89  
90                  SessionErrors.add(renderRequest, e.getClass().getName());
91  
92                  return mapping.findForward("portlet.wiki.error");
93              }
94              else {
95                  throw e;
96              }
97          }
98  
99          return mapping.findForward(
100             getForward(renderRequest, "portlet.wiki.import_pages"));
101     }
102 
103     protected void importPages(
104             ActionRequest actionRequest, ActionResponse actionResponse)
105         throws Exception {
106 
107         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
108             actionRequest);
109 
110         String importProgressId = ParamUtil.getString(
111             uploadRequest, "importProgressId");
112 
113         ProgressTracker progressTracker = new ProgressTracker(
114             actionRequest, importProgressId);
115 
116         ProgressTrackerThreadLocal.setProgressTracker(progressTracker);
117 
118         progressTracker.start();
119 
120         long nodeId = ParamUtil.getLong(uploadRequest, "nodeId");
121         String importer = ParamUtil.getString(uploadRequest, "importer");
122 
123         int filesCount = ParamUtil.getInteger(uploadRequest, "filesCount", 10);
124 
125         File[] files = new File[filesCount];
126 
127         for (int i = 0; i < filesCount; i++) {
128             files[i] = uploadRequest.getFile("file" + i);
129         }
130 
131         NotificationThreadLocal.setEnabled(false);
132         WikiCacheThreadLocal.setClearCache(false);
133 
134         WikiNodeServiceUtil.importPages(
135             nodeId, importer, files, actionRequest.getParameterMap());
136 
137         WikiCacheUtil.clearCache(nodeId);
138 
139         progressTracker.finish();
140     }
141 
142 }