1 17 package org.alfresco.web.bean; 18 19 import java.text.MessageFormat ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.faces.context.FacesContext; 26 import javax.faces.event.ActionEvent; 27 28 import org.alfresco.error.AlfrescoRuntimeException; 29 import org.alfresco.filesys.CIFSServer; 30 import org.alfresco.filesys.server.filesys.DiskSharedDevice; 31 import org.alfresco.filesys.smb.server.repo.ContentContext; 32 import org.alfresco.filesys.smb.server.repo.ContentDiskInterface; 33 import org.alfresco.model.ContentModel; 34 import org.alfresco.repo.security.permissions.AccessDeniedException; 35 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 36 import org.alfresco.service.cmr.repository.NodeRef; 37 import org.alfresco.service.cmr.repository.NodeService; 38 import org.alfresco.service.cmr.repository.Path; 39 import org.alfresco.service.cmr.repository.TemplateImageResolver; 40 import org.alfresco.service.cmr.repository.TemplateNode; 41 import org.alfresco.service.cmr.rule.RuleService; 42 import org.alfresco.service.cmr.search.SearchService; 43 import org.alfresco.service.cmr.security.PermissionService; 44 import org.alfresco.service.namespace.NamespaceService; 45 import org.alfresco.web.app.Application; 46 import org.alfresco.web.app.context.UIContextService; 47 import org.alfresco.web.bean.repository.Node; 48 import org.alfresco.web.bean.repository.Repository; 49 import org.alfresco.web.bean.repository.User; 50 import org.alfresco.web.bean.wizard.NewSpaceWizard; 51 import org.alfresco.web.config.ClientConfigElement; 52 import org.alfresco.web.ui.common.Utils; 53 import org.alfresco.web.ui.common.component.IBreadcrumbHandler; 54 import org.alfresco.web.ui.common.component.UIBreadcrumb; 55 import org.alfresco.web.ui.common.component.UIModeList; 56 import org.alfresco.web.ui.repo.component.IRepoBreadcrumbHandler; 57 import org.alfresco.web.ui.repo.component.shelf.UIShelf; 58 import org.apache.log4j.Logger; 59 60 63 public class NavigationBean 64 { 65 68 public NavigationBean() 69 { 70 initFromClientConfig(); 71 } 72 73 74 77 80 public void setNodeService(NodeService nodeService) 81 { 82 this.nodeService = nodeService; 83 } 84 85 88 public void setSearchService(SearchService searchService) 89 { 90 this.searchService = searchService; 91 } 92 93 96 public void setNamespaceService(NamespaceService namespaceService) 97 { 98 this.namespaceService = namespaceService; 99 } 100 101 104 public void setRuleService(RuleService ruleService) 105 { 106 this.ruleService = ruleService; 107 } 108 109 112 public void setCifsServer(CIFSServer cifsServer) 113 { 114 this.cifsServer = cifsServer; 115 } 116 117 120 public void setContentDiskDriver(ContentDiskInterface contentDiskDriver) 121 { 122 this.contentDiskDriver = contentDiskDriver; 123 } 124 125 128 public User getCurrentUser() 129 { 130 return Application.getCurrentUser(FacesContext.getCurrentInstance()); 131 } 132 133 138 public boolean isShelfExpanded() 139 { 140 return this.shelfExpanded; 141 } 142 143 148 public void setShelfExpanded(boolean expanded) 149 { 150 this.shelfExpanded = expanded; 151 } 152 153 156 public boolean[] getShelfItemExpanded() 157 { 158 return this.shelfItemExpanded; 159 } 160 161 164 public void setShelfItemExpanded(boolean[] shelfItemExpanded) 165 { 166 this.shelfItemExpanded = shelfItemExpanded; 167 } 168 169 172 public String getToolbarLocation() 173 { 174 return this.toolbarLocation; 175 } 176 177 180 public void setToolbarLocation(String toolbarLocation) 181 { 182 this.toolbarLocation = toolbarLocation; 183 } 184 185 188 public String getHelpUrl() 189 { 190 return this.helpUrl; 191 } 192 193 196 public void setHelpUrl(String helpUrl) 197 { 198 this.helpUrl = helpUrl; 199 } 200 201 204 public int getRuleCount() 205 { 206 Node node = getCurrentNode(); 207 return (node != null ? this.ruleService.countRules(node.getNodeRef()) : 0); 208 } 209 210 213 public SearchContext getSearchContext() 214 { 215 return this.searchContext; 216 } 217 218 221 public void setSearchContext(SearchContext searchContext) 222 { 223 this.searchContext = searchContext; 224 225 UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans(); 226 } 227 228 231 public String getCurrentNodeId() 232 { 233 return this.currentNodeId; 234 } 235 236 243 public void setCurrentNodeId(String currentNodeId) 244 { 245 if (s_logger.isDebugEnabled()) 246 s_logger.debug("Setting current node id to: " + currentNodeId); 247 248 if (currentNodeId == null) 249 { 250 throw new AlfrescoRuntimeException("Can not set the current node id to null"); 251 } 252 253 this.currentNodeId = currentNodeId; 255 256 this.currentNode = null; 258 this.searchContext = null; 259 260 UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans(); 262 263 this.currentNode = null; 266 } 267 268 271 public boolean getCurrentNodeHasTemplate() 272 { 273 boolean templateView = false; 274 Node node = getCurrentNode(); 275 if (node.hasAspect(ContentModel.ASPECT_TEMPLATABLE)) 276 { 277 NodeRef templateRef = (NodeRef)node.getProperties().get(ContentModel.PROP_TEMPLATE); 278 try 279 { 280 templateView = (templateRef != null && this.nodeService.exists(templateRef)); 281 } 282 catch (AccessDeniedException err) 283 { 284 } 286 } 287 return templateView; 288 } 289 290 293 public String getCurrentNodeTemplate() 294 { 295 String strRef = null; 296 if (getCurrentNodeHasTemplate() == true) 297 { 298 strRef = getCurrentNode().getProperties().get(ContentModel.PROP_TEMPLATE).toString(); 299 } 300 return strRef; 301 } 302 303 308 public Map getTemplateModel() 309 { 310 HashMap model = new HashMap (1, 1.0f); 311 312 FacesContext fc = FacesContext.getCurrentInstance(); 313 TemplateNode spaceNode = new TemplateNode(getCurrentNode().getNodeRef(), Repository.getServiceRegistry(fc), 314 new TemplateImageResolver() { 315 public String resolveImagePathForName(String filename, boolean small) { 316 return Utils.getFileTypeImage(filename, small); 317 } 318 }); 319 model.put("space", spaceNode); 320 321 return model; 322 } 323 324 327 public void resetCurrentNodeProperties() 328 { 329 this.currentNode = null; 330 } 331 332 335 public Map <String , Object > getNodeProperties() 336 { 337 return getCurrentNode().getProperties(); 338 } 339 340 343 public Node getCurrentNode() 344 { 345 if (this.currentNode == null) 346 { 347 if (this.currentNodeId == null) 348 { 349 throw new AlfrescoRuntimeException("Cannot retrieve current Node if NodeId is null!"); 350 } 351 352 if (s_logger.isDebugEnabled()) 353 s_logger.debug("Caching properties for node id: " + this.currentNodeId); 354 355 NodeRef nodeRef; 356 Node node; 357 Map <String , Object > props; 358 try 359 { 360 nodeRef = new NodeRef(Repository.getStoreRef(), this.currentNodeId); 362 node = new Node(nodeRef); 363 364 props = node.getProperties(); 367 } 368 catch (InvalidNodeRefException refErr) 369 { 370 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 371 FacesContext.getCurrentInstance(), ERROR_DELETED_FOLDER), new Object [] {this.currentNodeId}) ); 372 373 nodeRef = new NodeRef(Repository.getStoreRef(), Application.getCompanyRootId()); 374 node = new Node(nodeRef); 375 props = node.getProperties(); 376 } 377 String icon = (String )props.get("app:icon"); 378 props.put("icon", icon != null ? icon : NewSpaceWizard.SPACE_ICON_DEFAULT); 379 Path path = this.nodeService.getPath(nodeRef); 380 381 DiskSharedDevice diskShare = cifsServer.getConfiguration().getPrimaryFilesystem(); 383 384 if (diskShare != null) 385 { 386 ContentContext contentCtx = (ContentContext) diskShare.getContext(); 387 NodeRef rootNode = contentCtx.getRootNode(); 388 String cifsPath = Repository.getNamePath(this.nodeService, path, rootNode, "\\", "file:///" + getCIFSServerPath(diskShare)); 389 390 node.getProperties().put("cifsPath", cifsPath); 391 node.getProperties().put("cifsPathLabel", cifsPath.substring(8)); } 393 394 this.currentNode = node; 395 } 396 397 return this.currentNode; 398 } 399 400 403 public List <IBreadcrumbHandler> getLocation() 404 { 405 if (this.location == null) 406 { 407 NodeRef homeSpaceRef; 409 List <IBreadcrumbHandler> elements = new ArrayList <IBreadcrumbHandler>(1); 410 if (getCurrentNodeId() == null) 411 { 412 User user = Application.getCurrentUser(FacesContext.getCurrentInstance()); 413 homeSpaceRef = new NodeRef(Repository.getStoreRef(), user.getHomeSpaceId()); 414 } 415 else 416 { 417 homeSpaceRef = new NodeRef(Repository.getStoreRef(), getCurrentNodeId()); 418 } 419 420 setCurrentNodeId(homeSpaceRef.getId()); 422 423 String homeSpaceName = Repository.getNameForNode(this.nodeService, homeSpaceRef); 425 elements.add(new NavigationBreadcrumbHandler(homeSpaceRef, homeSpaceName)); 426 setLocation(elements); 427 } 428 429 return this.location; 430 } 431 432 435 public void setLocation(List <IBreadcrumbHandler> location) 436 { 437 this.location = location; 438 } 439 440 443 public boolean getIsGuest() 444 { 445 return Application.getCurrentUser(FacesContext.getCurrentInstance()).getUserName().equals(PermissionService.GUEST_AUTHORITY); 446 } 447 448 454 public void setupDispatchContext(Node node) 455 { 456 this.dispatchContext = node; 457 } 458 459 462 public void resetDispatchContext() 463 { 464 this.dispatchContext = null; 465 } 466 467 473 public Node getDispatchContextNode() 474 { 475 return this.dispatchContext; 476 } 477 478 479 482 486 public void toggleShelf(ActionEvent event) 487 { 488 this.shelfExpanded = !this.shelfExpanded; 489 } 490 491 494 public void shelfGroupToggled(ActionEvent event) 495 { 496 UIShelf.ShelfEvent shelfEvent = (UIShelf.ShelfEvent)event; 497 this.shelfItemExpanded[shelfEvent.Index] = shelfEvent.Expanded; 498 } 499 500 504 public void toolbarLocationChanged(ActionEvent event) 505 { 506 FacesContext context = FacesContext.getCurrentInstance(); 507 try 508 { 509 UIModeList locationList = (UIModeList)event.getComponent(); 510 String location = locationList.getValue().toString(); 511 setToolbarLocation(location); 512 513 if (LOCATION_COMPANY.equals(location)) 514 { 515 List <IBreadcrumbHandler> elements = new ArrayList <IBreadcrumbHandler>(1); 516 NodeRef companyRootRef = new NodeRef(Repository.getStoreRef(), Application.getCompanyRootId()); 517 String companySpaceName = Repository.getNameForNode(this.nodeService, companyRootRef); 518 elements.add(new NavigationBreadcrumbHandler(companyRootRef, companySpaceName)); 519 setLocation(elements); 520 setCurrentNodeId(companyRootRef.getId()); 521 } 522 else if (LOCATION_HOME.equals(location)) 523 { 524 List <IBreadcrumbHandler> elements = new ArrayList <IBreadcrumbHandler>(1); 525 String homeSpaceId = Application.getCurrentUser(context).getHomeSpaceId(); 526 NodeRef homeSpaceRef = new NodeRef(Repository.getStoreRef(), homeSpaceId); 527 String homeSpaceName = Repository.getNameForNode(this.nodeService, homeSpaceRef); 528 elements.add(new NavigationBreadcrumbHandler(homeSpaceRef, homeSpaceName)); 529 setLocation(elements); 530 setCurrentNodeId(homeSpaceRef.getId()); 531 } 532 533 context.getApplication().getNavigationHandler().handleNavigation(context, null, "browse"); 535 } 536 catch (InvalidNodeRefException refErr) 537 { 538 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 539 FacesContext.getCurrentInstance(), Repository.ERROR_NOHOME), Application.getCurrentUser(context).getHomeSpaceId()), refErr ); 540 } 541 catch (Exception err) 542 { 543 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 544 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 545 } 546 } 547 548 552 public String getCIFSServerPath(DiskSharedDevice diskShare) 553 { 554 if (this.cifsServerPath == null) 555 { 556 StringBuilder buf = new StringBuilder (24); 557 558 String serverName = this.cifsServer.getConfiguration().getServerName(); 559 if (serverName != null && serverName.length() != 0) 560 { 561 buf.append("\\\\") 562 .append(serverName) 563 .append("\\"); 564 buf.append(diskShare.getName()); 565 } 566 567 this.cifsServerPath = buf.toString(); 568 } 569 570 return this.cifsServerPath; 571 } 572 573 574 577 580 private void initFromClientConfig() 581 { 582 this.clientConfig = Application.getClientConfig(FacesContext.getCurrentInstance()); 583 this.helpUrl = clientConfig.getHelpUrl(); 584 this.shelfExpanded = clientConfig.isShelfVisible(); 585 } 586 587 588 591 594 public class NavigationBreadcrumbHandler implements IRepoBreadcrumbHandler 595 { 596 private static final long serialVersionUID = 4833194653193016638L; 597 598 603 public NavigationBreadcrumbHandler(NodeRef ref, String label) 604 { 605 this.label = label; 606 this.ref = ref; 607 } 608 609 612 public String toString() 613 { 614 return this.label; 615 } 616 617 620 public String navigationOutcome(UIBreadcrumb breadcrumb) 621 { 622 FacesContext fc = FacesContext.getCurrentInstance(); 624 setCurrentNodeId(ref.getId()); 625 setLocation( (List )breadcrumb.getValue() ); 626 627 setupDispatchContext(new Node(ref)); 629 630 if (fc.getViewRoot().getViewId().equals(BrowseBean.BROWSE_VIEW_ID)) 631 { 632 return null; 633 } 634 else 635 { 636 return "browse"; 637 } 638 } 639 640 public NodeRef getNodeRef() 641 { 642 return this.ref; 643 } 644 645 private String label; 646 private NodeRef ref; 647 } 648 649 650 653 private static Logger s_logger = Logger.getLogger(NavigationBean.class); 654 655 656 private static final String LOCATION_COMPANY = "company"; 657 private static final String LOCATION_HOME = "home"; 658 659 private static final String ERROR_DELETED_FOLDER = "error_deleted_folder"; 660 661 662 protected NodeService nodeService; 663 664 665 protected SearchService searchService; 666 667 668 protected NamespaceService namespaceService; 669 670 671 protected RuleService ruleService; 672 673 674 protected CIFSServer cifsServer; 675 676 677 protected ContentDiskInterface contentDiskDriver; 678 679 680 protected ClientConfigElement clientConfig = null; 681 682 683 private String cifsServerPath; 684 685 686 private String currentNodeId; 687 688 689 private Node currentNode = null; 690 691 692 private Node dispatchContext = null; 693 694 695 private String toolbarLocation = LOCATION_HOME; 696 697 698 private SearchContext searchContext; 699 700 701 private boolean shelfExpanded = true; 702 703 704 private boolean[] shelfItemExpanded = new boolean[] {true, true, true, false, false}; 705 706 707 private List <IBreadcrumbHandler> location = null; 708 709 710 private String helpUrl; 711 } 712 | Popular Tags |