001    /**
002     * Copyright (c) 2000-2013 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.kernel.util.StreamUtil;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.wiki.NoSuchNodeException;
029    import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
030    import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
031    import com.liferay.portlet.wiki.util.WikiCacheUtil;
032    
033    import java.io.InputStream;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Jorge Ferrer
047     */
048    public class ImportPagesAction extends PortletAction {
049    
050            @Override
051            public void processAction(
052                            ActionMapping actionMapping, ActionForm actionForm,
053                            PortletConfig portletConfig, ActionRequest actionRequest,
054                            ActionResponse actionResponse)
055                    throws Exception {
056    
057                    try {
058                            importPages(actionRequest, actionResponse);
059    
060                            sendRedirect(actionRequest, actionResponse);
061                    }
062                    catch (Exception e) {
063                            if (e instanceof NoSuchNodeException ||
064                                    e instanceof PrincipalException) {
065    
066                                    SessionErrors.add(actionRequest, e.getClass());
067    
068                                    setForward(actionRequest, "portlet.wiki.error");
069                            }
070                            else if (e instanceof PortalException) {
071                                    SessionErrors.add(actionRequest, e.getClass());
072                            }
073                            else {
074                                    throw e;
075                            }
076                    }
077            }
078    
079            @Override
080            public ActionForward render(
081                            ActionMapping actionMapping, ActionForm actionForm,
082                            PortletConfig portletConfig, RenderRequest renderRequest,
083                            RenderResponse renderResponse)
084                    throws Exception {
085    
086                    try {
087                            ActionUtil.getNode(renderRequest);
088                    }
089                    catch (Exception e) {
090                            if (e instanceof NoSuchNodeException ||
091                                    e instanceof PrincipalException) {
092    
093                                    SessionErrors.add(renderRequest, e.getClass());
094    
095                                    return actionMapping.findForward("portlet.wiki.error");
096                            }
097                            else {
098                                    throw e;
099                            }
100                    }
101    
102                    return actionMapping.findForward(
103                            getForward(renderRequest, "portlet.wiki.import_pages"));
104            }
105    
106            protected void importPages(
107                            ActionRequest actionRequest, ActionResponse actionResponse)
108                    throws Exception {
109    
110                    UploadPortletRequest uploadPortletRequest =
111                            PortalUtil.getUploadPortletRequest(actionRequest);
112    
113                    String importProgressId = ParamUtil.getString(
114                            uploadPortletRequest, "importProgressId");
115    
116                    ProgressTracker progressTracker = new ProgressTracker(
117                            actionRequest, importProgressId);
118    
119                    ProgressTrackerThreadLocal.setProgressTracker(progressTracker);
120    
121                    progressTracker.start();
122    
123                    long nodeId = ParamUtil.getLong(uploadPortletRequest, "nodeId");
124                    String importer = ParamUtil.getString(uploadPortletRequest, "importer");
125    
126                    int filesCount = ParamUtil.getInteger(
127                            uploadPortletRequest, "filesCount", 10);
128    
129                    InputStream[] inputStreams = new InputStream[filesCount];
130    
131                    try {
132                            for (int i = 0; i < filesCount; i++) {
133                                    inputStreams[i] = uploadPortletRequest.getFileAsStream(
134                                            "file" + i);
135                            }
136    
137                            NotificationThreadLocal.setEnabled(false);
138                            WikiCacheThreadLocal.setClearCache(false);
139    
140                            WikiNodeServiceUtil.importPages(
141                                    nodeId, importer, inputStreams,
142                                    actionRequest.getParameterMap());
143                    }
144                    finally {
145                            for (InputStream inputStream : inputStreams) {
146                                    StreamUtil.cleanUp(inputStream);
147                            }
148                    }
149    
150                    WikiCacheUtil.clearCache(nodeId);
151    
152                    progressTracker.finish();
153            }
154    
155    }