001/* 002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.admin.repo; 023 024import java.util.List; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.core.api.Blob; 029import org.nuxeo.ecm.core.api.CoreSession; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.DocumentRef; 032import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; 033import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 034 035/** 036 * Unrestricted Runner for the statistics gathering 037 * 038 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a> 039 */ 040public class StatsTaskRunner extends UnrestrictedSessionRunner { 041 042 protected static final Log log = LogFactory.getLog(StatsTaskRunner.class); 043 044 protected final boolean includeBlob; 045 046 protected final DocumentRef rootref; 047 048 protected final StatsTask hostTask; 049 050 public StatsTaskRunner(String repositoryName, boolean includeBlob, DocumentRef rootref, StatsTask hostTask) { 051 super(repositoryName); 052 this.includeBlob = includeBlob; 053 this.rootref = rootref; 054 this.hostTask = hostTask; 055 } 056 057 private void recurse(DocumentModel doc) { 058 fetchInfoFromDoc(session, doc); 059 if (doc.isFolder()) { 060 long children = 0; 061 for (DocumentModel child : session.getChildren(doc.getRef())) { 062 children += 1; 063 if (child.isFolder()) { 064 StatsTask newTask = hostTask.getNextTask(child); 065 if (newTask != null) { 066 hostTask.exec(newTask); 067 } else { 068 recurse(child); 069 } 070 } else { 071 fetchInfoFromDoc(session, child); 072 } 073 } 074 hostTask.getInfo().childrenCount(children, doc.getPath()); 075 } 076 } 077 078 private void fetchInfoFromDoc(CoreSession session, DocumentModel doc) { 079 080 if (includeBlob) { 081 BlobHolder bh = doc.getAdapter(BlobHolder.class); 082 if (bh != null) { 083 List<Blob> blobs = bh.getBlobs(); 084 if (blobs != null) { 085 for (Blob blob : blobs) { 086 if (blob != null) { 087 hostTask.getInfo().addBlob(blob.getLength(), doc.getPath()); 088 } 089 } 090 } 091 } 092 } 093 094 if (doc.isVersion()) { 095 hostTask.getInfo().addDoc(doc.getType(), doc.getPath(), true); 096 } else { 097 hostTask.getInfo().addDoc(doc.getType(), doc.getPath()); 098 List<DocumentModel> versions = session.getVersions(doc.getRef()); 099 for (DocumentModel version : versions) { 100 fetchInfoFromDoc(session, version); 101 } 102 } 103 104 } 105 106 @Override 107 public void run() { 108 DocumentModel root = session.getDocument(rootref); 109 recurse(root); 110 } 111 112}