1 25 package org.infoglue.cms.controllers.kernel.impl.simple; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import org.apache.log4j.Logger; 32 import org.exolab.castor.jdo.Database; 33 import org.infoglue.cms.entities.kernel.BaseEntityVO; 34 import org.infoglue.cms.entities.management.Category; 35 import org.infoglue.cms.entities.management.CategoryVO; 36 import org.infoglue.cms.entities.management.impl.simple.CategoryImpl; 37 import org.infoglue.cms.exception.SystemException; 38 import org.infoglue.cms.security.InfoGluePrincipal; 39 40 41 50 public class CategoryController extends BaseController 51 { 52 private final static Logger logger = Logger.getLogger(CategoryController.class.getName()); 53 54 private static final CategoryController instance = new CategoryController(); 55 private static final ContentCategoryController contentCategoryStore = ContentCategoryController.getController(); 56 57 private static final String findByParent = new StringBuffer ("SELECT c ") 58 .append("FROM org.infoglue.cms.entities.management.impl.simple.CategoryImpl c ") 59 .append("WHERE c.parentId = $1 ") 60 .append("ORDER BY c.name ASC").toString(); 61 62 private static final String findActiveByParent = new StringBuffer ("SELECT c ") 63 .append("FROM org.infoglue.cms.entities.management.impl.simple.CategoryImpl c ") 64 .append("WHERE c.parentId = $1 ") 65 .append("AND c.active = $2 ") 66 .append("ORDER BY c.name ASC").toString(); 67 68 private static final String findRootCategories = new StringBuffer ("SELECT c ") 69 .append("FROM org.infoglue.cms.entities.management.impl.simple.CategoryImpl c ") 70 .append("WHERE is_undefined(c.parentId) ") 71 .append("ORDER BY c.name ASC").toString(); 72 73 private static final String findActiveRootCategories = new StringBuffer ("SELECT c ") 74 .append("FROM org.infoglue.cms.entities.management.impl.simple.CategoryImpl c ") 75 .append("WHERE is_undefined(c.parentId) ") 76 .append("AND c.active = $1 ") 77 .append("ORDER BY c.name ASC").toString(); 78 79 public static CategoryController getController() 80 { return instance; } 81 82 private CategoryController() 83 {} 84 85 92 public CategoryVO findById(Integer id) throws SystemException 93 { 94 return (CategoryVO)getVOWithId(CategoryImpl.class, id); 95 } 96 97 104 public Category findById(Integer id, Database db) throws SystemException 105 { 106 return (Category)getObjectWithId(CategoryImpl.class, id, db); 107 } 108 109 116 public CategoryVO findByPath(String path) throws SystemException 117 { 118 CategoryVO categoryVO = null; 119 120 String [] nodes = path.substring(1).split("/"); 121 122 if(nodes.length > 0) 123 { 124 List rootCategories = findRootCategories(); 125 String name = nodes[0]; 126 categoryVO = getCategoryVOWithNameInList(rootCategories, name); 127 128 for(int i = 1; i < nodes.length; i++) 129 { 130 categoryVO = getCategoryVOWithNameInList(findByParent(categoryVO.getId()), nodes[i]); 131 } 132 } 133 134 return categoryVO; 135 } 136 137 144 public CategoryVO findByPath(String path, Database db) throws SystemException 145 { 146 CategoryVO categoryVO = null; 147 148 String [] nodes = path.substring(1).split("/"); 149 150 if(nodes.length > 0) 151 { 152 List rootCategories = findRootCategoryVOList(db); 153 String name = nodes[0]; 154 categoryVO = getCategoryVOWithNameInList(rootCategories, name); 155 156 for(int i = 1; i < nodes.length; i++) 157 { 158 categoryVO = getCategoryVOWithNameInList(findByParent(categoryVO.getId(), db), nodes[i]); 159 } 160 } 161 162 return categoryVO; 163 } 164 165 171 172 private CategoryVO getCategoryVOWithNameInList(List categoryVOList, String name) 173 { 174 CategoryVO categoryVO = null; 175 176 Iterator categoryVOListIterator = categoryVOList.iterator(); 177 while(categoryVOListIterator.hasNext()) 178 { 179 CategoryVO currentCategoryVO = (CategoryVO)categoryVOListIterator.next(); 180 logger.info("currentCategoryVO:" + currentCategoryVO.getName() + "=" + name); 181 if(currentCategoryVO.getName().equalsIgnoreCase(name)) 182 { 183 categoryVO = currentCategoryVO; 184 break; 185 } 186 } 187 188 return categoryVO; 189 } 190 191 192 199 public List findByParent(Integer parentId) throws SystemException 200 { 201 List params = new ArrayList (); 202 params.add(parentId); 203 return executeQuery(findByParent, params); 204 } 205 206 213 public List findByParent(Integer parentId, Database db) throws SystemException 214 { 215 List params = new ArrayList (); 216 params.add(parentId); 217 return executeQuery(findByParent, params, db); 218 } 219 220 227 public List findActiveByParent(Integer parentId) throws SystemException 228 { 229 List params = new ArrayList (); 230 params.add(parentId); 231 params.add(Boolean.TRUE); 232 return executeQuery(findActiveByParent, params); 233 } 234 235 242 public CategoryVO findWithChildren(Integer id) throws SystemException 243 { 244 CategoryVO c = findById(id); 245 c.setChildren(findByParent(c.getId())); 246 return c; 247 } 248 249 256 public CategoryVO findWithChildren(Integer id, Database db) throws SystemException 257 { 258 Category c = findById(id, db); 259 c.getValueObject().setChildren(toVOList(findByParent(c.getId(), db))); 260 return c.getValueObject(); 261 } 262 263 269 public List findRootCategories() throws SystemException 270 { 271 return executeQuery(findRootCategories); 272 } 273 274 280 public List findRootCategories(Database db) throws SystemException 281 { 282 return executeQuery(findRootCategories, db); 283 } 284 285 291 public List findRootCategoryVOList(Database db) throws SystemException 292 { 293 List categories = executeQuery(findRootCategories, db); 294 return (categories != null) ? toVOList(categories) : null; 295 } 296 297 303 public List findAllActiveCategories() throws SystemException 304 { 305 List params = new ArrayList (); 306 params.add(Boolean.TRUE); 307 List roots = executeQuery(findActiveRootCategories, params); 308 for (Iterator iter = roots.iterator(); iter.hasNext();) 309 { 310 CategoryVO root = (CategoryVO) iter.next(); 311 root.setChildren(findAllActiveChildren(root.getId())); 312 } 313 return roots; 314 } 315 316 317 322 public List getAuthorizedActiveChildren(Integer parentId, InfoGluePrincipal infogluePrincipal) throws SystemException 323 { 324 List children = findActiveByParent(parentId); 325 for (Iterator iter = children.iterator(); iter.hasNext();) 326 { 327 CategoryVO child = (CategoryVO) iter.next(); 328 if(!getIsAccessApproved(child.getCategoryId(), infogluePrincipal)) 329 { 330 iter.remove(); 331 } 332 333 List subChildren = findAllActiveChildren(child.getId()); 334 Iterator subChildrenIterator = subChildren.iterator(); 335 while(subChildrenIterator.hasNext()) 336 { 337 CategoryVO subChild = (CategoryVO) subChildrenIterator.next(); 338 if(getIsAccessApproved(subChild.getCategoryId(), infogluePrincipal)) 339 { 340 child.getChildren().add(subChild); 341 } 342 } 343 } 344 return children; 345 } 346 347 352 public List findAllActiveChildren(Integer parentId) throws SystemException 353 { 354 List children = findActiveByParent(parentId); 355 for (Iterator iter = children.iterator(); iter.hasNext();) 356 { 357 CategoryVO child = (CategoryVO) iter.next(); 358 child.setChildren(findAllActiveChildren(child.getId())); 359 } 360 return children; 361 } 362 363 370 public CategoryVO save(CategoryVO c) throws SystemException 371 { 372 return (c.isUnsaved()) 373 ? create(c) 374 : (CategoryVO)updateEntity(CategoryImpl.class, c); 375 } 376 377 380 private CategoryVO create(CategoryVO c) throws SystemException 381 { 382 CategoryImpl impl = new CategoryImpl(c); 383 return ((CategoryImpl)createEntity(impl)).getValueObject(); 384 } 385 386 394 public CategoryVO moveCategory(Integer categoryId, Integer newParentId) throws SystemException 395 { 396 CategoryVO category = findById(categoryId); 397 category.setParentId(newParentId); 398 return save(category); 399 } 400 401 411 public void delete(Integer id) throws SystemException 412 { 413 contentCategoryStore.deleteByCategory(id); 414 deleteEntity(CategoryImpl.class, id); 415 deleteChildren(id); 416 } 417 418 421 private void deleteChildren(Integer id) throws SystemException 422 { 423 List children = findByParent(id); 424 for (Iterator iter = children.iterator(); iter.hasNext();) 425 delete(((CategoryVO) iter.next()).getId()); 426 } 427 428 431 public BaseEntityVO getNewVO() 432 { 433 return new CategoryVO(); 434 } 435 436 437 440 441 public boolean getIsAccessApproved(Integer categoryId, InfoGluePrincipal infoGluePrincipal) throws SystemException 442 { 443 logger.info("getIsAccessApproved for " + categoryId + " AND " + infoGluePrincipal); 444 boolean hasAccess = false; 445 446 Database db = CastorDatabaseService.getDatabase(); 447 448 beginTransaction(db); 449 450 try 451 { 452 hasAccess = AccessRightController.getController().getIsPrincipalAuthorized(db, infoGluePrincipal, "Category.Read", categoryId.toString()); 453 454 commitTransaction(db); 455 } 456 catch(Exception e) 457 { 458 logger.error("An error occurred so we should not complete the transaction:" + e, e); 459 rollbackTransaction(db); 460 throw new SystemException(e.getMessage()); 461 } 462 463 return hasAccess; 464 } 465 } 466 | Popular Tags |