Coverage Report - net.sourceforge.pebble.service.StaticPageService
 
Classes in this File Line Coverage Branch Coverage Complexity
StaticPageService
0%
0/72
0%
0/14
3.429
 
 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.service;
 33  
 
 34  
 import net.sourceforge.pebble.ContentCache;
 35  
 import net.sourceforge.pebble.comparator.StaticPageByNameComparator;
 36  
 import net.sourceforge.pebble.dao.DAOFactory;
 37  
 import net.sourceforge.pebble.dao.PersistenceException;
 38  
 import net.sourceforge.pebble.dao.StaticPageDAO;
 39  
 import net.sourceforge.pebble.domain.Blog;
 40  
 import net.sourceforge.pebble.domain.StaticPage;
 41  
 import org.apache.commons.logging.Log;
 42  
 import org.apache.commons.logging.LogFactory;
 43  
 
 44  
 import java.util.ArrayList;
 45  
 import java.util.Collections;
 46  
 import java.util.Date;
 47  
 import java.util.List;
 48  
 
 49  
 /**
 50  
  * Service that encompasses all functionality related to getting, putting
 51  
  * and removing static pages.
 52  
  *
 53  
  * @author    Simon Brown
 54  
  */
 55  0
 public class StaticPageService {
 56  
 
 57  0
   private static final Log log = LogFactory.getLog(StaticPageService.class);
 58  
 
 59  
   /**
 60  
    * Gets the list of static pages for the given blog.
 61  
    *
 62  
    * @param blog    the Blog
 63  
    * @return  a list of BlogEntry instances
 64  
    * @throws  StaticPageServiceException if something goes wrong
 65  
    */
 66  
   public List<StaticPage> getStaticPages(Blog blog) throws StaticPageServiceException {
 67  0
     List<StaticPage> staticPages = new ArrayList<StaticPage>();
 68  
     try {
 69  0
       DAOFactory factory = DAOFactory.getConfiguredFactory();
 70  0
       StaticPageDAO dao = factory.getStaticPageDAO();
 71  0
       staticPages.addAll(dao.loadStaticPages(blog));
 72  0
     } catch (PersistenceException pe) {
 73  0
       throw new StaticPageServiceException(blog, pe);
 74  0
     }
 75  
 
 76  0
     Collections.sort(staticPages, new StaticPageByNameComparator());
 77  
 
 78  0
     return staticPages;
 79  
   }
 80  
 
 81  
   /**
 82  
    * Gets the page with the specified id.
 83  
    *
 84  
    * @param pageId   the id of the static page
 85  
    * @param blog    the Blog
 86  
    * @return  a Page instance, or null if the page couldn't be found
 87  
    * @throws  StaticPageServiceException if something goes wrong
 88  
    */
 89  
   public StaticPage getStaticPageById(Blog blog, String pageId) throws StaticPageServiceException {
 90  
     StaticPage staticPage;
 91  0
     ContentCache cache = ContentCache.getInstance();
 92  
 
 93  
     try {
 94  0
       staticPage = cache.getStaticPage(blog, pageId);
 95  0
       if (staticPage != null) {
 96  0
         log.debug("Got static page " + pageId+ " from cache");
 97  
       } else {
 98  0
         log.debug("Loading static page " + pageId+ " from disk");
 99  
 
 100  0
         DAOFactory factory = DAOFactory.getConfiguredFactory();
 101  0
         StaticPageDAO dao = factory.getStaticPageDAO();
 102  0
         staticPage = dao.loadStaticPage(blog, pageId);
 103  0
         if (staticPage != null) {
 104  0
           staticPage.setPersistent(true);
 105  0
           cache.putStaticPage(staticPage);
 106  
         }
 107  
       }
 108  0
     } catch (PersistenceException pe) {
 109  0
       throw new StaticPageServiceException(blog, pe);
 110  0
     }
 111  
 
 112  0
     if (staticPage != null) {
 113  0
       staticPage = (StaticPage)staticPage.clone();
 114  
     }
 115  
 
 116  0
     return staticPage;
 117  
   }
 118  
 
 119  
   /**
 120  
    * Gets the static page with the specified name.
 121  
    *
 122  
    * @param name    the name of the static page
 123  
    * @param blog    the Blog
 124  
    * @return  a StaticPage instance, or null if the page couldn't be found
 125  
    * @throws  StaticPageServiceException if something goes wrong
 126  
    */
 127  
   public StaticPage getStaticPageByName(Blog blog, String name) throws StaticPageServiceException {
 128  0
     String id = blog.getStaticPageIndex().getStaticPage(name);
 129  0
     return getStaticPageById(blog, id);
 130  
   }
 131  
 
 132  
   /**
 133  
    * Puts the static page.
 134  
    *
 135  
    * @param   staticPage    the StaticPage instance to store
 136  
    * @throws  StaticPageServiceException if something goes wrong
 137  
    */
 138  
   public void putStaticPage(StaticPage staticPage) throws StaticPageServiceException {
 139  0
     ContentCache cache = ContentCache.getInstance();
 140  0
     DAOFactory factory = DAOFactory.getConfiguredFactory();
 141  0
     StaticPageDAO dao = factory.getStaticPageDAO();
 142  0
     Blog blog = staticPage.getBlog();
 143  
 
 144  0
     synchronized (blog) {
 145  
       try {
 146  0
         StaticPage sp = getStaticPageById(blog, staticPage.getId());
 147  
 
 148  0
         if (!staticPage.isPersistent() && sp != null) {
 149  
           // the static page is new but one exists with the same ID already
 150  
           // - increment the date/ID and try again
 151  0
           staticPage.setDate(new Date(staticPage.getDate().getTime() + 1));
 152  0
           putStaticPage(staticPage);
 153  
         } else {
 154  0
           dao.storeStaticPage(staticPage);
 155  0
           staticPage.setPersistent(true);
 156  0
           cache.removeStaticPage(staticPage);
 157  
         }
 158  
 
 159  0
         staticPage.getBlog().getSearchIndex().index(staticPage);
 160  0
         staticPage.getBlog().getStaticPageIndex().index(staticPage);
 161  0
       } catch (PersistenceException pe) {
 162  0
         throw new StaticPageServiceException(blog, pe);
 163  0
       }
 164  0
     }
 165  0
   }
 166  
 
 167  
   /**
 168  
    * Removes a static page.
 169  
    *
 170  
    * @param staticPage    the StaticPage instance to remove
 171  
    * @throws  StaticPageServiceException if something goes wrong
 172  
    */
 173  
   public void removeStaticPage(StaticPage staticPage) throws StaticPageServiceException {
 174  0
     ContentCache cache = ContentCache.getInstance();
 175  0
     DAOFactory factory = DAOFactory.getConfiguredFactory();
 176  0
     StaticPageDAO dao = factory.getStaticPageDAO();
 177  0
     Blog blog = staticPage.getBlog();
 178  
 
 179  
     try {
 180  0
       dao.removeStaticPage(staticPage);
 181  0
       cache.removeStaticPage(staticPage);
 182  
 
 183  0
       staticPage.getBlog().getSearchIndex().unindex(staticPage);
 184  0
       staticPage.getBlog().getStaticPageIndex().unindex(staticPage);
 185  0
     } catch (PersistenceException pe) {
 186  
       // remove from the cache so that it's picked up from storage when accessed next
 187  0
       cache.removeStaticPage(staticPage);
 188  
 
 189  0
       throw new StaticPageServiceException(staticPage.getBlog(), pe);
 190  0
     }
 191  0
   }
 192  
 
 193  
   /**
 194  
    * Locks a given static page.
 195  
    *
 196  
    * @param staticPage    the static page to lock
 197  
    * @return  true if the page could be locked, false otherwise
 198  
    */
 199  
   public boolean lock(StaticPage staticPage) {
 200  0
     if (staticPage.isPersistent()) {
 201  0
       boolean success = DAOFactory.getConfiguredFactory().getStaticPageDAO().lock(staticPage);
 202  0
       ContentCache.getInstance().removeStaticPage(staticPage);
 203  
 
 204  0
       return success;
 205  
     } else {
 206  0
       return true;
 207  
     }
 208  
   }
 209  
 
 210  
   /**
 211  
    * Unlocks a given static page.
 212  
    *
 213  
    * @param staticPage    the static page to unlock
 214  
    * @return  true if the page could be unlocked, false otherwise
 215  
    */
 216  
   public boolean unlock(StaticPage staticPage) {
 217  0
     if (staticPage.isPersistent()) {
 218  0
       boolean success = DAOFactory.getConfiguredFactory().getStaticPageDAO().unlock(staticPage);
 219  0
       ContentCache.getInstance().removeStaticPage(staticPage);
 220  
 
 221  0
       return success;
 222  
     } else {
 223  0
       return true;
 224  
     }
 225  
   }
 226  
 
 227  
 }