148 lines
4.6 KiB
Kotlin
148 lines
4.6 KiB
Kotlin
package com.restapi.controllers
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature
|
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
|
import com.restapi.domain.Product
|
|
import org.apache.poi.ss.usermodel.Cell
|
|
import org.apache.poi.ss.usermodel.CellType
|
|
import org.apache.poi.ss.usermodel.Row
|
|
import org.apache.poi.ss.usermodel.WorkbookFactory
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook
|
|
import java.io.ByteArrayInputStream
|
|
import java.io.ByteArrayOutputStream
|
|
import java.io.FileInputStream
|
|
import java.io.InputStream
|
|
|
|
fun CreateExcel(productList: List<Product>): InputStream {
|
|
val wb = XSSFWorkbook()
|
|
val sh = wb.createSheet()
|
|
val rows: Row = sh.createRow(0)
|
|
rows.createCell(0).setCellValue("Name")
|
|
rows.createCell(1).setCellValue("Description")
|
|
rows.createCell(2).setCellValue("HSN")
|
|
rows.createCell(3).setCellValue("UOM")
|
|
|
|
var rowNum = 1
|
|
for (product in productList) {
|
|
val row: Row = sh.createRow(rowNum++)
|
|
|
|
row.createCell(0).setCellValue(product.name)
|
|
row.createCell(1).setCellValue(product.description)
|
|
row.createCell(2).setCellValue(product.hsnCode)
|
|
|
|
val uomCell: Cell = row.createCell(3)
|
|
uomCell.setCellValue(product.uom?.name ?: "")
|
|
}
|
|
|
|
val baos = ByteArrayOutputStream()
|
|
wb.write(baos)
|
|
wb.close()
|
|
|
|
return ByteArrayInputStream(baos.toByteArray())
|
|
|
|
}
|
|
|
|
data class validateExcel(
|
|
val name: String,
|
|
val description: String,
|
|
val hsnCode: String,
|
|
val ok: Boolean,
|
|
val err: String,
|
|
)
|
|
|
|
val app_common_om = jacksonObjectMapper().apply {
|
|
registerModule(JavaTimeModule())
|
|
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
|
}
|
|
|
|
fun ExcelRead(): String{
|
|
val inputStream = FileInputStream("C:\\Users\\vinay\\IdeaProjects\\readymixerp_modules_api_git\\Untitled 1.xlsx")
|
|
val workbook = WorkbookFactory.create(inputStream)
|
|
val workSheet = workbook.getSheetAt(0)
|
|
var h = false
|
|
//Header check
|
|
// if(workSheet.getRow(0).getCell(0).stringCellValue.equals("Name")){
|
|
// if(workSheet.getRow(0).getCell(1).stringCellValue.equals("Description")){
|
|
// if(workSheet.getRow(0).getCell(2).stringCellValue.equals("HSN")){
|
|
// if(workSheet.getRow(0).getCell(3).stringCellValue.equals("UOM")){
|
|
// return true
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
val resp = arrayListOf<validateExcel>()
|
|
workSheet.rowIterator().forEach { row ->
|
|
|
|
if (row == null) return@forEach
|
|
|
|
if (h) {
|
|
|
|
val pName = row.getCell(0).run {
|
|
when {
|
|
this == null -> ""
|
|
this.cellType == CellType.STRING -> this.stringCellValue
|
|
else -> ""
|
|
}
|
|
}
|
|
|
|
val pDesc = row.getCell(1).run {
|
|
when {
|
|
this == null -> ""
|
|
this.cellType == CellType.STRING -> this.stringCellValue
|
|
else -> ""
|
|
}
|
|
}
|
|
val pHsn = row.getCell(2).run {
|
|
when {
|
|
this == null -> ""
|
|
this.cellType == CellType.STRING -> this.stringCellValue
|
|
else -> ""
|
|
}
|
|
}
|
|
|
|
if (pName.isEmpty() && pDesc.isEmpty() && pHsn.isEmpty()) {
|
|
return@forEach
|
|
}
|
|
if (pName.isEmpty()) {
|
|
resp.add(
|
|
validateExcel(
|
|
name = pName,
|
|
description = pDesc,
|
|
hsnCode = pHsn,
|
|
ok = false,
|
|
err = "Product name is required"
|
|
)
|
|
)
|
|
return@forEach
|
|
}
|
|
if (pDesc.isEmpty()) {
|
|
resp.add(
|
|
validateExcel(
|
|
name = pName,
|
|
description = pDesc,
|
|
hsnCode = pHsn,
|
|
ok = false,
|
|
err = "Product description is required"
|
|
)
|
|
)
|
|
return@forEach
|
|
}
|
|
if (pHsn.isEmpty()) {
|
|
resp.add(
|
|
validateExcel(
|
|
name = pName,
|
|
description = pDesc,
|
|
hsnCode = pHsn,
|
|
ok = false,
|
|
err = "Product HSN is required"
|
|
)
|
|
)
|
|
return@forEach
|
|
}
|
|
}
|
|
h = true
|
|
}
|
|
return app_common_om.writeValueAsString(resp)
|
|
} |