KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > Visit


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

15 package org.apache.tapestry.vlib;
16
17 import java.io.Serializable JavaDoc;
18 import java.sql.Timestamp JavaDoc;
19
20 import org.apache.tapestry.IRequestCycle;
21 import org.apache.tapestry.vlib.ejb.Person;
22
23 /**
24  * The visit object for the {@link VirtualLibraryEngine}.
25  *
26  * Primarily, this is used to access the home interfaces and EJB instances, and
27  * to identify who the logged in user is.
28  *
29  *
30  * @author Howard Lewis Ship
31  * @version $Id: Visit.java,v 1.7 2004/02/19 17:37:49 hlship Exp $
32  *
33  **/

34
35 public class Visit implements Serializable JavaDoc
36 {
37     private static final long serialVersionUID = 8589862098677603655L;
38     
39     /**
40      * Used to identify the logged in user.
41      *
42      **/

43
44     private transient Person _user;
45     private Integer JavaDoc _userId;
46
47     /**
48      * Set when the user first logs in. This is the time of their previous
49      * login (logging in returns the old value then updates the value
50      * for subsequent logins).
51      *
52      **/

53
54     private Timestamp JavaDoc _lastAccess;
55
56     /**
57      * Returns the time the user last accessed the database, which may
58      * be null if the user hasn't logged in yet.
59      *
60      **/

61
62     public Timestamp JavaDoc getLastAccess()
63     {
64         return _lastAccess;
65     }
66
67     /**
68      * Gets the logged-in user, or null if the user is not logged in.
69      *
70      **/

71
72     public Person getUser(IRequestCycle cycle)
73     {
74         if (_user != null)
75             return _user;
76
77         if (_userId == null)
78             return null;
79
80         VirtualLibraryEngine vengine = (VirtualLibraryEngine) cycle.getEngine();
81
82         _user = vengine.readPerson(_userId);
83
84         return _user;
85     }
86
87     /**
88      * Returns the id of the logged in user, or null if the
89      * user is not logged in.
90      *
91      **/

92
93     public Integer JavaDoc getUserId()
94     {
95         return _userId;
96     }
97
98     /**
99      * Changes the logged in user. This is only invoked from the
100      * {@link org.apache.tapestry.vlib.pages.Login} page.
101      *
102      **/

103
104     public void setUser(Person value)
105     {
106         _lastAccess = null;
107         _user = value;
108         _userId = null;
109
110         if (_user == null)
111             return;
112
113         _userId = _user.getId();
114
115         _lastAccess = _user.getLastAccess();
116     }
117
118     /**
119      * Returns true if the user is logged in.
120      *
121      **/

122
123     public boolean isUserLoggedIn()
124     {
125         return _userId != null;
126     }
127
128     /**
129      * Returns true if the user has not been identified (has not
130      * logged in).
131      *
132      **/

133
134     public boolean isUserLoggedOut()
135     {
136         return _userId == null;
137     }
138
139     public boolean isLoggedInUser(Integer JavaDoc id)
140     {
141         if (_userId == null)
142             return false;
143
144         return _userId.equals(id);
145     }
146
147     /**
148      * Invoked by pages after they perform an operation that changes the backend
149      * database in such a way that cached data is no longer valid.
150      *
151      **/

152
153     public void clearCache()
154     {
155         _user = null;
156     }
157
158 }
Popular Tags