| 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 | |
|
| 33 | |
package net.sourceforge.pebble.index; |
| 34 | |
|
| 35 | |
import net.sourceforge.pebble.comparator.ReverseBlogEntryIdComparator; |
| 36 | |
import net.sourceforge.pebble.domain.Blog; |
| 37 | |
import net.sourceforge.pebble.domain.BlogEntry; |
| 38 | |
import net.sourceforge.pebble.domain.Day; |
| 39 | |
import org.apache.commons.logging.Log; |
| 40 | |
import org.apache.commons.logging.LogFactory; |
| 41 | |
|
| 42 | |
import java.io.*; |
| 43 | |
import java.util.*; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public class BlogEntryIndex { |
| 51 | |
|
| 52 | 4 | private static final Log log = LogFactory.getLog(BlogEntryIndex.class); |
| 53 | |
|
| 54 | |
private Blog blog; |
| 55 | |
|
| 56 | 2720 | private List<String> indexEntries = new ArrayList<String>(); |
| 57 | 2720 | private List<String> publishedIndexEntries = new ArrayList<String>(); |
| 58 | 2720 | private List<String> unpublishedIndexEntries = new ArrayList<String>(); |
| 59 | |
|
| 60 | 2720 | public BlogEntryIndex(Blog blog) { |
| 61 | 2720 | this.blog = blog; |
| 62 | |
|
| 63 | 2720 | readIndex(true); |
| 64 | 2720 | readIndex(false); |
| 65 | 2720 | } |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public void clear() { |
| 71 | 36 | indexEntries = new ArrayList<String>(); |
| 72 | 36 | publishedIndexEntries = new ArrayList<String>(); |
| 73 | 36 | unpublishedIndexEntries = new ArrayList<String>(); |
| 74 | 36 | writeIndex(true); |
| 75 | 36 | writeIndex(false); |
| 76 | 36 | } |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public synchronized void index(Collection<BlogEntry> blogEntries) { |
| 84 | 36 | for (BlogEntry blogEntry : blogEntries) { |
| 85 | 36 | Day day = blog.getBlogForDay(blogEntry.getDate()); |
| 86 | 36 | if (blogEntry.isPublished()) { |
| 87 | 36 | publishedIndexEntries.add(blogEntry.getId()); |
| 88 | 36 | day.addPublishedBlogEntry(blogEntry.getId()); |
| 89 | |
} else { |
| 90 | 0 | unpublishedIndexEntries.add(blogEntry.getId()); |
| 91 | 0 | day.addUnpublishedBlogEntry(blogEntry.getId()); |
| 92 | |
} |
| 93 | 36 | indexEntries.add(blogEntry.getId()); |
| 94 | 36 | } |
| 95 | |
|
| 96 | 36 | Collections.sort(indexEntries, new ReverseBlogEntryIdComparator()); |
| 97 | 36 | Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator()); |
| 98 | 36 | Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator()); |
| 99 | |
|
| 100 | 36 | writeIndex(true); |
| 101 | 36 | writeIndex(false); |
| 102 | 36 | } |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
public synchronized void index(BlogEntry blogEntry) { |
| 110 | 628 | Day day = blog.getBlogForDay(blogEntry.getDate()); |
| 111 | 628 | if (blogEntry.isPublished()) { |
| 112 | 184 | publishedIndexEntries.add(blogEntry.getId()); |
| 113 | 184 | day.addPublishedBlogEntry(blogEntry.getId()); |
| 114 | 184 | writeIndex(true); |
| 115 | |
} else { |
| 116 | 444 | unpublishedIndexEntries.add(blogEntry.getId()); |
| 117 | 444 | day.addUnpublishedBlogEntry(blogEntry.getId()); |
| 118 | 444 | writeIndex(false); |
| 119 | |
} |
| 120 | 628 | indexEntries.add(blogEntry.getId()); |
| 121 | |
|
| 122 | 628 | Collections.sort(indexEntries, new ReverseBlogEntryIdComparator()); |
| 123 | 628 | Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator()); |
| 124 | 628 | Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator()); |
| 125 | 628 | } |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
public synchronized void unindex(BlogEntry blogEntry) { |
| 133 | 72 | Day day = blog.getBlogForDay(blogEntry.getDate()); |
| 134 | 72 | day.removeBlogEntry(blogEntry); |
| 135 | |
|
| 136 | 72 | indexEntries.remove(blogEntry.getId()); |
| 137 | 72 | publishedIndexEntries.remove(blogEntry.getId()); |
| 138 | 72 | unpublishedIndexEntries.remove(blogEntry.getId()); |
| 139 | |
|
| 140 | 72 | writeIndex(true); |
| 141 | 72 | writeIndex(false); |
| 142 | 72 | } |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
private void readIndex(boolean published) { |
| 148 | |
File indexFile; |
| 149 | 5440 | if (published) { |
| 150 | 2720 | indexFile = new File(blog.getIndexesDirectory(), "blogentries-published.index"); |
| 151 | |
} else { |
| 152 | 2720 | indexFile = new File(blog.getIndexesDirectory(), "blogentries-unpublished.index"); |
| 153 | |
} |
| 154 | |
|
| 155 | 5440 | if (indexFile.exists()) { |
| 156 | |
try { |
| 157 | 0 | BufferedReader reader = new BufferedReader(new FileReader(indexFile)); |
| 158 | 0 | String indexEntry = reader.readLine(); |
| 159 | 0 | while (indexEntry != null) { |
| 160 | 0 | indexEntries.add(indexEntry); |
| 161 | |
|
| 162 | |
|
| 163 | 0 | Date date = new Date(Long.parseLong(indexEntry)); |
| 164 | 0 | Day day = blog.getBlogForDay(date); |
| 165 | |
|
| 166 | 0 | if (published) { |
| 167 | 0 | publishedIndexEntries.add(indexEntry); |
| 168 | 0 | day.addPublishedBlogEntry(indexEntry); |
| 169 | |
} else { |
| 170 | 0 | unpublishedIndexEntries.add(indexEntry); |
| 171 | 0 | day.addUnpublishedBlogEntry(indexEntry); |
| 172 | |
} |
| 173 | |
|
| 174 | 0 | indexEntry = reader.readLine(); |
| 175 | 0 | } |
| 176 | |
|
| 177 | 0 | reader.close(); |
| 178 | 0 | } catch (Exception e) { |
| 179 | 0 | log.error("Error while reading index", e); |
| 180 | 0 | } |
| 181 | |
} |
| 182 | |
|
| 183 | 5440 | Collections.sort(indexEntries, new ReverseBlogEntryIdComparator()); |
| 184 | 5440 | Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator()); |
| 185 | 5440 | Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator()); |
| 186 | 5440 | } |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
private void writeIndex(boolean published) { |
| 192 | |
try { |
| 193 | |
File indexFile; |
| 194 | 916 | if (published) { |
| 195 | 328 | indexFile = new File(blog.getIndexesDirectory(), "blogentries-published.index"); |
| 196 | |
} else { |
| 197 | 588 | indexFile = new File(blog.getIndexesDirectory(), "blogentries-unpublished.index"); |
| 198 | |
} |
| 199 | 916 | BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile)); |
| 200 | |
|
| 201 | 916 | if (published) { |
| 202 | 328 | for (String indexEntry : publishedIndexEntries) { |
| 203 | 248 | writer.write(indexEntry); |
| 204 | 248 | writer.newLine(); |
| 205 | |
} |
| 206 | |
} else { |
| 207 | 588 | for (String indexEntry : unpublishedIndexEntries) { |
| 208 | 772 | writer.write(indexEntry); |
| 209 | 772 | writer.newLine(); |
| 210 | |
} |
| 211 | |
} |
| 212 | |
|
| 213 | 916 | writer.flush(); |
| 214 | 916 | writer.close(); |
| 215 | 0 | } catch (Exception e) { |
| 216 | 0 | log.error("Error while writing index", e); |
| 217 | 916 | } |
| 218 | 916 | } |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
public int getNumberOfBlogEntries() { |
| 226 | 32 | return indexEntries.size(); |
| 227 | |
} |
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
public int getNumberOfPublishedBlogEntries() { |
| 235 | 0 | return publishedIndexEntries.size(); |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
public int getNumberOfUnpublishedBlogEntries() { |
| 244 | 0 | return unpublishedIndexEntries.size(); |
| 245 | |
} |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
public List<String> getBlogEntries() { |
| 253 | 88 | return new ArrayList<String>(indexEntries); |
| 254 | |
} |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
public List<String> getPublishedBlogEntries() { |
| 262 | 84 | return new ArrayList<String>(publishedIndexEntries); |
| 263 | |
} |
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
public List<String> getUnpublishedBlogEntries() { |
| 271 | 0 | return new ArrayList<String>(unpublishedIndexEntries); |
| 272 | |
} |
| 273 | |
|
| 274 | |
} |