KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > WeblogManagerImpl


1 /*
2  * Created on Feb 24, 2003
3  */

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 JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.TreeMap JavaDoc;
30 import org.roller.model.RollerFactory;
31
32 /**
33  * Abstract base implementation using PersistenceStrategy.
34  * @author Dave Johnson
35  * @author Lance Lavandowska
36  */

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     /* inline creation of reverse comparator, anonymous inner class */
45     private Comparator reverseComparator = new ReverseComparator();
46     /*
47     new Comparator()
48     {
49         public int compare(Object o1, Object o2)
50         {
51             return -1 * ((Date) o1).compareTo((Date)o2);
52         }
53     };
54     */

55     
56     private SimpleDateFormat JavaDoc formatter = DateUtil.get8charDateFormat();
57
58     public abstract List JavaDoc getWeblogEntries(
59                     WebsiteData website,
60                     Date JavaDoc startDate,
61                     Date JavaDoc endDate,
62                     String JavaDoc catName,
63                     String JavaDoc status,
64                     Integer JavaDoc maxEntries,
65                     Boolean JavaDoc pinned) throws RollerException;
66
67     public abstract List JavaDoc getNextPrevEntries(
68                     WeblogEntryData current,
69                     String JavaDoc 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     //------------------------------------------------ WeblogCategoryData CRUD
83

84     /**
85      * @see org.roller.model.WeblogManager#createWeblogCategory()
86      */

87     public WeblogCategoryData createWeblogCategory()
88     {
89         return new WeblogCategoryData();
90     }
91
92     /**
93      * @see org.roller.model.WeblogManager#createWeblogCategory(
94      * org.roller.pojos.WebsiteData, org.roller.pojos.WeblogCategoryData,
95      * java.lang.String, java.lang.String, java.lang.String)
96      */

97     public WeblogCategoryData createWeblogCategory(
98         WebsiteData website,
99         WeblogCategoryData parent,
100         String JavaDoc name,
101         String JavaDoc description,
102         String JavaDoc image) throws RollerException
103     {
104         return new WeblogCategoryData(
105             null, website, parent, name, description, image);
106     }
107
108     public WeblogCategoryData retrieveWeblogCategory(String JavaDoc id)
109         throws RollerException
110     {
111         return (WeblogCategoryData) mStrategy.load(
112             id,
113             WeblogCategoryData.class);
114     }
115
116     //--------------------------------------------- WeblogCategoryData Queries
117

118     public WeblogCategoryData getWeblogCategoryByPath(
119         WebsiteData website, String JavaDoc categoryPath) throws RollerException
120     {
121         return getWeblogCategoryByPath(website, null, categoryPath);
122     }
123
124     public String JavaDoc getPath(WeblogCategoryData category) throws RollerException
125     {
126         if (null == category.getParent())
127         {
128             return "/";
129         }
130         else
131         {
132             String JavaDoc 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 JavaDoc path)
140         throws RollerException
141     {
142         final Iterator JavaDoc cats;
143         final String JavaDoc[] 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 JavaDoc[] subpath = new String JavaDoc[pathArray.length - 1];
175                     System.arraycopy(pathArray, 1, subpath, 0, subpath.length);
176
177                     String JavaDoc pathString= Utilities.stringArrayToString(subpath,"/");
178                     return getWeblogCategoryByPath(website, possibleMatch, pathString);
179                 }
180             }
181         }
182
183         // The category did not match and neither did any sub-categories
184
return null;
185     }
186
187     //----------------------------------------------- WeblogCategoryAssoc CRUD
188

189     public WeblogCategoryAssoc createWeblogCategoryAssoc()
190     {
191         return new WeblogCategoryAssoc();
192     }
193
194     public WeblogCategoryAssoc createWeblogCategoryAssoc(
195         WeblogCategoryData category,
196         WeblogCategoryData ancestor,
197         String JavaDoc relation) throws RollerException
198     {
199         return new WeblogCategoryAssoc(null, category, ancestor, relation);
200     }
201
202     public WeblogCategoryAssoc retrieveWeblogCategoryAssoc(String JavaDoc id) throws RollerException
203     {
204         return (WeblogCategoryAssoc)mStrategy.load(id, WeblogCategoryAssoc.class);
205     }
206
207     //------------------------------------------------------- CommentData CRUD
208

209     public void removeComment(String JavaDoc id) throws RollerException
210     {
211         mStrategy.remove(id, CommentData.class);
212     }
213
214     public void removeComments(String JavaDoc[] 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 JavaDoc entryId) throws RollerException
223     {
224         List JavaDoc comments = getComments(entryId, false); // get all Comments
225
Iterator JavaDoc it = comments.iterator();
226         while (it.hasNext())
227         {
228             removeComment( ((CommentData)it.next()).getId() );
229         }
230     }
231
232     //---------------------------------------------------- CommentData Queries
233

234     public CommentData retrieveComment(String JavaDoc id) throws RollerException
235     {
236         return (CommentData) mStrategy.load(id, CommentData.class);
237     }
238
239     public List JavaDoc getComments(String JavaDoc entryId) throws RollerException
240     {
241         return getComments(entryId, true);
242     }
243
244     //--------------------------------------------------- WeblogEntryData CRUD
245

246     public WeblogEntryData retrieveWeblogEntry(String JavaDoc id)
247         throws RollerException
248     {
249         return (WeblogEntryData) mStrategy.load(
250             id, WeblogEntryData.class);
251     }
252
253     public void removeWeblogEntry(String JavaDoc 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     //------------------------------------------------ WeblogEntryData Queries
262

263     /**
264      * Gets the Date of the latest Entry publish time, before the end of today,
265      * for all WeblogEntries
266      *
267      * @param userName
268      * @return Date
269      * @throws RollerException
270      */

271     public Date JavaDoc getWeblogLastPublishTime(String JavaDoc userName)
272         throws RollerException
273     {
274         return getWeblogLastPublishTime(userName, null);
275     }
276
277     //--------------------------------------------------------- Implementation
278

279     /**
280      * Get weblog entries.
281      * @see org.roller.model.WeblogManager#getWeblogEntries(
282      * java.lang.String,
283      * java.util.Date,
284      * java.util.Date,
285      * java.lang.String,
286      * java.lang.String,
287      * java.lang.Integer)
288      */

289     public List JavaDoc getWeblogEntries(
290                     WebsiteData website,
291                     Date JavaDoc startDate,
292                     Date JavaDoc endDate,
293                     String JavaDoc catName,
294                     String JavaDoc status,
295                     Integer JavaDoc maxEntries) throws RollerException
296     {
297         return getWeblogEntries(
298                     website,
299                     startDate,
300                     endDate,
301                     catName,
302                     status,
303                     maxEntries,
304                     null);
305     }
306
307     /**
308      * Get webloog entries in range specified by offset and length.
309      * @see org.roller.model.WeblogManager#getWeblogEntries(
310      * java.lang.String,
311      * java.util.Date,
312      * java.util.Date,
313      * java.lang.String,
314      * java.lang.String,
315      * int offset,
316      * int length)
317      */

318     public List JavaDoc getWeblogEntries(
319                     WebsiteData website,
320                     Date JavaDoc startDate,
321                     Date JavaDoc endDate,
322                     String JavaDoc catName,
323                     String JavaDoc status,
324                     int offset,
325                     int range) throws RollerException
326     {
327         List JavaDoc filtered = new ArrayList JavaDoc();
328         List JavaDoc entries = getWeblogEntries(
329                     website,
330                     startDate,
331                     endDate,
332                     catName,
333                     status,
334                     new Integer JavaDoc(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     /**
348      * @see org.roller.model.WeblogManager#getWeblogEntryDayMap(
349      * java.lang.String,
350      * java.util.Date,
351      * java.util.Date,
352      * java.lang.String,
353      * java.lang.String,
354      * java.lang.Integer)
355      */

356     public Map JavaDoc getWeblogEntryObjectMap(
357                     WebsiteData website,
358                     Date JavaDoc startDate,
359                     Date JavaDoc endDate,
360                     String JavaDoc catName,
361                     String JavaDoc status,
362                     Integer JavaDoc maxEntries) throws RollerException
363     {
364         return getWeblogEntryMap(
365                         website,
366                         startDate,
367                         endDate,
368                         catName,
369                         status,
370                         maxEntries,
371                         false);
372     }
373     
374     /**
375      * @see org.roller.model.WeblogManager#getWeblogEntryDayMap(
376      * java.lang.String,
377      * java.util.Date,
378      * java.util.Date,
379      * java.lang.String,
380      * java.lang.String,
381      * java.lang.Integer)
382      */

383     public Map JavaDoc getWeblogEntryStringMap(
384                     WebsiteData website,
385                     Date JavaDoc startDate,
386                     Date JavaDoc endDate,
387                     String JavaDoc catName,
388                     String JavaDoc status,
389                     Integer JavaDoc 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 JavaDoc getWeblogEntryMap(
402                     WebsiteData website,
403                     Date JavaDoc startDate,
404                     Date JavaDoc endDate,
405                     String JavaDoc catName,
406                     String JavaDoc status,
407                     Integer JavaDoc maxEntries,
408                     boolean stringsOnly) throws RollerException
409     {
410         TreeMap JavaDoc map = new TreeMap JavaDoc(reverseComparator);
411        
412         List JavaDoc entries = getWeblogEntries(
413                         website,
414                         startDate,
415                         endDate,
416                         catName,
417                         status,
418                         maxEntries);
419         
420         Calendar JavaDoc cal = Calendar.getInstance();
421         if (website != null)
422         {
423             cal.setTimeZone(website.getTimeZoneInstance());
424         }
425         
426         for (Iterator JavaDoc wbItr = entries.iterator(); wbItr.hasNext();)
427         {
428             WeblogEntryData entry = (WeblogEntryData) wbItr.next();
429             Date JavaDoc 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 JavaDoc dayEntries = (List JavaDoc) map.get(sDate);
438                 if (dayEntries == null)
439                 {
440                     dayEntries = new ArrayList JavaDoc();
441                     map.put(sDate, dayEntries);
442                 }
443                 dayEntries.add(entry);
444             }
445         }
446         return map;
447     }
448     
449     /*
450      * @see org.roller.model.WeblogManager#getNextEntry(org.roller.pojos.WeblogEntryData)
451      */

452     public List JavaDoc getNextEntries(
453             WeblogEntryData current, String JavaDoc catName, int maxEntries)
454         throws RollerException
455     {
456         return getNextPrevEntries(current, catName, maxEntries, true);
457     }
458
459     /*
460      * @see org.roller.model.WeblogManager#getPreviousEntry(org.roller.pojos.WeblogEntryData)
461      */

462     public List JavaDoc getPreviousEntries(
463             WeblogEntryData current, String JavaDoc catName, int maxEntries)
464         throws RollerException
465     {
466         return getNextPrevEntries(current, catName, maxEntries, false);
467     }
468
469     public WeblogEntryData getNextEntry(WeblogEntryData current, String JavaDoc catName)
470         throws RollerException
471     {
472         WeblogEntryData entry = null;
473         List JavaDoc 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 JavaDoc catName)
482         throws RollerException
483     {
484         WeblogEntryData entry = null;
485         List JavaDoc 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     /**
494      * @see org.roller.model.WeblogManager#getWeblogEntriesPinnedToMain(int)
495      */

496     public List JavaDoc getWeblogEntriesPinnedToMain(Integer JavaDoc max) throws RollerException
497     {
498         return getWeblogEntries(
499             null, null, new Date JavaDoc(), null, null, max, Boolean.TRUE);
500     }
501
502     /**
503      * Get absolute URL to this website.
504      * @return Absolute URL to this website.
505      */

506     public String JavaDoc getUrl(UserData user, String JavaDoc contextUrl)
507     {
508         String JavaDoc url =
509             Utilities.escapeHTML(contextUrl + "/page/" + user.getUserName());
510         return url;
511     }
512
513 }
514
Popular Tags