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