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