Coverage Report - net.sourceforge.pebble.domain.CategoryBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
CategoryBuilder
93%
61/65
96%
25/26
2.364
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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.domain;
 33  
 
 34  
 import java.util.ArrayList;
 35  
 import java.util.Collections;
 36  
 import java.util.Iterator;
 37  
 import java.util.List;
 38  
 
 39  
 import net.sourceforge.pebble.util.I18n;
 40  
 
 41  
 /**
 42  
  * A class to manage blog categories.
 43  
  *
 44  
  * @author    Simon Brown
 45  
  */
 46  
 public class CategoryBuilder {
 47  
 
 48  
   /** the category separator */
 49  
   private static final String CATEGORY_SEPARATOR = "/";
 50  
 
 51  
   /** the owning blog */
 52  
   private Blog blog;
 53  
 
 54  
   /** the root category */
 55  
   private Category rootCategory;
 56  
 
 57  
   /**
 58  
    * Creates a new instance.
 59  
    */
 60  2776
   public CategoryBuilder(Blog blog) {
 61  2776
     this.blog = blog;
 62  2776
   }
 63  
 
 64  
   /**
 65  
    * Creates a new instance.
 66  
    */
 67  836
   public CategoryBuilder(Blog blog, Category rootCategory) {
 68  836
     this.blog = blog;
 69  836
     this.rootCategory = rootCategory;
 70  836
   }
 71  
 
 72  
   /**
 73  
    * Adds a category.
 74  
    *
 75  
    * @param category    a Category instance
 76  
    */
 77  
   public void addCategory(Category category) {
 78  3000
     category.setBlog(blog);
 79  
 
 80  3000
     if (category.isRootCategory()) {
 81  2776
       this.rootCategory = category;
 82  
     } else {
 83  224
       Category parent = getParent(category, true);
 84  224
       parent.addSubCategory(category);
 85  
     }
 86  3000
   }
 87  
 
 88  
   /**
 89  
    * Removes a category.
 90  
    *
 91  
    * @param category    a Category instance
 92  
    */
 93  
   public void removeCategory(Category category) {
 94  0
     Category parent = getParent(category);
 95  0
     parent.removeSubCategory(category);
 96  0
   }
 97  
 
 98  
   /**
 99  
    * Gets (and creates if necessary), the parent of the specified category.
 100  
    *
 101  
    * @param category    the Category to find the parent of
 102  
    * @param create      true if the parent should be created if it doesn't
 103  
    *                    exist, false otherwise
 104  
    * @return  a Category instance
 105  
    *
 106  
    */
 107  
   private Category getParent(Category category, boolean create) {
 108  224
     String id = category.getId();
 109  224
     int index = id.lastIndexOf(CATEGORY_SEPARATOR);
 110  224
     String parentId = id.substring(0, index);
 111  224
     if (parentId.equals("")) {
 112  
       // the parent is the root category
 113  196
       parentId = "/";
 114  
     }
 115  
 
 116  224
     return getCategory(parentId, create);
 117  
   }
 118  
 
 119  
   /**
 120  
    * Gets the parent of the specified category.
 121  
    *
 122  
    * @return  a Category instance
 123  
    */
 124  
   private Category getParent(Category category) {
 125  0
     return getParent(category, false);
 126  
   }
 127  
 
 128  
   /**
 129  
    * Gets (and creates if necessary), the specified category.
 130  
    *
 131  
    * @param id          the id of the category to find
 132  
    * @param create      true if the category should be created if it doesn't
 133  
    *                    exist, false otherwise
 134  
    * @return  a Category instance
 135  
    */
 136  
   private Category getCategory(String id, boolean create) {
 137  4116
     if (id == null || !id.startsWith("/")) {
 138  12
       id = "/" + id;
 139  
     }
 140  
 
 141  4116
     if (id.equals("/")) {
 142  3744
       if (rootCategory == null) {
 143  2760
         Category category = new Category("/", I18n.getMessage(blog.getLocale(), "category.all"));
 144  2760
         addCategory(category);
 145  
       }
 146  
 
 147  3744
       return rootCategory;
 148  
     } else {
 149  372
       Category parentCategory = getRootCategory();
 150  372
       Category category = null;
 151  372
       boolean found = false;
 152  372
       int index = 0;
 153  
       do {
 154  376
         index = id.indexOf("/", index+1);
 155  
         String categoryId;
 156  376
         if (index == -1) {
 157  372
           categoryId = id.substring(0, id.length());
 158  
         } else {
 159  4
           categoryId = id.substring(0, index);
 160  
         }
 161  
 
 162  376
         found = false;
 163  376
         Iterator it = parentCategory.getSubCategories().iterator();
 164  456
         while (it.hasNext()) {
 165  208
           category = (Category)it.next();
 166  208
           if (category.getId().equals(categoryId)) {
 167  128
             found = true;
 168  128
             break;
 169  
           }
 170  
         }
 171  
 
 172  376
         if (!found) {
 173  248
           if (create) {
 174  8
             category = new Category(categoryId, categoryId);
 175  8
             addCategory(category);
 176  
           } else {
 177  240
             return null;
 178  
           }
 179  
         }
 180  
 
 181  136
         parentCategory = category;
 182  136
       } while (index != -1);
 183  
 
 184  132
       return category;
 185  
     }
 186  
   }
 187  
 
 188  
   /**
 189  
    * Gets the specified category.
 190  
    *
 191  
    * @param id          the id of the category to find
 192  
    * @return  a Category instance, or null if not found
 193  
    */
 194  
   public Category getCategory(String id) {
 195  344
     return getCategory(id, false);
 196  
   }
 197  
 
 198  
   /**
 199  
    * Gets the root category.
 200  
    *
 201  
    * @return  a Category instance
 202  
    */
 203  
   public Category getRootCategory() {
 204  3548
     return getCategory("/", true);
 205  
   }
 206  
 
 207  
   /**
 208  
    * Gets a collection containing all blog categories,
 209  
    * ordered by category name.
 210  
    *
 211  
    * @return  a sorted List of Category instances
 212  
    */
 213  
   public List<Category> getCategories() {
 214  376
     List<Category> allCategories = new ArrayList<Category>();
 215  376
     allCategories.addAll(getCategories(getRootCategory()));
 216  376
     Collections.sort(allCategories);
 217  
 
 218  376
     return allCategories;
 219  
   }
 220  
 
 221  
   public List<Category> getCategories(Category category) {
 222  480
     List<Category> allCategories = new ArrayList<Category>();
 223  480
     allCategories.add(category);
 224  480
     Iterator it = category.getSubCategories().iterator();
 225  584
     while (it.hasNext()) {
 226  104
       allCategories.addAll(getCategories((Category)it.next()));
 227  
     }
 228  
 
 229  480
     return allCategories;
 230  
   }
 231  
 
 232  
 }