Guest User

NodeSizeWebScript.java

a guest
Sep 13th, 2017
35
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.acme.nodesize;
  2.  
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.logging.Logger;
  7.  
  8. import org.alfresco.model.ContentModel;
  9. import org.alfresco.service.cmr.repository.ChildAssociationRef;
  10. import org.alfresco.service.cmr.repository.ContentData;
  11. import org.alfresco.service.cmr.repository.NodeRef;
  12. import org.alfresco.service.cmr.repository.NodeService;
  13. import org.alfresco.service.namespace.QName;
  14. import org.alfresco.service.namespace.RegexQNamePattern;
  15. import org.springframework.extensions.webscripts.Cache;
  16. import org.springframework.extensions.webscripts.DeclarativeWebScript;
  17. import org.springframework.extensions.webscripts.Status;
  18. import org.springframework.extensions.webscripts.WebScriptRequest;
  19.  
  20. /**
  21. * Calculate node content size recursively.
  22. * Modified from http://www.dedunu.info/2015/03/alfresco-calculate-folder-size-using.html
  23. *
  24. * @author Zhihai Liu
  25. *
  26. */
  27. public class NodeSizeWebScript extends DeclarativeWebScript {
  28. long count;
  29. static Logger log = Logger.getLogger(NodeSizeWebScript.class.getName());
  30. private NodeService nodeService;
  31.  
  32. public final void setNodeService(final NodeService nodeService) {
  33. this.nodeService = nodeService;
  34. }
  35.  
  36. @Override
  37. protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
  38. Map<String, Object> model = new HashMap<String, Object>();
  39. String nodeRefId = req.getParameter("nodeRef");
  40. NodeRef nodeRef = new NodeRef(nodeRefId);
  41.  
  42. String nodeName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
  43. long size = getNodeSize(nodeRef);
  44. long noOfFiles = getNoOfFiles(nodeRef);
  45. log.info("no of files"+ noOfFiles);
  46. model.put("nodeName", nodeName);
  47. model.put("size", Long.toString(size));
  48. model.put("noOfFiles",noOfFiles);
  49. return model;
  50. }
  51.  
  52. private long getNodeSize(NodeRef nodeRef) {
  53. long size = 0;
  54. // Collecting current node size
  55. ContentData contentData = (ContentData) nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
  56. try {
  57. size = contentData.getSize();
  58. } catch (Exception e) {
  59. size = 0;
  60. }
  61.  
  62. // Collecting child nodes' sizes
  63. // even a document (cm:content) can have child nodes, such as thumbnail
  64. List<ChildAssociationRef> chilAssocsList = nodeService.getChildAssocs(nodeRef);
  65.  
  66. for (ChildAssociationRef childAssociationRef : chilAssocsList) {
  67. NodeRef childNodeRef = childAssociationRef.getChildRef();
  68. log.info("filenames"+childNodeRef);
  69. size = size + getNodeSize(childNodeRef);
  70. }
  71. return size;
  72. }
  73.  
  74. private long getNoOfFiles(NodeRef nodeRef) {
  75. // Collecting current node size
  76. // Collecting child nodes' sizes
  77. // even a document (cm:content) can have child nodes, such as thumbnail
  78. List<ChildAssociationRef> chilAssocsList = nodeService.getChildAssocs(nodeRef);
  79. for (ChildAssociationRef childAssociationRef : chilAssocsList) {
  80. NodeRef childNodeRef = childAssociationRef.getChildRef();
  81. count += 1;
  82.  
  83.  
  84. }
  85. return count;
  86. }
  87.  
  88. }
RAW Paste Data