001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.upload.UploadPortletRequest;
020    import com.liferay.portal.kernel.util.NotificationThreadLocal;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.ProgressTracker;
023    import com.liferay.portal.kernel.util.ProgressTrackerThreadLocal;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.wiki.NoSuchNodeException;
028    import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
029    import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
030    import com.liferay.portlet.wiki.util.WikiCacheUtil;
031    
032    import java.io.File;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Jorge Ferrer
046     */
047    public class ImportPagesAction extends PortletAction {
048    
049            public void processAction(
050                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
051                            ActionRequest actionRequest, ActionResponse actionResponse)
052                    throws Exception {
053    
054                    try {
055                            importPages(actionRequest, actionResponse);
056    
057                            sendRedirect(actionRequest, actionResponse);
058                    }
059                    catch (Exception e) {
060                            if (e instanceof NoSuchNodeException ||
061                                    e instanceof PrincipalException) {
062    
063                                    SessionErrors.add(actionRequest, e.getClass().getName());
064    
065                                    setForward(actionRequest, "portlet.wiki.error");
066                            }
067                            else if (e instanceof PortalException) {
068                                    SessionErrors.add(actionRequest, e.getClass().getName());
069                            }
070                            else {
071                                    throw e;
072                            }
073                    }
074            }
075    
076            public ActionForward render(
077                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
078                            RenderRequest renderRequest, RenderResponse renderResponse)
079                    throws Exception {
080    
081                    try {
082                            ActionUtil.getNode(renderRequest);
083                    }
084                    catch (Exception e) {
085                            if (e instanceof NoSuchNodeException ||
086                                    e instanceof PrincipalException) {
087    
088                                    SessionErrors.add(renderRequest, e.getClass().getName());
089    
090                                    return mapping.findForward("portlet.wiki.error");
091                            }
092                            else {
093                                    throw e;
094                            }
095                    }
096    
097                    return mapping.findForward(
098                            getForward(renderRequest, "portlet.wiki.import_pages"));
099            }
100    
101            protected void importPages(
102                            ActionRequest actionRequest, ActionResponse actionResponse)
103                    throws Exception {
104    
105                    UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
106                            actionRequest);
107    
108                    String importProgressId = ParamUtil.getString(
109                            uploadRequest, "importProgressId");
110    
111                    ProgressTracker progressTracker = new ProgressTracker(
112                            actionRequest, importProgressId);
113    
114                    ProgressTrackerThreadLocal.setProgressTracker(progressTracker);
115    
116                    progressTracker.start();
117    
118                    long nodeId = ParamUtil.getLong(uploadRequest, "nodeId");
119                    String importer = ParamUtil.getString(uploadRequest, "importer");
120    
121                    int filesCount = ParamUtil.getInteger(uploadRequest, "filesCount", 10);
122    
123                    File[] files = new File[filesCount];
124    
125                    for (int i = 0; i < filesCount; i++) {
126                            files[i] = uploadRequest.getFile("file" + i);
127                    }
128    
129                    NotificationThreadLocal.setEnabled(false);
130                    WikiCacheThreadLocal.setClearCache(false);
131    
132                    WikiNodeServiceUtil.importPages(
133                            nodeId, importer, files, actionRequest.getParameterMap());
134    
135                    WikiCacheUtil.clearCache(nodeId);
136    
137                    progressTracker.finish();
138            }
139    
140    }