KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > technorati > TagCloudPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.technorati;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.blog.Blog;
36 import org.blojsom.blog.Entry;
37 import org.blojsom.fetcher.Fetcher;
38 import org.blojsom.fetcher.FetcherException;
39 import org.blojsom.plugin.Plugin;
40 import org.blojsom.plugin.PluginException;
41 import org.blojsom.util.BlojsomUtils;
42
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.TreeMap JavaDoc;
48
49 /**
50  * Tag Cloud plugin
51  *
52  * @author David Czarnecki
53  * @since blojsom 3.0
54  * @version $Id: TagCloudPlugin.java,v 1.4 2006/04/30 21:57:05 czarneckid Exp $
55  */

56 public class TagCloudPlugin implements Plugin {
57
58     private Log _logger = LogFactory.getLog(TagCloudPlugin.class);
59
60     private static final String JavaDoc TAG_QUERY_PARAM = "tq";
61     private static final String JavaDoc BLOJSOM_PLUGIN_TAG_CLOUD_MAP = "BLOJSOM_PLUGIN_TAG_CLOUD_MAP";
62     private static final int MIN_FONTSIZE = 1;
63     private static final int MAX_FONTSIZE = 10;
64
65     private Fetcher _fetcher;
66
67     /**
68      * Create a new instance of the tag cloud plugin
69      */

70     public TagCloudPlugin() {
71     }
72
73     /**
74      * Set the {@link Fetcher}
75      *
76      * @param fetcher {@link Fetcher}
77      */

78     public void setFetcher(Fetcher fetcher) {
79         _fetcher = fetcher;
80     }
81
82     /**
83      * Initialize this plugin. This method only called when the plugin is instantiated.
84      *
85      * @throws org.blojsom.plugin.PluginException
86      * If there is an error initializing the plugin
87      */

88     public void init() throws PluginException {
89     }
90
91     /**
92      * Process the blog entries
93      *
94      * @param httpServletRequest Request
95      * @param httpServletResponse Response
96      * @param blog {@link Blog} instance
97      * @param context Context
98      * @param entries Blog entries retrieved for the particular request
99      * @return Modified set of blog entries
100      * @throws PluginException If there is an error processing the blog entries
101      */

102     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
103         TreeMap JavaDoc tagMap = new TreeMap JavaDoc();
104         String JavaDoc tagQuery = BlojsomUtils.getRequestValue(TAG_QUERY_PARAM, httpServletRequest);
105         Integer JavaDoc maxTagCount = new Integer JavaDoc(1);
106         Entry[] entriesMatchingTag = new Entry[0];
107
108         if (!BlojsomUtils.checkNullOrBlank(tagQuery)) {
109             try {
110                 entriesMatchingTag = _fetcher.findEntriesByMetadataKeyValue(blog, TechnoratiTagsPlugin.METADATA_TECHNORATI_TAGS,
111                         tagQuery, true, true);
112                 if (_logger.isDebugEnabled()) {
113                     _logger.debug("Entries matching tag: " + entriesMatchingTag.length);
114                 }
115             } catch (FetcherException e) {
116                 e.printStackTrace();
117                 if (_logger.isErrorEnabled()) {
118                     _logger.error(e);
119                 }
120             }
121         }
122
123         Entry[] entriesForTagMap;
124         try {
125             entriesForTagMap = _fetcher.findEntriesWithMetadataKey(blog, TechnoratiTagsPlugin.METADATA_TECHNORATI_TAGS);
126
127             for (int i = 0; i < entriesForTagMap.length; i++) {
128                 Entry entry = entriesForTagMap[i];
129
130                 if (BlojsomUtils.checkMapForKey(entry.getMetaData(), TechnoratiTagsPlugin.METADATA_TECHNORATI_TAGS)) {
131                     String JavaDoc[] tags = BlojsomUtils.parseOnlyCommaList((String JavaDoc) entry.getMetaData().get(TechnoratiTagsPlugin.METADATA_TECHNORATI_TAGS));
132                     String JavaDoc tag;
133                     if (tags != null && tags.length > 0) {
134                         for (int j = 0; j < tags.length; j++) {
135                             tag = tags[j].trim();
136
137                             if (tagMap.containsKey(tag)) {
138                                 Integer JavaDoc tagCount = (Integer JavaDoc) tagMap.get(tag);
139                                 tagCount = new Integer JavaDoc(tagCount.intValue() + 1);
140                                 if (tagCount.intValue() > maxTagCount.intValue()) {
141                                     maxTagCount = new Integer JavaDoc(tagCount.intValue());
142                                 }
143
144                                 tagMap.put(tag, tagCount);
145                             } else {
146                                 tagMap.put(tag, new Integer JavaDoc(1));
147                             }
148                         }
149                     }
150                 }
151             }
152
153             Iterator JavaDoc tagKeyIterator = tagMap.keySet().iterator();
154             while (tagKeyIterator.hasNext()) {
155                 String JavaDoc tag = (String JavaDoc) tagKeyIterator.next();
156                 Integer JavaDoc tagCount = (Integer JavaDoc) tagMap.get(tag);
157                 int tagRank = rankTagPerEntries(tagCount.intValue(), 1, maxTagCount.intValue());
158
159                 if (_logger.isDebugEnabled()) {
160                     _logger.debug("Tag rank for " + tag + " tag: " + tagRank);
161                 }
162                 tagMap.put(tag, new Integer JavaDoc(tagRank));
163             }
164         } catch (FetcherException e) {
165             if (_logger.isErrorEnabled()) {
166                 _logger.error(e);
167             }
168         }
169
170         context.put(BLOJSOM_PLUGIN_TAG_CLOUD_MAP, tagMap);
171
172         if (!BlojsomUtils.checkNullOrBlank(tagQuery) && (entriesMatchingTag != null) && (entriesMatchingTag.length > 0)) {
173             return entriesMatchingTag;
174         }
175
176         return entries;
177     }
178
179     /**
180      * Calculate a scaled ranking for a given tag count and number of entries
181      *
182      * @param tagCount Total count for a given tag
183      * @param minTagCount Minimum number of tags
184      * @param maxTagCount Maximum number of tags
185      * @return Ranked distribution between 1 and TAG_DISTRIBUTION_MAX
186      */

187     private int rankTagPerEntries(int tagCount, int minTagCount, int maxTagCount) {
188         if (minTagCount == maxTagCount) {
189             return MAX_FONTSIZE;
190         }
191
192         double scaledCount = (double) (tagCount - minTagCount) / (maxTagCount - minTagCount);
193         double scaledSize = scaledCount * (MAX_FONTSIZE - MIN_FONTSIZE) + MIN_FONTSIZE;
194
195         return (int) Math.ceil(scaledSize);
196     }
197
198     /**
199      * Perform any cleanup for the plugin. Called after {@link #process}.
200      *
201      * @throws org.blojsom.plugin.PluginException
202      * If there is an error performing cleanup for this plugin
203      */

204     public void cleanup() throws PluginException {
205     }
206
207     /**
208      * Called when BlojsomServlet is taken out of service
209      *
210      * @throws org.blojsom.plugin.PluginException
211      * If there is an error in finalizing this plugin
212      */

213     public void destroy() throws PluginException {
214     }
215 }
Popular Tags