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