1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import javax.portlet.PortletRequest;
26  import javax.portlet.PortletSession;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpSession;
30  
31  /**
32   * <a href="ProgressTracker.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Jorge Ferrer
35   *
36   */
37  public class ProgressTracker {
38  
39      public static final String PERCENT =
40          ProgressTracker.class.getName() + "_PERCENT";
41  
42      public ProgressTracker(HttpServletRequest request, String progressId) {
43          _request = request;
44          _progressId = progressId;
45      }
46  
47      public ProgressTracker(PortletRequest portletRequest, String progressId) {
48          _portletRequest = portletRequest;
49          _progressId = progressId;
50      }
51  
52      public void start() {
53          updateProgress(1);
54      }
55  
56      public void updateProgress(int percentage) {
57          if (_request != null) {
58              HttpSession session = _request.getSession(true);
59  
60              session.setAttribute(
61                  PERCENT + _progressId, new Integer(percentage));
62          }
63          else {
64              PortletSession portletSession = _portletRequest.getPortletSession(
65                  true);
66  
67              portletSession.setAttribute(
68                  PERCENT + _progressId, new Integer(percentage),
69                  PortletSession.APPLICATION_SCOPE);
70          }
71      }
72  
73      public void finish() {
74          if (_request != null) {
75              HttpSession session = _request.getSession(true);
76  
77              session.removeAttribute(PERCENT + _progressId);
78          }
79          else {
80              PortletSession portletSession = _portletRequest.getPortletSession(
81                  true);
82  
83              portletSession.removeAttribute(
84                  PERCENT + _progressId, PortletSession.APPLICATION_SCOPE);
85          }
86      }
87  
88      private HttpServletRequest _request;
89      private PortletRequest _portletRequest;
90      private String _progressId;
91  
92  }