001/*
002 * (C) Copyright 2013 Nuxeo SA (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-2.1.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 *     Martin Pernollet
016 */
017
018package org.nuxeo.ecm.platform.groups.audit.service.acl.data;
019
020import org.nuxeo.ecm.core.api.CoreSession;
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.DocumentModelList;
023import org.nuxeo.ecm.platform.groups.audit.service.acl.filter.IContentFilter;
024
025/**
026 * Gather various data and statistics about a document tree
027 *
028 * @author Martin Pernollet <mpernollet@nuxeo.com>
029 */
030public class DataProcessorRecursive extends DataProcessor implements IDataProcessor {
031    public DataProcessorRecursive(IContentFilter filter) {
032        super(filter);
033    }
034
035    /** {@inheritDoc}. timeout ignored */
036    @Override
037    public void analyze(CoreSession session, DocumentModel doc, int timeout) {
038        init();
039        doAnalyze(session, doc, 0);
040        log();
041    }
042
043    /**
044     * Analyze recursively the document tree. After calling this method, on can retrieve:
045     * <ul>
046     * <li>the tree depth
047     * <li>all user and groups mentioned in the documents' ACLs
048     * <li>all permission names mentioned in the documents' ACLs
049     * </ul>
050     * Note that root is considered as a document, so a repository made of: <code>
051     * <pre>
052     *  /
053     *  |-folder1
054     *  |-folder2
055     * </pre>
056     * </code> has a depth of 2. Once called, the method erase previous results.
057     *
058     * @param session
059     * @param doc
060     */
061    protected void doAnalyze(CoreSession session, DocumentModel doc, int depth) {
062        initSummarySet();
063        processDocument(doc);
064
065        // continue working recursively
066        DocumentModelList list = session.getChildren(doc.getRef());
067        for (DocumentModel child : list) {
068            doAnalyze(session, child, depth + 1);
069        }
070    }
071}