1 11 package org.jboss.portlet.forums.helper; 12 13 import java.lang.reflect.Field ; 14 import java.lang.reflect.Method ; 15 import java.lang.reflect.Modifier ; 16 import java.sql.Connection ; 17 import java.sql.PreparedStatement ; 18 import java.sql.ResultSet ; 19 import java.sql.SQLException ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import javax.ejb.EJBLocalHome ; 26 import javax.ejb.EJBLocalObject ; 27 28 import org.apache.log4j.Logger; 29 import org.apache.lucene.analysis.standard.StandardAnalyzer; 30 import org.apache.lucene.queryParser.ParseException; 31 import org.apache.lucene.queryParser.QueryParser; 32 import org.apache.lucene.search.BooleanQuery; 33 import org.apache.lucene.search.Query; 34 import org.jboss.portal.common.util.Tools; 35 import org.jboss.portlet.forums.ForumsConstants; 36 import org.jboss.portlet.forums.model.Category; 37 import org.jboss.portlet.forums.model.Forum; 38 39 43 public class ForumsTools 44 { 45 private static final Logger log = Logger.getLogger(ForumsTools.class); 46 private static final StandardAnalyzer analyzer = new StandardAnalyzer(); 47 48 56 public static void radioButton(StringBuffer buffer, 57 String name, 58 String value, 59 boolean selected) 60 { 61 buffer.append("<input type=\"radio\" name=\"").append(name).append("\" value=\"").append(value).append("\"") 62 .append(selected ? " checked=\"checked\"/>" : "/>"); 63 } 64 65 76 public static String createImageLink(String url, 77 String target, 78 String image, 79 String title, 80 String alt) 81 { 82 return "<a HREF=\"" + url + "\" target=\"" + target + "\"><img SRC=\"" + image + "\" " + "alt=\"" + alt 83 + "\" title=\"" + title + "\" border=\"0\"></a>"; 84 } 85 86 96 public static String createImageLink(String url, 97 String image, 98 String title, 99 String alt) 100 { 101 return "<a HREF=\"" + url + "\"><img SRC=\"" + image + "\" " + "alt=\"" + alt + "\" title=\"" + title 102 + "\" border=\"0\"></a>"; 103 } 104 105 114 public static String createImageLink(String url, 115 String image, 116 String title) 117 { 118 return createImageLink(url, image, title, title); 119 } 120 121 129 public static String createLink(String url, 130 String text) 131 { 132 return "<a HREF=\"" + url + "\">" + text + "</a>"; 133 } 134 135 144 public static String createLink(String url, 145 String target, 146 String text) 147 { 148 return "<a HREF=\"" + url + "\" target=\"" + target + "\">" + text + "</a>"; 149 } 150 151 160 public static Query createQuery(String text, 161 String field, 162 boolean isOr) 163 { 164 try 165 { 166 QueryParser parser = new QueryParser(field, analyzer); 167 parser.setOperator(isOr ? QueryParser.DEFAULT_OPERATOR_OR : QueryParser.DEFAULT_OPERATOR_AND); 168 return parser.parse(text); 169 } 170 catch (ParseException e) 171 { 172 log.error("Cannot parse query " + text, e); 173 return DUMMY_QUERY; 174 } 175 } 176 177 private static final Query DUMMY_QUERY = new BooleanQuery(); 178 179 187 public static String listForums(List forums, 188 int selectedId) 189 { 190 Iterator result = forums.iterator(); 191 192 StringBuffer buffer = new StringBuffer (); 194 195 while (result.hasNext()) 197 { 198 Forum forum = (Forum) result.next(); 199 buffer.append("<option value=\"").append(forum.getID()).append("\"") 200 .append((selectedId == forum.getID().intValue()) ? " selected=\"selected\"" : "").append(">") 201 .append(forum.getName()).append("</option>\n"); 202 } 203 204 return buffer.toString(); 205 } 206 207 215 public static String listCategories(List categories, 216 int selectedId) 217 { 218 Iterator result = categories.iterator(); 219 220 StringBuffer buffer = new StringBuffer (); 222 223 while (result.hasNext()) 225 { 226 Category category = (Category) result.next(); 227 buffer.append("<option value=\"").append(category.getID()).append("\"") 228 .append((selectedId == category.getID().intValue()) ? " selected=\"selected\"" : "").append(">") 229 .append(category.getTitle()).append("</option>\n"); 230 } 231 232 return buffer.toString(); 233 } 234 235 243 public static String selectDays(String name, 244 int days) 245 { 246 StringBuffer selectDays = new StringBuffer ("<select name=\"").append(name).append("\">"); 247 for (int i = 0; i < ForumsConstants.PREVIOUS_DAYS.length; i++) 248 { 249 selectDays.append("<option value=\"").append(ForumsConstants.PREVIOUS_DAYS[i]).append("\"") 250 .append((days == ForumsConstants.PREVIOUS_DAYS[i]) ? " selected=\"selected\"" : "").append(">") 251 .append(ForumsConstants.PREVIOUS_DAYS_TEXT_TOPICS[i]).append("</option>"); 252 } 253 254 selectDays.append("</select>"); 255 return selectDays.toString(); 256 } 257 258 261 public static void batchUpdate(Connection conn, 262 String updateQuery, 263 String selectQuery, 264 int size) 265 throws SQLException 266 { 267 PreparedStatement updater = null; 268 PreparedStatement selecter = null; 269 ResultSet rs = null; 270 try 271 { 272 updater = conn.prepareStatement(updateQuery); 273 274 selecter = conn.prepareStatement(selectQuery); 277 rs = selecter.executeQuery(); 278 while (rs.next()) 279 { 280 for (int i = 1; i <= size; i++) 281 { 282 updater.setObject(i, 283 rs.getObject(i)); 284 } 285 286 updater.executeUpdate(); 287 } 288 } 289 finally 290 { 291 if (!conn.getAutoCommit()) 292 { 293 conn.commit(); 294 } 295 296 Tools.safeClose(rs); 297 Tools.safeClose(updater); 298 Tools.safeClose(selecter); 299 } 300 } 301 302 329 338 public static String pageNumber(int start, 339 int size, 340 int per) 341 { 342 return "${bb.Page_of_0}" + (int) Math.ceil((double) start / (double) (per)) + "${bb.Page_of_1}" 343 + (int) Math.ceil((double) size / (double) per) + "${bb.Page_of_2}"; 344 } 345 346 354 public static String selectEJBs(EJBLocalHome home, 355 Method getter, 356 Object key, 357 boolean all) 358 { 359 try 360 { 361 Object [] empty = new Object [0]; 363 364 Method findAll = home.getClass().getMethod("findAll", 366 new Class [0]); 367 Iterator result = ((Collection ) findAll.invoke(home, empty)).iterator(); 368 369 StringBuffer buffer = new StringBuffer (); 371 372 while (result.hasNext()) 374 { 375 EJBLocalObject ejb = (EJBLocalObject ) result.next(); 376 Object ejbKey = ejb.getPrimaryKey(); 377 boolean equals = ejbKey.equals(key); 378 if (all || equals) 379 { 380 buffer.append("<option value=\"").append(ejbKey).append("\"") 381 .append(equals ? " selected=\"selected\"" : "").append(">").append(getter.invoke(ejb, empty)) 382 .append("</option>\n"); 383 } 384 } 385 386 return buffer.toString(); 388 } 389 catch (Exception e) 390 { 391 log.error("Cannot list ejbs", e); 392 return ""; 393 } 394 } 395 396 404 public static String [][] collectFields(Class clazz, 405 String prefix) 406 { 407 Field [] fields = clazz.getDeclaredFields(); 408 List values = new ArrayList (fields.length); 409 for (int i = 0; i < fields.length; i++) 410 { 411 Field field = fields[i]; 412 int modifier = field.getModifiers(); 413 if (field.getName().startsWith(prefix) 414 && Modifier.isFinal(modifier) 415 && Modifier.isPublic(modifier) 416 && Modifier.isStatic(modifier)) 417 { 418 try 419 { 420 values.add(new String [] 421 { 422 field.getName(), 423 (String ) field.get(null) 424 }); 425 } 426 catch (Exception ignore) 427 { 428 Logger.getLogger(clazz).error("", ignore); 429 } 430 } 431 } 432 433 return (String [][]) values.toArray(new String [values.size()][]); 434 } 435 436 443 public static Object head(Collection c) 444 { 445 Iterator iterator = c.iterator(); 446 return iterator.hasNext() ? iterator.next() : null; 447 } 448 449 457 public static final String computeCacheKey(boolean format_1_00, 458 String forumId) 459 { 460 return (format_1_00 ? "rss/1.00" : "rss/0.91") + ((forumId != null) ? ("/" + forumId) : ""); 461 } 462 463 470 public static boolean isNullOrBlank(String s) 471 { 472 if (s != null) 473 { 474 for (int i = 0; i < s.length(); i++) 475 { 476 if (!Character.isWhitespace(s.charAt(i))) 477 { 478 return false; 479 } 480 } 481 } 482 483 return true; 484 } 485 } | Popular Tags |