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.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 int getProgress() {
042                    if (_request != null) {
043                            HttpSession session = _request.getSession();
044    
045                            return (Integer)session.getAttribute(PERCENT + _progressId);
046                    }
047                    else {
048                            PortletSession portletSession = _portletRequest.getPortletSession();
049    
050                            return (Integer)portletSession.getAttribute(PERCENT + _progressId);
051                    }
052            }
053    
054            public void start() {
055                    updateProgress(1);
056            }
057    
058            public void updateProgress(int percentage) {
059                    if (_request != null) {
060                            HttpSession session = _request.getSession();
061    
062                            session.setAttribute(
063                                    PERCENT + _progressId, new Integer(percentage));
064                    }
065                    else {
066                            PortletSession portletSession = _portletRequest.getPortletSession();
067    
068                            portletSession.setAttribute(
069                                    PERCENT + _progressId, new Integer(percentage),
070                                    PortletSession.APPLICATION_SCOPE);
071                    }
072            }
073    
074            public void finish() {
075                    if (_request != null) {
076                            HttpSession session = _request.getSession();
077    
078                            session.removeAttribute(PERCENT + _progressId);
079                    }
080                    else {
081                            PortletSession portletSession = _portletRequest.getPortletSession();
082    
083                            portletSession.removeAttribute(
084                                    PERCENT + _progressId, PortletSession.APPLICATION_SCOPE);
085                    }
086            }
087    
088            private HttpServletRequest _request;
089            private PortletRequest _portletRequest;
090            private String _progressId;
091    
092    }