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