1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
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 | |
|
47 | |
|
48 | |
|
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 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 2728 | readIndex(); |
64 | 2728 | } |
65 | |
|
66 | |
|
67 | |
|
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 | |
|
79 | |
|
80 | |
|
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 | |
|
96 | |
|
97 | |
|
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 | |
|
111 | |
|
112 | |
|
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 | |
|
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 | |
|
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 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
public List<String> getRecentBlogEntries(Category category) { |
184 | 12 | return new ArrayList<String>(category.getBlogEntries()); |
185 | |
} |
186 | |
|
187 | |
} |