Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/bean/SimpleTrackedUser.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/bean/SimpleTrackedUser.java,v 1.13 2003/09/30 15:13:10 joe Exp $
2    * $Revision: 1.13 $
3    * $Date: 2003/09/30 15:13:10 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.webapps.bean;
31  
32  import com.RuntimeCollective.webapps.RuntimeDataSource;
33  import com.RuntimeCollective.webapps.RuntimeParameters;
34  import com.RuntimeCollective.webapps.bean.SimpleUser;
35  import com.RuntimeCollective.webapps.bean.TrackedUser;
36  import com.RuntimeCollective.webapps.WebappsException;
37  import com.RuntimeCollective.webapps.DateUtils;
38  
39  import java.sql.SQLException;
40  import java.util.Date;
41  import java.util.List;
42  import java.util.Vector;
43  
44  /**
45   * The default implementation of TrackedUser.
46   *
47   * @version $Id: SimpleTrackedUser.java,v 1.13 2003/09/30 15:13:10 joe Exp $
48   */
49  public class SimpleTrackedUser extends SimpleUser implements TrackedUser {
50  
51      private static final String DELETE_FROM = "delete from ";
52      private static final String SELECT_DATA = "select t.no_logins, t.max_sim_ses, t.last_login, t.archived from "; 
53      private static final String WHERE_ID = " where id = ";
54      private static final String WHERE_T_ID = " t where t.id = ";
55      private static final String SELECT_ID = "select id from ";
56      private static final String NO_LOGINS = "no_logins";
57      private static final String MAXSIMSES = "max_sim_ses";
58      private static final String LASTLOG = "last_login";
59      private static final String ARCHIVED = "archived";
60      private static final String WHERE_USER = " where user_id = ";
61      private static final String AND = " and ";
62      private static final String LAST_USED = " - last_used_date < 60 * ";
63      private static final String SPACE = " ";
64      private static final String SPACE_OPEN = " (";
65      private static final String CLOSE = ")";
66  
67  
68      // EntityBean specific
69  
70      /** The name of the database table for this bean type. */
71      public static final String DATABASE_TABLE = "webapps_trackeduser";
72  
73      /** Save this bean to the database. */
74      public void save() throws SQLException {
75    try {
76        // save SimpleUser data
77        super.save();
78  
79        String dbalias = RuntimeDataSource.getDefaultAlias();
80        
81        // save SimpleTrackedUser data
82        RuntimeDataSource.save(id, DATABASE_TABLE, new String[] { NO_LOGINS, MAXSIMSES, LASTLOG, ARCHIVED }, new Object[] { new Integer(NoOfLogins), new Integer(MaxNoOfSimultaneousSessions), LastLoginDate, new Boolean(Archived) } );
83        
84    } catch (SQLException e) {
85        throw new WebappsException("SimpleTrackedUser could not save() : "+e);
86    }
87      }
88  
89       
90      /** Delete this bean from the database. */
91      public void delete() throws SQLException {
92    try {
93        String[] updates;
94  
95        // delete SimpleTrackedUser specific data
96        updates = new String[] { (new StringBuffer(50)).append(DELETE_FROM).append(DATABASE_TABLE).append(WHERE_ID).append(id).toString() };
97        RuntimeDataSource.update( updates);
98  
99        // delete SimpleUser data
100       super.delete();
101       
102   } catch (SQLException e) {
103       throw new WebappsException("SimpleTrackedUser could not delete() : "+e);
104   }
105     }
106 
107 
108     // SimpleTrackedUser specific
109 
110     /** The NoOfLogins.
111      */
112     protected int NoOfLogins = 0;
113 
114     /** Set the NoOfLogins.
115      * @param noOfLogins, an int
116      */
117     public void setNoOfLogins(int noOfLogins) {
118   NoOfLogins = noOfLogins;
119     }
120 
121     /** Get the NoOfLogins
122      * @return an integer
123      */
124     public int getNoOfLogins() {
125   return NoOfLogins;
126     }
127 
128     /** Increase NoOfLogins by 1.
129      */
130     public void increaseNoOfLogins() {
131   NoOfLogins++;
132     }
133 
134     /** The MaxNoOfSimultaneousSessions.
135      */
136     protected int MaxNoOfSimultaneousSessions = 0;
137 
138     /** Set the MaxNoOfSimultaneousSessions.
139      * @param maxNoOfSimultaneousSessions, an int
140      */
141     public void setMaxNoOfSimultaneousSessions(int maxNoOfSimultaneousSessions) {
142   MaxNoOfSimultaneousSessions = maxNoOfSimultaneousSessions;
143     }
144 
145     /** Get the MaxNoOfSimultaneousSessions
146      * @return an integer
147      */
148     public int getMaxNoOfSimultaneousSessions() {
149   return MaxNoOfSimultaneousSessions;
150     }
151 
152     /** Get the number of current/active sessions, eg sessions which haven't timed out.
153      * @return an integer
154      */
155     public int getNoOfCurrentSessions() {
156 
157         try {
158             int[] matching = RuntimeDataSource.queryInts((new StringBuffer(90))
159                                                          .append(SELECT_ID)
160                                                          .append(Session.DATABASE_TABLE)
161                                                          .append(WHERE_USER)
162                                                          .append(id)
163                                                          .append(AND)
164                                                          .append(RuntimeDataSource.toSqlString(new Date()))
165                                                          .append(LAST_USED)
166                                                          .append(RuntimeParameters.get(Session.TIMEOUT_KEY))
167                                                          .toString());
168             
169             if (matching != null)
170                 return matching.length;
171             
172             else
173                 return 0;
174             
175         } catch (SQLException e) {
176             RuntimeParameters.logDebug(this, "getNoOfCurrentSessions() could not check the database : "+e);
177             return 99999;
178         }
179     }
180     
181     /** The LastLoginDate.
182      */
183     protected Date LastLoginDate = null;
184 
185     /** Set the LastLoginDate.
186      * @param lastLoginDate, a Date
187      */
188     public void setLastLoginDate(Date lastLoginDate) {
189   LastLoginDate = lastLoginDate;
190     }
191 
192     /** Get the LastLoginDate
193      * @return a Date
194      */
195     public Date getLastLoginDate() {
196   return LastLoginDate;
197     }
198 
199     /** The Archived.
200      */
201     protected boolean Archived = false;
202 
203     /** Set the Archived.
204      * @param archived, a boolean
205      */
206     public void setArchived(boolean archived) {
207   Archived = archived;
208     }
209 
210     /** Get the Archived
211      * @return a boolean
212      */
213     public boolean getArchived() {
214   return Archived;
215     }
216 
217     /** Get the Archived
218      * @return a boolean
219      */
220     public boolean isArchived() {
221   return getArchived();
222     }
223 
224     /**
225      * Construct a new blank SimpleTrackedUser, giving it a new unique ID.
226      */
227     public SimpleTrackedUser() throws SQLException {
228   // initialize a SimpleUser
229   super();
230 
231   // initialize SimpleTrackedUser specific data
232   NoOfLogins = 0;
233   MaxNoOfSimultaneousSessions = 0;
234   LastLoginDate = null;
235   Archived = false;
236     }
237 
238     /** Get a current SimpleTrackedUser from the RuntimeDataSource, given an id.
239      * @param id ID of the SimpleTrackedUser.
240      * @exception SQLException is thrown if no such SimpleTrackedUser exists.
241      */
242     public SimpleTrackedUser(int id) throws SQLException {
243   // load a SimpleUser
244   super(id);
245 
246   // load SimpleTrackedUser specific data
247   String dbalias = RuntimeDataSource.getDefaultAlias();
248   Object[] result = RuntimeDataSource.queryRow(dbalias, (new StringBuffer(70)).append(SELECT_DATA).append(DATABASE_TABLE).append(WHERE_T_ID).append(id).toString());
249   if (result.length != 4) {
250       throw new SQLException("Cannot load SimpleTrackedUser "+id+": "+result.length+" fields found in "+DATABASE_TABLE+" instead of 4.");
251   }
252 
253   if (result[0] != null)
254       NoOfLogins = Integer.parseInt(result[0].toString());
255   else
256       NoOfLogins = 0;
257   if (result[1] != null)
258       MaxNoOfSimultaneousSessions = Integer.parseInt(result[1].toString());
259   else
260       MaxNoOfSimultaneousSessions = 0;
261   if (result[2] != null)
262       LastLoginDate = RuntimeDataSource.toDate(result[2].toString());
263   else
264       LastLoginDate = null;
265         Archived = RuntimeDataSource.toboolean(result[3].toString());
266     }
267 
268 
269     /** Get the descriptor of this user: first last (email). */
270     public String getNameDescriptor() {
271         return getFirstName()
272             .concat(SPACE)
273             .concat(getLastName())
274             .concat(SPACE_OPEN)
275             .concat(getEmail())
276             .concat(CLOSE);
277     }
278 
279 }
280 
281