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