KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > community > builders > Community


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
11 package org.mmbase.applications.community.builders;
12
13 import java.util.*;
14
15 import org.mmbase.module.core.*;
16 import org.mmbase.util.*;
17 import org.mmbase.util.logging.*;
18
19 /**
20  * This builder implements additional functionality and methods to handle
21  * community objects.
22  * Added functionality involve opening and closing all channels related to
23  * this community, and expanding a URL obtained form the 'maps' builder with
24  * a community number (SCAN only).
25  *
26  * @author Dirk-Jan Hoekstra
27  * @author Pierre van Rooden
28  * @version $Id: Community.java,v 1.21 2005/11/23 15:45:13 pierre Exp $
29  */

30
31 public class Community extends MMObjectBuilder {
32
33     /** Community type : chatbox */
34     public static final String JavaDoc STR_CHATBOX = "chatbox";
35     /** Community type : forum */
36     public static final String JavaDoc STR_FORUM = "forum";
37
38     //logger
39
private static final Logger log = Logging.getLoggerInstance(Community.class);
40
41     private Channel channelBuilder;
42     private MMObjectBuilder mapBuilder;
43     // indicates whether this builder has been activated for the community application
44
private boolean active = false;
45
46     /**
47      * Constructor
48      */

49     public Community() {
50     }
51
52     public boolean init() {
53         boolean result = super.init();
54         activate();
55         return result;
56     }
57
58     /**
59      * Activates the community builder for the community application by associating it with other community builders
60      * @return true if activation worked
61      */

62     public boolean activate() {
63         if (!active) {
64             mapBuilder = mmb.getMMObject("maps");
65             channelBuilder = (Channel)mmb.getMMObject("channel");
66             active = channelBuilder != null;
67         }
68         return active;
69     }
70
71     /**
72      * Opens all the channels that are connected to this community
73      *
74      */

75     public void openAllCommunities() {
76         //ensure the communitybuilder is initialized.
77
init();
78         if (channelBuilder == null) {
79             log.error("No channel builder");
80             return;
81         }
82         ClusterBuilder cluster=mmb.getClusterBuilder();
83
84         Vector builders = new Vector();
85         builders.add("community");
86         builders.add("channel");
87
88         Vector fields = new Vector();
89         fields.add("community.number");
90         fields.add("channel.number");
91         Vector allchannels=cluster.searchMultiLevelVector(null,fields,"YES",builders,
92                "WHERE channel.open = "+Channel.OPEN+" OR channel.open = "+Channel.WANT_OPEN,
93                null,null);
94         if (allchannels!=null) {
95             for (Iterator channels=allchannels.iterator(); channels.hasNext(); ) {
96                 MMObjectNode channel = (MMObjectNode)channels.next();
97                 log.info("open channel"+channel);
98                 channelBuilder.open(channel.getNodeValue("channel"),channel.getNodeValue("community"));
99             }
100         }
101     }
102
103     /**
104      * Opens all the channels that are connected to this community
105      *
106      * @param community The community node of which to open all the channels.
107      */

108     public void openAllChannels(MMObjectNode community) {
109         if (channelBuilder == null) {
110             log.error("No channel builder");
111             return;
112         }
113         Enumeration relatedChannels = mmb.getInsRel().getRelated(community.getNumber(), channelBuilder.getNumber());
114         while (relatedChannels.hasMoreElements()) {
115             channelBuilder.open((MMObjectNode)relatedChannels.nextElement());
116         }
117     }
118
119     /**
120      * Closes all the channels of the community.
121      *
122      * @param community The community of which to close all the channels.
123      */

124     public void closeAllChannels(MMObjectNode community) {
125         if (channelBuilder == null) {
126             log.error("No channel builder");
127             return;
128         }
129         Enumeration relatedChannels = mmb.getInsRel().getRelated(community.getNumber(), channelBuilder.getNumber());
130         while (relatedChannels.hasMoreElements()) {
131             channelBuilder.close((MMObjectNode)relatedChannels.nextElement());
132         }
133     }
134
135     /**
136      * Handles the $MOD-MMBASE-BUILDER-community-commands.
137      * Commands handled by this command are:
138      * <ul>
139      * <li> communitynr-OPEN : opens all channels that are connected to this community</li>
140      * <li> communitynr-CLOSE: closes all channels that are connected to this community</li>
141      * </ul>
142      * @param sp the current page context
143      * @param tok the tokenized command
144      * @return the empty string
145      */

146     public String JavaDoc replace(PageInfo sp, StringTokenizer tok) {
147         // The first thing we expect is a community number.
148
if (!tok.hasMoreElements()) {
149             log.error("replace(): community number expected after $MOD-BUILDER-community-.");
150             return "";
151         }
152         MMObjectNode community = getNode(tok.nextToken());
153
154         if (tok.hasMoreElements()) {
155             String JavaDoc cmd = tok.nextToken();
156             if (cmd.equals("OPEN")) openAllChannels(community);
157             if (cmd.equals("CLOSE")) closeAllChannels(community);
158         }
159         return "";
160     }
161
162     /**
163      * Retrieves a URL from a related Map object, and append the community number
164      * to the URL.
165      * This requires the presence of a 'maps' builder, which should have a
166      * functional 'getDefaultURL' method.
167      *
168      * @param src The number of the community MMObjectNode.
169      * @return the resulting URL, or <code>null</code> if not map-node was
170      * associated with this community.
171      * @deprecated There is no maps definition available in cvs.
172      * In addition, this method only produces SCAN-format urls.
173      */

174     public String JavaDoc getDefaultUrl(int src) {
175         if (mapBuilder==null) return null;
176         Enumeration e= mmb.getInsRel().getRelated(src, mapBuilder.getNumber());
177         if (!e.hasMoreElements()) {
178             log.debug("GetDefaultURL Could not find related map for community node " + src);
179             return null;
180         }
181         MMObjectNode mapNode = (MMObjectNode)e.nextElement();
182         String JavaDoc URL = mapBuilder.getDefaultUrl(mapNode.getNumber());
183         if (URL!=null) {
184             URL += "+" + src;
185         }
186         return URL;
187     }
188 }
189
Popular Tags