KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > infra > UserContext


1 /*
2  * Copyright 2004 JavaFree.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.javabb.infra;
18
19 import java.util.Date JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.javabb.vo.Topic;
27 import org.javabb.vo.User;
28
29 import com.opensymphony.webwork.ServletActionContext;
30 import com.opensymphony.xwork.ActionContext;
31
32 /**
33  * This class will hold all transient data for the current user.
34  * @author Ronald Tetsuo Miura
35  * @since 14/02/2005
36  */

37 public class UserContext {
38
39     private static final Log LOG = LogFactory.getLog(UserContext.class);
40
41     private static final Date JavaDoc DEFAULT_LAST_VISIT_TIMESTAMP = new Date JavaDoc(0);
42     /**
43      * Key for the user context in the session scope.
44      */

45     public static final String JavaDoc KEY_USER_CONTEXT = "javabb.user.context";
46
47     private User _user = null;
48     private Date JavaDoc _lastVisitTimestamp = null;
49     private Set JavaDoc _readTopicIds = new HashSet JavaDoc();
50
51     /**
52      * @return user context for the current thread
53      */

54     public synchronized static UserContext getContext() {
55         Map JavaDoc session = ActionContext.getContext().getSession();
56         if(session == null){
57             return null;
58         }
59         
60         if (!session.containsKey(KEY_USER_CONTEXT)) {
61             session.put(KEY_USER_CONTEXT, new UserContext());
62         }
63         return (UserContext) session.get(KEY_USER_CONTEXT);
64     }
65
66     /**
67      * @return the user logged in this session.
68      */

69     public User getUser() {
70         return _user;
71     }
72
73     /**
74      * Sets the user logged in this session.
75      * @param user
76      */

77     public void setUser(User user) {
78         if (_user == null && user != null) {
79             _lastVisitTimestamp = user.getLastVisitTimestamp();
80         }
81         _user = user;
82         ActionContext.getContext().getSession().put("jbbuser", _user);
83         ServletActionContext.getRequest().getSession().putValue("jbbuser", _user);
84     }
85
86     /**
87      * @return true if user is authenticated
88      */

89     public boolean isAuthenticated() {
90         return _user != null;
91     }
92
93     public void deauthenticate() {
94         _user = null;
95         ActionContext.getContext().getSession().remove("jbbuser");
96     }
97
98     /**
99      * @return timestamp of the last visit
100      */

101     public Date JavaDoc getLastVisitTimestamp() {
102         if (_lastVisitTimestamp == null) {
103             return DEFAULT_LAST_VISIT_TIMESTAMP;
104         }
105         return _lastVisitTimestamp;
106     }
107
108     public boolean isTopicRead(Topic topic) {
109         try {
110             boolean beforeLastVisit = topic.getLastPostDate().before(getLastVisitTimestamp());
111             return _readTopicIds.contains(topic.getId()) || beforeLastVisit;
112         } catch (RuntimeException JavaDoc e) {
113             return true;
114         }
115     }
116
117     public void setTopicRead(Long JavaDoc topicId) {
118         _readTopicIds.add(topicId);
119     }
120
121     public void setTopicUnread(Long JavaDoc topicId) {
122         _readTopicIds.remove(topicId);
123     }
124 }
125
Popular Tags