Coverage Report - net.sourceforge.pebble.dao.file.FileCategoryDAO
 
Classes in this File Line Coverage Branch Coverage Complexity
FileCategoryDAO
57%
33/57
50%
3/6
2.333
 
 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  
 
 33  
 package net.sourceforge.pebble.dao.file;
 34  
 
 35  
 import net.sourceforge.pebble.dao.CategoryDAO;
 36  
 import net.sourceforge.pebble.dao.PersistenceException;
 37  
 import net.sourceforge.pebble.domain.Blog;
 38  
 import net.sourceforge.pebble.domain.Category;
 39  
 import net.sourceforge.pebble.domain.CategoryBuilder;
 40  
 import org.apache.commons.logging.Log;
 41  
 import org.apache.commons.logging.LogFactory;
 42  
 
 43  
 import javax.xml.bind.JAXBContext;
 44  
 import javax.xml.bind.JAXBElement;
 45  
 import javax.xml.bind.Marshaller;
 46  
 import javax.xml.bind.Unmarshaller;
 47  
 import java.io.File;
 48  
 import java.io.FileWriter;
 49  
 import java.util.List;
 50  
 
 51  
 /**
 52  
  * DAO responsible for managing the storage of category definitions.
 53  
  *
 54  
  * @author    Simon Brown
 55  
  */
 56  
 public class FileCategoryDAO implements CategoryDAO {
 57  
 
 58  
   /** the name of the file containing the category information */
 59  
   private static final String CATEGORIES_FILE_NAME = "categories.xml";
 60  
 
 61  
   /** the log used by this class */
 62  4
   private static Log log = LogFactory.getLog(FileCategoryDAO.class);
 63  
 
 64  
   private JAXBContext jaxbContext;
 65  
 
 66  
   /**
 67  
    * Default, no args constructor.
 68  
    */
 69  5020
   public FileCategoryDAO() {
 70  
     try {
 71  5020
       jaxbContext = JAXBContext.newInstance(getClass().getPackage().getName());
 72  0
     } catch (Exception e) {
 73  0
       e.printStackTrace();
 74  5020
     }
 75  5020
   }
 76  
 
 77  
   /**
 78  
    * Gets the categories for a particular blog.
 79  
    *
 80  
    * @param blog    the owning Blog instance
 81  
    * @return  a Collection of Category instances
 82  
    * @throws  PersistenceException    if categories cannot be loaded
 83  
    */
 84  
   public Category getCategories(Blog blog) throws PersistenceException {
 85  32
     CategoryBuilder categoryBuilder = new CategoryBuilder(blog);
 86  32
     File source = new File(blog.getRoot(), CATEGORIES_FILE_NAME);
 87  32
     if (source.exists()) {
 88  
       try {
 89  0
         Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
 90  0
         JAXBElement<CategoriesType> controller = (JAXBElement)unmarshaller.unmarshal(source);
 91  0
         CategoriesType categoriesType = controller.getValue();
 92  
 
 93  0
         for (CategoryType categoryType : categoriesType.getCategory()) {
 94  0
           Category category = new Category(categoryType.getId(), categoryType.getName());
 95  0
           category.setBlog(blog);
 96  0
           category.setTags(categoryType.getTags());
 97  
 
 98  0
           categoryBuilder.addCategory(category);
 99  0
         }
 100  0
       } catch (Exception e) {
 101  0
         log.error(e.getMessage(), e);
 102  0
         e.printStackTrace();
 103  0
         throw new PersistenceException(e.getMessage());
 104  0
       }
 105  
     }                                                           
 106  
 
 107  32
     return categoryBuilder.getRootCategory();
 108  
   }
 109  
 
 110  
   /**
 111  
    * Adds the specified category.
 112  
    *
 113  
    * @param category    the Category instance to be added
 114  
    * @param blog    the owning blog
 115  
    * @throws PersistenceException   if something goes wrong storing the category
 116  
    */
 117  
   public void addCategory(Category category, Blog blog) throws PersistenceException {
 118  32
     store(blog);
 119  32
   }
 120  
 
 121  
   /**
 122  
    * Updates the specified category.
 123  
    *
 124  
    * @param updatedCategory   the Category instance to be updated
 125  
    * @param blog    the owning blog
 126  
    * @throws PersistenceException   if something goes wrong storing the category
 127  
    */
 128  
   public void updateCategory(Category updatedCategory, Blog blog) throws PersistenceException {
 129  0
     store(blog);
 130  0
   }
 131  
 
 132  
   /**
 133  
    * Removes the specified category.
 134  
    *
 135  
    * @param category    the Category instance to be removed
 136  
    * @param blog    the owning blog
 137  
    * @throws PersistenceException   if something goes wrong removing the category
 138  
    */
 139  
   public void deleteCategory(Category category, Blog blog) throws PersistenceException {
 140  0
     store(blog);
 141  0
   }
 142  
 
 143  
   /**
 144  
    * Helper method to store all categories for a given blog.
 145  
    *
 146  
    * @param blog      the blog to which the categories belong
 147  
    * @throws  PersistenceException    if the categories cannnot be stored
 148  
    */
 149  
   private void store(Blog blog) throws PersistenceException {
 150  32
     List<Category> categories = blog.getCategories();
 151  32
     File destination = new File(blog.getRoot(), CATEGORIES_FILE_NAME);
 152  
     try {
 153  32
       Marshaller marshaller = jaxbContext.createMarshaller();
 154  32
       CategoriesType categoriesType = new CategoriesType();
 155  
 
 156  32
       for (Category category : categories) {
 157  36
         CategoryType categoryType = new CategoryType();
 158  36
         categoryType.setId(category.getId());
 159  36
         categoryType.setName(category.getName());
 160  36
         categoryType.setTags(category.getTags());
 161  36
         categoriesType.getCategory().add(categoryType);
 162  36
       }
 163  
 
 164  32
       log.debug("Saving to " + destination.getAbsolutePath());
 165  32
       ObjectFactory objectFactory = new ObjectFactory();
 166  32
       JAXBElement jaxbElement = objectFactory.createCategories(categoriesType);
 167  
 
 168  32
       marshaller.setProperty("jaxb.formatted.output", true);
 169  32
       marshaller.setProperty("jaxb.encoding", blog.getCharacterEncoding());
 170  32
       FileWriter writer = new FileWriter(destination);
 171  32
       marshaller.marshal(jaxbElement, writer);
 172  32
       writer.flush();
 173  32
       writer.close();
 174  0
     } catch (Exception e) {
 175  0
       log.error(e.getMessage(), e);
 176  0
       e.printStackTrace();
 177  0
       throw new PersistenceException(e.getMessage());
 178  32
     }
 179  32
   }
 180  
 
 181  
 }