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.Collections;
023import java.util.List;
024
025import org.nuxeo.elasticsearch.api.ElasticSearchIndexing;
026import org.nuxeo.elasticsearch.commands.IndexingCommand;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * Abstract class for sharing code between ElasticSearch related workers
031 *
032 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
033 */
034public abstract class AbstractIndexingWorker extends BaseIndexingWorker {
035
036    private static final long serialVersionUID = 1L;
037
038    protected final List<IndexingCommand> cmds;
039
040    public AbstractIndexingWorker(IndexingCommand cmd) {
041        this.cmds = Collections.singletonList(cmd);
042        this.repositoryName = cmd.getRepositoryName();
043        this.docId = cmd.getTargetDocumentId();
044    }
045
046    public AbstractIndexingWorker(String repositoryName, List<IndexingCommand> cmds) {
047        this.cmds = cmds;
048        this.repositoryName = repositoryName;
049        if (!cmds.isEmpty()) {
050            this.docId = cmds.get(0).getTargetDocumentId();
051        }
052    }
053
054    @Override
055    public void doWork() {
056        openSystemSession();
057        for (IndexingCommand cmd : cmds) {
058            cmd.attach(session);
059        }
060        ElasticSearchIndexing esi = Framework.getService(ElasticSearchIndexing.class);
061        doIndexingWork(esi, cmds);
062    }
063
064    protected abstract void doIndexingWork(ElasticSearchIndexing esi, List<IndexingCommand> cmds);
065
066}