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.portal.kernel.util;
016    
017    import javax.portlet.PortletRequest;
018    import javax.portlet.PortletSession;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpSession;
022    
023    /**
024     * @author Jorge Ferrer
025     */
026    public class ProgressTracker {
027    
028            public static final String PERCENT =
029                    ProgressTracker.class.getName() + "_PERCENT";
030    
031            public ProgressTracker(HttpServletRequest request, String progressId) {
032                    _request = request;
033                    _progressId = progressId;
034            }
035    
036            public ProgressTracker(PortletRequest portletRequest, String progressId) {
037                    _portletRequest = portletRequest;
038                    _progressId = progressId;
039            }
040    
041            public void finish() {
042                    if (_request != null) {
043                            HttpSession session = _request.getSession();
044    
045                            session.removeAttribute(PERCENT + _progressId);
046                    }
047                    else {
048                            PortletSession portletSession = _portletRequest.getPortletSession();
049    
050                            portletSession.removeAttribute(
051                                    PERCENT + _progressId, PortletSession.APPLICATION_SCOPE);
052                    }
053            }
054    
055            public int getProgress() {
056                    if (_request != null) {
057                            HttpSession session = _request.getSession();
058    
059                            return (Integer)session.getAttribute(PERCENT + _progressId);
060                    }
061                    else {
062                            PortletSession portletSession = _portletRequest.getPortletSession();
063    
064                            return (Integer)portletSession.getAttribute(PERCENT + _progressId);
065                    }
066            }
067    
068            public void start() {
069                    updateProgress(1);
070            }
071    
072            public void updateProgress(int percentage) {
073                    if (_request != null) {
074                            HttpSession session = _request.getSession();
075    
076                            session.setAttribute(
077                                    PERCENT + _progressId, new Integer(percentage));
078                    }
079                    else {
080                            PortletSession portletSession = _portletRequest.getPortletSession();
081    
082                            portletSession.setAttribute(
083                                    PERCENT + _progressId, new Integer(percentage),
084                                    PortletSession.APPLICATION_SCOPE);
085                    }
086            }
087    
088            private PortletRequest _portletRequest;
089            private String _progressId;
090            private HttpServletRequest _request;
091    
092    }