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 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.core.api.impl; 020 021import java.util.ArrayList; 022import java.util.Iterator; 023import java.util.List; 024import java.util.NoSuchElementException; 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.DocumentModelIterator; 031import org.nuxeo.ecm.core.api.DocumentRef; 032import org.nuxeo.ecm.core.api.Filter; 033import org.nuxeo.ecm.core.api.IdRef; 034 035/** 036 * Iterator over the children of a folder. 037 */ 038public class DocumentModelChildrenIterator implements DocumentModelIterator { 039 040 private static final Log log = LogFactory.getLog(DocumentModelChildrenIterator.class); 041 042 private CoreSession session; 043 044 private String type; 045 046 private Filter filter; 047 048 private Iterator<String> it; 049 050 private DocumentModel next; 051 052 public DocumentModelChildrenIterator(CoreSession session, DocumentRef parentRef, String type, Filter filter) 053 { 054 this.session = session; 055 this.type = type; 056 this.filter = filter; 057 058 // fetch all the children ids now 059 List<DocumentRef> refs = session.getChildrenRefs(parentRef, null); 060 List<String> ids = new ArrayList<>(refs.size()); 061 for (DocumentRef ref : refs) { 062 ids.add(ref.toString()); // always an IdRef 063 } 064 it = ids.iterator(); 065 } 066 067 @Override 068 public boolean hasNext() { 069 if (next != null) { 070 return true; 071 } 072 // this could be optimized to retrieve batches of documents instead of 073 // one at a time; this would need to call SQLSession.getDocumentsById 074 // through a new CoreSession API (or improve getDocuments) 075 for (;;) { 076 if (!it.hasNext()) { 077 return false; 078 } 079 String id = it.next(); 080 IdRef idRef = new IdRef(id); 081 if (!session.exists(idRef)) { 082 continue; 083 } 084 DocumentModel doc; 085 doc = session.getDocument(idRef); 086 if (accept(doc)) { 087 next = doc; 088 return true; 089 } 090 // continue 091 } 092 } 093 094 private boolean accept(DocumentModel doc) { 095 if (type != null && !type.equals(doc.getType())) { 096 return false; 097 } 098 if (filter != null && !filter.accept(doc)) { 099 return false; 100 } 101 return true; 102 } 103 104 @Override 105 public DocumentModel next() { 106 if (!hasNext()) { 107 throw new NoSuchElementException(); 108 } 109 DocumentModel res = next; 110 next = null; 111 return res; 112 } 113 114 @Override 115 public void remove() { 116 throw new UnsupportedOperationException(); 117 } 118 119 @Override 120 public Iterator<DocumentModel> iterator() { 121 return this; // NOSONAR this iterable does not support multiple traversals 122 } 123 124 @Override 125 public long size() { 126 return UNKNOWN_SIZE; 127 } 128 129}