Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart7.png 33% of files have more coverage
46   187   16   5,75
10   93   0,35   8
8     2  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  CategoryIndex       Line # 50 46 0% 16 21 67,2% 0.671875
 
  (72)
 
1    /*
2    * Copyright (c) 2003-2006, 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    private static final Log log = LogFactory.getLog(CategoryIndex.class);
53   
54    private Blog blog;
55   
 
56  1338 toggle public CategoryIndex(Blog blog) {
57  1338 this.blog = blog;
58   
59    // File indexes = new File(blog.getIndexesDirectory());
60    // if (!indexes.exists()) {
61    // indexes.mkdir();
62    // }
63  1338 readIndex();
64    }
65   
66    /**
67    * Clears the index.
68    */
 
69  18 toggle public void clear() {
70  18 for (Category category : blog.getCategories()) {
71  18 category.removeAllBlogEntries();
72    }
73   
74  18 writeIndex();
75    }
76   
77    /**
78    * Indexes one or more blog entries.
79    *
80    * @param blogEntries a List of BlogEntry instances
81    */
 
82  18 toggle public synchronized void index(Collection<BlogEntry> blogEntries) {
83  18 for (BlogEntry blogEntry : blogEntries) {
84  18 if (blogEntry.isPublished()) {
85  18 for (Category category: blogEntry.getCategories()) {
86  0 category.addBlogEntry(blogEntry.getId());
87    }
88    }
89    }
90   
91  18 writeIndex();
92    }
93   
94    /**
95    * Indexes a single blog entry.
96    *
97    * @param blogEntry a BlogEntry instance
98    */
 
99  96 toggle public synchronized void index(BlogEntry blogEntry) {
100  96 if (blogEntry.isPublished()) {
101  94 for (Category category : blogEntry.getCategories()) {
102  34 category.addBlogEntry(blogEntry.getId());
103    }
104   
105  94 writeIndex();
106    }
107    }
108   
109    /**
110    * Unindexes a single blog entry.
111    *
112    * @param blogEntry a BlogEntry instance
113    */
 
114  6 toggle public synchronized void unindex(BlogEntry blogEntry) {
115  6 for (Category category : blog.getCategories()) {
116  8 category.removeBlogEntry(blogEntry.getId());
117    }
118   
119  6 writeIndex();
120    }
121   
122    /**
123    * Helper method to load the index.
124    */
 
125  1338 toggle private void readIndex() {
126  1338 File indexFile = new File(blog.getIndexesDirectory(), "categories.index");
127  1338 if (indexFile.exists()) {
128  0 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    }
144   
145  0 reader.close();
146    } catch (Exception e) {
147  0 log.error("Error while reading index", e);
148    }
149    }
150    }
151   
152    /**
153    * Helper method to write out the index to disk.
154    */
 
155  136 toggle private void writeIndex() {
156  136 try {
157  136 File indexFile = new File(blog.getIndexesDirectory(), "categories.index");
158  136 BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));
159   
160  136 for (Category category : blog.getCategories()) {
161  176 writer.write(category.getId());
162  176 writer.write("=");
163  176 for (String blogEntry : category.getBlogEntries()) {
164  68 writer.write(blogEntry);
165  68 writer.write(",");
166    }
167  176 writer.newLine();
168    }
169   
170  136 writer.flush();
171  136 writer.close();
172    } catch (Exception e) {
173  0 log.error("Error while writing index", e);
174    }
175    }
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  6 toggle public List<String> getRecentBlogEntries(Category category) {
184  6 return new ArrayList<String>(category.getBlogEntries());
185    }
186   
187    }