Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart8.png 29% of files have more coverage
84   244   20   10,5
14   143   0,24   8
8     2,5  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  BlogService       Line # 55 84 0% 20 24 77,4% 0.7735849
 
  (218)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.domain;
33   
34    import net.sourceforge.pebble.api.event.blogentry.BlogEntryEvent;
35    import net.sourceforge.pebble.api.event.comment.CommentEvent;
36    import net.sourceforge.pebble.api.event.trackback.TrackBackEvent;
37    import net.sourceforge.pebble.dao.BlogEntryDAO;
38    import net.sourceforge.pebble.dao.DAOFactory;
39    import net.sourceforge.pebble.dao.PersistenceException;
40    import net.sourceforge.pebble.ContentCache;
41    import org.apache.commons.logging.Log;
42    import org.apache.commons.logging.LogFactory;
43   
44    import java.util.ArrayList;
45    import java.util.Date;
46    import java.util.List;
47    import java.util.Collection;
48   
49    /**
50    * Service that encompasses all functionality related to getting, putting
51    * and removing blog entries.
52    *
53    * @author Simon Brown
54    */
 
55    public class BlogService {
56   
57    private static final Log log = LogFactory.getLog(BlogService.class);
58   
59    /**
60    * Gets the blog entry with the specified id.
61    *
62    * @param blogEntryId the id of the blog entry
63    * @return a BlogEntry instance, or null if the entry couldn't be found
64    */
 
65  790 toggle public BlogEntry getBlogEntry(Blog blog, String blogEntryId) throws BlogServiceException {
66  790 BlogEntry blogEntry = null;
67  790 ContentCache cache = ContentCache.getInstance();
68   
69    // is the blog entry already in the cache?
70  790 blogEntry = cache.getBlogEntry(blog, blogEntryId);
71  790 if (blogEntry != null) {
72  190 log.debug("Got blog entry " + blogEntryId + " from cache");
73    } else {
74  600 log.debug("Loading blog entry " + blogEntryId + " from disk");
75  600 BlogEntryDAO dao = DAOFactory.getConfiguredFactory().getBlogEntryDAO();
76  600 try {
77  600 blogEntry = dao.loadBlogEntry(blog, blogEntryId);
78   
79  600 if (blogEntry != null) {
80    // place in the cache for faster lookup next time
81  284 cache.putBlogEntry(blogEntry);
82    }
83    } catch (PersistenceException pe) {
84  0 throw new BlogServiceException(blog, pe);
85    }
86    }
87   
88  790 if (blogEntry != null) {
89  474 blogEntry = (BlogEntry)blogEntry.clone();
90  474 blogEntry.setEventsEnabled(true);
91  474 blogEntry.setPersistent(true);
92    }
93   
94  790 return blogEntry;
95    }
96   
 
97  0 toggle public List<BlogEntry> getBlogEntries(Blog blog, int year, int month, int day) throws BlogServiceException {
98  0 Day d = blog.getBlogForDay(year, month, day);
99  0 List<String> blogEntryIds = d.getBlogEntries();
100  0 return getBlogEntries(blog, blogEntryIds);
101    }
102   
 
103  266 toggle public List<BlogEntry> getBlogEntries(Blog blog, int year, int month) throws BlogServiceException {
104  266 Month m = blog.getBlogForMonth(year, month);
105  266 List<String> blogEntryIds = m.getBlogEntries();
106  266 return getBlogEntries(blog, blogEntryIds);
107    }
108   
109    /**
110    * Gets all blog entries for the specified blog.
111    *
112    * @return a List of BlogEntry objects
113    */
 
114  0 toggle public Collection<BlogEntry> getBlogEntries(Blog blog) throws BlogServiceException {
115  0 BlogEntryDAO dao = DAOFactory.getConfiguredFactory().getBlogEntryDAO();
116  0 Collection<BlogEntry> blogEntries;
117  0 try {
118  0 blogEntries = dao.loadBlogEntries(blog);
119  0 for (BlogEntry blogEntry : blogEntries) {
120  0 blogEntry.setPersistent(true);
121    }
122   
123    } catch (PersistenceException pe) {
124  0 throw new BlogServiceException(blog, pe);
125    }
126   
127  0 return blogEntries;
128    }
129   
 
130  266 toggle private List<BlogEntry> getBlogEntries(Blog blog, List<String> blogEntryIds) throws BlogServiceException {
131  266 List<BlogEntry> blogEntries = new ArrayList<BlogEntry>();
132   
133  266 for (String blogEntryId : blogEntryIds) {
134  44 BlogEntry blogEntry = getBlogEntry(blog, blogEntryId);
135  44 blogEntries.add(blogEntry);
136    }
137   
138  266 return blogEntries;
139    }
140   
141    /**
142    * Puts the blog entry with the specified id.
143    */
 
144  486 toggle public void putBlogEntry(BlogEntry blogEntry) throws BlogServiceException {
145  486 DAOFactory factory = DAOFactory.getConfiguredFactory();
146  486 BlogEntryDAO dao = factory.getBlogEntryDAO();
147  486 Blog blog = blogEntry.getBlog();
148  486 ContentCache cache = ContentCache.getInstance();
149   
150  486 synchronized (blog) {
151  486 try {
152  486 BlogEntry be = getBlogEntry(blog, blogEntry.getId());
153   
154  486 if (!blogEntry.isPersistent() && be != null) {
155    // the blog entry is new but one exists with the same ID already
156    // - increment the date/ID and try again
157  132 blogEntry.setDate(new Date(blogEntry.getDate().getTime() + 1));
158  132 putBlogEntry(blogEntry);
159    } else {
160  354 if (!blogEntry.isPersistent()) {
161  276 dao.storeBlogEntry(blogEntry);
162  276 blogEntry.insertEvent(new BlogEntryEvent(blogEntry, BlogEntryEvent.BLOG_ENTRY_ADDED));
163   
164  276 for (Comment comment : blogEntry.getComments()) {
165  12 blogEntry.addEvent(new CommentEvent(comment, CommentEvent.COMMENT_ADDED));
166    }
167  276 for (TrackBack trackBack : blogEntry.getTrackBacks()) {
168  12 blogEntry.addEvent(new TrackBackEvent(trackBack, TrackBackEvent.TRACKBACK_ADDED));
169    }
170    } else {
171  78 dao.storeBlogEntry(blogEntry);
172  78 if (blogEntry.isDirty()) {
173  12 blogEntry.insertEvent(new BlogEntryEvent(blogEntry, blogEntry.getPropertyChangeEvents()));
174    }
175    }
176   
177  354 blogEntry.getBlog().getEventDispatcher().fireEvents(blogEntry);
178   
179    // and store the blog entry now that listeners have been fired
180  354 dao.storeBlogEntry(blogEntry);
181  354 cache.removeBlogEntry(blogEntry);
182    }
183   
184  486 blogEntry.setPersistent(true);
185    } catch (PersistenceException pe) {
186  0 throw new BlogServiceException(blog, pe);
187    } finally {
188  486 blogEntry.clearPropertyChangeEvents();
189  486 blogEntry.clearEvents();
190  486 blogEntry.setEventsEnabled(true);
191    }
192    }
193    }
194   
195    /**
196    * Removes this blog entry.
197    */
 
198  18 toggle public void removeBlogEntry(BlogEntry blogEntry) throws BlogServiceException {
199  18 Blog blog = blogEntry.getBlog();
200  18 ContentCache cache = ContentCache.getInstance();
201   
202  18 try {
203  18 DAOFactory factory = DAOFactory.getConfiguredFactory();
204  18 BlogEntryDAO dao = factory.getBlogEntryDAO();
205  18 dao.removeBlogEntry(blogEntry);
206  18 blogEntry.setPersistent(false);
207   
208    // remove from cache
209  18 cache.removeBlogEntry(blogEntry);
210   
211  18 blogEntry.insertEvent(new BlogEntryEvent(blogEntry, BlogEntryEvent.BLOG_ENTRY_REMOVED));
212   
213    // and remove all of the responses, so the appropriate events are raised
214    // and the responses get unindexed
215  18 for (Comment comment : blogEntry.getComments()) {
216  8 blogEntry.addEvent(new CommentEvent(comment, CommentEvent.COMMENT_REMOVED));
217    }
218  18 for (TrackBack trackBack : blogEntry.getTrackBacks()) {
219  6 blogEntry.addEvent(new TrackBackEvent(trackBack, TrackBackEvent.TRACKBACK_REMOVED));
220    }
221   
222  18 blogEntry.getBlog().getEventDispatcher().fireEvents(blogEntry);
223    } catch (PersistenceException pe) {
224  0 throw new BlogServiceException(blogEntry.getBlog(), pe);
225    }
226    }
227   
228    /**
229    * Gets the blog entry with the specified id.
230    *
231    * @param responseId the id of the response
232    * @return a response instance, or null if the entry couldn't be found
233    */
 
234  0 toggle public Response getResponse(Blog blog, String responseId) throws BlogServiceException {
235  0 String blogEntryId = responseId.substring(responseId.indexOf("/")+1, responseId.lastIndexOf("/"));
236  0 BlogEntry blogEntry = getBlogEntry(blog, blogEntryId);
237  0 if (blogEntry != null) {
238  0 return blogEntry.getResponse(responseId);
239    } else {
240  0 return null;
241    }
242    }
243   
244    }