001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.technorati;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.fetcher.Fetcher;
037: import org.blojsom.fetcher.FetcherException;
038: import org.blojsom.plugin.Plugin;
039: import org.blojsom.plugin.PluginException;
040: import org.blojsom.util.BlojsomUtils;
041:
042: import javax.servlet.http.HttpServletRequest;
043: import javax.servlet.http.HttpServletResponse;
044: import java.util.Iterator;
045: import java.util.Map;
046: import java.util.TreeMap;
047:
048: /**
049: * Tag Cloud plugin
050: *
051: * @author David Czarnecki
052: * @since blojsom 3.0
053: * @version $Id: TagCloudPlugin.java,v 1.6 2007/01/17 02:35:14 czarneckid Exp $
054: */
055: public class TagCloudPlugin implements Plugin {
056:
057: private Log _logger = LogFactory.getLog(TagCloudPlugin.class);
058:
059: private static final String TAG_QUERY_PARAM = "tq";
060: private static final String BLOJSOM_PLUGIN_TAG_CLOUD_MAP = "BLOJSOM_PLUGIN_TAG_CLOUD_MAP";
061: private static final int MIN_FONTSIZE = 1;
062: private static final int MAX_FONTSIZE = 10;
063:
064: private Fetcher _fetcher;
065:
066: /**
067: * Create a new instance of the tag cloud plugin
068: */
069: public TagCloudPlugin() {
070: }
071:
072: /**
073: * Set the {@link Fetcher}
074: *
075: * @param fetcher {@link Fetcher}
076: */
077: public void setFetcher(Fetcher fetcher) {
078: _fetcher = fetcher;
079: }
080:
081: /**
082: * Initialize this plugin. This method only called when the plugin is instantiated.
083: *
084: * @throws org.blojsom.plugin.PluginException
085: * If there is an error initializing the plugin
086: */
087: public void init() throws PluginException {
088: }
089:
090: /**
091: * Process the blog entries
092: *
093: * @param httpServletRequest Request
094: * @param httpServletResponse Response
095: * @param blog {@link Blog} instance
096: * @param context Context
097: * @param entries Blog entries retrieved for the particular request
098: * @return Modified set of blog entries
099: * @throws PluginException If there is an error processing the blog entries
100: */
101: public Entry[] process(HttpServletRequest httpServletRequest,
102: HttpServletResponse httpServletResponse, Blog blog,
103: Map context, Entry[] entries) throws PluginException {
104: TreeMap tagMap = new TreeMap();
105: String tagQuery = BlojsomUtils.getRequestValue(TAG_QUERY_PARAM,
106: httpServletRequest);
107: Integer maxTagCount = new Integer(1);
108: Entry[] entriesMatchingTag = new Entry[0];
109:
110: if (!BlojsomUtils.checkNullOrBlank(tagQuery)) {
111: try {
112: entriesMatchingTag = _fetcher
113: .findEntriesByMetadataKeyValue(
114: blog,
115: TechnoratiTagsPlugin.METADATA_TECHNORATI_TAGS,
116: tagQuery, true, true);
117: if (_logger.isDebugEnabled()) {
118: _logger.debug("Entries matching tag: "
119: + entriesMatchingTag.length);
120: }
121: } catch (FetcherException e) {
122: if (_logger.isErrorEnabled()) {
123: _logger.error(e);
124: }
125: }
126: }
127:
128: Entry[] entriesForTagMap;
129: try {
130: entriesForTagMap = _fetcher
131: .findEntriesWithMetadataKey(
132: blog,
133: TechnoratiTagsPlugin.METADATA_TECHNORATI_TAGS);
134:
135: for (int i = 0; i < entriesForTagMap.length; i++) {
136: Entry entry = entriesForTagMap[i];
137:
138: if (BlojsomUtils.checkMapForKey(entry.getMetaData(),
139: TechnoratiTagsPlugin.METADATA_TECHNORATI_TAGS)) {
140: String[] tags = BlojsomUtils
141: .parseOnlyCommaList((String) entry
142: .getMetaData()
143: .get(
144: TechnoratiTagsPlugin.METADATA_TECHNORATI_TAGS));
145: String tag;
146: if (tags != null && tags.length > 0) {
147: for (int j = 0; j < tags.length; j++) {
148: tag = tags[j].trim();
149:
150: if (tagMap.containsKey(tag)) {
151: Integer tagCount = (Integer) tagMap
152: .get(tag);
153: tagCount = new Integer(tagCount
154: .intValue() + 1);
155: if (tagCount.intValue() > maxTagCount
156: .intValue()) {
157: maxTagCount = new Integer(tagCount
158: .intValue());
159: }
160:
161: tagMap.put(tag, tagCount);
162: } else {
163: tagMap.put(tag, new Integer(1));
164: }
165: }
166: }
167: }
168: }
169:
170: Iterator tagKeyIterator = tagMap.keySet().iterator();
171: while (tagKeyIterator.hasNext()) {
172: String tag = (String) tagKeyIterator.next();
173: Integer tagCount = (Integer) tagMap.get(tag);
174: int tagRank = rankTagPerEntries(tagCount.intValue(), 1,
175: maxTagCount.intValue());
176:
177: if (_logger.isDebugEnabled()) {
178: _logger.debug("Tag rank for " + tag + " tag: "
179: + tagRank);
180: }
181: tagMap.put(tag, new Integer(tagRank));
182: }
183: } catch (FetcherException e) {
184: if (_logger.isErrorEnabled()) {
185: _logger.error(e);
186: }
187: }
188:
189: context.put(BLOJSOM_PLUGIN_TAG_CLOUD_MAP, tagMap);
190:
191: if (!BlojsomUtils.checkNullOrBlank(tagQuery)
192: && (entriesMatchingTag != null)
193: && (entriesMatchingTag.length > 0)) {
194: return entriesMatchingTag;
195: }
196:
197: return entries;
198: }
199:
200: /**
201: * Calculate a scaled ranking for a given tag count and number of entries
202: *
203: * @param tagCount Total count for a given tag
204: * @param minTagCount Minimum number of tags
205: * @param maxTagCount Maximum number of tags
206: * @return Ranked distribution between 1 and TAG_DISTRIBUTION_MAX
207: */
208: private int rankTagPerEntries(int tagCount, int minTagCount,
209: int maxTagCount) {
210: if (minTagCount == maxTagCount) {
211: return MAX_FONTSIZE;
212: }
213:
214: double scaledCount = (double) (tagCount - minTagCount)
215: / (maxTagCount - minTagCount);
216: double scaledSize = scaledCount * (MAX_FONTSIZE - MIN_FONTSIZE)
217: + MIN_FONTSIZE;
218:
219: return (int) Math.ceil(scaledSize);
220: }
221:
222: /**
223: * Perform any cleanup for the plugin. Called after {@link #process}.
224: *
225: * @throws org.blojsom.plugin.PluginException
226: * If there is an error performing cleanup for this plugin
227: */
228: public void cleanup() throws PluginException {
229: }
230:
231: /**
232: * Called when BlojsomServlet is taken out of service
233: *
234: * @throws org.blojsom.plugin.PluginException
235: * If there is an error in finalizing this plugin
236: */
237: public void destroy() throws PluginException {
238: }
239: }
|