001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, PortletConfig portletConfig,
053                            ActionRequest actionRequest, ActionResponse actionResponse)
054                    throws Exception {
055    
056                    try {
057                            importPages(actionRequest, actionResponse);
058    
059                            sendRedirect(actionRequest, actionResponse);
060                    }
061                    catch (Exception e) {
062                            if (e instanceof NoSuchNodeException ||
063                                    e instanceof PrincipalException) {
064    
065                                    SessionErrors.add(actionRequest, e.getClass().getName());
066    
067                                    setForward(actionRequest, "portlet.wiki.error");
068                            }
069                            else if (e instanceof PortalException) {
070                                    SessionErrors.add(actionRequest, e.getClass().getName());
071                            }
072                            else {
073                                    throw e;
074                            }
075                    }
076            }
077    
078            @Override
079            public ActionForward render(
080                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
081                            RenderRequest renderRequest, RenderResponse renderResponse)
082                    throws Exception {
083    
084                    try {
085                            ActionUtil.getNode(renderRequest);
086                    }
087                    catch (Exception e) {
088                            if (e instanceof NoSuchNodeException ||
089                                    e instanceof PrincipalException) {
090    
091                                    SessionErrors.add(renderRequest, e.getClass().getName());
092    
093                                    return mapping.findForward("portlet.wiki.error");
094                            }
095                            else {
096                                    throw e;
097                            }
098                    }
099    
100                    return mapping.findForward(
101                            getForward(renderRequest, "portlet.wiki.import_pages"));
102            }
103    
104            protected void importPages(
105                            ActionRequest actionRequest, ActionResponse actionResponse)
106                    throws Exception {
107    
108                    UploadPortletRequest uploadPortletRequest =
109                            PortalUtil.getUploadPortletRequest(actionRequest);
110    
111                    String importProgressId = ParamUtil.getString(
112                            uploadPortletRequest, "importProgressId");
113    
114                    ProgressTracker progressTracker = new ProgressTracker(
115                            actionRequest, importProgressId);
116    
117                    ProgressTrackerThreadLocal.setProgressTracker(progressTracker);
118    
119                    progressTracker.start();
120    
121                    long nodeId = ParamUtil.getLong(uploadPortletRequest, "nodeId");
122                    String importer = ParamUtil.getString(uploadPortletRequest, "importer");
123    
124                    int filesCount = ParamUtil.getInteger(
125                            uploadPortletRequest, "filesCount", 10);
126    
127                    InputStream[] inputStreams = new InputStream[filesCount];
128    
129                    try {
130                            for (int i = 0; i < filesCount; i++) {
131                                    inputStreams[i] = uploadPortletRequest.getFileAsStream(
132                                            "file" + i);
133                            }
134    
135                            NotificationThreadLocal.setEnabled(false);
136                            WikiCacheThreadLocal.setClearCache(false);
137    
138                            WikiNodeServiceUtil.importPages(
139                                    nodeId, importer, inputStreams,
140                                    actionRequest.getParameterMap());
141                    }
142                    finally {
143                            for (InputStream inputStream : inputStreams) {
144                                    StreamUtil.cleanUp(inputStream);
145                            }
146                    }
147    
148                    WikiCacheUtil.clearCache(nodeId);
149    
150                    progressTracker.finish();
151            }
152    
153    }