1 18 19 package org.apache.roller.ui.rendering.model; 20 21 import java.util.ArrayList ; 22 import java.util.Calendar ; 23 import java.util.Date ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import org.apache.commons.lang.StringUtils; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.roller.RollerException; 32 import org.apache.roller.model.RefererManager; 33 import org.apache.roller.model.Roller; 34 import org.apache.roller.model.RollerFactory; 35 import org.apache.roller.model.UserManager; 36 import org.apache.roller.model.WeblogManager; 37 import org.apache.roller.pojos.PermissionsData; 38 import org.apache.roller.pojos.Template; 39 import org.apache.roller.pojos.UserData; 40 import org.apache.roller.pojos.WeblogEntryData; 41 import org.apache.roller.pojos.WebsiteData; 42 import org.apache.roller.pojos.wrapper.UserDataWrapper; 43 import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper; 44 import org.apache.roller.pojos.wrapper.WebsiteDataWrapper; 45 import org.apache.roller.ui.rendering.pagers.CommentsPager; 46 import org.apache.roller.ui.rendering.pagers.Pager; 47 import org.apache.roller.ui.rendering.pagers.UsersPager; 48 import org.apache.roller.ui.rendering.pagers.WeblogEntriesListPager; 49 import org.apache.roller.ui.rendering.pagers.WeblogsPager; 50 import org.apache.roller.ui.rendering.util.WeblogPageRequest; 51 import org.apache.roller.ui.rendering.util.WeblogRequest; 52 import org.apache.roller.util.URLUtilities; 53 54 55 58 public class SiteModel implements Model { 59 60 private static Log log = LogFactory.getLog(SiteModel.class); 61 62 private WebsiteData weblog = null; 63 private WeblogRequest weblogRequest = null; 64 private String pageLink = null; 65 private int pageNum = 0; 66 67 68 public String getModelName() { 69 return "site"; 70 } 71 72 public void init(Map initData) throws RollerException { 73 74 this.weblogRequest = (WeblogRequest) initData.get("weblogRequest"); 76 if(this.weblogRequest == null) { 77 throw new RollerException("expected weblogRequest from init data"); 78 } 79 80 if (weblogRequest instanceof WeblogPageRequest) { 81 Template weblogPage = ((WeblogPageRequest)weblogRequest).getWeblogPage(); 82 pageLink = (weblogPage != null) ? weblogPage.getLink() : null; 83 pageNum = ((WeblogPageRequest)weblogRequest).getPageNum(); 84 } 85 86 weblog = weblogRequest.getWeblog(); 88 } 89 90 92 97 public Pager getWeblogEntriesPager(int sinceDays, int length) { 98 99 String pagerUrl = URLUtilities.getWeblogPageURL(weblog, 100 weblogRequest.getLocale(), pageLink, 101 null, null, null, 0, false); 102 103 return new WeblogEntriesListPager( 104 pagerUrl, null, null, null, 105 weblogRequest.getLocale(), 106 sinceDays, 107 pageNum, 108 length); 109 } 110 111 112 119 public Pager getWeblogEntriesPager(WebsiteData queryWeblog, int sinceDays, int length) { 120 return getWeblogEntriesPager(queryWeblog, null, null, sinceDays, length); 121 } 122 123 131 public Pager getWeblogEntriesPager(WebsiteData queryWeblog, UserData user, int sinceDays, int length) { 132 return getWeblogEntriesPager(queryWeblog, user, null, sinceDays, length); 133 } 134 135 144 public Pager getWeblogEntriesPager(WebsiteData queryWeblog, UserData user, String cat, int sinceDays, int length) { 145 146 String pagerUrl = URLUtilities.getWeblogPageURL(weblog, 147 weblogRequest.getLocale(), pageLink, 148 null, null, null, 0, false); 149 150 return new WeblogEntriesListPager( 151 pagerUrl, queryWeblog, user, cat, 152 weblogRequest.getLocale(), 153 sinceDays, 154 pageNum, 155 length); 156 } 157 158 159 165 public Pager getCommentsPager(int sinceDays, int length) { 166 167 String pagerUrl = URLUtilities.getWeblogPageURL(weblog, 168 weblogRequest.getLocale(), pageLink, 169 null, null, null, 0, false); 170 171 return new CommentsPager( 172 pagerUrl, 173 weblogRequest.getLocale(), 174 sinceDays, 175 pageNum, 176 length); 177 } 178 179 180 181 public Pager getUsersByLetterPager(String letter, int sinceDays, int length) { 182 183 String pagerUrl = URLUtilities.getWeblogPageURL(weblog, 184 weblogRequest.getLocale(), pageLink, 185 null, null, null, 0, false); 186 187 if(letter != null && StringUtils.isEmpty(letter)) { 188 letter = null; 189 } 190 191 return new UsersPager( 192 pagerUrl, 193 letter, 194 weblogRequest.getLocale(), 195 sinceDays, 196 pageNum, 197 length); 198 } 199 200 201 202 public Pager getWeblogsByLetterPager(String letter, int sinceDays, int length) { 203 204 String pagerUrl = URLUtilities.getWeblogPageURL(weblog, 205 weblogRequest.getLocale(), pageLink, 206 null, null, null, 0, false); 207 208 if(letter != null && StringUtils.isEmpty(letter)) { 209 letter = null; 210 } 211 212 return new WeblogsPager( 213 pagerUrl, 214 letter, 215 weblogRequest.getLocale(), 216 sinceDays, 217 pageNum, 218 length); 219 } 220 221 223 228 public Map getUserNameLetterMap() { 229 Map results = new HashMap (); 230 try { 231 Roller roller = RollerFactory.getRoller(); 232 UserManager umgr = roller.getUserManager(); 233 results = umgr.getUserNameLetterMap(); 234 } catch (Exception e) { 235 log.error("ERROR: fetching username letter map", e); 236 } 237 return results; 238 } 239 240 241 246 public Map getWeblogHandleLetterMap() { 247 Map results = new HashMap (); 248 try { 249 Roller roller = RollerFactory.getRoller(); 250 UserManager umgr = roller.getUserManager(); 251 results = umgr.getWeblogHandleLetterMap(); 252 } catch (Exception e) { 253 log.error("ERROR: fetching weblog handle letter map", e); 254 } 255 return results; 256 } 257 258 259 262 public List getUsersWeblogs(String userName) { 263 List results = new ArrayList (); 264 try { 265 Roller roller = RollerFactory.getRoller(); 266 UserManager umgr = roller.getUserManager(); 267 UserData user = umgr.getUserByUserName(userName); 268 List perms = umgr.getAllPermissions(user); 269 for (Iterator it = perms.iterator(); it.hasNext();) { 270 PermissionsData perm = (PermissionsData) it.next(); 271 results.add(WebsiteDataWrapper.wrap(perm.getWebsite())); 272 } 273 } catch (Exception e) { 274 log.error("ERROR: fetching weblog list", e); 275 } 276 return results; 277 } 278 279 280 283 public List getWeblogsUsers(String handle) { 284 List results = new ArrayList (); 285 try { 286 Roller roller = RollerFactory.getRoller(); 287 UserManager umgr = roller.getUserManager(); 288 WebsiteData website = umgr.getWebsiteByHandle(handle); 289 List perms = umgr.getAllPermissions(website); 290 for (Iterator it = perms.iterator(); it.hasNext();) { 291 PermissionsData perm = (PermissionsData) it.next(); 292 results.add(UserDataWrapper.wrap(perm.getUser())); 293 } 294 } catch (Exception e) { 295 log.error("ERROR: fetching weblog list", e); 296 } 297 return results; 298 } 299 300 301 302 303 public UserDataWrapper getUser(String username) { 304 UserDataWrapper wrappedUser = null; 305 try { 306 Roller roller = RollerFactory.getRoller(); 307 UserManager umgr = roller.getUserManager(); 308 UserData user = umgr.getUserByUserName(username, Boolean.TRUE); 309 wrappedUser = UserDataWrapper.wrap(user); 310 } catch (Exception e) { 311 log.error("ERROR: fetching users by letter", e); 312 } 313 return wrappedUser; 314 } 315 316 317 318 public WebsiteDataWrapper getWeblog(String handle) { 319 WebsiteDataWrapper wrappedWebsite = null; 320 try { 321 Roller roller = RollerFactory.getRoller(); 322 UserManager umgr = roller.getUserManager(); 323 WebsiteData website = umgr.getWebsiteByHandle(handle); 324 wrappedWebsite = WebsiteDataWrapper.wrap(website); 325 } catch (Exception e) { 326 log.error("ERROR: fetching users by letter", e); 327 } 328 return wrappedWebsite; 329 } 330 331 332 334 340 public List getNewWeblogs(int sinceDays, int length) { 341 List results = new ArrayList (); 342 Calendar cal = Calendar.getInstance(); 343 cal.setTime(new Date ()); 344 cal.add(Calendar.DATE, -1 * sinceDays); 345 Date startDate = cal.getTime(); 346 try { 347 Roller roller = RollerFactory.getRoller(); 348 UserManager umgr = roller.getUserManager(); 349 List weblogs = umgr.getWebsites( 350 null, Boolean.TRUE, Boolean.TRUE, startDate, null, 0, length); 351 for (Iterator it = weblogs.iterator(); it.hasNext();) { 352 WebsiteData website = (WebsiteData) it.next(); 353 results.add(WebsiteDataWrapper.wrap(website)); 354 } 355 } catch (Exception e) { 356 log.error("ERROR: fetching weblog list", e); 357 } 358 return results; 359 } 360 361 362 367 public List getNewUsers(int sinceDays, int length) { 368 List results = new ArrayList (); 369 try { 370 Roller roller = RollerFactory.getRoller(); 371 UserManager umgr = roller.getUserManager(); 372 List users = umgr.getUsers(0, length); 373 for (Iterator it = users.iterator(); it.hasNext();) { 374 UserData user = (UserData) it.next(); 375 results.add(UserDataWrapper.wrap(user)); 376 } 377 } catch (Exception e) { 378 log.error("ERROR: fetching weblog list", e); 379 } 380 return results; 381 } 382 383 384 389 public List getHotWeblogs(int sinceDays, int length) { 390 List results = new ArrayList (); 391 try { 392 Roller roller = RollerFactory.getRoller(); 393 RefererManager rmgr = roller.getRefererManager(); 394 results = rmgr.getHotWeblogs(sinceDays, 0, length); 395 } catch (Exception e) { 396 log.error("ERROR: fetching hot weblog list", e); 397 } 398 return results; 399 } 400 401 402 409 public List getMostCommentedWeblogs(int sinceDays , int length) { 410 List results = new ArrayList (); 411 Calendar cal = Calendar.getInstance(); 412 cal.setTime(new Date ()); 413 cal.add(Calendar.DATE, -1 * sinceDays); 414 Date startDate = cal.getTime(); 415 try { 416 Roller roller = RollerFactory.getRoller(); 417 UserManager umgr = roller.getUserManager(); 418 results = umgr.getMostCommentedWebsites( 419 startDate, new Date (), 0, length); 420 } catch (Exception e) { 421 log.error("ERROR: fetching commented weblog list", e); 422 } 423 return results; 424 } 425 426 427 434 public List getMostCommentedWeblogEntries( 435 List cats, int sinceDays, int length) { 436 List results = new ArrayList (); 437 Calendar cal = Calendar.getInstance(); 438 cal.setTime(new Date ()); 439 cal.add(Calendar.DATE, -1 * sinceDays); 440 Date startDate = cal.getTime(); 441 try { 442 Roller roller = RollerFactory.getRoller(); 443 WeblogManager wmgr = roller.getWeblogManager(); 444 results = wmgr.getMostCommentedWeblogEntries( 445 null, startDate, new Date (), 0, length); 446 } catch (Exception e) { 447 log.error("ERROR: fetching commented weblog entries list", e); 448 } 449 return results; 450 } 451 452 457 public List getPinnedWeblogEntries(int length) { 458 List results = new ArrayList (); 459 try { 460 Roller roller = RollerFactory.getRoller(); 461 WeblogManager wmgr = roller.getWeblogManager(); 462 List weblogs = wmgr.getWeblogEntriesPinnedToMain(new Integer (length)); 463 for (Iterator it = weblogs.iterator(); it.hasNext();) { 464 WeblogEntryData entry = (WeblogEntryData) it.next(); 465 results.add(WeblogEntryDataWrapper.wrap(entry)); 466 } 467 } catch (Exception e) { 468 log.error("ERROR: fetching pinned weblog entries", e); 469 } 470 return results; 471 } 472 } 473 | Popular Tags |