|
@@ -6,19 +6,18 @@ import com.swagger.rest.models.Enum
|
|
|
import com.swagger.rest.repositories.*
|
|
|
import com.swagger.rest.services.*
|
|
|
import jakarta.servlet.http.HttpServletResponse
|
|
|
+import org.springframework.http.HttpHeaders
|
|
|
import org.springframework.http.HttpStatus
|
|
|
+import org.springframework.http.MediaType
|
|
|
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.ByteArray
|
|
|
import kotlin.RuntimeException
|
|
|
import kotlin.String
|
|
|
import kotlin.Throws
|
|
@@ -44,123 +43,100 @@ class MaintenanceController(
|
|
|
private val bugService: BugService
|
|
|
) {
|
|
|
|
|
|
- @GetMapping("/maintenance")
|
|
|
+ @GetMapping("/maintenance", produces = ["text/csv;charset=utf8;"])
|
|
|
@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())
|
|
|
+ fun exportToCSV(response: HttpServletResponse, @RequestParam tableName: String): ResponseEntity<*> {
|
|
|
+// 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)
|
|
|
+ return ResponseEntity<Any>(HttpStatus.BAD_REQUEST)
|
|
|
} else {
|
|
|
-
|
|
|
val list = when (tableName) {
|
|
|
- "user" -> userRepository.findAll()
|
|
|
+ "user" -> userRepository.findAll().map {
|
|
|
+ "${it.id},\"${it.username}\",\"${it.password}\",\"${it.name}\""
|
|
|
+ }
|
|
|
+
|
|
|
"project" -> projectRepository.findAll().map {
|
|
|
- ProjectOutput(
|
|
|
- id = it.id, name = it.name, description = it.description, owner = it.owner!!.id.toString()
|
|
|
- )
|
|
|
+ "${it.id},\"${it.name}\",\"${it.description}\",${it.owner!!.id}"
|
|
|
}
|
|
|
|
|
|
"platform" -> platformRepository.findAll().map {
|
|
|
- PlatformOutput(
|
|
|
- id = it.id, name = it.name, project = it.project!!.id.toString()
|
|
|
- )
|
|
|
+ "${it.id},\"${it.name}\",${it.project!!.id}"
|
|
|
}
|
|
|
|
|
|
"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()
|
|
|
- )
|
|
|
+ "${it.id},${it.role},${it.project!!.id},${it.user!!.id}"
|
|
|
}
|
|
|
|
|
|
"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()
|
|
|
- )
|
|
|
+ "${it.id},${SimpleDateFormat("dd-MMM-yy HH:mm:ss").format(it.created)},\"${it.description}\",${it.dev_status},\"${it.goodday_url}\",\"${it.image_url}\",${it.level},${it.status},${it.dev!!.id},${it.platform!!.id},${it.qc!!.id}"
|
|
|
}
|
|
|
|
|
|
"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
|
|
|
- )
|
|
|
+ "${it.id},\"${it.content}\",${SimpleDateFormat("dd-MMM-yy HH:mm:ss").format(it.created)},${it.bug!!.id},${it.creator!!.id}"
|
|
|
}
|
|
|
|
|
|
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"
|
|
|
+ "user" -> listOf("User ID,\"Username\",\"Password\",\"Name\"")
|
|
|
+ "project" -> listOf("Project ID,\"Description\",\"Name\",Owner")
|
|
|
+ "platform" -> listOf("Platform ID,\"Name\",Project")
|
|
|
+ "project_member" -> listOf("Member ID,Role,Project,User")
|
|
|
+ "comment" -> listOf("Bug ID,\"Content\",Created,Bug,Creator")
|
|
|
+ "bug" -> listOf(
|
|
|
+ "Bug ID,Created,\"Description\",Dev Status,\"Goodday Url\",\"Image Url\",Level,Status,Dev,Platform,Qc"
|
|
|
)
|
|
|
|
|
|
- else -> arrayOf()
|
|
|
+ else -> listOf()
|
|
|
}
|
|
|
- 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()
|
|
|
+ val csvContent = StringBuilder()
|
|
|
+ csvContent.append("${csvHeader.joinToString("")}\n")
|
|
|
+ csvContent.append(list.joinToString("\n"))
|
|
|
+ val headers = HttpHeaders()
|
|
|
+// headers.contentType = MediaType.APPLICATION_OCTET_STREAM
|
|
|
+// headers.setContentDispositionFormData("attachment", "$tableName.csv")
|
|
|
+// val fileName = String(tableName.toByteArray(), charset("ISO8859-1"))
|
|
|
+// headers["content-disposition"] = "attachment;filename=$fileName.csv"
|
|
|
+// response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=$tableName.csv")
|
|
|
+ response.contentType = MediaType.APPLICATION_OCTET_STREAM.toString()
|
|
|
+// response.addHeade
|
|
|
+ response.addHeader(
|
|
|
+ HttpHeaders.CONTENT_DISPOSITION,
|
|
|
+ "attachment; filename=$tableName.csv"
|
|
|
+ )
|
|
|
+ response.outputStream.write(String(csvContent).toByteArray())
|
|
|
+ response.outputStream.flush()
|
|
|
+ response.outputStream.close()
|
|
|
+
|
|
|
+ return ResponseEntity("ok", HttpStatus.OK)
|
|
|
+// return ResponseEntity(csvContent, headers, HttpStatus.OK)
|
|
|
+
|
|
|
+// val cacheResultHandler = CacheDataManager()
|
|
|
+// val inputStream: InputStream
|
|
|
+// val byteArray: ByteArray
|
|
|
+// val httpHeaders = HttpHeaders()
|
|
|
+// val inputStreamResource: InputStreamResource = cacheResultHandler.exportCacheResults(response)
|
|
|
+// httpHeaders[HttpHeaders.CONTENT_DISPOSITION] = "attachment; filename=$tableName.csv"
|
|
|
+ //convert inputStream to bytes
|
|
|
+// inputStream = inputStreamResource.inputStream
|
|
|
+// val buffer = ByteArrayOutputStream()
|
|
|
+// var nRead: Int
|
|
|
+// val data = ByteArray(1024)
|
|
|
+// while ((inputStream.read(data, 0, data.size).also { nRead = it }) != -1) {
|
|
|
+// buffer.write(data, 0, nRead)
|
|
|
// }
|
|
|
+// buffer.flush()
|
|
|
+// byteArray = buffer.toByteArray()
|
|
|
+// httpHeaders.contentLength = byteArray.size.toLong()
|
|
|
+// return ResponseEntity(byteArray, httpHeaders, HttpStatus.OK)
|
|
|
}
|
|
|
- //todo multi / zip download?
|
|
|
+// return ResponseEntity<Any>(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
}
|
|
|
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
@@ -170,8 +146,8 @@ class MaintenanceController(
|
|
|
fun import(
|
|
|
@RequestParam file: MultipartFile, @RequestParam tableName: String, @RequestParam overwrite: Boolean
|
|
|
): ResponseEntity<Any> {
|
|
|
- val userId = userRepository.getUserByUsername(SecurityContextHolder.getContext().authentication.name)
|
|
|
- val valid = memberRepository.validAdmin(userId.id.toString())
|
|
|
+// 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)
|
|
@@ -227,7 +203,6 @@ class MaintenanceController(
|
|
|
else -> listOf()
|
|
|
}
|
|
|
mapData.removeIf { p -> existing.contains(p["id"].toString().toLong()) && !overwrite }
|
|
|
-
|
|
|
val check = when (tableName) {
|
|
|
"project" -> mapData.forEach {
|
|
|
val projectIn = ProjectInput()
|
|
@@ -303,7 +278,7 @@ class MaintenanceController(
|
|
|
.any { it.ordinal == bugInput.statusOrdinal }
|
|
|
) Enum.Status.values()[bugInput.statusOrdinal!!].name else throw Error()
|
|
|
bugInput.goodday_url = m["goodday_url"].toString()
|
|
|
- bugInput.image_url = m["image_url"].toString()
|
|
|
+ bugInput.image_url = m["image_url"].toString().drop(1).dropLast(1)
|
|
|
bugInput.platformId = m["platform"].toString().toLong()
|
|
|
bugInput.platform = platformRepository.findById(bugInput.platformId).orElseThrow { Error() }.name
|
|
|
bugInput.dev = m["dev"].toString().toLong()
|
|
@@ -317,7 +292,7 @@ class MaintenanceController(
|
|
|
}
|
|
|
val project = Project()
|
|
|
val platform = Platform()
|
|
|
- var user = User()
|
|
|
+ val user = User()
|
|
|
val member = ProjectMember()
|
|
|
val comment = Comment()
|
|
|
val bug = Bug()
|
|
@@ -325,21 +300,25 @@ class MaintenanceController(
|
|
|
val unit = when (tableName) {
|
|
|
"project" -> mapData.forEach {
|
|
|
project.id = it["id"].toString().toLong()
|
|
|
- project.name = it["name"].toString()
|
|
|
- project.description = it["description"].toString()
|
|
|
+ project.name = it["name"].toString().dropLast(1).drop(1)
|
|
|
+ project.description = it["description"].toString().dropLast(1).drop(1)
|
|
|
project.owner = userRepository.findById(it["owner"].toString().toLong()).get()
|
|
|
+ projectRepository.save(project)
|
|
|
}
|
|
|
|
|
|
"user" -> mapData.forEach {
|
|
|
- user = jacksonObjectMapper().readValue(
|
|
|
- jacksonObjectMapper().writeValueAsString(it), User::class.java
|
|
|
- )
|
|
|
+ user.id = it["id"].toString().toLong()
|
|
|
+ user.name = it["name"].toString().dropLast(1).drop(1)
|
|
|
+ user.password = it["password"].toString().dropLast(1).drop(1)
|
|
|
+ user.username = it["username"].toString().dropLast(1).drop(1)
|
|
|
+ userRepository.save(user)
|
|
|
}
|
|
|
|
|
|
"platform" -> mapData.forEach {
|
|
|
platform.id = it["id"].toString().toLong()
|
|
|
- platform.name = it["name"].toString()
|
|
|
+ platform.name = it["name"].toString().dropLast(1).drop(1)
|
|
|
platform.project = projectRepository.findById(it["project"].toString().toLong()).get()
|
|
|
+ platformRepository.save(platform)
|
|
|
}
|
|
|
|
|
|
"project_member" -> mapData.forEach {
|
|
@@ -347,47 +326,40 @@ class MaintenanceController(
|
|
|
member.role = Enum.Member.values()[Integer.parseInt(it["role"].toString())].ordinal
|
|
|
member.project = projectRepository.findById(it["project"].toString().toLong()).get()
|
|
|
member.user = userRepository.findById(it["user"].toString().toLong()).get()
|
|
|
+ memberRepository.save(member)
|
|
|
}
|
|
|
|
|
|
"comment" -> mapData.forEach {
|
|
|
comment.id = it["id"].toString().toLong()
|
|
|
- comment.content = it["content"].toString()
|
|
|
+ comment.content = it["content"].toString().dropLast(1).drop(1)
|
|
|
comment.created =
|
|
|
SimpleDateFormat("dd-MMM-yy HH:mm:ss", Locale.ENGLISH).parse(it["created"].toString())
|
|
|
comment.bug = bugRepository.findById(it["bug"].toString().toLong()).get()
|
|
|
comment.creator = userRepository.findById(it["creator"].toString().toLong()).get()
|
|
|
+ commentRepository.save(comment)
|
|
|
}
|
|
|
|
|
|
"bug" -> mapData.forEach {
|
|
|
bug.id = it["id"].toString().toLong()
|
|
|
bug.created = SimpleDateFormat("dd-MMM-yy HH:mm:ss", Locale.ENGLISH).parse(it["created"].toString())
|
|
|
- bug.description = it["description"].toString()
|
|
|
+ bug.description = it["description"].toString().dropLast(1).drop(1)
|
|
|
bug.dev_status = Enum.Dev_Status.values()[Integer.parseInt(it["dev_status"].toString())].ordinal
|
|
|
- bug.goodday_url = it["goodday_url"].toString()
|
|
|
- bug.image_url = it["image_url"].toString()
|
|
|
+ bug.goodday_url = it["goodday_url"].toString().dropLast(1).drop(1)
|
|
|
+ bug.image_url = it["image_url"].toString().dropLast(1).drop(1)
|
|
|
bug.level = Enum.Level.values()[Integer.parseInt(it["level"].toString())].ordinal
|
|
|
bug.status = Enum.Status.values()[Integer.parseInt(it["status"].toString())].ordinal
|
|
|
bug.dev = userRepository.findById(it["dev"].toString().toLong()).get()
|
|
|
bug.platform = platformRepository.findById(it["platform"].toString().toLong()).get()
|
|
|
bug.qc = userRepository.findById(it["qc"].toString().toLong()).get()
|
|
|
+ bugRepository.save(bug)
|
|
|
}
|
|
|
|
|
|
else -> Unit
|
|
|
}
|
|
|
}
|
|
|
- if (found == 0) {
|
|
|
- when (tableName) {
|
|
|
- "project" -> projectRepository.save(project)
|
|
|
- "platform" -> platformRepository.save(platform)
|
|
|
- "project_member" -> memberRepository.save(member)
|
|
|
- "user" -> userRepository.save(user)
|
|
|
- "comment" -> commentRepository.save(comment)
|
|
|
- "bug" -> bugRepository.save(bug)
|
|
|
- else -> Unit
|
|
|
- }
|
|
|
- }
|
|
|
return if (found == 0) ResponseEntity<Any>(HttpStatus.OK) else ResponseEntity<Any>(HttpStatus.BAD_REQUEST)
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|