|
@@ -0,0 +1,287 @@
|
|
|
+package com.swagger.rest.controllers
|
|
|
+
|
|
|
+import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
|
|
+import com.swagger.rest.models.*
|
|
|
+import com.swagger.rest.models.Enum
|
|
|
+import com.swagger.rest.repositories.*
|
|
|
+import jakarta.servlet.http.HttpServletResponse
|
|
|
+import org.springframework.http.HttpStatus
|
|
|
+import org.springframework.http.ResponseEntity
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder
|
|
|
+import org.springframework.web.bind.annotation.*
|
|
|
+import org.springframework.web.multipart.MultipartFile
|
|
|
+import org.supercsv.io.CsvBeanWriter
|
|
|
+import org.supercsv.io.ICsvBeanWriter
|
|
|
+import org.supercsv.prefs.CsvPreference
|
|
|
+import java.io.IOException
|
|
|
+import java.text.SimpleDateFormat
|
|
|
+import java.util.*
|
|
|
+import kotlin.Any
|
|
|
+import kotlin.Boolean
|
|
|
+import kotlin.String
|
|
|
+import kotlin.Throws
|
|
|
+import kotlin.Unit
|
|
|
+import kotlin.arrayOf
|
|
|
+import kotlin.toString
|
|
|
+
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/v1")
|
|
|
+class MaintenanceController(
|
|
|
+ private val projectRepository: ProjectRepository,
|
|
|
+ private val userRepository: UserRepository,
|
|
|
+ private val platformRepository: PlatformRepository,
|
|
|
+ private val memberRepository: MemberRepository,
|
|
|
+ private val bugRepository: BugRepository,
|
|
|
+ private val commentRepository: CommentRepository
|
|
|
+) {
|
|
|
+
|
|
|
+ @GetMapping("/maintenance")
|
|
|
+ @Throws(IOException::class)
|
|
|
+ fun exportToCSV(response: HttpServletResponse, @RequestParam tableName: String) {
|
|
|
+ val userId = userRepository.getUserByUsername(SecurityContextHolder.getContext().authentication.name)
|
|
|
+ val valid = memberRepository.validAdmin(userId.id.toString())
|
|
|
+ val tableList = listOf("project", "platform", "user", "project_member", "bug", "comment")
|
|
|
+// if (valid == 0) {
|
|
|
+// ResponseEntity<Any>(HttpStatus.FORBIDDEN)
|
|
|
+// } else {
|
|
|
+ if (!tableList.contains(tableName) || tableName.isEmpty()) {
|
|
|
+ ResponseEntity<Any>(HttpStatus.BAD_REQUEST)
|
|
|
+ } else {
|
|
|
+
|
|
|
+ val list = when (tableName) {
|
|
|
+ "user" -> userRepository.findAll()
|
|
|
+ "project" -> projectRepository.findAll().map {
|
|
|
+ ProjectOutput(
|
|
|
+ id = it.id, name = it.name, description = it.description, owner = it.owner!!.id.toString()
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ "platform" -> platformRepository.findAll().map {
|
|
|
+ PlatformOutput(
|
|
|
+ id = it.id, name = it.name, project = it.project!!.id.toString()
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ "project_member" -> memberRepository.findAll().map {
|
|
|
+ MemberOutput(
|
|
|
+ id = it.id,
|
|
|
+ project = it.project!!.id.toString(),
|
|
|
+ user = it.user!!.id.toString(),
|
|
|
+ role = Enum.Member.values()[it.role].ordinal.toString()
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ "bug" -> bugRepository.findAll().map {
|
|
|
+ BugOutput(
|
|
|
+ id = it.id,
|
|
|
+ created = SimpleDateFormat("dd-MMM-yy HH:mm:ss").format(it.created),
|
|
|
+ description = it.description,
|
|
|
+ qc = it.qc!!.id.toString(),
|
|
|
+ dev = it.dev!!.id.toString(),
|
|
|
+ platform = it.platform!!.id.toString(),
|
|
|
+ goodday_url = it.goodday_url,
|
|
|
+ image_url = it.image_url,
|
|
|
+ level = Enum.Level.values()[it.level].ordinal.toString(),
|
|
|
+ status = Enum.Status.values()[it.status].ordinal.toString(),
|
|
|
+ dev_status = Enum.Dev_Status.values()[it.dev_status].ordinal.toString()
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ "comment" -> commentRepository.findAll().map {
|
|
|
+ CommentOutput(
|
|
|
+ id = it.id,
|
|
|
+ bug = it.bug!!.id.toString(),
|
|
|
+ creator = it.creator!!.id.toString(),
|
|
|
+ created = SimpleDateFormat("dd-MMM-yy HH:mm:ss").format(it.created),
|
|
|
+ content = it.content
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ else -> listOf()
|
|
|
+ }
|
|
|
+ val csvHeader = when (tableName) {
|
|
|
+ "user" -> arrayOf("User ID", "Username", "Password", "Name")
|
|
|
+ "project" -> arrayOf("Project ID", "Description", "Name", "Owner")
|
|
|
+ "platform" -> arrayOf("Platform ID", "Name", "Project")
|
|
|
+ "project_member" -> arrayOf("Member ID", "Role", "Project", "User")
|
|
|
+ "comment" -> arrayOf("Bug ID", "Content", "Created", "Bug", "Creator")
|
|
|
+ "bug" -> arrayOf(
|
|
|
+ "Bug ID",
|
|
|
+ "created",
|
|
|
+ "Description",
|
|
|
+ "Dev Status",
|
|
|
+ "Goodday Url",
|
|
|
+ "Image Url",
|
|
|
+ "Level",
|
|
|
+ "Status",
|
|
|
+ "Dev",
|
|
|
+ "Platform",
|
|
|
+ "Qc"
|
|
|
+ )
|
|
|
+
|
|
|
+ else -> arrayOf()
|
|
|
+ }
|
|
|
+ val nameMapping = when (tableName) {
|
|
|
+ "user" -> arrayOf("id", "username", "password", "name")
|
|
|
+ "project" -> arrayOf("id", "description", "name", "owner")
|
|
|
+ "platform" -> arrayOf("id", "name", "project")
|
|
|
+ "project_member" -> arrayOf("id", "role", "project", "user")
|
|
|
+ "comment" -> arrayOf("id", "content", "created", "bug", "creator")
|
|
|
+ "bug" -> arrayOf(
|
|
|
+ "id",
|
|
|
+ "created",
|
|
|
+ "description",
|
|
|
+ "dev_status",
|
|
|
+ "goodday_url",
|
|
|
+ "image_url",
|
|
|
+ "level",
|
|
|
+ "status",
|
|
|
+ "dev",
|
|
|
+ "platform",
|
|
|
+ "qc"
|
|
|
+ )
|
|
|
+
|
|
|
+ else -> arrayOf()
|
|
|
+ }
|
|
|
+ val csvWriter: ICsvBeanWriter = CsvBeanWriter(response.writer, CsvPreference.STANDARD_PREFERENCE)
|
|
|
+ csvWriter.writeHeader(*csvHeader)
|
|
|
+ for (table in list) {
|
|
|
+ csvWriter.write(table, *nameMapping)
|
|
|
+ }
|
|
|
+ csvWriter.close()
|
|
|
+// }
|
|
|
+ }
|
|
|
+ //todo multi / zip download?
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/maintenance")
|
|
|
+ fun import(@RequestParam file: MultipartFile, @RequestParam tableName: String, @RequestParam overwrite: Boolean) {
|
|
|
+ val userId = userRepository.getUserByUsername(SecurityContextHolder.getContext().authentication.name)
|
|
|
+ val valid = memberRepository.validAdmin(userId.id.toString())
|
|
|
+ val tableList = listOf("project", "platform", "user", "project_member", "bug", "comment")
|
|
|
+// if (valid == 0) {
|
|
|
+// ResponseEntity<Any>(HttpStatus.FORBIDDEN)
|
|
|
+// } else {
|
|
|
+ if (!tableList.contains(tableName) || tableName.isEmpty()) {
|
|
|
+ ResponseEntity<Any>(HttpStatus.BAD_REQUEST)
|
|
|
+ } else {
|
|
|
+ val nameMapping = when (tableName) {
|
|
|
+ "user" -> arrayOf("id", "username", "password", "name")
|
|
|
+ "project" -> arrayOf("id", "description", "name", "owner")
|
|
|
+ "platform" -> arrayOf("id", "name", "project")
|
|
|
+ "project_member" -> arrayOf("id", "role", "project", "user")
|
|
|
+ "comment" -> arrayOf("id", "content", "created", "bug", "creator")
|
|
|
+ "bug" -> arrayOf(
|
|
|
+ "id",
|
|
|
+ "created",
|
|
|
+ "description",
|
|
|
+ "dev_status",
|
|
|
+ "goodday_url",
|
|
|
+ "image_url",
|
|
|
+ "level",
|
|
|
+ "status",
|
|
|
+ "dev",
|
|
|
+ "platform",
|
|
|
+ "qc"
|
|
|
+ )
|
|
|
+
|
|
|
+ else -> arrayOf()
|
|
|
+ }
|
|
|
+ val content = String(file.bytes)
|
|
|
+ val lines = content.lines()
|
|
|
+ val mapData: MutableList<MutableMap<String, Any>> = mutableListOf()
|
|
|
+ lines.forEachIndexed { ind, t ->
|
|
|
+ if (ind == 0 && arrayOf(t) !== nameMapping) {
|
|
|
+ ResponseEntity<Any>(HttpStatus.BAD_REQUEST)
|
|
|
+ } else if (t !== "" && ind > 0) {
|
|
|
+ val commas = t.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)".toRegex()).toTypedArray()
|
|
|
+ val map: MutableMap<String, Any> = mutableMapOf()
|
|
|
+ nameMapping.forEachIndexed { index, s ->
|
|
|
+ map[s] = commas[index]
|
|
|
+ }
|
|
|
+ mapData.add(map)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val existing = when (tableName) {
|
|
|
+ "project" -> projectRepository.getId()
|
|
|
+ "platform" -> platformRepository.getId()
|
|
|
+ "user" -> userRepository.getId()
|
|
|
+ "project_member" -> memberRepository.getId()
|
|
|
+ "bug" -> bugRepository.getId()
|
|
|
+ "comment" -> commentRepository.getId()
|
|
|
+ else -> listOf()
|
|
|
+ }
|
|
|
+ mapData.removeIf { p -> existing.contains(p["id"].toString().toLong()) && !overwrite }
|
|
|
+ val unit = when (tableName) {
|
|
|
+ "project" -> mapData.forEach {
|
|
|
+ val save = Project()
|
|
|
+ save.id = it["id"].toString().toLong()
|
|
|
+ save.name = it["name"].toString()
|
|
|
+ save.description = it["description"].toString()
|
|
|
+ save.owner = userRepository.findById(it["owner"].toString().toLong()).get()
|
|
|
+ projectRepository.save(save)
|
|
|
+ }
|
|
|
+
|
|
|
+ "user" -> mapData.forEach {
|
|
|
+ val save = jacksonObjectMapper().readValue(
|
|
|
+ jacksonObjectMapper().writeValueAsString(it), User::class.java
|
|
|
+ )
|
|
|
+ userRepository.save(save)
|
|
|
+ }
|
|
|
+
|
|
|
+ "platform" -> mapData.forEach {
|
|
|
+ val save = Platform()
|
|
|
+ save.id = it["id"].toString().toLong()
|
|
|
+ save.name = it["name"].toString()
|
|
|
+ save.project = projectRepository.findById(it["project"].toString().toLong()).get()
|
|
|
+ platformRepository.save(save)
|
|
|
+ }
|
|
|
+
|
|
|
+ "project_member" -> mapData.forEach {
|
|
|
+ val save = ProjectMember()
|
|
|
+ save.id = it["id"].toString().toLong()
|
|
|
+ save.role = Enum.Member.values()[Integer.parseInt(it["role"].toString())].ordinal
|
|
|
+ save.project = projectRepository.findById(it["project"].toString().toLong()).get()
|
|
|
+ save.user = userRepository.findById(it["user"].toString().toLong()).get()
|
|
|
+ memberRepository.save(save)
|
|
|
+ }
|
|
|
+
|
|
|
+ "comment" -> mapData.forEach {
|
|
|
+ val save = Comment()
|
|
|
+ save.id = it["id"].toString().toLong()
|
|
|
+ save.content = it["content"].toString()
|
|
|
+ save.created =
|
|
|
+ SimpleDateFormat("dd-MMM-yy HH:mm:ss", Locale.ENGLISH).parse(it["created"].toString())
|
|
|
+ save.bug = bugRepository.findById(it["bug"].toString().toLong()).get()
|
|
|
+ save.creator = userRepository.findById(it["creator"].toString().toLong()).get()
|
|
|
+ commentRepository.save(save)
|
|
|
+ }
|
|
|
+
|
|
|
+ "bug" -> mapData.forEach {
|
|
|
+ val save = Bug()
|
|
|
+ save.id = it["id"].toString().toLong()
|
|
|
+ save.created =
|
|
|
+ SimpleDateFormat("dd-MMM-yy HH:mm:ss", Locale.ENGLISH).parse(it["created"].toString())
|
|
|
+ save.description = it["description"].toString()
|
|
|
+ save.dev_status =
|
|
|
+ Enum.Dev_Status.values()[Integer.parseInt(it["dev_status"].toString())].ordinal
|
|
|
+ save.goodday_url = it["goodday_url"].toString()
|
|
|
+ save.image_url = it["image_url"].toString()
|
|
|
+ save.level = Enum.Level.values()[Integer.parseInt(it["level"].toString())].ordinal
|
|
|
+ save.status = Enum.Status.values()[Integer.parseInt(it["status"].toString())].ordinal
|
|
|
+ save.dev = userRepository.findById(it["dev"].toString().toLong()).get()
|
|
|
+ save.platform = platformRepository.findById(it["platform"].toString().toLong()).get()
|
|
|
+ save.qc = userRepository.findById(it["qc"].toString().toLong()).get()
|
|
|
+ bugRepository.save(save)
|
|
|
+ }
|
|
|
+
|
|
|
+ else -> Unit
|
|
|
+ }
|
|
|
+ ResponseEntity<Any>(HttpStatus.OK)
|
|
|
+// }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|