1 17 package org.alfresco.web.bean; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.faces.application.FacesMessage; 26 import javax.faces.context.FacesContext; 27 import javax.faces.model.DataModel; 28 import javax.faces.model.ListDataModel; 29 import javax.faces.model.SelectItem; 30 31 import org.alfresco.error.AlfrescoRuntimeException; 32 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 33 import org.alfresco.service.cmr.dictionary.DictionaryService; 34 import org.alfresco.service.cmr.dictionary.PropertyDefinition; 35 import org.alfresco.service.cmr.repository.AssociationRef; 36 import org.alfresco.service.cmr.repository.ChildAssociationRef; 37 import org.alfresco.service.cmr.repository.NodeRef; 38 import org.alfresco.service.cmr.repository.NodeService; 39 import org.alfresco.service.cmr.repository.Path; 40 import org.alfresco.service.cmr.repository.StoreRef; 41 import org.alfresco.service.cmr.search.ResultSet; 42 import org.alfresco.service.cmr.search.SearchService; 43 import org.alfresco.service.cmr.security.AccessPermission; 44 import org.alfresco.service.cmr.security.AccessStatus; 45 import org.alfresco.service.cmr.security.PermissionService; 46 import org.alfresco.service.namespace.NamespaceService; 47 import org.alfresco.service.namespace.QName; 48 import org.alfresco.service.namespace.RegexQNamePattern; 49 import org.alfresco.util.ISO9075; 50 import org.alfresco.web.app.servlet.DownloadContentServlet; 51 52 53 56 57 60 public class AdminNodeBrowseBean 61 { 62 63 private String queryLanguage = null; 64 65 66 private static List <SelectItem> queryLanguages = new ArrayList <SelectItem>(); 67 static 68 { 69 queryLanguages.add(new SelectItem("noderef")); 70 queryLanguages.add(new SelectItem(SearchService.LANGUAGE_XPATH)); 71 queryLanguages.add(new SelectItem(SearchService.LANGUAGE_LUCENE)); 72 queryLanguages.add(new SelectItem("selectnodes")); 73 } 74 75 private String query = null; 77 private SearchResults searchResults = new SearchResults((List <NodeRef>)null); 78 79 private DataModel stores = null; 81 private NodeRef nodeRef = null; 82 private QName nodeType = null; 83 private Path primaryPath = null; 84 private DataModel parents = null; 85 private DataModel aspects = null; 86 private DataModel properties = null; 87 private DataModel children = null; 88 private DataModel assocs = null; 89 private Boolean inheritPermissions = null; 90 private DataModel permissions = null; 91 92 private NodeService nodeService; 94 private DictionaryService dictionaryService; 95 private SearchService searchService; 96 private NamespaceService namespaceService; 97 private PermissionService permissionService; 98 99 102 public void setNodeService(NodeService nodeService) 103 { 104 this.nodeService = nodeService; 105 } 106 107 110 public void setSearchService(SearchService searchService) 111 { 112 this.searchService = searchService; 113 } 114 115 118 public void setDictionaryService(DictionaryService dictionaryService) 119 { 120 this.dictionaryService = dictionaryService; 121 } 122 123 126 public void setNamespaceService(NamespaceService namespaceService) 127 { 128 this.namespaceService = namespaceService; 129 } 130 131 134 public void setPermissionService(PermissionService permissionService) 135 { 136 this.permissionService = permissionService; 137 } 138 139 144 public DataModel getStores() 145 { 146 if (stores == null) 147 { 148 List <StoreRef> storeRefs = nodeService.getStores(); 149 stores = new ListDataModel(storeRefs); 150 } 151 return stores; 152 } 153 154 159 public NodeRef getNodeRef() 160 { 161 if (nodeRef == null) 162 { 163 nodeRef = nodeService.getRootNode(new StoreRef("system", "system")); 164 } 165 return nodeRef; 166 } 167 168 173 private void setNodeRef(NodeRef nodeRef) 174 { 175 this.nodeRef = nodeRef; 176 177 primaryPath = null; 179 nodeType = null; 180 parents = null; 181 aspects = null; 182 properties = null; 183 children = null; 184 assocs = null; 185 inheritPermissions = null; 186 permissions = null; 187 } 188 189 194 public QName getNodeType() 195 { 196 if (nodeType == null) 197 { 198 nodeType = nodeService.getType(getNodeRef()); 199 } 200 return nodeType; 201 } 202 203 208 public String getPrimaryPath() 209 { 210 if (primaryPath == null) 211 { 212 primaryPath = nodeService.getPath(getNodeRef()); 213 } 214 return ISO9075.decode(primaryPath.toString()); 215 } 216 217 222 public NodeRef getPrimaryParent() 223 { 224 getPrimaryPath(); 225 Path.Element element = primaryPath.last(); 226 NodeRef parentRef = ((Path.ChildAssocElement)element).getRef().getParentRef(); 227 return parentRef; 228 } 229 230 235 public DataModel getAspects() 236 { 237 if (aspects == null) 238 { 239 List <QName> aspectNames = new ArrayList <QName>(nodeService.getAspects(getNodeRef())); 240 aspects = new ListDataModel(aspectNames); 241 } 242 return aspects; 243 } 244 245 250 public DataModel getParents() 251 { 252 if (parents == null) 253 { 254 List <ChildAssociationRef> parentRefs = nodeService.getParentAssocs(getNodeRef()); 255 parents = new ListDataModel(parentRefs); 256 } 257 return parents; 258 } 259 260 265 public DataModel getProperties() 266 { 267 if (properties == null) 268 { 269 Map <QName, Serializable > propertyValues = nodeService.getProperties(getNodeRef()); 270 List <Property> nodeProperties = new ArrayList <Property>(propertyValues.size()); 271 for (Map.Entry <QName, Serializable > property : propertyValues.entrySet()) 272 { 273 nodeProperties.add(new Property(property.getKey(), property.getValue())); 274 } 275 properties = new ListDataModel(nodeProperties); 276 } 277 return properties; 278 } 279 280 285 public boolean getInheritPermissions() 286 { 287 if (inheritPermissions == null) 288 { 289 inheritPermissions = permissionService.getInheritParentPermissions(nodeRef); 290 } 291 return inheritPermissions.booleanValue(); 292 } 293 294 299 public DataModel getPermissions() 300 { 301 if (permissions == null) 302 { 303 AccessStatus readPermissions = permissionService.hasPermission(nodeRef, PermissionService.READ_PERMISSIONS); 304 if (readPermissions.equals(AccessStatus.ALLOWED)) 305 { 306 List <AccessPermission> nodePermissions = new ArrayList <AccessPermission>(permissionService.getAllSetPermissions(nodeRef)); 307 permissions = new ListDataModel(nodePermissions); 308 } 309 else 310 { 311 List <NoReadPermissionGranted> noReadPermissions = new ArrayList <NoReadPermissionGranted>(1); 312 noReadPermissions.add(new NoReadPermissionGranted()); 313 permissions = new ListDataModel(noReadPermissions); 314 } 315 } 316 return permissions; 317 } 318 319 324 public DataModel getChildren() 325 { 326 if (children == null) 327 { 328 List <ChildAssociationRef> assocRefs = nodeService.getChildAssocs(getNodeRef()); 329 children = new ListDataModel(assocRefs); 330 } 331 return children; 332 } 333 334 339 public DataModel getAssocs() 340 { 341 if (assocs == null) 342 { 343 List <AssociationRef> assocRefs = nodeService.getTargetAssocs(getNodeRef(), RegexQNamePattern.MATCH_ALL); 344 assocs = new ListDataModel(assocRefs); 345 } 346 return assocs; 347 } 348 349 354 public String getQueryLanguage() 355 { 356 return queryLanguage; 357 } 358 359 364 public void setQueryLanguage(String queryLanguage) 365 { 366 this.queryLanguage = queryLanguage; 367 } 368 369 374 public String getQuery() 375 { 376 return query; 377 } 378 379 384 public void setQuery(String query) 385 { 386 this.query = query; 387 } 388 389 394 public List getQueryLanguages() 395 { 396 return queryLanguages; 397 } 398 399 404 public SearchResults getSearchResults() 405 { 406 return searchResults; 407 } 408 409 414 public String selectStore() 415 { 416 StoreRef storeRef = (StoreRef)stores.getRowData(); 417 NodeRef rootNode = nodeService.getRootNode(storeRef); 418 setNodeRef(rootNode); 419 return "success"; 420 } 421 422 427 public String selectStores() 428 { 429 stores = null; 430 return "success"; 431 } 432 433 438 public String selectPrimaryPath() 439 { 440 setNodeRef(nodeRef); 442 return "success"; 443 } 444 445 450 public String selectPrimaryParent() 451 { 452 setNodeRef(getPrimaryParent()); 453 return "success"; 454 } 455 456 461 public String selectParent() 462 { 463 ChildAssociationRef assocRef = (ChildAssociationRef)parents.getRowData(); 464 NodeRef parentRef = assocRef.getParentRef(); 465 setNodeRef(parentRef); 466 return "success"; 467 } 468 469 474 public String selectToNode() 475 { 476 AssociationRef assocRef = (AssociationRef)assocs.getRowData(); 477 NodeRef targetRef = assocRef.getTargetRef(); 478 setNodeRef(targetRef); 479 return "success"; 480 } 481 482 487 public String selectNodeProperty() 488 { 489 Property property = (Property)properties.getRowData(); 490 Property.Value value = (Property.Value)property.getValues().getRowData(); 491 NodeRef nodeRef = (NodeRef)value.getValue(); 492 setNodeRef(nodeRef); 493 return "success"; 494 } 495 496 501 public String selectChild() 502 { 503 ChildAssociationRef assocRef = (ChildAssociationRef)children.getRowData(); 504 NodeRef childRef = assocRef.getChildRef(); 505 setNodeRef(childRef); 506 return "success"; 507 } 508 509 514 public String selectResultNode() 515 { 516 ChildAssociationRef assocRef = (ChildAssociationRef)searchResults.getRows().getRowData(); 517 NodeRef childRef = assocRef.getChildRef(); 518 setNodeRef(childRef); 519 return "success"; 520 } 521 522 527 public String submitSearch() 528 { 529 try 530 { 531 if (queryLanguage.equals("noderef")) 532 { 533 NodeRef nodeRef = new NodeRef(query); 535 boolean exists = nodeService.exists(nodeRef); 536 if (!exists) 537 { 538 throw new AlfrescoRuntimeException("Node " + nodeRef + " does not exist."); 539 } 540 setNodeRef(nodeRef); 541 return "node"; 542 } 543 else if (queryLanguage.equals("selectnodes")) 544 { 545 List <NodeRef> nodes = searchService.selectNodes(getNodeRef(), query, null, namespaceService, false); 546 searchResults = new SearchResults(nodes); 547 return "search"; 548 } 549 550 searchResults = new SearchResults(searchService.query(getNodeRef().getStoreRef(), queryLanguage, query)); 552 return "search"; 553 } 554 catch(Throwable e) 555 { 556 FacesContext context = FacesContext.getCurrentInstance(); 557 FacesMessage message = new FacesMessage(); 558 message.setSeverity(FacesMessage.SEVERITY_ERROR); 559 message.setDetail("Search failed due to: " + e.toString()); 560 context.addMessage("searchForm:query", message); 561 return "error"; 562 } 563 } 564 565 568 public class Property 569 { 570 private QName name; 571 private boolean isCollection = false; 572 private DataModel values; 573 private String datatype; 574 private String residual; 575 576 582 public Property(QName name, Serializable value) 583 { 584 this.name = name; 585 586 PropertyDefinition propDef = dictionaryService.getProperty(name); 587 if (propDef != null) 588 { 589 datatype = propDef.getDataType().getName().toString(); 590 residual = "false"; 591 } 592 else 593 { 594 residual = "true"; 595 } 596 597 List <Value> values = new ArrayList <Value>(); 600 if (value instanceof Collection ) 601 { 602 isCollection = true; 603 for (Serializable multiValue : (Collection <Serializable >)value) 604 { 605 values.add(new Value(multiValue)); 606 } 607 } 608 else 609 { 610 values.add(new Value(value)); 611 } 612 this.values = new ListDataModel(values); 613 } 614 615 620 public QName getName() 621 { 622 return name; 623 } 624 625 630 public String getDataType() 631 { 632 return datatype; 633 } 634 635 640 public DataModel getValues() 641 { 642 return values; 643 } 644 645 650 public String getResidual() 651 { 652 return residual; 653 } 654 655 660 public boolean isAny() 661 { 662 return (datatype == null) ? false : datatype.equals(DataTypeDefinition.ANY.toString()); 663 } 664 665 670 public boolean isCollection() 671 { 672 return isCollection; 673 } 674 675 676 679 public class Value 680 { 681 private Serializable value; 682 683 688 public Value(Serializable value) 689 { 690 this.value = value; 691 } 692 693 698 public Serializable getValue() 699 { 700 return value; 701 } 702 703 708 public String getDataType() 709 { 710 String datatype = Property.this.getDataType(); 711 if (datatype == null || datatype.equals(DataTypeDefinition.ANY.toString())) 712 { 713 if (value != null) 714 { 715 datatype = dictionaryService.getDataType(value.getClass()).getName().toString(); 716 } 717 } 718 return datatype; 719 } 720 721 726 public String getUrl() 727 { 728 String url = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath(); 729 url += DownloadContentServlet.generateBrowserURL(nodeRef, "file.bin"); 730 url += "?property=" + name; 731 return url; 732 } 733 734 739 public boolean isContent() 740 { 741 String datatype = getDataType(); 742 return (datatype == null) ? false : datatype.equals(DataTypeDefinition.CONTENT.toString()); 743 } 744 745 750 public boolean isNodeRef() 751 { 752 String datatype = getDataType(); 753 return (datatype == null) ? false : datatype.equals(DataTypeDefinition.NODE_REF.toString()) || datatype.equals(DataTypeDefinition.CATEGORY.toString()); 754 } 755 756 761 public boolean isNullValue() 762 { 763 return value == null; 764 } 765 } 766 } 767 768 771 public class NoReadPermissionGranted 772 { 773 public String getPermission() 774 { 775 return PermissionService.READ_PERMISSIONS; 776 } 777 778 public String getAuthority() 779 { 780 return "[Current Authority]"; 781 } 782 783 public String getAccessStatus() 784 { 785 return "Not Granted"; 786 } 787 } 788 789 792 public class SearchResults 793 { 794 private int length = 0; 795 private DataModel rows; 796 797 802 public SearchResults(ResultSet resultSet) 803 { 804 rows = new ListDataModel(); 805 if (resultSet != null) 806 { 807 rows.setWrappedData(resultSet.getChildAssocRefs()); 808 length = resultSet.length(); 809 } 810 } 811 812 817 public SearchResults(List <NodeRef> resultSet) 818 { 819 rows = new ListDataModel(); 820 if (resultSet != null) 821 { 822 List <ChildAssociationRef> assocRefs = new ArrayList <ChildAssociationRef>(resultSet.size()); 823 for (NodeRef nodeRef : resultSet) 824 { 825 ChildAssociationRef childAssocRef = nodeService.getPrimaryParent(nodeRef); 826 assocRefs.add(childAssocRef); 827 } 828 rows.setWrappedData(assocRefs); 829 length = resultSet.size(); 830 } 831 } 832 833 838 public int getLength() 839 { 840 return length; 841 } 842 843 848 public DataModel getRows() 849 { 850 return rows; 851 } 852 } 853 854 } 855 | Popular Tags |