KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > auth > OnlineUserAction


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/auth/OnlineUserAction.java,v 1.18 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.18 $
5  * $Date: 2006/04/14 17:05:26 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.auth;
42
43 import java.sql.Timestamp JavaDoc;
44 import java.util.Locale JavaDoc;
45
46 import javax.servlet.http.HttpServletRequest JavaDoc;
47
48 import com.mvnforum.MVNForumResourceBundle;
49 import net.myvietnam.mvncore.util.DateUtil;
50 import net.myvietnam.mvncore.util.StringUtil;
51 import net.myvietnam.mvncore.web.GenericRequest;
52
53 public class OnlineUserAction {
54
55     private String JavaDoc url;
56     //private String desc;
57
private Action action;
58     private Timestamp JavaDoc firstRequestTime;
59     private Timestamp JavaDoc lastRequestTime;
60     private String JavaDoc remoteAddr;
61     private String JavaDoc userAgent;
62
63     // NOTE: these 2 variables seam to duplicate variables in OnlineUserImpl
64
// but we need it when display all online users (listonlineuser)
65
private int memberID;
66     private String JavaDoc memberName;
67     private boolean invisible = false;
68
69     private int sessionCount = 1;
70     /**
71      * default constructor
72      */

73     OnlineUserAction() {
74         /*
75          * We must separete 2 duplicate method calls since we will
76          * update the lastRequestTime, but not firstRequestTime
77          */

78         firstRequestTime = DateUtil.getCurrentGMTTimestamp();
79         lastRequestTime = DateUtil.getCurrentGMTTimestamp();
80     }
81
82 /****************************************************************
83  * Public method
84  ****************************************************************/

85
86     public String JavaDoc getDesc(HttpServletRequest JavaDoc request) {
87         if (action == null) {
88             // This issue is in the Realm auth integretion
89
return "cannot get description (action is null)";
90         }
91         return action.getLocalizedDesc(request);
92     }
93
94     public String JavaDoc getUrl() {
95         return url;
96     }
97
98     public int getMemberID() {
99         return memberID;
100     }
101
102     public String JavaDoc getMemberName() {
103         return memberName;
104     }
105
106     public boolean isInvisibleMember() {
107         return invisible;
108     }
109
110     public Timestamp JavaDoc getFirstRequestTime() {
111         return firstRequestTime;
112     }
113
114     public Timestamp JavaDoc getLastRequestTime() {
115         return lastRequestTime;
116     }
117
118     public int getSessionCount() {
119         return sessionCount;
120     }
121
122     public void setSessionCount(int count) {
123         sessionCount = count;
124     }
125
126     public void increaseSessionCount(int delta) {
127         sessionCount += delta;
128     }
129
130     public void resetSessionCount() {
131         sessionCount = 1;
132     }
133
134     // util method
135
public String JavaDoc getOnlineDurarionDesc(Timestamp JavaDoc currentTime, Locale JavaDoc locale) {
136         int duration = (int) (currentTime.getTime() - firstRequestTime.getTime());
137         return getTimeString(duration, locale);
138     }
139
140     public String JavaDoc getDurationSinceLastRequestDesc(Timestamp JavaDoc currentTime, Locale JavaDoc locale) {
141         int duration = (int) (currentTime.getTime() - lastRequestTime.getTime());
142         return getTimeString(duration, locale);
143     }
144
145     private String JavaDoc getTimeString(int duration, Locale JavaDoc locale) {
146         long hours = duration / DateUtil.HOUR;
147         long remain = duration - (hours * DateUtil.HOUR);
148         long minutes = remain / DateUtil.MINUTE;
149         StringBuffer JavaDoc time = new StringBuffer JavaDoc(64);
150
151         if (hours > 0) {//there is hour
152
time.append(hours).append(" ");
153             if (hours == 1) {
154                 time.append(MVNForumResourceBundle.getString(locale, "mvnforum.common.date.1_hour"));
155             } else {
156                 time.append(MVNForumResourceBundle.getString(locale, "mvnforum.common.date.X_hour"));
157             }
158         }
159
160         if (minutes > 0) {//there is minute
161
if (hours > 0) {
162                 time.append(" ").append(MVNForumResourceBundle.getString(locale, "mvnforum.common.date.hhmm_and")).append(" ");
163             }
164             time.append(minutes).append(" ");
165             if (minutes == 1) {
166                 time.append(MVNForumResourceBundle.getString(locale, "mvnforum.common.date.1_minute"));
167             } else {
168                 time.append(MVNForumResourceBundle.getString(locale, "mvnforum.common.date.X_minutes"));
169             }
170         } else {// no minute
171
// if there is no hour, should show "0 minute"
172
if (hours == 0) {
173                 time.append("0 ").append(MVNForumResourceBundle.getString(locale, "mvnforum.common.date.1_minute"));
174             }
175         }
176
177         return time.toString();
178     }
179
180     public String JavaDoc getRemoteAddr() {
181         return remoteAddr;
182     }
183
184     public String JavaDoc getUserAgent() {
185         return userAgent;
186     }
187
188
189     /****************************************************************
190      * Default package method
191      ****************************************************************/

192     protected void initRemoteAddr_UserAgent(HttpServletRequest JavaDoc request) {
193         setUserAgent(request.getHeader("User-Agent"));
194         setRemoteAddr(request.getRemoteAddr());
195     }
196
197     protected void initRemoteAddr_UserAgent(GenericRequest request) {
198         if (request.isServletRequest()) {
199             setUserAgent(request.getServletRequest().getHeader("User-Agent"));
200             setRemoteAddr(request.getServletRequest().getRemoteAddr());
201         } else {
202             //@todo: implement it later
203
//setUserAgent(request.getHeader("User-Agent"));
204
setRemoteAddr(request.getRemoteAddr());
205         }
206     }
207
208     void setAction(Action action) {
209         //this.desc = action.getDesc();
210
this.url = action.getUrl();
211         this.action = action;
212     }
213
214     /** @todo use method DateUtil.updateCurrentGMTTimestamp() */
215     void updateLastRequestTime() {
216         DateUtil.updateCurrentGMTTimestamp(lastRequestTime);
217     }
218
219     void setMemberID(int memberID) {
220         this.memberID = memberID;
221     }
222
223     void setMemberName(String JavaDoc memberName) {
224         this.memberName = memberName;
225     }
226
227     void setMemberInvisible(boolean invisible) {
228         this.invisible = invisible;
229     }
230
231     public void setRemoteAddr(String JavaDoc remoteAddr) {
232         this.remoteAddr = remoteAddr;
233     }
234
235     public void setUserAgent(String JavaDoc userAgent) {
236         this.userAgent = StringUtil.getEmptyStringIfNull(userAgent);
237     }
238 }
239
Popular Tags