001/* 002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.admin.repo; 021 022import java.util.List; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.ecm.core.api.Blob; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.DocumentRef; 030import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; 031import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 032 033/** 034 * Unrestricted Runner for the statistics gathering 035 * 036 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a> 037 */ 038public class StatsTaskRunner extends UnrestrictedSessionRunner { 039 040 protected static final Log log = LogFactory.getLog(StatsTaskRunner.class); 041 042 protected final boolean includeBlob; 043 044 protected final DocumentRef rootref; 045 046 protected final StatsTask hostTask; 047 048 public StatsTaskRunner(String repositoryName, boolean includeBlob, DocumentRef rootref, StatsTask hostTask) { 049 super(repositoryName); 050 this.includeBlob = includeBlob; 051 this.rootref = rootref; 052 this.hostTask = hostTask; 053 } 054 055 private void recurse(DocumentModel doc) { 056 fetchInfoFromDoc(session, doc); 057 if (doc.isFolder()) { 058 long children = 0; 059 for (DocumentModel child : session.getChildren(doc.getRef())) { 060 children += 1; 061 if (child.isFolder()) { 062 StatsTask newTask = hostTask.getNextTask(child); 063 if (newTask != null) { 064 hostTask.exec(newTask); 065 } else { 066 recurse(child); 067 } 068 } else { 069 fetchInfoFromDoc(session, child); 070 } 071 } 072 hostTask.getInfo().childrenCount(children, doc.getPath()); 073 } 074 } 075 076 private void fetchInfoFromDoc(CoreSession session, DocumentModel doc) { 077 078 if (includeBlob) { 079 BlobHolder bh = doc.getAdapter(BlobHolder.class); 080 if (bh != null) { 081 List<Blob> blobs = bh.getBlobs(); 082 if (blobs != null) { 083 for (Blob blob : blobs) { 084 if (blob != null) { 085 hostTask.getInfo().addBlob(blob.getLength(), doc.getPath()); 086 } 087 } 088 } 089 } 090 } 091 092 if (doc.isVersion()) { 093 hostTask.getInfo().addDoc(doc.getType(), doc.getPath(), true); 094 } else { 095 hostTask.getInfo().addDoc(doc.getType(), doc.getPath()); 096 List<DocumentModel> versions = session.getVersions(doc.getRef()); 097 for (DocumentModel version : versions) { 098 fetchInfoFromDoc(session, version); 099 } 100 } 101 102 } 103 104 @Override 105 public void run() { 106 DocumentModel root = session.getDocument(rootref); 107 recurse(root); 108 } 109 110}