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 * Nuxeo 016 */ 017 018package org.nuxeo.elasticsearch.work; 019 020import org.nuxeo.ecm.core.api.DocumentModel; 021import org.nuxeo.ecm.core.api.DocumentModelIterator; 022import org.nuxeo.ecm.core.work.api.Work; 023import org.nuxeo.ecm.core.work.api.WorkManager; 024import org.nuxeo.elasticsearch.api.ElasticSearchIndexing; 025import org.nuxeo.elasticsearch.commands.IndexingCommand; 026import org.nuxeo.runtime.api.Framework; 027 028import java.util.List; 029 030/** 031 * Worker to index children recursively 032 * 033 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 034 */ 035public class ChildrenIndexingWorker extends AbstractIndexingWorker implements Work { 036 037 private static final long serialVersionUID = 724369727479693496L; 038 039 public ChildrenIndexingWorker(IndexingCommand cmd) { 040 super(cmd); 041 } 042 043 @Override 044 public String getTitle() { 045 return " ElasticSearch indexing children for cmd " + (cmds.isEmpty() ? "null" : cmds.get(0)); 046 } 047 048 @Override 049 protected void doIndexingWork(ElasticSearchIndexing esi, List<IndexingCommand> cmds) { 050 if (cmds.isEmpty()) { 051 return; 052 } 053 IndexingCommand cmd = cmds.get(0); 054 DocumentModel doc = getDocument(cmd); 055 if (doc == null) { 056 return; 057 } 058 DocumentModelIterator iter = session.getChildrenIterator(doc.getRef()); 059 while (iter.hasNext()) { 060 // Add a session save to process cache invalidation 061 session.save(); 062 DocumentModel child = iter.next(); 063 064 IndexingCommand childCommand = cmd.clone(child); 065 esi.indexNonRecursive(childCommand); 066 if (child.isFolder()) { 067 ChildrenIndexingWorker subWorker = new ChildrenIndexingWorker(childCommand); 068 WorkManager wm = Framework.getLocalService(WorkManager.class); 069 wm.schedule(subWorker); 070 } 071 } 072 073 } 074 075 private DocumentModel getDocument(IndexingCommand cmd) { 076 DocumentModel doc = cmd.getTargetDocument(); 077 if (doc == null) { 078 // doc has been deleted 079 return null; 080 } 081 return doc; 082 } 083 084}