File Source: filecategorydao.java

         /* 
    P/P   *  Method: net.sourceforge.pebble.dao.file.FileCategoryDAO__static_init
          * 
          *  Postconditions:
          *    init'ed(log)
          */
     1  package net.sourceforge.pebble.dao.file;
     2  
     3  import net.sourceforge.pebble.dao.CategoryDAO;
     4  import net.sourceforge.pebble.dao.PersistenceException;
     5  import net.sourceforge.pebble.domain.Blog;
     6  import net.sourceforge.pebble.domain.Category;
     7  import net.sourceforge.pebble.domain.CategoryBuilder;
     8  import org.apache.commons.logging.Log;
     9  import org.apache.commons.logging.LogFactory;
    10  
         /* 
    P/P   *  Method: void net.sourceforge.pebble.dao.file.FileCategoryDAO()
          * 
          *  Presumptions:
          *    java.lang.Class:getPackage(...)@39 != null
          *    java.lang.Object:getClass(...)@39 != null
          * 
          *  Postconditions:
          *    possibly_updated(this.jaxbContext)
          */
    11  import javax.xml.bind.JAXBContext;
    12  import javax.xml.bind.JAXBElement;
    13  import javax.xml.bind.Marshaller;
    14  import javax.xml.bind.Unmarshaller;
    15  import java.io.File;
    16  import java.io.FileWriter;
    17  import java.util.List;
    18  
    19  /**
    20   * DAO responsible for managing the storage of category definitions.
    21   *
    22   * @author    Simon Brown
    23   */
    24  public class FileCategoryDAO implements CategoryDAO {
    25  
    26    /** the name of the file containing the category information */
    27    private static final String CATEGORIES_FILE_NAME = "categories.xml";
    28  
    29    /** the log used by this class */
    30    private static Log log = LogFactory.getLog(FileCategoryDAO.class);
    31  
    32    private JAXBContext jaxbContext;
    33  
    34    /**
    35     * Default, no args constructor.
    36     */
    37    public FileCategoryDAO() {
    38      try {
    39        jaxbContext = JAXBContext.newInstance(getClass().getPackage().getName());
    40      } catch (Exception e) {
    41        e.printStackTrace();
    42      }
    43    }
    44  
    45    /**
    46     * Gets the categories for a particular blog.
    47     *
    48     * @param blog    the owning Blog instance
    49     * @return  a Collection of Category instances
    50     * @throws  PersistenceException    if categories cannot be loaded
    51     */
    52    public Category getCategories(Blog blog) throws PersistenceException {
    53      CategoryBuilder categoryBuilder = new CategoryBuilder(blog);
    54      File source = new File(blog.getRoot(), CATEGORIES_FILE_NAME);
    55      if (source.exists()) {
    56        try {
    57          Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    58          JAXBElement<CategoriesType> controller = (JAXBElement)unmarshaller.unmarshal(source);
    59          CategoriesType categoriesType = controller.getValue();
    60  
    61          for (CategoryType categoryType : categoriesType.getCategory()) {
    62            Category category = new Category(categoryType.getId(), categoryType.getName());
    63            category.setBlog(blog);
    64            category.setTags(categoryType.getTags());
    65  
    66            categoryBuilder.addCategory(category);
    67          }
    68        } catch (Exception e) {
    69          log.error(e.getMessage(), e);
    70          e.printStackTrace();
    71          throw new PersistenceException(e.getMessage());
    72        }
    73      }                                                           
    74  
    75      return categoryBuilder.getRootCategory();
    76    }
    77  
    78    /**
    79     * Adds the specified category.
    80     *
    81     * @param category    the Category instance to be added
    82     * @param blog    the owning blog
    83     * @throws PersistenceException   if something goes wrong storing the category
    84     */
           /* 
    P/P     *  Method: void addCategory(Category, Blog)
            */
    85    public void addCategory(Category category, Blog blog) throws PersistenceException {
    86      store(blog);
    87    }
    88  
    89    /**
    90     * Updates the specified category.
    91     *
    92     * @param updatedCategory   the Category instance to be updated
    93     * @param blog    the owning blog
    94     * @throws PersistenceException   if something goes wrong storing the category
    95     */
           /* 
    P/P     *  Method: void updateCategory(Category, Blog)
            */
    96    public void updateCategory(Category updatedCategory, Blog blog) throws PersistenceException {
    97      store(blog);
    98    }
    99  
   100    /**
   101     * Removes the specified category.
   102     *
   103     * @param category    the Category instance to be removed
   104     * @param blog    the owning blog
   105     * @throws PersistenceException   if something goes wrong removing the category
   106     */
           /* 
    P/P     *  Method: void deleteCategory(Category, Blog)
            */
   107    public void deleteCategory(Category category, Blog blog) throws PersistenceException {
   108      store(blog);
   109    }
   110  
   111    /**
   112     * Helper method to store all categories for a given blog.
   113     *
   114     * @param blog      the blog to which the categories belong
   115     * @throws  PersistenceException    if the categories cannnot be stored
   116     */
   117    private void store(Blog blog) throws PersistenceException {
   118      List<Category> categories = blog.getCategories();
   119      File destination = new File(blog.getRoot(), CATEGORIES_FILE_NAME);
   120      try {
   121        Marshaller marshaller = jaxbContext.createMarshaller();
   122        CategoriesType categoriesType = new CategoriesType();
   123  
   124        for (Category category : categories) {
   125          CategoryType categoryType = new CategoryType();
   126          categoryType.setId(category.getId());
   127          categoryType.setName(category.getName());
   128          categoryType.setTags(category.getTags());
   129          categoriesType.getCategory().add(categoryType);
   130        }
   131  
   132        log.debug("Saving to " + destination.getAbsolutePath());
   133        ObjectFactory objectFactory = new ObjectFactory();
   134        JAXBElement jaxbElement = objectFactory.createCategories(categoriesType);
   135  
   136        marshaller.setProperty("jaxb.formatted.output", true);
   137        marshaller.setProperty("jaxb.encoding", blog.getCharacterEncoding());
   138        FileWriter writer = new FileWriter(destination);
   139        marshaller.marshal(jaxbElement, writer);
   140        writer.flush();
   141        writer.close();
   142      } catch (Exception e) {
   143        log.error(e.getMessage(), e);
   144        e.printStackTrace();
   145        throw new PersistenceException(e.getMessage());
   146      }
   147    }
   148  
   149  }








SofCheck Inspector Build Version : 2.22510
filecategorydao.java 2010-Jun-25 19:40:32
filecategorydao.class 2010-Jul-19 20:23:40