001/*
002 * (C) Copyright 2015 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 *     Benoit Delbosc
018 */
019package org.nuxeo.ecm.automation.elasticsearch;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.api.NuxeoPrincipal;
032import org.nuxeo.elasticsearch.api.ElasticSearchAdmin;
033import org.nuxeo.elasticsearch.api.ElasticSearchIndexing;
034import org.nuxeo.elasticsearch.commands.IndexingCommand;
035
036import java.util.Arrays;
037import java.util.concurrent.ExecutionException;
038
039/**
040 * Run Elasticsearch indexing operation
041 *
042 * @since 8.1
043 */
044@Operation(id = ElasticsearchIndexOperation.ID, category = Constants.CAT_SERVICES, label = "Elasticsearch Indexing", since = "8.1",
045        description = "Enable to index Nuxeo documents.")
046public class ElasticsearchIndexOperation {
047
048    public static final String ID = "Elasticsearch.Index";
049
050    private static final Log log = LogFactory.getLog(Log.class);
051
052    @Context
053    protected OperationContext ctx;
054
055    @Context
056    protected ElasticSearchIndexing esi;
057
058    @Context
059    protected ElasticSearchAdmin esa;
060
061    @Context
062    protected CoreSession repo;
063
064    @OperationMethod
065    public void run() {
066        checkAccess();
067        esi.reindexRepository(repo.getRepositoryName());
068    }
069
070    private void checkAccess() {
071        NuxeoPrincipal principal = ctx.getPrincipal();
072        if (principal == null || ! principal.isAdministrator()) {
073            throw new NuxeoException("Unauthorized access: " + principal);
074        }
075    }
076
077    @OperationMethod
078    public void run(String nxql) {
079        checkAccess();
080        esi.runReindexingWorker(repo.getRepositoryName(), nxql);
081    }
082
083    @OperationMethod
084    public void run(DocumentModel doc) {
085        checkAccess();
086        // 1. delete existing index
087        IndexingCommand cmd = new IndexingCommand(doc, IndexingCommand.Type.DELETE, false, true);
088        esi.runIndexingWorker(Arrays.asList(cmd));
089        // 2. wait for the deletion to be completed
090        try {
091            esa.prepareWaitForIndexing().get();
092        } catch (InterruptedException e) {
093            Thread.currentThread().interrupt();
094            throw new NuxeoException("Interrupted");
095        } catch (ExecutionException e) {
096            throw new NuxeoException(e);
097        }
098        // 3. index recursive from path
099        cmd = new IndexingCommand(doc, IndexingCommand.Type.INSERT, false, true);
100        esi.runIndexingWorker(Arrays.asList(cmd));
101    }
102
103}