1 17 package org.alfresco.web.bean; 18 19 import java.io.Serializable ; 20 import java.text.MessageFormat ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import javax.faces.context.FacesContext; 29 import javax.faces.event.ActionEvent; 30 import javax.transaction.UserTransaction ; 31 32 import org.alfresco.model.ContentModel; 33 import org.alfresco.service.cmr.repository.ChildAssociationRef; 34 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 35 import org.alfresco.service.cmr.repository.NodeRef; 36 import org.alfresco.service.cmr.repository.NodeService; 37 import org.alfresco.service.cmr.search.CategoryService; 38 import org.alfresco.service.cmr.search.CategoryService.Depth; 39 import org.alfresco.service.cmr.search.CategoryService.Mode; 40 import org.alfresco.service.namespace.QName; 41 import org.alfresco.web.app.Application; 42 import org.alfresco.web.app.context.IContextListener; 43 import org.alfresco.web.app.context.UIContextService; 44 import org.alfresco.web.bean.repository.Node; 45 import org.alfresco.web.bean.repository.Repository; 46 import org.alfresco.web.ui.common.Utils; 47 import org.alfresco.web.ui.common.component.IBreadcrumbHandler; 48 import org.alfresco.web.ui.common.component.UIActionLink; 49 import org.alfresco.web.ui.common.component.UIBreadcrumb; 50 import org.alfresco.web.ui.common.component.UIModeList; 51 import org.alfresco.web.ui.common.component.data.UIRichList; 52 import org.alfresco.web.ui.repo.component.IRepoBreadcrumbHandler; 53 import org.apache.log4j.Logger; 54 55 60 public class CategoriesBean implements IContextListener 61 { 62 private static final String DEFAULT_OUTCOME = "finish"; 63 64 private static final String MSG_CATEGORIES = "categories"; 65 66 private static Logger logger = Logger.getLogger(CategoriesBean.class); 67 68 69 protected NodeService nodeService; 70 71 protected CategoryService categoryService; 72 73 74 protected UIRichList categoriesRichList; 75 76 77 private Node category = null; 78 79 80 private NodeRef categoryRef = null; 81 82 83 private Node actionCategory = null; 84 85 86 private Collection <ChildAssociationRef> members = null; 87 88 89 private String name = null; 90 private String description = null; 91 92 93 private String viewMode = "icons"; 94 95 96 private List <IBreadcrumbHandler> location = null; 97 98 99 102 105 public CategoriesBean() 106 { 107 UIContextService.getInstance(FacesContext.getCurrentInstance()).registerBean(this); 108 } 109 110 111 114 117 public void setNodeService(NodeService nodeService) 118 { 119 this.nodeService = nodeService; 120 } 121 122 125 public void setCategoryService(CategoryService categoryService) 126 { 127 this.categoryService = categoryService; 128 } 129 130 133 public void setCategoriesRichList(UIRichList list) 134 { 135 this.categoriesRichList = list; 136 } 137 138 141 public UIRichList getCategoriesRichList() 142 { 143 return this.categoriesRichList; 144 } 145 146 149 public String getDescription() 150 { 151 return this.description; 152 } 153 154 157 public void setDescription(String description) 158 { 159 this.description = description; 160 } 161 162 165 public String getViewMode() 166 { 167 return this.viewMode; 168 } 169 170 173 public void setViewMode(String viewMode) 174 { 175 this.viewMode = viewMode; 176 } 177 178 181 public String getName() 182 { 183 return this.name; 184 } 185 186 189 public void setName(String name) 190 { 191 this.name = name; 192 } 193 194 197 public int getMembers() 198 { 199 return (this.members != null ? this.members.size() : 0); 200 } 201 202 205 public Node getActionCategory() 206 { 207 return this.actionCategory; 208 } 209 210 213 public void setActionCategory(Node node) 214 { 215 this.actionCategory = node; 216 217 if (node != null) 218 { 219 this.name = node.getName(); 221 this.description = (String )node.getProperties().get(ContentModel.PROP_DESCRIPTION); 222 this.members = this.categoryService.getChildren(node.getNodeRef(), Mode.MEMBERS, Depth.ANY); 223 } 224 else 225 { 226 this.name = null; 227 this.description = null; 228 this.members = Collections.emptyList(); 229 } 230 } 231 232 235 public Node getCurrentCategory() 236 { 237 if (this.category == null) 238 { 239 if (this.categoryRef != null) 240 { 241 this.category = new Node(this.categoryRef); 242 } 243 } 244 245 return this.category; 246 } 247 248 251 public String getCurrentCategoryId() 252 { 253 if (this.categoryRef != null) 254 { 255 return categoryRef.getId(); 256 } 257 else 258 { 259 return null; 260 } 261 } 262 263 270 public void setCurrentCategory(NodeRef ref) 271 { 272 if (logger.isDebugEnabled()) 273 logger.debug("Setting current category: " + ref); 274 275 this.categoryRef = ref; 277 278 this.category = null; 280 281 contextUpdated(); 283 } 284 285 288 public List <IBreadcrumbHandler> getLocation() 289 { 290 if (this.location == null) 291 { 292 List <IBreadcrumbHandler> loc = new ArrayList <IBreadcrumbHandler>(8); 293 loc.add(new CategoryBreadcrumbHandler(null, 294 Application.getMessage(FacesContext.getCurrentInstance(), MSG_CATEGORIES))); 295 296 this.location = loc; 297 } 298 return this.location; 299 } 300 301 304 public void setLocation(List <IBreadcrumbHandler> location) 305 { 306 this.location = location; 307 } 308 309 313 public List <Node> getCategories() 314 { 315 List <Node> categories; 316 317 UserTransaction tx = null; 318 try 319 { 320 FacesContext context = FacesContext.getCurrentInstance(); 321 tx = Repository.getUserTransaction(context, true); 322 tx.begin(); 323 324 Collection <ChildAssociationRef> refs; 325 if (this.categoryRef == null) 326 { 327 refs = this.categoryService.getCategories(Repository.getStoreRef(), ContentModel.ASPECT_GEN_CLASSIFIABLE, Depth.IMMEDIATE); 329 } 330 else 331 { 332 refs = this.categoryService.getChildren(this.categoryRef, Mode.SUB_CATEGORIES, Depth.IMMEDIATE); 334 } 335 categories = new ArrayList <Node>(refs.size()); 336 for (ChildAssociationRef child : refs) 337 { 338 Node categoryNode = new Node(child.getChildRef()); 339 categoryNode.getProperties(); 341 categories.add(categoryNode); 342 } 343 344 tx.commit(); 346 } 347 catch (InvalidNodeRefException refErr) 348 { 349 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 350 FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object [] {refErr.getNodeRef()}) ); 351 categories = Collections.<Node>emptyList(); 352 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 353 } 354 catch (Throwable err) 355 { 356 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 357 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 358 categories = Collections.<Node>emptyList(); 359 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 360 } 361 362 return categories; 363 } 364 365 368 public void setupCategoryAction(ActionEvent event) 369 { 370 UIActionLink link = (UIActionLink)event.getComponent(); 371 Map <String , String > params = link.getParameterMap(); 372 String id = params.get("id"); 373 if (id != null && id.length() != 0) 374 { 375 if (logger.isDebugEnabled()) 376 logger.debug("Setup for action, setting current Category to: " + id); 377 378 try 379 { 380 NodeRef ref = new NodeRef(Repository.getStoreRef(), id); 382 Node node = new Node(ref); 383 384 setActionCategory(node); 386 387 contextUpdated(); 389 } 390 catch (InvalidNodeRefException refErr) 391 { 392 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 393 FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object [] {id}) ); 394 } 395 } 396 } 397 398 401 public void clearCategoryAction(ActionEvent event) 402 { 403 setActionCategory(null); 404 405 contextUpdated(); 407 } 408 409 413 public void clickCategory(ActionEvent event) 414 { 415 UIActionLink link = (UIActionLink)event.getComponent(); 416 Map <String , String > params = link.getParameterMap(); 417 String id = params.get("id"); 418 if (id != null && id.length() != 0) 419 { 420 try 421 { 422 NodeRef ref = new NodeRef(Repository.getStoreRef(), id); 423 424 updateUILocation(ref); 426 } 427 catch (InvalidNodeRefException refErr) 428 { 429 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 430 FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object [] {id}) ); 431 } 432 } 433 } 434 435 438 public String finishCreate() 439 { 440 String outcome = DEFAULT_OUTCOME; 441 442 UserTransaction tx = null; 443 try 444 { 445 FacesContext context = FacesContext.getCurrentInstance(); 446 tx = Repository.getUserTransaction(context); 447 tx.begin(); 448 449 NodeRef ref; 451 if (categoryRef == null) 452 { 453 ref = this.categoryService.createRootCategory(Repository.getStoreRef(), ContentModel.ASPECT_GEN_CLASSIFIABLE, this.name); 454 } 455 else 456 { 457 ref = this.categoryService.createCategory(categoryRef, this.name); 458 } 459 460 Map <QName, Serializable > titledProps = new HashMap <QName, Serializable >(1, 1.0f); 462 titledProps.put(ContentModel.PROP_DESCRIPTION, this.description); 463 this.nodeService.addAspect(ref, ContentModel.ASPECT_TITLED, titledProps); 464 465 tx.commit(); 467 } 468 catch (Throwable err) 469 { 470 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 472 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 473 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 474 outcome = null; 475 } 476 477 return outcome; 478 } 479 480 483 public String finishEdit() 484 { 485 String outcome = DEFAULT_OUTCOME; 486 487 UserTransaction tx = null; 488 try 489 { 490 FacesContext context = FacesContext.getCurrentInstance(); 491 tx = Repository.getUserTransaction(context); 492 tx.begin(); 493 494 NodeRef nodeRef = getActionCategory().getNodeRef(); 496 this.nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, this.name); 497 498 if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_TITLED) == false) 500 { 501 Map <QName, Serializable > titledProps = new HashMap <QName, Serializable >(1, 1.0f); 502 titledProps.put(ContentModel.PROP_DESCRIPTION, this.description); 503 this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_TITLED, titledProps); 504 } 505 else 506 { 507 this.nodeService.setProperty(nodeRef, ContentModel.PROP_DESCRIPTION, this.description); 508 } 509 510 tx.commit(); 512 513 List <IBreadcrumbHandler> location = getLocation(); 515 IBreadcrumbHandler handler = location.get(location.size() - 1); 516 517 if ( nodeRef.equals(((IRepoBreadcrumbHandler)handler).getNodeRef()) ) 519 { 520 IBreadcrumbHandler newHandler = new CategoryBreadcrumbHandler( 522 nodeRef, Repository.getNameForNode(nodeService, nodeRef)); 523 location.set(location.size() - 1, newHandler); 524 } 525 } 526 catch (Throwable err) 527 { 528 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 530 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 531 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 532 outcome = null; 533 } 534 535 return outcome; 536 } 537 538 541 public String finishDelete() 542 { 543 String outcome = DEFAULT_OUTCOME; 544 545 if (getActionCategory() != null) 546 { 547 UserTransaction tx = null; 548 try 549 { 550 FacesContext context = FacesContext.getCurrentInstance(); 551 tx = Repository.getUserTransaction(context); 552 tx.begin(); 553 554 NodeRef categoryNodeRef = getActionCategory().getNodeRef(); 556 this.categoryService.deleteCategory(categoryNodeRef); 557 558 if (this.members != null && this.members.size() > 0) 561 { 562 for (ChildAssociationRef childRef : this.members) 563 { 564 List <NodeRef> list = new ArrayList <NodeRef>(this.members.size()); 565 566 NodeRef member = childRef.getChildRef(); 567 Collection <NodeRef> categories = (Collection <NodeRef>)this.nodeService. 568 getProperty(member, ContentModel.PROP_CATEGORIES); 569 570 for (NodeRef category : categories) 571 { 572 if (category.equals(categoryNodeRef) == false) 573 { 574 list.add(category); 575 } 576 } 577 578 this.nodeService.setProperty(member, ContentModel.PROP_CATEGORIES, (Serializable )list); 580 } 581 } 582 583 tx.commit(); 585 586 List <IBreadcrumbHandler> location = getLocation(); 588 IBreadcrumbHandler handler = location.get(location.size() - 1); 589 590 if ( categoryNodeRef.equals(((IRepoBreadcrumbHandler)handler).getNodeRef()) ) 592 { 593 location.remove(location.size() - 1); 594 595 if (location.size() != 0) 597 { 598 handler = location.get(location.size() - 1); 599 this.setCurrentCategory(((IRepoBreadcrumbHandler)handler).getNodeRef()); 600 } 601 } 602 603 setActionCategory(null); 605 } 606 catch (Throwable err) 607 { 608 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 610 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 611 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 612 outcome = null; 613 } 614 } 615 616 return outcome; 617 } 618 619 624 public void viewModeChanged(ActionEvent event) 625 { 626 UIModeList viewList = (UIModeList)event.getComponent(); 627 628 setViewMode(viewList.getValue().toString()); 630 } 631 632 635 private void updateUILocation(NodeRef ref) 636 { 637 String name = Repository.getNameForNode(this.nodeService, ref); 638 this.location.add(new CategoryBreadcrumbHandler(ref, name)); 639 this.setCurrentCategory(ref); 640 } 641 642 643 646 649 public void contextUpdated() 650 { 651 if (logger.isDebugEnabled()) 652 logger.debug("Invalidating Category Management Components..."); 653 654 this.category = null; 656 657 this.categoriesRichList.setValue(null); 659 } 660 661 662 665 668 private class CategoryBreadcrumbHandler implements IRepoBreadcrumbHandler 669 { 670 private static final long serialVersionUID = 3831234653171036630L; 671 672 678 public CategoryBreadcrumbHandler(NodeRef nodeRef, String label) 679 { 680 this.label = label; 681 this.nodeRef = nodeRef; 682 } 683 684 687 public String toString() 688 { 689 return this.label; 690 } 691 692 695 public String navigationOutcome(UIBreadcrumb breadcrumb) 696 { 697 setCurrentCategory(this.nodeRef); 700 setLocation( (List )breadcrumb.getValue() ); 701 702 return null; 703 } 704 705 public NodeRef getNodeRef() 706 { 707 return this.nodeRef; 708 } 709 710 private NodeRef nodeRef; 711 private String label; 712 } 713 } 714 | Popular Tags |