1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ImportPagesAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          try {
65              importPages(actionRequest, actionResponse);
66  
67              sendRedirect(actionRequest, actionResponse);
68          }
69          catch (Exception e) {
70              if (e instanceof NoSuchNodeException ||
71                  e instanceof PrincipalException) {
72  
73                  SessionErrors.add(actionRequest, e.getClass().getName());
74  
75                  setForward(actionRequest, "portlet.wiki.error");
76              }
77              else if (e instanceof ImportFilesException ||
78                       e instanceof NoSuchNodeException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81              }
82              else {
83                  throw e;
84              }
85          }
86      }
87  
88      public ActionForward render(
89              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
90              RenderRequest renderRequest, RenderResponse renderResponse)
91          throws Exception {
92  
93          try {
94              ActionUtil.getNode(renderRequest);
95          }
96          catch (Exception e) {
97              if (e instanceof NoSuchNodeException ||
98                  e instanceof PrincipalException) {
99  
100                 SessionErrors.add(renderRequest, e.getClass().getName());
101 
102                 return mapping.findForward("portlet.wiki.error");
103             }
104             else {
105                 throw e;
106             }
107         }
108 
109         return mapping.findForward(
110             getForward(renderRequest, "portlet.wiki.import_pages"));
111     }
112 
113     protected void importPages(
114             ActionRequest actionRequest, ActionResponse actionResponse)
115         throws Exception {
116 
117         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
118             actionRequest);
119 
120         String importProgressId = ParamUtil.getString(
121             uploadRequest, "importProgressId");
122 
123         ProgressTracker progressTracker = new ProgressTracker(
124             actionRequest, importProgressId);
125 
126         ProgressTrackerThreadLocal.setProgressTracker(progressTracker);
127 
128         progressTracker.start();
129 
130         long nodeId = ParamUtil.getLong(uploadRequest, "nodeId");
131         String importer = ParamUtil.getString(uploadRequest, "importer");
132 
133         int filesCount = ParamUtil.getInteger(uploadRequest, "filesCount", 10);
134 
135         File[] files = new File[filesCount];
136 
137         for (int i = 0; i < filesCount; i++) {
138             files[i] = uploadRequest.getFile("file" + i);
139         }
140 
141         NotificationThreadLocal.setNotificationEnabled(false);
142         WikiCacheThreadLocal.setClearCache(false);
143 
144         WikiNodeServiceUtil.importPages(
145             nodeId, importer, files, actionRequest.getParameterMap());
146 
147         WikiCacheUtil.clearCache(nodeId);
148 
149         progressTracker.finish();
150     }
151 
152 }