1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.upload.UploadPortletRequest;
24  import com.liferay.portal.kernel.util.NotificationThreadLocal;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.ProgressTracker;
27  import com.liferay.portal.kernel.util.ProgressTrackerThreadLocal;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.wiki.ImportFilesException;
32  import com.liferay.portlet.wiki.NoSuchNodeException;
33  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
34  import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
35  import com.liferay.portlet.wiki.util.WikiCacheUtil;
36  
37  import java.io.File;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.RenderRequest;
43  import javax.portlet.RenderResponse;
44  
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionForward;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="ImportPagesAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Jorge Ferrer
53   *
54   */
55  public class ImportPagesAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          try {
63              importPages(actionRequest, actionResponse);
64  
65              sendRedirect(actionRequest, actionResponse);
66          }
67          catch (Exception e) {
68              if (e instanceof NoSuchNodeException ||
69                  e instanceof PrincipalException) {
70  
71                  SessionErrors.add(actionRequest, e.getClass().getName());
72  
73                  setForward(actionRequest, "portlet.wiki.error");
74              }
75              else if (e instanceof ImportFilesException ||
76                       e instanceof NoSuchNodeException) {
77  
78                  SessionErrors.add(actionRequest, e.getClass().getName());
79              }
80              else {
81                  throw e;
82              }
83          }
84      }
85  
86      public ActionForward render(
87              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
88              RenderRequest renderRequest, RenderResponse renderResponse)
89          throws Exception {
90  
91          try {
92              ActionUtil.getNode(renderRequest);
93          }
94          catch (Exception e) {
95              if (e instanceof NoSuchNodeException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(renderRequest, e.getClass().getName());
99  
100                 return mapping.findForward("portlet.wiki.error");
101             }
102             else {
103                 throw e;
104             }
105         }
106 
107         return mapping.findForward(
108             getForward(renderRequest, "portlet.wiki.import_pages"));
109     }
110 
111     protected void importPages(
112             ActionRequest actionRequest, ActionResponse actionResponse)
113         throws Exception {
114 
115         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
116             actionRequest);
117 
118         String importProgressId = ParamUtil.getString(
119             uploadRequest, "importProgressId");
120 
121         ProgressTracker progressTracker = new ProgressTracker(
122             actionRequest, importProgressId);
123 
124         ProgressTrackerThreadLocal.setProgressTracker(progressTracker);
125 
126         progressTracker.start();
127 
128         long nodeId = ParamUtil.getLong(uploadRequest, "nodeId");
129         String importer = ParamUtil.getString(uploadRequest, "importer");
130 
131         int filesCount = ParamUtil.getInteger(uploadRequest, "filesCount", 10);
132 
133         File[] files = new File[filesCount];
134 
135         for (int i = 0; i < filesCount; i++) {
136             files[i] = uploadRequest.getFile("file" + i);
137         }
138 
139         NotificationThreadLocal.setNotificationEnabled(false);
140         WikiCacheThreadLocal.setClearCache(false);
141 
142         WikiNodeServiceUtil.importPages(
143             nodeId, importer, files, actionRequest.getParameterMap());
144 
145         WikiCacheUtil.clearCache(nodeId);
146 
147         progressTracker.finish();
148     }
149 
150 }