summaryrefslogtreecommitdiff
path: root/src/main/kotlin/dev/dnpm/etl/processor/web/ConfigController.kt
blob: 5bd25b7b9973223ba448dbca56c2fdc71824d612 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * This file is part of ETL-Processor
 *
 * Copyright (c) 2023       Comprehensive Cancer Center Mainfranken
 * Copyright (c) 2023-2026  Paul-Christian Volkmer, Datenintegrationszentrum Philipps-Universität Marburg and Contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package dev.dnpm.etl.processor.web

import dev.dnpm.etl.processor.monitoring.*
import dev.dnpm.etl.processor.output.MtbFileSender
import dev.dnpm.etl.processor.pseudonym.Generator
import dev.dnpm.etl.processor.security.Role
import dev.dnpm.etl.processor.security.Token
import dev.dnpm.etl.processor.security.TokenService
import dev.dnpm.etl.processor.security.UserRole
import dev.dnpm.etl.processor.security.UserRoleService
import dev.dnpm.etl.processor.services.TransformationService
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.http.MediaType
import org.springframework.http.codec.ServerSentEvent
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Flux
import reactor.core.publisher.Sinks

@Controller
@RequestMapping(path = ["configs"])
class ConfigController(
    @param:Qualifier("connectionCheckUpdateProducer")
    private val connectionCheckUpdateProducer: Sinks.Many<ConnectionCheckResult>,
    private val transformationService: TransformationService,
    private val pseudonymGenerator: Generator,
    private val mtbFileSender: MtbFileSender,
    private val connectionCheckServices: List<ConnectionCheckService>,
    private val tokenService: TokenService?,
    private val userRoleService: UserRoleService?,
) {

  @GetMapping
  fun index(model: Model): String {
    val outputConnectionAvailable =
        connectionCheckServices
            .filterIsInstance<OutputConnectionCheckService>()
            .firstOrNull()
            ?.connectionAvailable()

    val gPasConnectionAvailable =
        connectionCheckServices
            .filterIsInstance<GPasConnectionCheckService>()
            .firstOrNull()
            ?.connectionAvailable()

    val gIcsConnectionAvailable =
        connectionCheckServices
            .filterIsInstance<GIcsConnectionCheckService>()
            .firstOrNull()
            ?.connectionAvailable()

    model.addAttribute("pseudonymGenerator", pseudonymGenerator.javaClass.simpleName)
    model.addAttribute("mtbFileSender", mtbFileSender.javaClass.simpleName)
    model.addAttribute("mtbFileEndpoint", mtbFileSender.endpoint())
    model.addAttribute("outputConnectionAvailable", outputConnectionAvailable)
    model.addAttribute("gPasConnectionAvailable", gPasConnectionAvailable)
    model.addAttribute("gIcsConnectionAvailable", gIcsConnectionAvailable)
    model.addAttribute("tokensEnabled", tokenService != null)
    if (tokenService != null) {
      model.addAttribute("tokens", tokenService.findAll())
    } else {
      model.addAttribute("tokens", emptyList<Token>())
    }
    model.addAttribute("transformations", transformationService.getTransformations())
    if (userRoleService != null) {
      model.addAttribute("userRolesEnabled", true)
      model.addAttribute("userRoles", userRoleService.findAll())
    } else {
      model.addAttribute("userRolesEnabled", false)
      model.addAttribute("userRoles", emptyList<UserRole>())
    }
    return "configs"
  }

  @GetMapping(params = ["outputConnectionAvailable"])
  fun outputConnectionAvailable(model: Model): String {
    val outputConnectionAvailable =
        connectionCheckServices
            .filterIsInstance<OutputConnectionCheckService>()
            .first()
            .connectionAvailable()

    model.addAttribute("mtbFileSender", mtbFileSender.javaClass.simpleName)
    model.addAttribute("mtbFileEndpoint", mtbFileSender.endpoint())
    model.addAttribute("outputConnectionAvailable", outputConnectionAvailable)
    if (tokenService != null) {
      model.addAttribute("tokensEnabled", true)
      model.addAttribute("tokens", tokenService.findAll())
    } else {
      model.addAttribute("tokens", listOf<Token>())
    }

    return "configs/outputConnectionAvailable"
  }

  @GetMapping(params = ["gPasConnectionAvailable"])
  fun gPasConnectionAvailable(model: Model): String {
    val gPasConnectionAvailable =
        connectionCheckServices
            .filterIsInstance<GPasConnectionCheckService>()
            .firstOrNull()
            ?.connectionAvailable()

    model.addAttribute("mtbFileSender", mtbFileSender.javaClass.simpleName)
    model.addAttribute("mtbFileEndpoint", mtbFileSender.endpoint())
    model.addAttribute("gPasConnectionAvailable", gPasConnectionAvailable)
    if (tokenService != null) {
      model.addAttribute("tokensEnabled", true)
      model.addAttribute("tokens", tokenService.findAll())
    } else {
      model.addAttribute("tokens", listOf<Token>())
    }

    return "configs/gPasConnectionAvailable"
  }

  @GetMapping(params = ["gIcsConnectionAvailable"])
  fun gIcsConnectionAvailable(model: Model): String {
    val gIcsConnectionAvailable =
        connectionCheckServices
            .filterIsInstance<GIcsConnectionCheckService>()
            .firstOrNull()
            ?.connectionAvailable()

    model.addAttribute("mtbFileSender", mtbFileSender.javaClass.simpleName)
    model.addAttribute("mtbFileEndpoint", mtbFileSender.endpoint())
    model.addAttribute("gIcsConnectionAvailable", gIcsConnectionAvailable)
    if (tokenService != null) {
      model.addAttribute("tokensEnabled", true)
      model.addAttribute("tokens", tokenService.findAll())
    } else {
      model.addAttribute("tokens", listOf<Token>())
    }

    return "configs/gIcsConnectionAvailable"
  }

  @PostMapping(path = ["tokens"])
  fun addToken(@ModelAttribute("name") name: String, model: Model): String {
    if (tokenService == null) {
      model.addAttribute("tokensEnabled", false)
      model.addAttribute("success", false)
    } else {
      model.addAttribute("tokensEnabled", true)
      val result = tokenService.addToken(name)
      result.onSuccess {
        model.addAttribute("newTokenValue", it)
        model.addAttribute("success", true)
      }
      result.onFailure { model.addAttribute("success", false) }
      model.addAttribute("tokens", tokenService.findAll())
    }

    return "configs/tokens"
  }

  @DeleteMapping(path = ["tokens/{id}"])
  fun deleteToken(@PathVariable id: Long, model: Model): String {
    if (tokenService != null) {
      tokenService.deleteToken(id)

      model.addAttribute("tokensEnabled", true)
      model.addAttribute("tokens", tokenService.findAll())
    } else {
      model.addAttribute("tokensEnabled", false)
      model.addAttribute("tokens", listOf<Token>())
    }
    return "configs/tokens"
  }

  @DeleteMapping(path = ["userroles/{id}"])
  fun deleteUserRole(@PathVariable id: Long, model: Model): String {
    if (userRoleService != null) {
      userRoleService.deleteUserRole(id)

      model.addAttribute("userRolesEnabled", true)
      model.addAttribute("userRoles", userRoleService.findAll())
    } else {
      model.addAttribute("userRolesEnabled", false)
      model.addAttribute("userRoles", emptyList<UserRole>())
    }
    return "configs/userroles"
  }

  @PutMapping(path = ["userroles/{id}"])
  fun updateUserRole(
      @PathVariable id: Long,
      @ModelAttribute("role") role: Role,
      model: Model,
  ): String {
    if (userRoleService != null) {
      userRoleService.updateUserRole(id, role)

      model.addAttribute("userRolesEnabled", true)
      model.addAttribute("userRoles", userRoleService.findAll())
    } else {
      model.addAttribute("userRolesEnabled", false)
      model.addAttribute("userRoles", emptyList<UserRole>())
    }
    return "configs/userroles"
  }

  @GetMapping(path = ["events"], produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
  @ResponseBody
  fun events(): Flux<ServerSentEvent<Any>> {
    return connectionCheckUpdateProducer.asFlux().map {
      val event =
          when (it) {
            is ConnectionCheckResult.KafkaConnectionCheckResult -> "output-connection-check"
            is ConnectionCheckResult.RestConnectionCheckResult -> "output-connection-check"
            is ConnectionCheckResult.GPasConnectionCheckResult -> "gpas-connection-check"
            is ConnectionCheckResult.GIcsConnectionCheckResult -> "gics-connection-check"
          }

      ServerSentEvent.builder<Any>().event(event).id("none").data(it).build()
    }
  }
}