Coverage Report - net.sourceforge.pebble.index.CategoryIndex
 
Classes in this File Line Coverage Branch Coverage Complexity
CategoryIndex
68%
40/58
60%
17/28
3
 
 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.index;
 33  
 
 34  
 import net.sourceforge.pebble.domain.Blog;
 35  
 import net.sourceforge.pebble.domain.BlogEntry;
 36  
 import net.sourceforge.pebble.domain.Category;
 37  
 import org.apache.commons.logging.Log;
 38  
 import org.apache.commons.logging.LogFactory;
 39  
 
 40  
 import java.util.ArrayList;
 41  
 import java.util.List;
 42  
 import java.util.Collection;
 43  
 import java.io.*;
 44  
 
 45  
 /**
 46  
  * Represents the category index for a blog.
 47  
  *
 48  
  * @author    Simon Brown
 49  
  */
 50  
 public class CategoryIndex {
 51  
 
 52  4
   private static final Log log = LogFactory.getLog(CategoryIndex.class);
 53  
 
 54  
   private Blog blog;
 55  
 
 56  2728
   public CategoryIndex(Blog blog) {
 57  2728
     this.blog = blog;
 58  
 
 59  
 //    File indexes = new File(blog.getIndexesDirectory());
 60  
 //    if (!indexes.exists()) {
 61  
 //      indexes.mkdir();
 62  
 //    }
 63  2728
     readIndex();
 64  2728
   }
 65  
 
 66  
   /**
 67  
    * Clears the index.
 68  
    */
 69  
   public void clear() {
 70  36
     for (Category category : blog.getCategories()) {
 71  36
       category.removeAllBlogEntries();
 72  
     }
 73  
 
 74  36
     writeIndex();
 75  36
   }
 76  
 
 77  
   /**
 78  
    * Indexes one or more blog entries.
 79  
    *
 80  
    * @param blogEntries   a List of BlogEntry instances
 81  
    */
 82  
   public synchronized void index(Collection<BlogEntry> blogEntries) {
 83  36
     for (BlogEntry blogEntry : blogEntries) {
 84  36
       if (blogEntry.isPublished()) {
 85  36
         for (Category category: blogEntry.getCategories()) {
 86  0
           category.addBlogEntry(blogEntry.getId());
 87  
         }
 88  
       }
 89  
     }
 90  
 
 91  36
     writeIndex();
 92  36
   }
 93  
 
 94  
   /**
 95  
    * Indexes a single blog entry.
 96  
    *
 97  
    * @param blogEntry   a BlogEntry instance
 98  
    */
 99  
   public synchronized void index(BlogEntry blogEntry) {
 100  192
     if (blogEntry.isPublished()) {
 101  188
       for (Category category : blogEntry.getCategories()) {
 102  68
         category.addBlogEntry(blogEntry.getId());
 103  
       }
 104  
 
 105  188
       writeIndex();
 106  
     }
 107  192
   }
 108  
 
 109  
   /**
 110  
    * Unindexes a single blog entry.
 111  
    *
 112  
    * @param blogEntry   a BlogEntry instance
 113  
    */
 114  
   public synchronized void unindex(BlogEntry blogEntry) {
 115  12
     for (Category category : blog.getCategories()) {
 116  16
       category.removeBlogEntry(blogEntry.getId());
 117  
     }
 118  
 
 119  12
     writeIndex();
 120  12
   }
 121  
 
 122  
   /**
 123  
    * Helper method to load the index.
 124  
    */
 125  
   private void readIndex() {
 126  2728
     File indexFile = new File(blog.getIndexesDirectory(), "categories.index");
 127  2728
     if (indexFile.exists()) {
 128  
       try {
 129  0
         BufferedReader reader = new BufferedReader(new FileReader(indexFile));
 130  0
         String indexEntry = reader.readLine();
 131  0
         while (indexEntry != null) {
 132  0
           String[] tuple = indexEntry.split("=");
 133  0
           Category category = blog.getCategory(tuple[0]);
 134  
 
 135  0
           if (tuple.length > 1 && tuple[1] != null) {
 136  0
             String[] blogEntries = tuple[1].split(",");
 137  0
             for (String blogEntry : blogEntries) {
 138  0
               category.addBlogEntry(blogEntry);
 139  
             }
 140  
           }
 141  
 
 142  0
           indexEntry = reader.readLine();
 143  0
         }
 144  
 
 145  0
         reader.close();
 146  0
       } catch (Exception e) {
 147  0
         log.error("Error while reading index", e);
 148  0
       }
 149  
     }
 150  2728
   }
 151  
 
 152  
   /**
 153  
    * Helper method to write out the index to disk.
 154  
    */
 155  
   private void writeIndex() {
 156  
     try {
 157  272
       File indexFile = new File(blog.getIndexesDirectory(), "categories.index");
 158  272
       BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));
 159  
 
 160  272
       for (Category category : blog.getCategories()) {
 161  352
         writer.write(category.getId());
 162  352
         writer.write("=");
 163  352
         for (String blogEntry : category.getBlogEntries()) {
 164  136
           writer.write(blogEntry);
 165  136
           writer.write(",");
 166  
         }
 167  352
         writer.newLine();
 168  
       }
 169  
 
 170  272
       writer.flush();
 171  272
       writer.close();
 172  0
     } catch (Exception e) {
 173  0
       log.error("Error while writing index", e);
 174  272
     }
 175  272
   }
 176  
 
 177  
   /**
 178  
    * Gets the the list of blog entries for a given category.
 179  
    *
 180  
    * @param category    a category
 181  
    * @return  a List of blog entry IDs
 182  
    */
 183  
   public List<String> getRecentBlogEntries(Category category) {
 184  12
     return new ArrayList<String>(category.getBlogEntries());
 185  
   }
 186  
 
 187  
 }