categoryindex.java

### File Source: categoryindex.java

         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
               /*
        P/P     *  Method: net.sourceforge.pebble.index.CategoryIndex__static_init
                *
                *  Postconditions:
                *    init'ed(log)
                */
        52    private static final Log log = LogFactory.getLog(CategoryIndex.class);
        53
        54    private Blog blog;
        55
               /*
        P/P     *  Method: void net.sourceforge.pebble.index.CategoryIndex(Blog)
                *
                *  Preconditions:
                *    blog != null
                *
                *  Postconditions:
                *    this.blog == blog
                *    this.blog != null
                *
                *  Preconditions:
                *    (soft) init'ed(blog.rootCategory)
                *
                *  Postconditions:
                *    init'ed(new ArrayList(Category#1) num objects)
                *    init'ed(new ArrayList(Category#2) num objects)
                *    init'ed(new ArrayList(Category#3) num objects)
                *    init'ed(new Category(getCategory#2*) num objects)
                *    init'ed(new Category(getCategory#2*).blog)
                *    init'ed(new Category(getCategory#2*).blogEntries)
                *    init'ed(new Category(getCategory#2*).id)
                *    init'ed(new Category(getCategory#2*).name)
                *    init'ed(new Category(getCategory#2*).parent)
                *    init'ed(new Category(getCategory#2*).subCategories)
                *    ...
                */
        56    public CategoryIndex(Blog blog) {
        57      this.blog = blog;
        58
        59  //    File indexes = new File(blog.getIndexesDirectory());
        60  //    if (!indexes.exists()) {
        61  //      indexes.mkdir();
        62  //    }
        63      readIndex();
        64    }
        65
        66    /**
        67     * Clears the index.
        68     */
        69    public void clear() {
                 /*
        P/P       *  Method: void clear()
                  *
                  *  Preconditions:
                  *    this.blog != null
                  *
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@70 != null
                  *
                  *  Postconditions:
                  *    init'ed(new ArrayList(removeAllBlogEntries#1) num objects)
                  *
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@70: {1}, {0}
                  *
                  *  Preconditions:
                  *    init'ed(this.blog.rootCategory)
                  *
                  *  Postconditions:
                  *    init'ed(new ArrayList(Category#1) num objects)
                  *    init'ed(new ArrayList(Category#2) num objects)
                  *    init'ed(new ArrayList(Category#3) num objects)
                  *    init'ed(new Category(getCategory#2) num objects)
                  *    possibly_updated(new Category(getCategory#2).blog)
                  *    init'ed(new Category(getCategory#2).blogEntries)
                  *    possibly_updated(new Category(getCategory#2).id)
                  *    init'ed(new Category(getCategory#2).name)
                  *    possibly_updated(new Category(getCategory#2).parent)
                  *    possibly_updated(new Category(getCategory#2).subCategories)
                  *    ...
                  */
        70      for (Category category : blog.getCategories()) {
        71        category.removeAllBlogEntries();
        72      }
        73
        74      writeIndex();
        75    }
        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) {
                 /*
        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
                  *
                  *  Presumptions:
                  *    blogEntry.state@83 != null
                  *    category.blogEntries@85 != null
                  *    java.util.Iterator:next(...)@83 != null
                  *    java.util.Iterator:next(...)@85 != null
                  *
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@83: {1}, {0}
                  *    java.util.Iterator:hasNext(...)@85: {1}, {0}
                  *
                  *  Preconditions:
                  *    (soft) init'ed(this.blog.rootCategory)
                  *
                  *  Postconditions:
                  *    init'ed(new ArrayList(Category#1) num objects)
                  *    init'ed(new ArrayList(Category#2) num objects)
                  *    init'ed(new ArrayList(Category#3) num objects)
                  *    init'ed(new Category(getCategory#2) num objects)
                  *    possibly_updated(new Category(getCategory#2).blog)
                  *    init'ed(new Category(getCategory#2).blogEntries)
                  *    possibly_updated(new Category(getCategory#2).id)
                  *    init'ed(new Category(getCategory#2).name)
                  *    possibly_updated(new Category(getCategory#2).parent)
                  *    init'ed(new Category(getCategory#2).subCategories)
                  *    ...
                  */
        83      for (BlogEntry blogEntry : blogEntries) {
        84        if (blogEntry.isPublished()) {
        85          for (Category category: blogEntry.getCategories()) {
        86            category.addBlogEntry(blogEntry.getId());
        87          }
        88        }
        89      }
        90
        91      writeIndex();
        92    }
        93
        94    /**
        95     * Indexes a single blog entry.
        96     *
        97     * @param blogEntry   a BlogEntry instance
        98     */
        99    public synchronized void index(BlogEntry blogEntry) {
                 /*
        P/P       *  Method: void index(BlogEntry)
                  *
                  *  Preconditions:
                  *    blogEntry != null
                  *    blogEntry.state != null
                  *    (soft) init'ed(blogEntry.categories)
                  *    (soft) init'ed(blogEntry.id)
                  *    (soft) init'ed(blogEntry.state.name)
                  *    (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#5).name != null
                  *    (soft) this.blog != null
                  *
                  *  Presumptions:
                  *    category.blogEntries@101 != null
                  *    java.util.Iterator:next(...)@101 != null
                  *
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@101: {1}, {0}
                  *
                  *  Preconditions:
                  *    (soft) init'ed(this.blog.rootCategory)
                  *
                  *  Postconditions:
                  *    init'ed(new ArrayList(Category#1) num objects)
                  *    init'ed(new ArrayList(Category#2) num objects)
                  *    init'ed(new ArrayList(Category#3) num objects)
                  *    init'ed(new Category(getCategory#2) num objects)
                  *    possibly_updated(new Category(getCategory#2).blog)
                  *    init'ed(new Category(getCategory#2).blogEntries)
                  *    possibly_updated(new Category(getCategory#2).id)
                  *    init'ed(new Category(getCategory#2).name)
                  *    possibly_updated(new Category(getCategory#2).parent)
                  *    init'ed(new Category(getCategory#2).subCategories)
                  *    ...
                  */
       100      if (blogEntry.isPublished()) {
       101        for (Category category : blogEntry.getCategories()) {
       102          category.addBlogEntry(blogEntry.getId());
       103        }
       104
       105        writeIndex();
       106      }
       107    }
       108
       109    /**
       110     * Unindexes a single blog entry.
       111     *
       112     * @param blogEntry   a BlogEntry instance
       113     */
       114    public synchronized void unindex(BlogEntry blogEntry) {
                 /*
        P/P       *  Method: void unindex(BlogEntry)
                  *
                  *  Preconditions:
                  *    this.blog != null
                  *    (soft) blogEntry != null
                  *    (soft) init'ed(blogEntry.id)
                  *
                  *  Presumptions:
                  *    category.blogEntries@115 != null
                  *    java.util.Iterator:next(...)@115 != null
                  *
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@115: {1}, {0}
                  *
                  *  Preconditions:
                  *    init'ed(this.blog.rootCategory)
                  *
                  *  Postconditions:
                  *    init'ed(new ArrayList(Category#1) num objects)
                  *    init'ed(new ArrayList(Category#2) num objects)
                  *    init'ed(new ArrayList(Category#3) num objects)
                  *    init'ed(new Category(getCategory#2) num objects)
                  *    possibly_updated(new Category(getCategory#2).blog)
                  *    init'ed(new Category(getCategory#2).blogEntries)
                  *    possibly_updated(new Category(getCategory#2).id)
                  *    init'ed(new Category(getCategory#2).name)
                  *    possibly_updated(new Category(getCategory#2).parent)
                  *    possibly_updated(new Category(getCategory#2).subCategories)
                  *    ...
                  */
       115      for (Category category : blog.getCategories()) {
       116        category.removeBlogEntry(blogEntry.getId());
       117      }
       118
       119      writeIndex();
       120    }
       121
       122    /**
       123     * Helper method to load the index.
       124     */
       125    private void readIndex() {
                 /*
        P/P       *  Method: void readIndex()
                  *
                  *  Preconditions:
                  *    this.blog != null
                  *
                  *  Presumptions:
                  *    blogEntries.length@136 <= 232-1
                  *    org.apache.commons.logging.LogFactory:getLog(...)@52 != null
                  *    tuple.length@132 >= 1
                  *
                  *  Test Vectors:
                  *    java.io.File:exists(...)@127: {0}, {1}
                  *    tuple.length@132: {1}, {2..+Inf}
                  *    tuple[1]@132: Addr_Set{null}, Inverse{null}
                  *
                  *  Preconditions:
                  *    (soft) init'ed(this.blog.rootCategory)
                  *
                  *  Presumptions:
                  *    category...blogEntries@133 != null
                  *    category.blogEntries@133 != null
                  *
                  *  Postconditions:
                  *    init'ed(new ArrayList(Category#1) num objects)
                  *    init'ed(new ArrayList(Category#2) num objects)
                  *    init'ed(new ArrayList(Category#3) num objects)
                  *    init'ed(new Category(getCategory#2*) num objects)
                  *    init'ed(new Category(getCategory#2*).blog)
                  *    init'ed(new Category(getCategory#2*).blogEntries)
                  *    init'ed(new Category(getCategory#2*).id)
                  *    init'ed(new Category(getCategory#2*).name)
                  *    init'ed(new Category(getCategory#2*).parent)
                  *    init'ed(new Category(getCategory#2*).subCategories)
                  *    ...
                  */
       126      File indexFile = new File(blog.getIndexesDirectory(), "categories.index");
       127      if (indexFile.exists()) {
       128        try {
       129          BufferedReader reader = new BufferedReader(new FileReader(indexFile));
       130          String indexEntry = reader.readLine();
       131          while (indexEntry != null) {
       132            String[] tuple = indexEntry.split("=");
       133            Category category = blog.getCategory(tuple[0]);
       134
       135            if (tuple.length > 1 && tuple[1] != null) {
       136              String[] blogEntries = tuple[1].split(",");
       137              for (String blogEntry : blogEntries) {
    +  138                category.addBlogEntry(blogEntry);
       139              }
       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
       152    /**
       153     * Helper method to write out the index to disk.
       154     */
       155    private void writeIndex() {
       156      try {
                   /*
        P/P         *  Method: void writeIndex()
                    *
                    *  Preconditions:
                    *    (soft) this.blog != null
                    *
                    *  Presumptions:
                    *    java.util.Iterator:next(...)@160 != null
                    *    org.apache.commons.logging.LogFactory:getLog(...)@52 != null
                    *
                    *  Test Vectors:
                    *    java.util.Iterator:hasNext(...)@160: {1}, {0}
                    *    java.util.Iterator:hasNext(...)@163: {1}, {0}
                    *
                    *  Preconditions:
                    *    (soft) init'ed(this.blog.rootCategory)
                    *
                    *  Postconditions:
                    *    init'ed(new ArrayList(Category#1) num objects)
                    *    init'ed(new ArrayList(Category#2) num objects)
                    *    init'ed(new ArrayList(Category#3) num objects)
                    *    init'ed(new Category(getCategory#2) num objects)
                    *    possibly_updated(new Category(getCategory#2).blog)
                    *    init'ed(new Category(getCategory#2).blogEntries)
                    *    possibly_updated(new Category(getCategory#2).id)
                    *    init'ed(new Category(getCategory#2).name)
                    *    possibly_updated(new Category(getCategory#2).parent)
                    *    init'ed(new Category(getCategory#2).subCategories)
                    *    ...
                    */
       157        File indexFile = new File(blog.getIndexesDirectory(), "categories.index");
       158        BufferedWriter writer = new BufferedWriter(new FileWriter(indexFile));
       159
       160        for (Category category : blog.getCategories()) {
       161          writer.write(category.getId());
       162          writer.write("=");
       163          for (String blogEntry : category.getBlogEntries()) {
       164            writer.write(blogEntry);
       165            writer.write(",");
       166          }
       167          writer.newLine();
       168        }
       169
       170        writer.flush();
       171        writer.close();
       172      } catch (Exception e) {
       173        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    public List<String> getRecentBlogEntries(Category category) {
                 /*
        P/P       *  Method: List getRecentBlogEntries(Category)
                  *
                  *  Preconditions:
                  *    category != null
                  *    init'ed(category.blogEntries)
                  *
                  *  Postconditions:
                  *    return_value == &new ArrayList(getRecentBlogEntries#1)
                  *    new ArrayList(getRecentBlogEntries#1) num objects == 1
                  */
       184      return new ArrayList<String>(category.getBlogEntries());
       185    }
       186
       187  }

SofCheck Inspector Build Version : 2.22510

categoryindex.java | 2010-Jun-25 19:40:32
categoryindex.class | 2010-Jul-19 20:23:38