View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.core.reference;
20  
21  import org.apache.syncope.common.lib.to.SchedTaskTO;
22  import org.apache.syncope.common.lib.types.IdRepoImplementationType;
23  import org.apache.syncope.common.lib.types.ImplementationEngine;
24  import org.apache.syncope.common.lib.types.TaskType;
25  import org.apache.syncope.core.logic.TaskLogic;
26  import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
27  import org.apache.syncope.core.persistence.api.entity.EntityFactory;
28  import org.apache.syncope.core.persistence.api.entity.Implementation;
29  import org.springframework.transaction.annotation.Transactional;
30  
31  public class ElasticsearchInit {
32  
33      private static final String ES_REINDEX = "org.apache.syncope.core.provisioning.java.job.ElasticsearchReindex";
34  
35      private final ImplementationDAO implementationDAO;
36  
37      private final EntityFactory entityFactory;
38  
39      private final TaskLogic taskLogic;
40  
41      public ElasticsearchInit(
42              final ImplementationDAO implementationDAO,
43              final EntityFactory entityFactory,
44              final TaskLogic taskLogic) {
45  
46          this.implementationDAO = implementationDAO;
47          this.entityFactory = entityFactory;
48          this.taskLogic = taskLogic;
49      }
50  
51      @Transactional
52      public void init() {
53          Implementation reindex = implementationDAO.findByType(IdRepoImplementationType.TASKJOB_DELEGATE).
54                  stream().
55                  filter(impl -> impl.getEngine() == ImplementationEngine.JAVA && ES_REINDEX.equals(impl.getBody())).
56                  findAny().
57                  orElseGet(() -> {
58                      Implementation impl = entityFactory.newEntity(Implementation.class);
59                      impl.setKey(ES_REINDEX);
60                      impl.setEngine(ImplementationEngine.JAVA);
61                      impl.setType(IdRepoImplementationType.TASKJOB_DELEGATE);
62                      impl.setBody(ES_REINDEX);
63                      return implementationDAO.save(impl);
64                  });
65  
66          SchedTaskTO task = new SchedTaskTO();
67          task.setJobDelegate(reindex.getKey());
68          task.setName("Elasticsearch Reindex");
69          task = taskLogic.createSchedTask(TaskType.SCHEDULED, task);
70  
71          taskLogic.execute(task.getKey(), null, false);
72      }
73  }