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