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 java.util.Comparator;
021import java.util.List;
022import java.util.TreeSet;
023
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.platform.groups.audit.service.acl.excel.ExcelBuilder;
027import org.nuxeo.ecm.platform.groups.audit.service.acl.filter.IContentFilter;
028import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider;
029
030public class DataProcessorPaginated extends DataProcessor {
031    public static int MAX_DOCUMENTS = ExcelBuilder.MAX_ROW - 2;
032
033    public static int DEFAULT_PAGE_SIZE = 1000;
034
035    public static int EXCEL_RENDERING_RESERVED_TIME = 90; // seconds
036
037    protected static int UNBOUNDED_PROCESS_TIME = -1;
038
039    protected int pageSize = DEFAULT_PAGE_SIZE;
040
041    public DataProcessorPaginated(IContentFilter filter) {
042        this(filter, DEFAULT_PAGE_SIZE);
043    }
044
045    public DataProcessorPaginated(IContentFilter filter, int pageSize) {
046        super(filter);
047        this.pageSize = pageSize;
048    }
049
050    @Override
051    protected void doAnalyze(CoreSession session, DocumentModel root, int timeout) {
052        // get data
053        DataFetch fetch = new DataFetch();
054        CoreQueryDocumentPageProvider pages = fetch.getAllChildrenPaginated(session, root, pageSize, false);
055        initSummarySet();
056
057        // analyse root
058        processDocument(root);
059
060        // handling processing time
061        t.tic();
062        int maxProcessTime = UNBOUNDED_PROCESS_TIME;
063        if (timeout > 0) {
064            maxProcessTime = timeout - EXCEL_RENDERING_RESERVED_TIME;
065            if (maxProcessTime <= 0) {
066                throw new IllegalArgumentException("can't start a time bounded process with a timeout < "
067                        + EXCEL_RENDERING_RESERVED_TIME + "(time period reserved for excel rendering)");
068            }
069        }
070
071        // process children documents
072        status = ProcessorStatus.SUCCESS;
073        // iterate over pages
074        overPages: do {
075            log.debug("will get page " + p);
076            final List<DocumentModel> page = pages.getCurrentPage();
077            log.debug("page retrieved with query: " + pages.getCurrentQuery());
078            log.debug("page size: " + page.size());
079
080            // iterate over current page content
081            for (DocumentModel m : page) {
082                processDocument(m);
083                t.toc(); // update elapsed time
084
085                // verify exit conditions
086                if (getNumberOfDocuments() == MAX_DOCUMENTS) {
087                    // log.debug("will interrupt doc)
088                    status = ProcessorStatus.ERROR_TOO_MANY_DOCUMENTS;
089                    break overPages;
090                }
091                if (maxProcessTime != UNBOUNDED_PROCESS_TIME && t.toc() >= maxProcessTime) {
092                    status = ProcessorStatus.ERROR_TOO_LONG_PROCESS;
093                    break overPages;
094                }
095            }
096            pages.nextPage();
097            log.debug("done page " + (p++));
098        } while (pages.isNextPageAvailable());
099    }
100
101    protected int p = 0;
102
103    @Override
104    public void initSummarySet() {
105        allDocuments = new TreeSet<DocumentSummary>(new Comparator<DocumentSummary>() {
106            @Override
107            public int compare(DocumentSummary arg0, DocumentSummary arg1) {
108                final String dp0 = arg0.getPath();
109                final String dp1 = arg1.getPath();
110                return dp0.compareTo(dp1);
111            }
112        });
113    }
114}