Coverage Report - net.sourceforge.pebble.domain.BlogService
 
Classes in this File Line Coverage Branch Coverage Complexity
BlogService
76%
71/93
85%
24/28
3.875
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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  1476
 public class BlogService {
 56  
 
 57  4
   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  
   public BlogEntry getBlogEntry(Blog blog, String blogEntryId) throws BlogServiceException {
 66  2132
     BlogEntry blogEntry = null;
 67  2132
     ContentCache cache = ContentCache.getInstance();
 68  
 
 69  
     // is the blog entry already in the cache?                                                  
 70  2132
     blogEntry = cache.getBlogEntry(blog, blogEntryId);
 71  2132
     if (blogEntry != null) {
 72  852
       log.debug("Got blog entry " + blogEntryId + " from cache");
 73  
     } else {
 74  1280
       log.debug("Loading blog entry " + blogEntryId + " from disk");
 75  1280
       BlogEntryDAO dao = DAOFactory.getConfiguredFactory().getBlogEntryDAO();
 76  
       try {
 77  1280
         blogEntry = dao.loadBlogEntry(blog, blogEntryId);
 78  
 
 79  1280
         if (blogEntry != null) {
 80  
           // place in the cache for faster lookup next time
 81  608
           cache.putBlogEntry(blogEntry);
 82  
         }
 83  0
       } catch (PersistenceException pe) {
 84  0
         throw new BlogServiceException(blog, pe);
 85  1280
       }
 86  
     }
 87  
 
 88  2132
     if (blogEntry != null) {
 89  1460
       blogEntry = (BlogEntry)blogEntry.clone();
 90  1460
       blogEntry.setEventsEnabled(true);
 91  1460
       blogEntry.setPersistent(true);
 92  
     }
 93  
 
 94  2132
     return blogEntry;
 95  
   }
 96  
 
 97  
   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  
   public List<BlogEntry> getBlogEntries(Blog blog, int year, int month) throws BlogServiceException {
 104  3076
     Month m = blog.getBlogForMonth(year, month);
 105  3076
     List<String> blogEntryIds = m.getBlogEntries();
 106  3076
     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  
   public Collection<BlogEntry> getBlogEntries(Blog blog) throws BlogServiceException {
 115  0
     BlogEntryDAO dao = DAOFactory.getConfiguredFactory().getBlogEntryDAO();
 116  
     Collection<BlogEntry> blogEntries;
 117  
     try {
 118  0
       blogEntries = dao.loadBlogEntries(blog);
 119  0
       for (BlogEntry blogEntry : blogEntries) {
 120  0
         blogEntry.setPersistent(true);
 121  
       }
 122  
 
 123  0
     } catch (PersistenceException pe) {
 124  0
       throw new BlogServiceException(blog, pe);
 125  0
     }
 126  
 
 127  0
     return blogEntries;
 128  
   }
 129  
 
 130  
   private List<BlogEntry> getBlogEntries(Blog blog, List<String> blogEntryIds) throws BlogServiceException {
 131  3076
     List<BlogEntry> blogEntries = new ArrayList<BlogEntry>();
 132  
 
 133  3076
     for (String blogEntryId : blogEntryIds) {
 134  536
       BlogEntry blogEntry = getBlogEntry(blog, blogEntryId);
 135  536
       blogEntries.add(blogEntry);
 136  536
     }
 137  
 
 138  3076
     return blogEntries;
 139  
   }
 140  
 
 141  
   /**
 142  
    * Puts the blog entry with the specified id.
 143  
    */
 144  
   public void putBlogEntry(BlogEntry blogEntry) throws BlogServiceException {
 145  1032
     DAOFactory factory = DAOFactory.getConfiguredFactory();
 146  1032
     BlogEntryDAO dao = factory.getBlogEntryDAO();
 147  1032
     Blog blog = blogEntry.getBlog();
 148  1032
     ContentCache cache = ContentCache.getInstance();
 149  
 
 150  1032
     synchronized (blog) {
 151  
       try {
 152  1032
         BlogEntry be = getBlogEntry(blog, blogEntry.getId());
 153  
 
 154  1032
         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  284
           blogEntry.setDate(new Date(blogEntry.getDate().getTime() + 1));
 158  284
           putBlogEntry(blogEntry);
 159  
         } else {
 160  748
           if (!blogEntry.isPersistent()) {
 161  592
             dao.storeBlogEntry(blogEntry);
 162  592
             blogEntry.insertEvent(new BlogEntryEvent(blogEntry, BlogEntryEvent.BLOG_ENTRY_ADDED));
 163  
 
 164  592
             for (Comment comment : blogEntry.getComments()) {
 165  24
               blogEntry.addEvent(new CommentEvent(comment, CommentEvent.COMMENT_ADDED));
 166  
             }
 167  592
             for (TrackBack trackBack : blogEntry.getTrackBacks()) {
 168  24
               blogEntry.addEvent(new TrackBackEvent(trackBack, TrackBackEvent.TRACKBACK_ADDED));
 169  
             }
 170  
           } else {
 171  156
             dao.storeBlogEntry(blogEntry);
 172  156
             if (blogEntry.isDirty()) {
 173  24
               blogEntry.insertEvent(new BlogEntryEvent(blogEntry, blogEntry.getPropertyChangeEvents()));
 174  
             }
 175  
           }
 176  
 
 177  748
           blogEntry.getBlog().getEventDispatcher().fireEvents(blogEntry);
 178  
 
 179  
           // and store the blog entry now that listeners have been fired
 180  748
           dao.storeBlogEntry(blogEntry);
 181  748
           cache.removeBlogEntry(blogEntry);
 182  
         }
 183  
 
 184  1032
         blogEntry.setPersistent(true);
 185  0
       } catch (PersistenceException pe) {
 186  0
         throw new BlogServiceException(blog, pe);
 187  
       } finally {
 188  1032
         blogEntry.clearPropertyChangeEvents();
 189  1032
         blogEntry.clearEvents();
 190  1032
         blogEntry.setEventsEnabled(true);
 191  1032
       }
 192  1032
     }
 193  1032
   }
 194  
 
 195  
   /**
 196  
    * Removes this blog entry.
 197  
    */
 198  
   public void removeBlogEntry(BlogEntry blogEntry) throws BlogServiceException {
 199  36
     Blog blog = blogEntry.getBlog();
 200  36
     ContentCache cache = ContentCache.getInstance();
 201  
 
 202  
     try {
 203  36
       DAOFactory factory = DAOFactory.getConfiguredFactory();
 204  36
       BlogEntryDAO dao = factory.getBlogEntryDAO();
 205  36
       dao.removeBlogEntry(blogEntry);
 206  36
       blogEntry.setPersistent(false);
 207  
 
 208  
       // remove from cache
 209  36
       cache.removeBlogEntry(blogEntry);
 210  
 
 211  36
       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  36
       for (Comment comment : blogEntry.getComments()) {
 216  16
         blogEntry.addEvent(new CommentEvent(comment, CommentEvent.COMMENT_REMOVED));
 217  
       }
 218  36
       for (TrackBack trackBack : blogEntry.getTrackBacks()) {
 219  12
         blogEntry.addEvent(new TrackBackEvent(trackBack, TrackBackEvent.TRACKBACK_REMOVED));
 220  
       }
 221  
 
 222  36
       blogEntry.getBlog().getEventDispatcher().fireEvents(blogEntry);
 223  0
     } catch (PersistenceException pe) {
 224  0
       throw new BlogServiceException(blogEntry.getBlog(), pe);
 225  36
     }
 226  36
   }
 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  
   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  
 }