KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > builders > People


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.builders;
11
12 import java.util.*;
13
14 import org.mmbase.cache.Cache;
15 import org.mmbase.module.core.*;
16 import org.mmbase.util.*;
17 import org.mmbase.util.logging.*;
18
19 /**
20  * People builder, adds a replace() methods which retrieves a people objectnumber based on the current user's (SCAN) session.
21  *
22  * @application Basic [builder]
23  * @author Daniel Ockeloen
24  * @version $Id: People.java,v 1.8 2004/09/27 13:24:06 marcel Exp $
25  */

26 public class People extends MMObjectBuilder {
27
28     private static Logger log = Logging.getLoggerInstance(People.class.getName());
29
30     /**
31      * Cache for the most active people
32      */

33     private Cache peopleCache = new Cache(100) {
34         public String JavaDoc getName() { return "PeopleCache"; }
35         public String JavaDoc getDescription() { return "Cache for most active people"; }
36         };
37
38     /**
39      * Obtains a string value by performing the provided command.
40      * The command can be called:
41      * <ul>
42      * <li>by SCAN : $MOD-MMBASE-BUILDER-people-[command]</li>
43      * <li>in jsp : cloud.getNodeManager("people").getInfo(command);</li>
44      * </lu>
45      * The command recognized by the people builder :
46      * <ul>
47      * <li>number : Get the object number for the current user's people object,
48      * based on the current user/cookie as defined by the key.
49      * Only works in SCAN, as it uses sessiondata from the scanpage.</li>
50      * </lu>
51      * @param sp The scanpage (containing http and user info) that calls the function
52      * @param tok a list of strings that describe the (sub)command to execute
53      * @return the result value as a <code>String</code>
54      */

55     public String JavaDoc replace(scanpage sp, StringTokenizer tok) {
56         if (tok.hasMoreTokens()) {
57             String JavaDoc cmd=tok.nextToken();
58             if (cmd.equals("number")) {
59                 int i=getNumber(sp.getSessionName());
60                 if (i!=-1) {
61                     return ""+i;
62                 }
63             }
64         }
65         return "";
66     }
67
68     /**
69      * Get the object number for the current user's people object, based on the current user/cookie as defined by the key.
70      * Only works in SCAN, when used with the users builder.
71      * @dependency users
72      * @performance reference to users should be set during init() method.
73      */

74     public int getNumber(String JavaDoc key) {
75         Integer JavaDoc n=(Integer JavaDoc)peopleCache.get(key);
76         if (n!=null) {
77             log.debug("People - "+key+" people found in cache.");
78             return n.intValue();
79         }
80         // obtain a number from users
81
Users bul=(Users)mmb.getMMObject("users");
82         if (bul!=null) {
83             int i=bul.getNumber(key);
84             if (i!=-1) {
85                 // lets find a related people object
86
MMObjectNode node=getNode(i);
87                 Enumeration e=node.getRelatedNodes("people").elements();
88                 if (e.hasMoreElements()) {
89                     MMObjectNode node2=(MMObjectNode)e.nextElement();
90                     if (node2!=null) {
91                         int number=node2.getNumber();
92                         peopleCache.put(key,new Integer JavaDoc(number));
93                         return number;
94                     }
95                 }
96             }
97         }
98         return -1;
99     }
100
101     /**
102      * flush caches of the (cookie defined) user
103      * also signals the session module
104      */

105     public void flushCache(String JavaDoc key) {
106         //bugfix #6583: NullPointerException when key = null
107
if(key!=null)
108             peopleCache.remove(key);
109     }
110 }
111
Popular Tags