File Source: blogentryindex.java

     1  package net.sourceforge.pebble.index;
     2  
     3  import net.sourceforge.pebble.comparator.ReverseBlogEntryIdComparator;
     4  import net.sourceforge.pebble.domain.Blog;
     5  import net.sourceforge.pebble.domain.BlogEntry;
     6  import net.sourceforge.pebble.domain.Day;
     7  import org.apache.commons.logging.Log;
     8  import org.apache.commons.logging.LogFactory;
     9  
    10  import java.io.*;
    11  import java.util.*;
    12  
    13  /**
    14   * Keeps an index of all blog entries, allowing efficient access at runtime.
    15   *
    16   * @author    Simon Brown
    17   */
    18  public class BlogEntryIndex {
    19  
           /* 
    P/P     *  Method: net.sourceforge.pebble.index.BlogEntryIndex__static_init
            * 
            *  Postconditions:
            *    init'ed(log)
            */
    20    private static final Log log = LogFactory.getLog(BlogEntryIndex.class);
    21  
    22    private Blog blog;
    23  
    24    private List<String> indexEntries = new ArrayList<String>();
    25    private List<String> publishedIndexEntries = new ArrayList<String>();
    26    private List<String> unpublishedIndexEntries = new ArrayList<String>();
    27  
           /* 
    P/P     *  Method: void net.sourceforge.pebble.index.BlogEntryIndex(Blog)
            * 
            *  Preconditions:
            *    blog != null
            * 
            *  Postconditions:
            *    this.blog == blog
            *    this.blog != null
            *    this.indexEntries == &new ArrayList(BlogEntryIndex#1)
            *    this.publishedIndexEntries == &new ArrayList(BlogEntryIndex#2)
            *    this.unpublishedIndexEntries == &new ArrayList(BlogEntryIndex#3)
            *    new ArrayList(BlogEntryIndex#1) num objects == 1
            *    new ArrayList(BlogEntryIndex#2) num objects == 1
            *    new ArrayList(BlogEntryIndex#3) num objects == 1
            * 
            *  Preconditions:
            *    (soft) blog.properties != null
            *    (soft) blog.years != null
            */
    28    public BlogEntryIndex(Blog blog) {
    29      this.blog = blog;
    30  
    31      readIndex(true);
    32      readIndex(false);
    33    }
    34  
    35    /**
    36     * Clears the index.
    37     */
    38    public void clear() {
             /* 
    P/P       *  Method: void clear()
              * 
              *  Preconditions:
              *    (soft) this.blog != null
              * 
              *  Postconditions:
              *    this.indexEntries == &new ArrayList(clear#1)
              *    this.publishedIndexEntries == &new ArrayList(clear#2)
              *    this.unpublishedIndexEntries == &new ArrayList(clear#3)
              *    new ArrayList(clear#1) num objects == 1
              *    new ArrayList(clear#2) num objects == 1
              *    new ArrayList(clear#3) num objects == 1
              */
    39      indexEntries = new ArrayList<String>();
    40      publishedIndexEntries = new ArrayList<String>();
    41      unpublishedIndexEntries = new ArrayList<String>();
    42      writeIndex(true);
    43      writeIndex(false);
    44    }
    45  
    46    /**
    47     * Indexes one or more blog entries.
    48     *
    49     * @param blogEntries   a List of BlogEntry instances
    50     */
    51    public synchronized void index(Collection<BlogEntry> blogEntries) {
             /* 
    P/P       *  Method: void index(Collection)
              * 
              *  Preconditions:
              *    blogEntries != null
              *    (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#5).name != null
              *    (soft) this.blog != null
              *    (soft) this.indexEntries != null
              *    (soft) this.publishedIndexEntries != null
              *    (soft) this.unpublishedIndexEntries != null
              * 
              *  Presumptions:
              *    blogEntry.state@52 != null
              *    java.util.Iterator:next(...)@52 != null
              * 
              *  Test Vectors:
              *    java.util.Iterator:hasNext(...)@52: {1}, {0}
              * 
              *  Preconditions:
              *    (soft) this.blog.properties != null
              *    (soft) this.blog.years != null
              * 
              *  Presumptions:
              *    day.blogEntries@53 != null
              *    day.publishedBlogEntries@53 != null
              *    day.unpublishedBlogEntries@53 != null
              */
    52      for (BlogEntry blogEntry : blogEntries) {
    53        Day day = blog.getBlogForDay(blogEntry.getDate());
    54        if (blogEntry.isPublished()) {
    55          publishedIndexEntries.add(blogEntry.getId());
+   56          day.addPublishedBlogEntry(blogEntry.getId());
    57        } else {
    58          unpublishedIndexEntries.add(blogEntry.getId());
+   59          day.addUnpublishedBlogEntry(blogEntry.getId());
    60        }
    61        indexEntries.add(blogEntry.getId());
    62      }
    63  
    64      Collections.sort(indexEntries, new ReverseBlogEntryIdComparator());
    65      Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator());
    66      Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator());
    67  
    68      writeIndex(true);
    69      writeIndex(false);
    70    }
    71  
    72    /**
    73     * Indexes a single blog entry.
    74     *
    75     * @param blogEntry   a BlogEntry instance
    76     */
    77    public synchronized void index(BlogEntry blogEntry) {
             /* 
    P/P       *  Method: void index(BlogEntry)
              * 
              *  Preconditions:
              *    blogEntry != null
              *    init'ed(blogEntry.date)
              *    init'ed(blogEntry.id)
              *    blogEntry.state != null
              *    this.blog != null
              *    this.indexEntries != null
              *    (soft) init'ed(blogEntry.state.name)
              *    (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#5).name != null
              *    (soft) this.publishedIndexEntries != null
              *    (soft) this.unpublishedIndexEntries != null
              *    ...
              * 
              *  Presumptions:
              *    day.blogEntries@78 != null
              *    day.publishedBlogEntries@78 != null
              *    day.unpublishedBlogEntries@78 != null
              */
    78      Day day = blog.getBlogForDay(blogEntry.getDate());
    79      if (blogEntry.isPublished()) {
    80        publishedIndexEntries.add(blogEntry.getId());
+   81        day.addPublishedBlogEntry(blogEntry.getId());
    82        writeIndex(true);
    83      } else {
    84        unpublishedIndexEntries.add(blogEntry.getId());
+   85        day.addUnpublishedBlogEntry(blogEntry.getId());
    86        writeIndex(false);
    87      }
    88      indexEntries.add(blogEntry.getId());
    89  
    90      Collections.sort(indexEntries, new ReverseBlogEntryIdComparator());
    91      Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator());
    92      Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator());
    93    }
    94  
    95    /**
    96     * Unindexes a single blog entry.
    97     *
    98     * @param blogEntry   a BlogEntry instance
    99     */
   100    public synchronized void unindex(BlogEntry blogEntry) {
             /* 
    P/P       *  Method: void unindex(BlogEntry)
              * 
              *  Preconditions:
              *    blogEntry != null
              *    init'ed(blogEntry.date)
              *    init'ed(blogEntry.id)
              *    this.blog != null
              *    this.indexEntries != null
              *    this.publishedIndexEntries != null
              *    this.unpublishedIndexEntries != null
              *    this.blog.properties != null
              *    this.blog.years != null
              * 
              *  Presumptions:
              *    day.blogEntries@101 != null
              *    day.publishedBlogEntries@101 != null
              *    day.unpublishedBlogEntries@101 != null
              */
   101      Day day = blog.getBlogForDay(blogEntry.getDate());
+  102      day.removeBlogEntry(blogEntry);
   103  
   104      indexEntries.remove(blogEntry.getId());
   105      publishedIndexEntries.remove(blogEntry.getId());
   106      unpublishedIndexEntries.remove(blogEntry.getId());
   107  
   108      writeIndex(true);
   109      writeIndex(false);
   110    }
   111  
   112    /**
   113     * Helper method to load the index.
   114     */
   115    private void readIndex(boolean published) {
   116      File indexFile;
             /* 
    P/P       *  Method: void readIndex(bool)
              * 
              *  Preconditions:
              *    this.blog != null
              *    (soft) this.indexEntries != null
              *    (soft) this.publishedIndexEntries != null
              *    (soft) this.unpublishedIndexEntries != null
              * 
              *  Presumptions:
              *    org.apache.commons.logging.LogFactory:getLog(...)@20 != null
              * 
              *  Test Vectors:
              *    published: {0}, {1}
              *    java.io.File:exists(...)@123: {0}, {1}
              * 
              *  Preconditions:
              *    (soft) this.blog.properties != null
              *    (soft) this.blog.years != null
              * 
              *  Presumptions:
              *    day.blogEntries@132 != null
              *    day.publishedBlogEntries@132 != null
              *    day.unpublishedBlogEntries@132 != null
              */
   117      if (published) {
   118        indexFile = new File(blog.getIndexesDirectory(), "blogentries-published.index");
   119      } else {
   120        indexFile = new File(blog.getIndexesDirectory(), "blogentries-unpublished.index");
   121      }
   122  
   123      if (indexFile.exists()) {
   124        try {
   125          BufferedReader reader = new BufferedReader(new FileReader(indexFile));
   126          String indexEntry = reader.readLine();
   127          while (indexEntry != null) {
   128            indexEntries.add(indexEntry);
   129  
   130            // and add it to the internal memory structures
   131            Date date = new Date(Long.parseLong(indexEntry));
   132            Day day = blog.getBlogForDay(date);
   133  
   134            if (published) {
   135              publishedIndexEntries.add(indexEntry);
+  136              day.addPublishedBlogEntry(indexEntry);
   137            } else {
   138              unpublishedIndexEntries.add(indexEntry);
+  139              day.addUnpublishedBlogEntry(indexEntry);
   140            }
   141  
   142            indexEntry = reader.readLine();
   143          }
   144  
   145          reader.close();
   146        } catch (Exception e) {
   147          log.error("Error while reading index", e);
   148        }
   149      }
   150  
   151      Collections.sort(indexEntries, new ReverseBlogEntryIdComparator());
   152      Collections.sort(publishedIndexEntries, new ReverseBlogEntryIdComparator());
   153      Collections.sort(unpublishedIndexEntries, new ReverseBlogEntryIdComparator());
   154    }
   155  
   156    /**
   157     * Helper method to write out the index to disk.
   158     */
   159    private void writeIndex(boolean published) {
   160      try {
   161        File indexFile;
               /* 
    P/P         *  Method: void writeIndex(bool)
                * 
                *  Preconditions:
                *    (soft) this.blog != null
                *    (soft) this.publishedIndexEntries != null
                *    (soft) this.unpublishedIndexEntries != null
                * 
                *  Presumptions:
                *    org.apache.commons.logging.LogFactory:getLog(...)@20 != null
                * 
                *  Test Vectors:
                *    published: {0}, {1}
                *    java.util.Iterator:hasNext(...)@175: {1}, {0}
                */
   162        if (published) {
   163          indexFile = new File(blog.getIndexesDirectory(), "blogentries-published.index");
   164        } else {
   165          indexFile = new File(blog.getIndexesDirectory(), "blogentries-unpublished.index");
   166        }
   167        BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));
   168  
   169        if (published) {
   170          for (String indexEntry : publishedIndexEntries) {
   171            writer.write(indexEntry);
   172            writer.newLine();
   173          }
   174        } else {
   175          for (String indexEntry : unpublishedIndexEntries) {
   176            writer.write(indexEntry);
   177            writer.newLine();
   178          }
   179        }
   180  
   181        writer.flush();
   182        writer.close();
   183      } catch (Exception e) {
   184        log.error("Error while writing index", e);
   185      }
   186    }
   187  
   188    /**
   189     * Gets the number of blog entries for this blog.
   190     *
   191     * @return  an int
   192     */
   193    public int getNumberOfBlogEntries() {
             /* 
    P/P       *  Method: int getNumberOfBlogEntries()
              * 
              *  Preconditions:
              *    this.indexEntries != null
              * 
              *  Postconditions:
              *    init'ed(return_value)
              */
   194      return indexEntries.size();
   195    }
   196  
   197    /**
   198     * Gets the number of published blog entries for this blog.
   199     *
   200     * @return  an int
   201     */
   202    public int getNumberOfPublishedBlogEntries() {
             /* 
    P/P       *  Method: int getNumberOfPublishedBlogEntries()
              * 
              *  Preconditions:
              *    this.publishedIndexEntries != null
              * 
              *  Postconditions:
              *    init'ed(return_value)
              */
   203      return publishedIndexEntries.size();
   204    }
   205  
   206    /**
   207     * Gets the number of unpublished blog entries for this blog.
   208     *
   209     * @return  an int
   210     */
   211    public int getNumberOfUnpublishedBlogEntries() {
             /* 
    P/P       *  Method: int getNumberOfUnpublishedBlogEntries()
              * 
              *  Preconditions:
              *    this.unpublishedIndexEntries != null
              * 
              *  Postconditions:
              *    init'ed(return_value)
              */
   212      return unpublishedIndexEntries.size();
   213    }
   214  
   215    /**
   216     * Gets the full list of blog entries.
   217     *
   218     * @return  a List of blog entry IDs
   219     */
   220    public List<String> getBlogEntries() {
             /* 
    P/P       *  Method: List getBlogEntries()
              * 
              *  Preconditions:
              *    init'ed(this.indexEntries)
              * 
              *  Postconditions:
              *    return_value == &new ArrayList(getBlogEntries#1)
              *    new ArrayList(getBlogEntries#1) num objects == 1
              */
   221      return new ArrayList<String>(indexEntries);
   222    }
   223  
   224    /**
   225     * Gets the full list of published blog entries.
   226     *
   227     * @return  a List of blog entry IDs
   228     */
   229    public List<String> getPublishedBlogEntries() {
             /* 
    P/P       *  Method: List getPublishedBlogEntries()
              * 
              *  Preconditions:
              *    init'ed(this.publishedIndexEntries)
              * 
              *  Postconditions:
              *    return_value == &new ArrayList(getPublishedBlogEntries#1)
              *    new ArrayList(getPublishedBlogEntries#1) num objects == 1
              */
   230      return new ArrayList<String>(publishedIndexEntries);
   231    }
   232  
   233    /**
   234     * Gets the full list of unpublished blog entries.
   235     *
   236     * @return  a List of blog entry IDs
   237     */
   238    public List<String> getUnpublishedBlogEntries() {
             /* 
    P/P       *  Method: List getUnpublishedBlogEntries()
              * 
              *  Preconditions:
              *    init'ed(this.unpublishedIndexEntries)
              * 
              *  Postconditions:
              *    return_value == &new ArrayList(getUnpublishedBlogEntries#1)
              *    new ArrayList(getUnpublishedBlogEntries#1) num objects == 1
              */
   239      return new ArrayList<String>(unpublishedIndexEntries);
   240    }
   241  
   242  }








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