001/*
002 * (C) Copyright 2014 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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.query.nxql;
018
019import org.nuxeo.ecm.core.api.CoreSession;
020import org.nuxeo.ecm.core.api.DocumentModel;
021import org.nuxeo.ecm.core.api.DocumentModelList;
022import org.nuxeo.ecm.core.api.Filter;
023import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
024
025/**
026 * Unrestricted session runner providing API for retrieving the result documents list.
027 *
028 * @since 6.0
029 */
030public class CoreQueryUnrestrictedSessionRunner extends UnrestrictedSessionRunner {
031
032    protected final String query;
033
034    protected final Filter filter;
035
036    protected final long limit;
037
038    protected final long offset;
039
040    protected final boolean countTotal;
041
042    protected final long countUpTo;
043
044    protected final boolean detachDocuments;
045
046    protected DocumentModelList docs;
047
048    public CoreQueryUnrestrictedSessionRunner(CoreSession session, String query, Filter filter, long limit,
049            long offset, boolean countTotal, long countUpTo, boolean detachDocuments) {
050        super(session);
051        this.query = query;
052        this.filter = filter;
053        this.limit = limit;
054        this.offset = offset;
055        this.countTotal = countTotal;
056        this.countUpTo = countUpTo;
057        this.detachDocuments = detachDocuments;
058    }
059
060    @Override
061    public void run() {
062        if (countTotal) {
063            docs = session.query(query, filter, limit, offset, countTotal);
064        } else {
065            docs = session.query(query, filter, limit, offset, countUpTo);
066        }
067        if (docs != null && detachDocuments) {
068            for (DocumentModel doc : docs) {
069                doc.detach(true);
070            }
071        }
072    }
073
074    public DocumentModelList getDocs() {
075        return docs;
076    }
077
078}