1 4 package org.roller.business; 5 6 import org.apache.commons.collections.comparators.ReverseComparator; 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 import org.roller.RollerException; 10 import org.roller.model.Roller; 11 import org.roller.model.WeblogManager; 12 import org.roller.pojos.CommentData; 13 import org.roller.pojos.UserData; 14 import org.roller.pojos.WeblogCategoryAssoc; 15 import org.roller.pojos.WeblogCategoryData; 16 import org.roller.pojos.WeblogEntryData; 17 import org.roller.pojos.WebsiteData; 18 import org.roller.util.DateUtil; 19 import org.roller.util.Utilities; 20 21 import java.text.SimpleDateFormat ; 22 import java.util.ArrayList ; 23 import java.util.Calendar ; 24 import java.util.Comparator ; 25 import java.util.Date ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.TreeMap ; 30 import org.roller.model.RollerFactory; 31 32 37 public abstract class WeblogManagerImpl implements WeblogManager 38 { 39 private static Log mLogger = 40 LogFactory.getFactory().getInstance(WeblogManagerImpl.class); 41 42 protected PersistenceStrategy mStrategy; 43 44 45 private Comparator reverseComparator = new ReverseComparator(); 46 55 56 private SimpleDateFormat formatter = DateUtil.get8charDateFormat(); 57 58 public abstract List getWeblogEntries( 59 WebsiteData website, 60 Date startDate, 61 Date endDate, 62 String catName, 63 String status, 64 Integer maxEntries, 65 Boolean pinned) throws RollerException; 66 67 public abstract List getNextPrevEntries( 68 WeblogEntryData current, 69 String catName, 70 int maxEntries, 71 boolean next) throws RollerException; 72 73 public WeblogManagerImpl(PersistenceStrategy strategy) 74 { 75 mStrategy = strategy; 76 } 77 78 public void release() 79 { 80 } 81 82 84 87 public WeblogCategoryData createWeblogCategory() 88 { 89 return new WeblogCategoryData(); 90 } 91 92 97 public WeblogCategoryData createWeblogCategory( 98 WebsiteData website, 99 WeblogCategoryData parent, 100 String name, 101 String description, 102 String image) throws RollerException 103 { 104 return new WeblogCategoryData( 105 null, website, parent, name, description, image); 106 } 107 108 public WeblogCategoryData retrieveWeblogCategory(String id) 109 throws RollerException 110 { 111 return (WeblogCategoryData) mStrategy.load( 112 id, 113 WeblogCategoryData.class); 114 } 115 116 118 public WeblogCategoryData getWeblogCategoryByPath( 119 WebsiteData website, String categoryPath) throws RollerException 120 { 121 return getWeblogCategoryByPath(website, null, categoryPath); 122 } 123 124 public String getPath(WeblogCategoryData category) throws RollerException 125 { 126 if (null == category.getParent()) 127 { 128 return "/"; 129 } 130 else 131 { 132 String parentPath = getPath(category.getParent()); 133 parentPath = "/".equals(parentPath) ? "" : parentPath; 134 return parentPath + "/" + category.getName(); 135 } 136 } 137 138 public WeblogCategoryData getWeblogCategoryByPath( 139 WebsiteData website, WeblogCategoryData category, String path) 140 throws RollerException 141 { 142 final Iterator cats; 143 final String [] pathArray = Utilities.stringToStringArray(path, "/"); 144 145 if (category == null && (null == path || "".equals(path.trim()))) 146 { 147 throw new RollerException("Bad arguments."); 148 } 149 150 if (path.trim().equals("/")) 151 { 152 return getRootWeblogCategory(website); 153 } 154 else if (category == null || path.trim().startsWith("/")) 155 { 156 cats = getRootWeblogCategory(website).getWeblogCategories().iterator(); 157 } 158 else 159 { 160 cats = category.getWeblogCategories().iterator(); 161 } 162 163 while (cats.hasNext()) 164 { 165 WeblogCategoryData possibleMatch = (WeblogCategoryData)cats.next(); 166 if (possibleMatch.getName().equals(pathArray[0])) 167 { 168 if (pathArray.length == 1) 169 { 170 return possibleMatch; 171 } 172 else 173 { 174 String [] subpath = new String [pathArray.length - 1]; 175 System.arraycopy(pathArray, 1, subpath, 0, subpath.length); 176 177 String pathString= Utilities.stringArrayToString(subpath,"/"); 178 return getWeblogCategoryByPath(website, possibleMatch, pathString); 179 } 180 } 181 } 182 183 return null; 185 } 186 187 189 public WeblogCategoryAssoc createWeblogCategoryAssoc() 190 { 191 return new WeblogCategoryAssoc(); 192 } 193 194 public WeblogCategoryAssoc createWeblogCategoryAssoc( 195 WeblogCategoryData category, 196 WeblogCategoryData ancestor, 197 String relation) throws RollerException 198 { 199 return new WeblogCategoryAssoc(null, category, ancestor, relation); 200 } 201 202 public WeblogCategoryAssoc retrieveWeblogCategoryAssoc(String id) throws RollerException 203 { 204 return (WeblogCategoryAssoc)mStrategy.load(id, WeblogCategoryAssoc.class); 205 } 206 207 209 public void removeComment(String id) throws RollerException 210 { 211 mStrategy.remove(id, CommentData.class); 212 } 213 214 public void removeComments(String [] ids) throws RollerException 215 { 216 for (int i = 0; i < ids.length; i++) 217 { 218 removeComment(ids[i]); 219 } 220 } 221 222 public void removeCommentsForEntry(String entryId) throws RollerException 223 { 224 List comments = getComments(entryId, false); Iterator it = comments.iterator(); 226 while (it.hasNext()) 227 { 228 removeComment( ((CommentData)it.next()).getId() ); 229 } 230 } 231 232 234 public CommentData retrieveComment(String id) throws RollerException 235 { 236 return (CommentData) mStrategy.load(id, CommentData.class); 237 } 238 239 public List getComments(String entryId) throws RollerException 240 { 241 return getComments(entryId, true); 242 } 243 244 246 public WeblogEntryData retrieveWeblogEntry(String id) 247 throws RollerException 248 { 249 return (WeblogEntryData) mStrategy.load( 250 id, WeblogEntryData.class); 251 } 252 253 public void removeWeblogEntry(String id) throws RollerException 254 { 255 Roller mRoller = RollerFactory.getRoller(); 256 mRoller.getRefererManager().removeReferersForEntry(id); 257 removeCommentsForEntry( id ); 258 mStrategy.remove(id, WeblogEntryData.class); 259 } 260 261 263 271 public Date getWeblogLastPublishTime(String userName) 272 throws RollerException 273 { 274 return getWeblogLastPublishTime(userName, null); 275 } 276 277 279 289 public List getWeblogEntries( 290 WebsiteData website, 291 Date startDate, 292 Date endDate, 293 String catName, 294 String status, 295 Integer maxEntries) throws RollerException 296 { 297 return getWeblogEntries( 298 website, 299 startDate, 300 endDate, 301 catName, 302 status, 303 maxEntries, 304 null); 305 } 306 307 318 public List getWeblogEntries( 319 WebsiteData website, 320 Date startDate, 321 Date endDate, 322 String catName, 323 String status, 324 int offset, 325 int range) throws RollerException 326 { 327 List filtered = new ArrayList (); 328 List entries = getWeblogEntries( 329 website, 330 startDate, 331 endDate, 332 catName, 333 status, 334 new Integer (offset + range), 335 null); 336 if (entries.size() < offset) 337 { 338 return filtered; 339 } 340 for (int i=offset; i<entries.size(); i++) 341 { 342 filtered.add(entries.get(i)); 343 } 344 return filtered; 345 } 346 347 356 public Map getWeblogEntryObjectMap( 357 WebsiteData website, 358 Date startDate, 359 Date endDate, 360 String catName, 361 String status, 362 Integer maxEntries) throws RollerException 363 { 364 return getWeblogEntryMap( 365 website, 366 startDate, 367 endDate, 368 catName, 369 status, 370 maxEntries, 371 false); 372 } 373 374 383 public Map getWeblogEntryStringMap( 384 WebsiteData website, 385 Date startDate, 386 Date endDate, 387 String catName, 388 String status, 389 Integer maxEntries) throws RollerException 390 { 391 return getWeblogEntryMap( 392 website, 393 startDate, 394 endDate, 395 catName, 396 status, 397 maxEntries, 398 true); 399 } 400 401 private Map getWeblogEntryMap( 402 WebsiteData website, 403 Date startDate, 404 Date endDate, 405 String catName, 406 String status, 407 Integer maxEntries, 408 boolean stringsOnly) throws RollerException 409 { 410 TreeMap map = new TreeMap (reverseComparator); 411 412 List entries = getWeblogEntries( 413 website, 414 startDate, 415 endDate, 416 catName, 417 status, 418 maxEntries); 419 420 Calendar cal = Calendar.getInstance(); 421 if (website != null) 422 { 423 cal.setTimeZone(website.getTimeZoneInstance()); 424 } 425 426 for (Iterator wbItr = entries.iterator(); wbItr.hasNext();) 427 { 428 WeblogEntryData entry = (WeblogEntryData) wbItr.next(); 429 Date sDate = DateUtil.getNoonOfDay(entry.getPubTime(), cal); 430 if (stringsOnly) 431 { 432 if (map.get(sDate) == null) 433 map.put(sDate, formatter.format(sDate)); 434 } 435 else 436 { 437 List dayEntries = (List ) map.get(sDate); 438 if (dayEntries == null) 439 { 440 dayEntries = new ArrayList (); 441 map.put(sDate, dayEntries); 442 } 443 dayEntries.add(entry); 444 } 445 } 446 return map; 447 } 448 449 452 public List getNextEntries( 453 WeblogEntryData current, String catName, int maxEntries) 454 throws RollerException 455 { 456 return getNextPrevEntries(current, catName, maxEntries, true); 457 } 458 459 462 public List getPreviousEntries( 463 WeblogEntryData current, String catName, int maxEntries) 464 throws RollerException 465 { 466 return getNextPrevEntries(current, catName, maxEntries, false); 467 } 468 469 public WeblogEntryData getNextEntry(WeblogEntryData current, String catName) 470 throws RollerException 471 { 472 WeblogEntryData entry = null; 473 List entryList = getNextEntries(current, catName, 1); 474 if (entryList != null && entryList.size() > 0) 475 { 476 entry = (WeblogEntryData)entryList.get(entryList.size()-1); 477 } 478 return entry; 479 } 480 481 public WeblogEntryData getPreviousEntry(WeblogEntryData current, String catName) 482 throws RollerException 483 { 484 WeblogEntryData entry = null; 485 List entryList = getPreviousEntries(current, catName, 1); 486 if (entryList != null && entryList.size() > 0) 487 { 488 entry = (WeblogEntryData)entryList.get(0); 489 } 490 return entry; 491 } 492 493 496 public List getWeblogEntriesPinnedToMain(Integer max) throws RollerException 497 { 498 return getWeblogEntries( 499 null, null, new Date (), null, null, max, Boolean.TRUE); 500 } 501 502 506 public String getUrl(UserData user, String contextUrl) 507 { 508 String url = 509 Utilities.escapeHTML(contextUrl + "/page/" + user.getUserName()); 510 return url; 511 } 512 513 } 514 | Popular Tags |