| 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.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 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public class CategoryBuilder { |
| 47 | |
|
| 48 | |
|
| 49 | |
private static final String CATEGORY_SEPARATOR = "/"; |
| 50 | |
|
| 51 | |
|
| 52 | |
private Blog blog; |
| 53 | |
|
| 54 | |
|
| 55 | |
private Category rootCategory; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 2776 | public CategoryBuilder(Blog blog) { |
| 61 | 2776 | this.blog = blog; |
| 62 | 2776 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | 836 | public CategoryBuilder(Blog blog, Category rootCategory) { |
| 68 | 836 | this.blog = blog; |
| 69 | 836 | this.rootCategory = rootCategory; |
| 70 | 836 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 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 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public void removeCategory(Category category) { |
| 94 | 0 | Category parent = getParent(category); |
| 95 | 0 | parent.removeSubCategory(category); |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 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 | |
|
| 113 | 196 | parentId = "/"; |
| 114 | |
} |
| 115 | |
|
| 116 | 224 | return getCategory(parentId, create); |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
private Category getParent(Category category) { |
| 125 | 0 | return getParent(category, false); |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 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 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
public Category getCategory(String id) { |
| 195 | 344 | return getCategory(id, false); |
| 196 | |
} |
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
public Category getRootCategory() { |
| 204 | 3548 | return getCategory("/", true); |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 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 | |
} |