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(importProgressId);
117    
118                    ProgressTrackerThreadLocal.setProgressTracker(progressTracker);
119    
120                    progressTracker.start(actionRequest);
121    
122                    long nodeId = ParamUtil.getLong(uploadPortletRequest, "nodeId");
123                    String importer = ParamUtil.getString(uploadPortletRequest, "importer");
124    
125                    int filesCount = ParamUtil.getInteger(
126                            uploadPortletRequest, "filesCount", 10);
127    
128                    InputStream[] inputStreams = new InputStream[filesCount];
129    
130                    try {
131                            for (int i = 0; i < filesCount; i++) {
132                                    inputStreams[i] = uploadPortletRequest.getFileAsStream(
133                                            "file" + i);
134                            }
135    
136                            NotificationThreadLocal.setEnabled(false);
137                            WikiCacheThreadLocal.setClearCache(false);
138    
139                            WikiNodeServiceUtil.importPages(
140                                    nodeId, importer, inputStreams,
141                                    actionRequest.getParameterMap());
142                    }
143                    finally {
144                            for (InputStream inputStream : inputStreams) {
145                                    StreamUtil.cleanUp(inputStream);
146                            }
147                    }
148    
149                    WikiCacheUtil.clearCache(nodeId);
150    
151                    progressTracker.finish(actionRequest);
152            }
153    
154    }