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.portal.kernel.util;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    import javax.portlet.PortletRequest;
021    import javax.portlet.PortletSession;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpSession;
025    
026    /**
027     * @author Jorge Ferrer
028     * @author Sergio González
029     */
030    public class ProgressTracker {
031    
032            public static final String PERCENT =
033                    ProgressTracker.class.getName() + "_PERCENT";
034    
035            public ProgressTracker(HttpServletRequest request, String progressId) {
036                    this(request.getSession(), progressId);
037            }
038    
039            public ProgressTracker(HttpSession session, String progressId) {
040                    _session = session;
041                    _progressId = progressId;
042    
043                    addProgress(ProgressStatusConstants.PREPARED, 0, StringPool.BLANK);
044            }
045    
046            public ProgressTracker(PortletRequest portletRequest, String progressId) {
047                    this(portletRequest.getPortletSession(), progressId);
048            }
049    
050            public ProgressTracker(PortletSession portletSession, String progressId) {
051                    _portletSession = portletSession;
052                    _progressId = progressId;
053    
054                    addProgress(ProgressStatusConstants.PREPARED, 0, StringPool.BLANK);
055            }
056    
057            public void addProgress(int status, int percent, String message) {
058                    Tuple tuple = new Tuple(percent, message);
059    
060                    _progress.put(status, tuple);
061            }
062    
063            public void finish() {
064                    if (_session != null) {
065                            _session.removeAttribute(PERCENT + _progressId);
066                    }
067                    else {
068                            _portletSession.removeAttribute(
069                                    PERCENT + _progressId, PortletSession.APPLICATION_SCOPE);
070                    }
071            }
072    
073            public String getMessage() {
074                    Tuple tuple = _progress.get(_status);
075    
076                    String message = GetterUtil.getString(tuple.getObject(1));
077    
078                    return message;
079            }
080    
081            public int getPercent() {
082                    return _percent;
083            }
084    
085            public int getStatus() {
086                    return _status;
087            }
088    
089            public void initialize() {
090                    if (_session != null) {
091                            _session.setAttribute(PERCENT + _progressId, this);
092                    }
093                    else {
094                            _portletSession.setAttribute(
095                                    PERCENT + _progressId, this, PortletSession.APPLICATION_SCOPE);
096                    }
097            }
098    
099            public void setPercent(int percent) {
100                    _percent = percent;
101            }
102    
103            public void setStatus(int status) {
104                    _status = status;
105    
106                    Tuple tuple = _progress.get(_status);
107    
108                    _percent = GetterUtil.getInteger(tuple.getObject(0));
109            }
110    
111            public void start() {
112                    setPercent(1);
113            }
114    
115            private int _percent;
116            private PortletSession _portletSession;
117            private Map<Integer, Tuple> _progress = new HashMap<Integer, Tuple>();
118            private String _progressId;
119            private HttpSession _session;
120            private int _status = ProgressStatusConstants.PREPARED;
121    
122    }