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.servlet.SessionErrors;
18  import com.liferay.portal.kernel.upload.UploadPortletRequest;
19  import com.liferay.portal.kernel.util.NotificationThreadLocal;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.ProgressTracker;
22  import com.liferay.portal.kernel.util.ProgressTrackerThreadLocal;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portlet.wiki.ImportFilesException;
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 ImportFilesException ||
70                       e instanceof NoSuchNodeException) {
71  
72                  SessionErrors.add(actionRequest, e.getClass().getName());
73              }
74              else {
75                  throw e;
76              }
77          }
78      }
79  
80      public ActionForward render(
81              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
82              RenderRequest renderRequest, RenderResponse renderResponse)
83          throws Exception {
84  
85          try {
86              ActionUtil.getNode(renderRequest);
87          }
88          catch (Exception e) {
89              if (e instanceof NoSuchNodeException ||
90                  e instanceof PrincipalException) {
91  
92                  SessionErrors.add(renderRequest, e.getClass().getName());
93  
94                  return mapping.findForward("portlet.wiki.error");
95              }
96              else {
97                  throw e;
98              }
99          }
100 
101         return mapping.findForward(
102             getForward(renderRequest, "portlet.wiki.import_pages"));
103     }
104 
105     protected void importPages(
106             ActionRequest actionRequest, ActionResponse actionResponse)
107         throws Exception {
108 
109         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
110             actionRequest);
111 
112         String importProgressId = ParamUtil.getString(
113             uploadRequest, "importProgressId");
114 
115         ProgressTracker progressTracker = new ProgressTracker(
116             actionRequest, importProgressId);
117 
118         ProgressTrackerThreadLocal.setProgressTracker(progressTracker);
119 
120         progressTracker.start();
121 
122         long nodeId = ParamUtil.getLong(uploadRequest, "nodeId");
123         String importer = ParamUtil.getString(uploadRequest, "importer");
124 
125         int filesCount = ParamUtil.getInteger(uploadRequest, "filesCount", 10);
126 
127         File[] files = new File[filesCount];
128 
129         for (int i = 0; i < filesCount; i++) {
130             files[i] = uploadRequest.getFile("file" + i);
131         }
132 
133         NotificationThreadLocal.setEnabled(false);
134         WikiCacheThreadLocal.setClearCache(false);
135 
136         WikiNodeServiceUtil.importPages(
137             nodeId, importer, files, actionRequest.getParameterMap());
138 
139         WikiCacheUtil.clearCache(nodeId);
140 
141         progressTracker.finish();
142     }
143 
144 }