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.io.IOException;
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentModelList;
031import org.nuxeo.ecm.core.api.SortInfo;
032import org.nuxeo.ecm.platform.query.api.PageProvider;
033import org.nuxeo.ecm.platform.query.api.PageProviderService;
034import org.nuxeo.ecm.platform.query.core.CoreQueryPageProviderDescriptor;
035import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider;
036import org.nuxeo.runtime.api.Framework;
037
038public class DataFetch {
039    private static Log log = LogFactory.getLog(DataFetch.class);
040
041    public static int DEFAULT_PAGE_SIZE = 100;
042
043    public static boolean ORDERBY_PATH = true;
044
045    public DocumentModelList getAllChildren(CoreSession session, DocumentModel doc) throws IOException {
046        String request = getChildrenDocQuery(doc, ORDERBY_PATH);
047        log.debug("start query: " + request);
048        DocumentModelList res = session.query(request);
049        log.debug("done query");
050
051        return res;
052    }
053
054    public PageProvider<DocumentModel> getAllChildrenPaginated(CoreSession session, DocumentModel doc)
055            {
056        return getAllChildrenPaginated(session, doc, DEFAULT_PAGE_SIZE, ORDERBY_PATH);
057    }
058
059    public CoreQueryDocumentPageProvider getAllChildrenPaginated(CoreSession session, DocumentModel doc, long pageSize,
060            boolean orderByPath) {
061        String request = getChildrenDocQuery(doc, orderByPath);
062        log.debug("will initialize a paginated query:" + request);
063        PageProviderService pps = Framework.getLocalService(PageProviderService.class);
064        CoreQueryPageProviderDescriptor desc = new CoreQueryPageProviderDescriptor();
065        desc.setPattern(request);
066
067        // page provider parameters & init
068        Long targetPage = null;
069        Long targetPageSize = pageSize;
070        List<SortInfo> sortInfos = null;
071        Object[] parameters = null;
072        Map<String, Serializable> props = new HashMap<String, Serializable>();
073        props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) session);
074
075        PageProvider<?> provider = pps.getPageProvider("", desc, null, sortInfos, targetPageSize, targetPage, props,
076                parameters);
077        // TODO: edit pps implementation to really set parameters!
078        provider.setPageSize(pageSize);
079        provider.setMaxPageSize(pageSize);
080        CoreQueryDocumentPageProvider cqdpp = (CoreQueryDocumentPageProvider) provider;
081        return cqdpp;
082    }
083
084    /* QUERIES */
085
086    public String getChildrenDocQuery(DocumentModel doc, boolean ordered) {
087        String parentPath = doc.getPathAsString();
088
089        String request = String.format("SELECT * FROM Document WHERE ecm:path STARTSWITH '%s' AND "
090                + "ecm:mixinType = 'Folderish' AND %s", parentPath, baseRequest());
091        if (ordered)
092            return request + " ORDER BY ecm:path";
093        else
094            return request;
095    }
096
097    /**
098     * Exclude documents:
099     * <ul>
100     * <li>from user workspaces
101     * <li>that are deleted (in trash)
102     * <li>that stand in user workspace
103     * </ul>
104     *
105     * @return
106     */
107    protected static String baseRequest() {
108        return "ecm:mixinType != 'HiddenInNavigation'" + " AND ecm:isCheckedInVersion = 0"
109                + " AND ecm:currentLifeCycleState != 'deleted'";
110    }
111}