ElasticsearchClient.scala 4.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package modules

import java.io.IOException
import java.util

import com.typesafe.config.{Config, ConfigObject}
import javax.inject.{Inject, Singleton}
import org.apache.http.{Header, HttpHost}
import org.apache.http.message.BasicHeader
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest
import org.elasticsearch.client.{RequestOptions, RestClient, RestHighLevelClient}
import org.elasticsearch.common.bytes.BytesReference
import org.elasticsearch.common.xcontent.{XContentFactory, XContentType}
import org.swissbib.memobase.oai.common.util.{OaiCommonConfig, OaiConfig, OaiConfigMetadataPrefixes, OaiConfigSets, OaiIdentifyConfig}
import play.Environment
import play.api.inject.ApplicationLifecycle
import utilities.FileUtil
import scala.jdk.CollectionConverters._

import scala.collection.mutable.ArrayBuffer
import scala.concurrent.Future

@Singleton
class ElasticsearchClient @Inject()(
                              lifecycle: ApplicationLifecycle,
                              private val config: Config,
                              private val env: Environment
                            ) extends ElasticsearchComponent {
  lifecycle.addStopHook(() => {
    //Future.successful(client.get.close())
    Future.successful(client.getOrElse(Option.empty))
  })

  //val client: Option[RestHighLevelClient] = connect()
  override val client: Option[RestHighLevelClient] = None

  //val oai = loadOaiConfig()

  override val oaiConfig: OaiConfig = loadOaiConfig()
  //uploadTemplates()

  private def loadOaiConfig(): OaiConfig = {

    val sI = config.getObject("oaiconfigs.identify").toConfig
          //todo: make this as implicit Conversion
    val identifyConfig = OaiIdentifyConfig(
          earliestDatestamp = sI.getString("earliestDatestamp"),
          deletedRecord = sI.getString("deletedRecord"),
          adminEmail = sI.getString("adminEmail"),
          repositoryName = sI.getString("repositoryName"),
          baseUrl = sI.getString("baseURL"),
          granularity = sI.getString("granularity"),
          protocolVersion = sI.getString("protocolVersion"))

    val sC = config.getObject("oaiconfigs.common").toConfig
    val commonConfig = OaiCommonConfig(
      xsi_schemaLocation = sC.getString("xsi_schemaLocation")
    )


    //collection conversions
    //https://stackoverflow.com/questions/8301947/what-is-the-difference-between-javaconverters-and-javaconversions-in-scala
    val oaiConfigSet = config.getStringList("oaiconfigs.sets").asScala.toSeq
    val oaiConfigPrefixes = config.getStringList("oaiconfigs.metadataPrefix").asScala.toSeq

    OaiConfig(
      commonConfig,
      identifyConfig,
      OaiConfigSets(oaiConfigSet),
      OaiConfigMetadataPrefixes(oaiConfigPrefixes) )

  }

  private def connect(): Option[RestHighLevelClient] = {
    val hosts = new ArrayBuffer[HttpHost]
    config.getStringList("index.hosts").forEach(
      value => {
        val hostPort = value.split(":")
        hosts += new HttpHost(hostPort(0), hostPort(1).toInt)
      }
    )
    val headers = Array(new BasicHeader("cluster.name", config.getString("index.cluster")).asInstanceOf[Header])
    Option(new RestHighLevelClient(RestClient.builder(hosts.toArray : _*).setDefaultHeaders(headers)))
  }

  private def uploadTemplates(): Unit = {
    config.getStringList("index.templatequeries").forEach((templateName: String) => {
        val template = FileUtil.readFile(templateName, env)
        val nameWithoutPath = templateName.substring(templateName.lastIndexOf("/") + 1).replaceAll(".mustache", "")
        val templatePrefix = config.getString("index.template_prefix")
        try {
          val request = new PutStoredScriptRequest
          request.id(templatePrefix + "_" + nameWithoutPath)
          val builder = XContentFactory.jsonBuilder
          builder.startObject
          builder.startObject("script")
          builder.field("lang", "mustache")
          // load mustache source as string to allow invalid JSON template features!
          builder.field("source", template.replace("\n", ""))
          builder.endObject
          builder.endObject
          request.content(BytesReference.bytes(builder), XContentType.JSON)
          client.get.putScript(request, RequestOptions.DEFAULT)
        } catch {
          case io: IOException =>
            //TODO: if the templates cannot be read the application should shut down.
            io.printStackTrace()
        }
    })
  }


}