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