1 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 ; 44 import javax.servlet.http.HttpServletResponse ; 45 import java.util.Iterator ; 46 import java.util.Map ; 47 import java.util.TreeMap ; 48 49 56 public class TagCloudPlugin implements Plugin { 57 58 private Log _logger = LogFactory.getLog(TagCloudPlugin.class); 59 60 private static final String TAG_QUERY_PARAM = "tq"; 61 private static final String 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 70 public TagCloudPlugin() { 71 } 72 73 78 public void setFetcher(Fetcher fetcher) { 79 _fetcher = fetcher; 80 } 81 82 88 public void init() throws PluginException { 89 } 90 91 102 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 103 TreeMap tagMap = new TreeMap (); 104 String tagQuery = BlojsomUtils.getRequestValue(TAG_QUERY_PARAM, httpServletRequest); 105 Integer maxTagCount = new Integer (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 [] tags = BlojsomUtils.parseOnlyCommaList((String ) entry.getMetaData().get(TechnoratiTagsPlugin.METADATA_TECHNORATI_TAGS)); 132 String 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 tagCount = (Integer ) tagMap.get(tag); 139 tagCount = new Integer (tagCount.intValue() + 1); 140 if (tagCount.intValue() > maxTagCount.intValue()) { 141 maxTagCount = new Integer (tagCount.intValue()); 142 } 143 144 tagMap.put(tag, tagCount); 145 } else { 146 tagMap.put(tag, new Integer (1)); 147 } 148 } 149 } 150 } 151 } 152 153 Iterator tagKeyIterator = tagMap.keySet().iterator(); 154 while (tagKeyIterator.hasNext()) { 155 String tag = (String ) tagKeyIterator.next(); 156 Integer tagCount = (Integer ) 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 (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 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 204 public void cleanup() throws PluginException { 205 } 206 207 213 public void destroy() throws PluginException { 214 } 215 } | Popular Tags |