1 19 20 package za.org.coefficient.modules.category; 21 22 import net.sf.hibernate.Hibernate; 23 24 import za.org.coefficient.authentication.Role; 25 import za.org.coefficient.core.Category; 26 import za.org.coefficient.core.Project; 27 import za.org.coefficient.interfaces.CoefficientContext; 28 import za.org.coefficient.modules.BaseModule; 29 import net.sf.hibernate.util.HibernateUtil; 30 import za.org.coefficient.util.ejb.SecurityUtil; 31 import za.org.coefficient.util.ejb.VelocityScreenUtil; 32 33 import java.util.ArrayList ; 34 import java.util.HashMap ; 35 import java.util.List ; 36 import java.util.StringTokenizer ; 37 38 55 public class CategoryManagement extends BaseModule { 56 58 public final String ACTION = "action"; 59 public final String ADD = "add"; 60 public final String ADDPAGE = "add.vm"; 61 public final String ALL = "All"; 62 public final String CATDISP = "catdisp"; 63 public final String CATEGORY = "category"; 64 public final String CHILDID = "childid"; 65 public final String CLOSE = "close"; 66 public final String DELETE = "delete"; 67 public final String DESCRIPTION = "description"; 68 public final String EDIT = "edit"; 69 public final String ERROR = "error"; 70 public final String GET = "get"; 71 public final String ID = "id"; 72 public final String INDEXPAGE = "index.vm"; 73 public final String LIST = "list"; 74 public final String LISTPAGE = "list.vm"; 75 public final String MODULE = "module"; 76 public final String MSG = "msg"; 77 public final String NAME = "name"; 78 public final String OP = "op"; 79 public final String OPEN = "open"; 80 public final String PARENT = "parent"; 81 public final String PATH = "path"; 82 public final String ROOT = "root"; 83 public final String SESSION_ID = "CategoryManagementBean"; 84 public final String TEMPPAGE = "temp.vm"; 85 86 String [] interpretOpRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC }; 90 91 93 public Category getCategoryByName(String name) { 94 Category root = getRoot(); 95 List categories; 96 categories = getSubCategoriesForDisplay("" + root.getId(), null); 97 CategoryDisplayData cat = new CategoryDisplayData(); 98 Category hit = new Category(); 99 int i; 100 StringTokenizer toks = new StringTokenizer (name, "/"); 101 String st; 102 while (toks.hasMoreElements()) { 103 st = (String ) toks.nextElement(); 104 for (i = 0; i < categories.size(); i++) { 105 cat = (CategoryDisplayData) (categories.get(i)); 106 if (st.equals(cat.getName())) { 107 hit = getCategory("" + cat.getId()); 108 categories = cat.getChildren(); 109 i = 0; 110 } 111 } 112 } 113 114 return hit; 115 } 116 117 public CoefficientContext getCategoryByName(CoefficientContext ctx) { 118 String name = ctx.getParameter(NAME, ""); 119 Category hit = getCategoryByName(name); 120 121 HashMap map = new HashMap (); 122 map.put(CATEGORY, hit); 123 List list; 124 list = listAll(); 125 map.put("list", list); 126 StringBuffer sb = VelocityScreenUtil.getProcessedScreen(TEMPPAGE, map); 127 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 128 return ctx; 129 } 130 131 public String getMainMethod() { 132 return "interpretOp"; 134 } 135 136 public String getModuleDescription() { 137 return "For mangement of categories"; 138 } 139 140 public String getModuleDisplayName() { 141 return "Category Administration"; 142 } 143 144 private CoefficientContext addCategory(CoefficientContext ctx) { 145 try { 146 String parentid = ctx.getParameter(ID, ""); 147 String childid = ctx.getParameter(CHILDID, ""); 148 Category parent = null; 149 Category category = null; 150 String name = ctx.getParameter(NAME, ""); 151 String description = ctx.getParameter(DESCRIPTION, ""); 152 HashMap map = new HashMap (); 153 if (name.trim() 154 .equals("")) { 155 String msg; 156 parent = getCategory(parentid); 157 if (!childid.trim() 158 .equals("")) { 159 msg = new String ("Editing Category"); 160 category = getCategory(childid); 161 map.put(CATEGORY, category); 162 } else { 163 msg = new String ("Adding category"); 164 } 165 map.put(MSG, msg); 166 map.put(MODULE, this); 167 map.put(PARENT, parent); 168 StringBuffer sb = 169 VelocityScreenUtil.getProcessedScreen(ADDPAGE, map); 170 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 171 } else { 172 if (name.equals(ALL)) { 173 map.put(MODULE, this); 174 map.put(ERROR, "Name must not be 'All'"); 175 StringBuffer sb = 176 VelocityScreenUtil.getProcessedScreen(INDEXPAGE, map); 177 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 178 } else { 179 parent = getCategory(parentid); 180 Category newCategory = new Category(); 181 newCategory.setName(name); 182 newCategory.setDescription(description); 183 newCategory.setParentCategory(parent); 184 if (!childid.trim() 185 .equals("")) { 186 Long id = null; 187 try { 188 id = new Long (childid); 189 } catch (Throwable t) { 190 System.out.println("throws " + t + " with " 191 + childid); 192 } 193 newCategory = getCategory(childid); 194 newCategory.setName(name); 195 newCategory.setDescription(description); 196 } 197 try { 198 HibernateUtil.saveOrUpdate(newCategory); 199 } catch (Throwable t) { 200 System.out.println("Adding Hibernate throws " + t); 201 } 202 viewCategoryTree(ctx, ""); 203 } 204 } 205 } catch (Throwable t) { 206 System.out.println("categoryManagement.addCategorythrows" + t); 207 } 208 return ctx; 209 } 210 211 public String canExecuteForRole(CoefficientContext ctx, String methodName, 212 Role usersHighestRole) { 213 String role = usersHighestRole.getDescription(); 214 int i; 215 216 if (methodName.equals("interpretOp")) { 217 for (i = 0; i < interpretOpRoles.length; i++) { 218 if (role.equals(interpretOpRoles[i])) { 219 return null; 220 } 221 } 222 return "not authorised"; 223 } 224 return null; 225 } 226 227 private synchronized void closeCategory(CoefficientContext ctx) { 228 try { 229 Object obj = ctx.getSessionAttribute(SESSION_ID); 230 List openList; 231 if (obj == null) { 232 openList = new ArrayList (); 233 openList.add(getRoot()); 234 } else { 235 openList = (ArrayList ) obj; 236 } 237 String id = ctx.getParameter(ID); 238 Category category = null; 239 if (id == null) { 240 category = getRoot(); 241 id = "" + category.getId(); 242 } else { 243 boolean b; 245 b = openList.remove(id); 246 } 247 ctx.setSessionAttribute(SESSION_ID, openList); 248 viewCategoryTree(ctx, ""); 249 } catch (Throwable t) { 250 System.out.println("categoryManagement.viewCategoryTree throws" + t); 251 t.printStackTrace(); 252 } 253 } 254 255 private synchronized void deleteCategory(CoefficientContext ctx) { 256 String id = ctx.getParameter(ID); 257 Object obj = ctx.getSessionAttribute(SESSION_ID); 258 List openList; 259 if (obj == null) { 260 openList = new ArrayList (); 261 openList.add(getRoot()); 262 } else { 263 openList = (ArrayList ) obj; 264 } 265 if (id == null) { 266 viewCategoryTree(ctx, ""); 267 } else { 268 List subCategories = getSubCategories(id); 270 if (subCategories.size() != 0) { 271 viewCategoryTree(ctx, "must delete subcategories first"); 272 } else { 273 openList.remove(id); 274 Category category = getCategory(id); 275 if (anyProjects(category)) { 276 viewCategoryTree(ctx, 277 "Can't delete. There are projects under this category"); 278 } else { 279 try { 280 HibernateUtil.delete(category); 281 } catch (Throwable t) { 282 System.out.println("delete Hibernate throws " + t); 283 } 284 viewCategoryTree(ctx, ""); 285 } 286 } 287 } 288 } 289 290 public void interpretOp(CoefficientContext ctx) { 291 String action = ctx.getParameter(ACTION); 295 296 if (action == null) { 300 viewCategoryTree(ctx, ""); 301 } else { 302 if (action.equals(ADD)) { 303 addCategory(ctx); 304 } 305 if (action.equals(CLOSE)) { 306 closeCategory(ctx); 307 } 308 if (action.equals(OPEN)) { 309 openCategory(ctx, ""); 310 } 311 if (action.equals(DELETE)) { 312 deleteCategory(ctx); 313 } 314 if (action.equals(EDIT)) { 315 addCategory(ctx); 316 } 317 if (action.equals(LIST)) { 318 listAll(ctx); 319 } 320 if (action.equals(GET)) { 321 getCategoryByName(ctx); 322 } 323 } 324 } 325 326 public List listAll() { 327 Category root = getRoot(); 328 329 List list = new ArrayList (); 333 List categories; 334 335 categories = getSubCategoriesForDisplay("" + root.getId(), null); 336 String prefix = new String (); 337 list(categories, prefix, list); 338 339 return list; 340 } 341 342 public CoefficientContext listAll(CoefficientContext ctx) { 343 List list; 344 list = listAll(); 345 346 HashMap map = new HashMap (); 347 map.put("list", list); 348 StringBuffer sb = VelocityScreenUtil.getProcessedScreen(TEMPPAGE, map); 349 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 350 return ctx; 351 } 352 353 public List listAllCategories() throws Exception { 354 return HibernateUtil.find("FROM " + Category.class.getName() 355 + " as cats where cats.parentCategory is not null"); 356 } 357 358 private CoefficientContext openCategory(CoefficientContext ctx, String msg) { 359 try { 360 CategoryDisplayData cat = null; 361 Category root = getRoot(); 362 Object obj = ctx.getSessionAttribute(SESSION_ID); 363 List openList; 364 if (obj == null) { 365 openList = new ArrayList (); 366 openList.add("" + root.getId()); 367 } else { 368 openList = (ArrayList ) obj; 369 } 370 371 String id = ctx.getParameter(ID); 372 Category category = null; 373 if (id == null) { 374 category = getRoot(); 375 id = "" + category.getId(); 376 } else { 377 category = getRoot(); 378 openList.add(id); 379 } 380 List subCategories = 381 getSubCategoriesForDisplay("" + root.getId(), openList); 382 cat = new CategoryDisplayData(); 383 cat.setId(category.getId()); 384 if (category.getParentCategory() == null) { 385 cat.setParentId(root.getId()); 386 } else { 387 cat.setParentId(category.getParentCategory().getId()); 388 } 389 cat.setName(category.getName()); 390 cat.setDescription(category.getDescription()); 391 cat.setChildren(subCategories); 392 HashMap map = new HashMap (); 393 if (!msg.trim() 394 .equals("")) { 395 map.put(ERROR, msg); 396 } 397 map.put(CATDISP, cat); 398 ctx.setSessionAttribute(SESSION_ID, openList); 399 map.put(ROOT, root); 400 StringBuffer sb = 401 VelocityScreenUtil.getProcessedScreen(LISTPAGE, map); 402 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 403 } catch (Throwable t) { 404 System.out.println("categoryManagement.viewCategoryTree throws" + t); 405 } 406 return ctx; 407 } 408 409 private CoefficientContext viewCategoryTree(CoefficientContext ctx, String msg) { 410 try { 411 CategoryDisplayData cat = null; 412 Category root = getRoot(); 413 Object obj = ctx.getSessionAttribute(SESSION_ID); 414 List openList; 415 if (obj == null) { 416 openList = new ArrayList (); 417 openList.add("" + root.getId()); 418 } else { 419 openList = (ArrayList ) obj; 420 } 421 422 String id = ctx.getParameter(ID); 423 Category category = getRoot(); 424 List subCategories = 425 getSubCategoriesForDisplay("" + root.getId(), openList); 426 cat = new CategoryDisplayData(); 427 cat.setId(category.getId()); 428 if (category.getParentCategory() == null) { 429 cat.setParentId(root.getId()); 430 } else { 431 cat.setParentId(category.getParentCategory().getId()); 432 } 433 cat.setName(category.getName()); 434 cat.setDescription(category.getDescription()); 435 cat.setChildren(subCategories); 436 HashMap map = new HashMap (); 437 if (!msg.trim() 438 .equals("")) { 439 map.put(ERROR, msg); 440 } 441 map.put(CATDISP, cat); 442 ctx.setSessionAttribute(SESSION_ID, openList); 443 map.put(ROOT, root); 444 StringBuffer sb = 445 VelocityScreenUtil.getProcessedScreen(LISTPAGE, map); 446 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 447 } catch (Throwable t) { 448 System.out.println("categoryManagement.viewCategoryTree throws" + t); 449 } 450 return ctx; 451 } 452 453 private Category getCategory(String id) { 454 List list = null; 455 Category cat = null; 456 try { 457 list = 458 HibernateUtil.find("FROM " + Category.class.getName() 459 + " as category WHERE category.id = ? ", new Long (id), 460 Hibernate.LONG); 461 462 int i; 463 for (i = 0; i < list.size(); i++) { 464 cat = (Category) (list.get(i)); 465 return cat; 466 } 467 } catch (Throwable t) { 468 System.out.println("getCategory Hibernate throws " + t); 469 } 470 471 return cat; 472 } 473 474 477 private Category getRoot() { 478 List list = null; 479 Category root = null; 480 try { 481 list = 482 HibernateUtil.find("FROM " + Category.class.getName() 483 + " as category WHERE category.name = ? " 484 + " ORDER BY category.id ", ALL, Hibernate.STRING); 485 486 int i; 487 for (i = 0; i < list.size(); i++) { 488 Category cat = (Category) (list.get(i)); 489 if (i > 0) { 490 HibernateUtil.delete(cat); 491 } else { 492 root = cat; 493 } 494 } 495 if (list.size() == 0) { 496 Category cat = new Category(); 497 cat.setName(ALL); 498 cat.setDescription("All Categories"); 499 cat.setParentCategory(null); 500 try { 501 HibernateUtil.saveOrUpdate(cat); 502 } catch (Throwable t) { 503 System.out.println("getRoot1 Hibernate throws " + t); 504 } 505 506 root = cat; 507 } 508 } catch (Throwable t) { 509 System.out.println("getRoot2 Hibernate throws " + t); 510 } 511 512 return root; 513 } 514 515 private List getSubCategories(String id) { 516 List list = null; 517 Category cat = null; 518 try { 519 list = 520 HibernateUtil.find("FROM " + Category.class.getName() 521 + " as category " 522 + " WHERE category.parentCategory.id = ? ", new Long (id), 523 Hibernate.LONG); 524 } catch (Throwable t) { 525 System.out.println("getSubCategories Hibernate throws " + t); 526 } 527 528 return list; 529 } 530 531 private List getSubCategoriesForDisplay(String id, List openList) { 532 List categories = getSubCategories(id); 533 Category cat; 534 535 List display = new ArrayList (); 536 CategoryDisplayData displaydata; 537 List childrenAsDisplay; 538 539 if ((openList == null) || openList.contains(id)) { 540 int i; 541 int j; 542 543 for (i = 0; i < categories.size(); i++) { 544 cat = (Category) categories.get(i); 545 displaydata = new CategoryDisplayData(); 546 displaydata.setId(cat.getId()); 547 displaydata.setName(cat.getName()); 548 displaydata.setDescription(cat.getDescription()); 549 childrenAsDisplay = 550 getSubCategoriesForDisplay("" + cat.getId(), openList); 551 displaydata.setChildren(childrenAsDisplay); 552 display.add(displaydata); 553 } 554 } 555 556 return display; 557 } 558 559 562 private boolean anyProjects(Category cat) { 563 List list = new ArrayList (); 564 try { 565 list = 566 HibernateUtil.find("select project from " + Project.class.getName() 567 + " as project, " + " category in project.categories " 568 + "where category.id = ?", 569 576 cat.getId(), Hibernate.LONG); 577 578 int i; 579 Project p; 580 for (i = 0; i < list.size(); i++) { 581 p = (Project) (list.get(i)); 582 } 583 } catch (Throwable t) { 584 System.out.println("anyProjects throws " + t); 585 } 586 587 return list.size() > 0; 588 } 589 590 private void list(List categories, String prefix, List output) { 591 CategoryDisplayData cat; 592 int i; 593 String st; 594 if (categories != null) { 595 for (i = 0; i < categories.size(); i++) { 596 cat = (CategoryDisplayData) (categories.get(i)); 597 output.add(prefix + "/" + cat.getName()); 598 list(cat.getChildren(), prefix + "/" + cat.getName(), output); 599 } 600 } 601 } 602 603 private void removeBranch(CategoryDisplayData cat, String id) { 604 if (id.equals("" + cat.getId())) { 605 cat.setChildren(new ArrayList ()); 606 } else { 607 int i; 608 609 List children = cat.getChildren(); 610 CategoryDisplayData c; 611 for (i = 0; i < children.size(); i++) { 612 c = (CategoryDisplayData) (children.get(i)); 613 removeBranch(c, id); 614 } 615 } 616 } 617 } 618 | Popular Tags |