001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.model;
020:
021: import java.util.ArrayList;
022: import java.util.Calendar;
023: import java.util.Date;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import org.apache.commons.lang.StringUtils;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.roller.RollerException;
032: import org.apache.roller.business.Roller;
033: import org.apache.roller.business.RollerFactory;
034: import org.apache.roller.business.UserManager;
035: import org.apache.roller.business.WeblogManager;
036: import org.apache.roller.pojos.HitCountData;
037: import org.apache.roller.pojos.PermissionsData;
038: import org.apache.roller.pojos.StatCount;
039: import org.apache.roller.pojos.Template;
040: import org.apache.roller.pojos.UserData;
041: import org.apache.roller.pojos.WeblogEntryData;
042: import org.apache.roller.pojos.WebsiteData;
043: import org.apache.roller.pojos.wrapper.UserDataWrapper;
044: import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
045: import org.apache.roller.pojos.wrapper.WebsiteDataWrapper;
046: import org.apache.roller.ui.rendering.pagers.CommentsPager;
047: import org.apache.roller.ui.rendering.pagers.Pager;
048: import org.apache.roller.ui.rendering.pagers.UsersPager;
049: import org.apache.roller.ui.rendering.pagers.WeblogEntriesListPager;
050: import org.apache.roller.ui.rendering.pagers.WeblogsPager;
051: import org.apache.roller.ui.rendering.util.WeblogFeedRequest;
052: import org.apache.roller.ui.rendering.util.WeblogPageRequest;
053: import org.apache.roller.ui.rendering.util.WeblogRequest;
054: import org.apache.roller.util.URLUtilities;
055:
056: /**
057: * Page model that provides access to site-wide users, weblogs and entries.
058: */
059: public class SiteModel implements Model {
060:
061: private static Log log = LogFactory.getLog(SiteModel.class);
062:
063: private WebsiteData weblog = null;
064: private WeblogRequest weblogRequest = null;
065: private List tags = new ArrayList();
066: private String pageLink = null;
067: private int pageNum = 0;
068:
069: public String getModelName() {
070: return "site";
071: }
072:
073: public void init(Map initData) throws RollerException {
074:
075: // we expect the init data to contain a weblogRequest object
076: this .weblogRequest = (WeblogRequest) initData
077: .get("weblogRequest");
078: if (this .weblogRequest == null) {
079: throw new RollerException(
080: "expected weblogRequest from init data");
081: }
082:
083: if (weblogRequest instanceof WeblogPageRequest) {
084: Template weblogPage = ((WeblogPageRequest) weblogRequest)
085: .getWeblogPage();
086: pageLink = (weblogPage != null) ? weblogPage.getLink()
087: : null;
088: pageNum = ((WeblogPageRequest) weblogRequest).getPageNum();
089: tags = ((WeblogPageRequest) weblogRequest).getTags();
090: } else if (weblogRequest instanceof WeblogFeedRequest) {
091: tags = ((WeblogFeedRequest) weblogRequest).getTags();
092: }
093:
094: // extract weblog object
095: weblog = weblogRequest.getWeblog();
096: }
097:
098: //----------------------------------------------------------------- Pagers
099:
100: /**
101: * Get pager ofWeblogEntry objects across all weblogs, in reverse chrono order by pubTime.
102: * @param sinceDays Limit to past X days in past (or -1 for no limit)
103: * @param length Max number of results to return
104: */
105: public Pager getWeblogEntriesPager(int sinceDays, int length) {
106:
107: String pagerUrl = URLUtilities.getWeblogPageURL(weblog,
108: weblogRequest.getLocale(), pageLink, null, null, null,
109: tags, 0, false);
110:
111: return new WeblogEntriesListPager(pagerUrl, null, null, null,
112: tags, weblogRequest.getLocale(), sinceDays, pageNum,
113: length);
114: }
115:
116: /**
117: * Get pager ofWeblogEntry objects across all weblogs,
118: * in reverse chrono order by pubTime.
119: * @param queryWeblog Restrict to this weblog
120: * @param sinceDays Limit to past X days in past (or -1 for no limit)
121: * @param length Max number of results to return
122: */
123: public Pager getWeblogEntriesPager(WebsiteData queryWeblog,
124: int sinceDays, int length) {
125: return getWeblogEntriesPager(queryWeblog, null, null,
126: sinceDays, length);
127: }
128:
129: /**
130: * Get pager ofWeblogEntry objects across all weblogs,
131: * in reverse chrono order by pubTime.
132: * @param queryWeblog Restrict to this weblog
133: * @param user Restrict to this user
134: * @param sinceDays Limit to past X days in past (or -1 for no limit)
135: * @param length Max number of results to return
136: */
137: public Pager getWeblogEntriesPager(WebsiteData queryWeblog,
138: UserData user, int sinceDays, int length) {
139: return getWeblogEntriesPager(queryWeblog, user, null,
140: sinceDays, length);
141: }
142:
143: /**
144: * Get pager ofWeblogEntry objects across all weblogs,
145: * in reverse chrono order by pubTime.
146: * @param queryWeblog Restrict to this weblog
147: * @param user Restrict to this user
148: * @param cat Restrict to this category
149: * @param sinceDays Limit to past X days in past (or -1 for no limit)
150: * @param length Max number of results to return
151: */
152: public Pager getWeblogEntriesPager(WebsiteData queryWeblog,
153: UserData user, String cat, int sinceDays, int length) {
154:
155: String pagerUrl = URLUtilities.getWeblogPageURL(weblog,
156: weblogRequest.getLocale(), pageLink, null, null, null,
157: tags, 0, false);
158:
159: return new WeblogEntriesListPager(pagerUrl, queryWeblog, user,
160: cat, tags, weblogRequest.getLocale(), sinceDays,
161: pageNum, length);
162: }
163:
164: /*
165: * Get pager of most recent Comment objects across all weblogs,
166: * in reverse chrono order by postTime.
167: * @param offset Offset into results (for paging)
168: * @param len Max number of results to return
169: */
170: public Pager getCommentsPager(int sinceDays, int length) {
171:
172: String pagerUrl = URLUtilities.getWeblogPageURL(weblog,
173: weblogRequest.getLocale(), pageLink, null, null, null,
174: null, 0, false);
175:
176: return new CommentsPager(pagerUrl, weblogRequest.getLocale(),
177: sinceDays, pageNum, length);
178: }
179:
180: /* Get pager of users whose names begin with specified letter */
181: public Pager getUsersByLetterPager(String letter, int sinceDays,
182: int length) {
183:
184: String pagerUrl = URLUtilities.getWeblogPageURL(weblog,
185: weblogRequest.getLocale(), pageLink, null, null, null,
186: null, 0, false);
187:
188: if (letter != null && StringUtils.isEmpty(letter)) {
189: letter = null;
190: }
191:
192: return new UsersPager(pagerUrl, letter, weblogRequest
193: .getLocale(), sinceDays, pageNum, length);
194: }
195:
196: /** Get pager of weblogs whose handles begin with specified letter */
197: public Pager getWeblogsByLetterPager(String letter, int sinceDays,
198: int length) {
199:
200: String pagerUrl = URLUtilities.getWeblogPageURL(weblog,
201: weblogRequest.getLocale(), pageLink, null, null, null,
202: null, 0, false);
203:
204: if (letter != null && StringUtils.isEmpty(letter)) {
205: letter = null;
206: }
207:
208: return new WeblogsPager(pagerUrl, letter, weblogRequest
209: .getLocale(), sinceDays, pageNum, length);
210: }
211:
212: //--------------------------------------------------- User/weblog directory
213:
214: /**
215: * Get map with 26 entries, one for each letter A-Z and
216: * containing integers reflecting the number of users whose
217: * names start with each letter.
218: */
219: public Map getUserNameLetterMap() {
220: Map results = new HashMap();
221: try {
222: Roller roller = RollerFactory.getRoller();
223: UserManager umgr = roller.getUserManager();
224: results = umgr.getUserNameLetterMap();
225: } catch (Exception e) {
226: log.error("ERROR: fetching username letter map", e);
227: }
228: return results;
229: }
230:
231: /**
232: * Get map with 26 entries, one for each letter A-Z and
233: * containing integers reflecting the number of weblogs whose
234: * names start with each letter.
235: */
236: public Map getWeblogHandleLetterMap() {
237: Map results = new HashMap();
238: try {
239: Roller roller = RollerFactory.getRoller();
240: UserManager umgr = roller.getUserManager();
241: results = umgr.getWeblogHandleLetterMap();
242: } catch (Exception e) {
243: log.error("ERROR: fetching weblog handle letter map", e);
244: }
245: return results;
246: }
247:
248: /**
249: * Return list of weblogs that user belongs to.
250: */
251: public List getUsersWeblogs(String userName) {
252: List results = new ArrayList();
253: try {
254: Roller roller = RollerFactory.getRoller();
255: UserManager umgr = roller.getUserManager();
256: UserData user = umgr.getUserByUserName(userName);
257: List perms = umgr.getAllPermissions(user);
258: for (Iterator it = perms.iterator(); it.hasNext();) {
259: PermissionsData perm = (PermissionsData) it.next();
260: results.add(WebsiteDataWrapper.wrap(perm.getWebsite()));
261: }
262: } catch (Exception e) {
263: log.error("ERROR: fetching weblog list", e);
264: }
265: return results;
266: }
267:
268: /**
269: * Return list of users that belong to website.
270: */
271: public List getWeblogsUsers(String handle) {
272: List results = new ArrayList();
273: try {
274: Roller roller = RollerFactory.getRoller();
275: UserManager umgr = roller.getUserManager();
276: WebsiteData website = umgr.getWebsiteByHandle(handle);
277: List perms = umgr.getAllPermissions(website);
278: for (Iterator it = perms.iterator(); it.hasNext();) {
279: PermissionsData perm = (PermissionsData) it.next();
280: results.add(UserDataWrapper.wrap(perm.getUser()));
281: }
282: } catch (Exception e) {
283: log.error("ERROR: fetching weblog list", e);
284: }
285: return results;
286: }
287:
288: /** Get User object by username */
289: public UserDataWrapper getUser(String username) {
290: UserDataWrapper wrappedUser = null;
291: try {
292: Roller roller = RollerFactory.getRoller();
293: UserManager umgr = roller.getUserManager();
294: UserData user = umgr.getUserByUserName(username,
295: Boolean.TRUE);
296: wrappedUser = UserDataWrapper.wrap(user);
297: } catch (Exception e) {
298: log.error("ERROR: fetching users by letter", e);
299: }
300: return wrappedUser;
301: }
302:
303: /** Get Website object by handle */
304: public WebsiteDataWrapper getWeblog(String handle) {
305: WebsiteDataWrapper wrappedWebsite = null;
306: try {
307: Roller roller = RollerFactory.getRoller();
308: UserManager umgr = roller.getUserManager();
309: WebsiteData website = umgr.getWebsiteByHandle(handle);
310: wrappedWebsite = WebsiteDataWrapper.wrap(website);
311: } catch (Exception e) {
312: log.error("ERROR: fetching users by letter", e);
313: }
314: return wrappedWebsite;
315: }
316:
317: //------------------------------------------------------- Small collections
318:
319: /*
320: * Get most collection of Website objects,
321: * in reverse chrono order by creationDate.
322: * @param offset Offset into results (for paging)
323: * @param len Max number of results to return
324: */
325: public List getNewWeblogs(int sinceDays, int length) {
326: List results = new ArrayList();
327: Calendar cal = Calendar.getInstance();
328: cal.setTime(new Date());
329: cal.add(Calendar.DATE, -1 * sinceDays);
330: Date startDate = cal.getTime();
331: try {
332: Roller roller = RollerFactory.getRoller();
333: UserManager umgr = roller.getUserManager();
334: List weblogs = umgr.getWebsites(null, Boolean.TRUE,
335: Boolean.TRUE, startDate, null, 0, length);
336: for (Iterator it = weblogs.iterator(); it.hasNext();) {
337: WebsiteData website = (WebsiteData) it.next();
338: results.add(WebsiteDataWrapper.wrap(website));
339: }
340: } catch (Exception e) {
341: log.error("ERROR: fetching weblog list", e);
342: }
343: return results;
344: }
345:
346: /*
347: * Get most recent User objects, in reverse chrono order by creationDate.
348: * @param offset Offset into results (for paging)
349: * @param len Max number of results to return
350: */
351: public List getNewUsers(int sinceDays, int length) {
352: List results = new ArrayList();
353: try {
354: Roller roller = RollerFactory.getRoller();
355: UserManager umgr = roller.getUserManager();
356: List users = umgr.getUsers(0, length);
357: for (Iterator it = users.iterator(); it.hasNext();) {
358: UserData user = (UserData) it.next();
359: results.add(UserDataWrapper.wrap(user));
360: }
361: } catch (Exception e) {
362: log.error("ERROR: fetching weblog list", e);
363: }
364: return results;
365: }
366:
367: /**
368: * Get list of WebsiteDisplay objects, ordered by number of hits.
369: * @param sinceDays Only consider weblogs updated in the last sinceDays
370: * @param len Max number of results to return
371: */
372: public List getHotWeblogs(int sinceDays, int length) {
373:
374: List results = new ArrayList();
375: try {
376: WeblogManager mgr = RollerFactory.getRoller()
377: .getWeblogManager();
378: List hotBlogs = mgr.getHotWeblogs(sinceDays, 0, length);
379:
380: Iterator hitCounts = hotBlogs.iterator();
381: while (hitCounts.hasNext()) {
382: HitCountData hitCount = (HitCountData) hitCounts.next();
383:
384: results.add(new StatCount(hitCount.getWeblog().getId(),
385: hitCount.getWeblog().getHandle(), hitCount
386: .getWeblog().getName(),
387: "statCount.weblogDayHits", hitCount
388: .getDailyHits()));
389: }
390:
391: } catch (Exception e) {
392: log.error("ERROR: fetching hot weblog list", e);
393: }
394:
395: return results;
396: }
397:
398: /**
399: * Get most collection of most commented websites, as StatCount objects,
400: * in descending order by number of comments.
401: * @param sinceDays Only consider weblogs updated in the last sinceDays
402: * @param offset Offset into results (for paging)
403: * @param length Max number of results to return
404: */
405: public List getMostCommentedWeblogs(int sinceDays, int length) {
406: List results = new ArrayList();
407: Calendar cal = Calendar.getInstance();
408: cal.setTime(new Date());
409: cal.add(Calendar.DATE, -1 * sinceDays);
410: Date startDate = cal.getTime();
411: try {
412: Roller roller = RollerFactory.getRoller();
413: UserManager umgr = roller.getUserManager();
414: results = umgr.getMostCommentedWebsites(startDate,
415: new Date(), 0, length);
416: } catch (Exception e) {
417: log.error("ERROR: fetching commented weblog list", e);
418: }
419: return results;
420: }
421:
422: /**
423: * Get most commented weblog entries across all weblogs, as StatCount
424: * objects, in descending order by number of comments.
425: * @param sinceDays Only consider weblogs updated in the last sinceDays
426: * @param cats To limit results to list of category names
427: * @param len Max number of results to return
428: */
429: public List getMostCommentedWeblogEntries(List cats, int sinceDays,
430: int length) {
431: List results = new ArrayList();
432: Calendar cal = Calendar.getInstance();
433: cal.setTime(new Date());
434: cal.add(Calendar.DATE, -1 * sinceDays);
435: Date startDate = cal.getTime();
436: try {
437: Roller roller = RollerFactory.getRoller();
438: WeblogManager wmgr = roller.getWeblogManager();
439: results = wmgr.getMostCommentedWeblogEntries(null,
440: startDate, new Date(), 0, length);
441: } catch (Exception e) {
442: log.error("ERROR: fetching commented weblog entries list",
443: e);
444: }
445: return results;
446: }
447:
448: /**
449: * Get pinned entries.
450: * @param sinceDays Only consider weblogs updated in the last sinceDays
451: * @param length Max number of results to return
452: */
453: public List getPinnedWeblogEntries(int length) {
454: List results = new ArrayList();
455: try {
456: Roller roller = RollerFactory.getRoller();
457: WeblogManager wmgr = roller.getWeblogManager();
458: List weblogs = wmgr
459: .getWeblogEntriesPinnedToMain(new Integer(length));
460: for (Iterator it = weblogs.iterator(); it.hasNext();) {
461: WeblogEntryData entry = (WeblogEntryData) it.next();
462: results.add(WeblogEntryDataWrapper.wrap(entry));
463: }
464: } catch (Exception e) {
465: log.error("ERROR: fetching pinned weblog entries", e);
466: }
467: return results;
468: }
469:
470: /**
471: *
472: * @param sinceDays
473: * @param length
474: * @return
475: */
476: public List getPopularTags(int sinceDays, int length) {
477: List results = new ArrayList();
478: Date startDate = null;
479: if (sinceDays > 0) {
480: Calendar cal = Calendar.getInstance();
481: cal.setTime(new Date());
482: cal.add(Calendar.DATE, -1 * sinceDays);
483: startDate = cal.getTime();
484: }
485:
486: try {
487: Roller roller = RollerFactory.getRoller();
488: WeblogManager wmgr = roller.getWeblogManager();
489: results = wmgr.getPopularTags(null, startDate, length);
490: } catch (Exception e) {
491: log.error("ERROR: fetching site tags list", e);
492: }
493: return results;
494: }
495:
496: }
|