From a3f76149795d6320bd08b508b97fb5f581e88abc Mon Sep 17 00:00:00 2001 From: vinay Date: Tue, 23 Jan 2024 16:59:25 +0530 Subject: [PATCH] Product Excel Validation --- api.http | 10 +- src/main/kotlin/com/restapi/Main.kt | 4 + .../com/restapi/controllers/Entities.kt | 11 +- .../kotlin/com/restapi/controllers/Excel.kt | 116 ++++++++++++++++-- 4 files changed, 127 insertions(+), 14 deletions(-) diff --git a/api.http b/api.http index 69bd308..b0d12b8 100644 --- a/api.http +++ b/api.http @@ -93,16 +93,16 @@ Authorization: {{auth-token}} DELETE http://localhost:9001/api/vehicle/KA01HD6667 Authorization: {{auth-token}} -### get po for id +### get products GET http://localhost:9001/api/vendor/product Authorization: {{auth-token}} -### get product for id -GET http://localhost:9001/api/vendor/product/7 +### create excel for products +POST http://localhost:9001/api/vendor/product/product-excel Authorization: {{auth-token}} -### get row -GET http://localhost:9001/api/vendor/product/7 +### get +GET http://localhost:9001/api/vendor/product/product-import Authorization: Bearer {{auth-token}} ### create product diff --git a/src/main/kotlin/com/restapi/Main.kt b/src/main/kotlin/com/restapi/Main.kt index 0c97abc..42fe33e 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -7,6 +7,7 @@ import com.restapi.config.Auth.validateAuthToken import com.restapi.controllers.* import com.restapi.domain.DataNotFoundException import com.restapi.domain.Product +import com.restapi.domain.Session.a import com.restapi.domain.Session.currentTenant import com.restapi.domain.Session.currentUser import com.restapi.domain.Session.objectMapper @@ -24,6 +25,7 @@ import io.javalin.http.util.RateLimitUtil import io.javalin.json.JavalinJackson import org.jose4j.jwt.consumer.InvalidJwtException import org.slf4j.LoggerFactory +import java.io.InputStream import java.security.MessageDigest import java.time.LocalDateTime import java.util.* @@ -140,6 +142,8 @@ fun main(args: Array) { //patch("/{id}", ProductCtrl::patch, Roles(Role.Explicit(listOf("ROLE_PRODUCT_UPDATE", "ROLE_ADMIN")))) delete("/{id}", ProductCtrl::delete, Roles(Role.Explicit(listOf("ROLE_PRODUCT_DELETE", "ROLE_ADMIN")))) get("", ProductCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) + post("/product-excel", ProductCtrl::prodExcel) + get("/product-import"){ ctx -> ctx.json(ExcelRead())} } path("/doc"){ post("", Document::create, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE", "ROLE_ADMIN")))) diff --git a/src/main/kotlin/com/restapi/controllers/Entities.kt b/src/main/kotlin/com/restapi/controllers/Entities.kt index 8654c34..60cc77f 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -18,6 +18,8 @@ import io.ebean.CallableSql import io.ebean.RawSqlBuilder import io.javalin.http.* import org.slf4j.LoggerFactory +import java.io.File +import java.io.InputStream import java.sql.Types import java.time.LocalDate import java.time.LocalDateTime @@ -446,9 +448,14 @@ object ProductCtrl { } fun update(ctx: Context) { - val id = ctx.pathParam("id") } + fun prodExcel(it: Context) { + val product = database.find(Product::class.java).findList() + it.result(CreateExcel(product)).contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") + .header("Content-Disposition", "attachment; filename=\"product.xlsx\"") + } + } object Quotation { @@ -590,4 +597,4 @@ object RequestForQuote { //shuld we compare the new body fields with preexisting ones and prepare a sql query to update those fields?? } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/restapi/controllers/Excel.kt b/src/main/kotlin/com/restapi/controllers/Excel.kt index 91b1f41..1478b08 100644 --- a/src/main/kotlin/com/restapi/controllers/Excel.kt +++ b/src/main/kotlin/com/restapi/controllers/Excel.kt @@ -1,7 +1,11 @@ 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 @@ -13,11 +17,12 @@ import java.io.InputStream fun CreateExcel(productList: List): InputStream { val wb = XSSFWorkbook() val sh = wb.createSheet() - sh.createRow(0).createCell(0).setCellValue("Name") - sh.createRow(0).createCell(1).setCellValue("Description") - sh.createRow(0).createCell(2).setCellValue("HSN") - sh.createRow(0).createCell(3).setCellValue("UOM") - + 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++) @@ -38,9 +43,106 @@ fun CreateExcel(productList: List): InputStream { } -fun ExcelRead(filename: String){ - val inputStream = FileInputStream("./${filename}.xlsx") +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() + 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) } \ No newline at end of file