naimul64

INsert area and user

Apr 23rd, 2018
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 9.10 KB | None | 0 0
  1. package com.progoti.surecash.merchant.KBFeatures
  2.  
  3. import com.progoti.surecash.District
  4. import com.progoti.surecash.scPartners.ScAreaForPartnerUsers
  5. import com.progoti.surecash.scPartners.ScAreaTypeForPartnerUsers
  6. import com.progoti.surecash.scPartners.ScPartnerUser
  7. import groovy.sql.Sql
  8. import org.apache.commons.lang.StringUtils
  9. import org.apache.poi.ss.usermodel.DataFormatter
  10. import org.apache.poi.xssf.usermodel.XSSFRow
  11.  
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.IOException;
  15. import java.util.Iterator;
  16.  
  17. import org.apache.poi.ss.usermodel.Cell;
  18. import org.apache.poi.ss.usermodel.Row;
  19. import org.apache.poi.ss.usermodel.Sheet;
  20. import org.apache.poi.ss.usermodel.Workbook;
  21. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  22.  
  23. class ExcelReaderController {
  24.     def dataSource
  25.  
  26.     def index() {}
  27.  
  28.     def readExcel() {
  29.         DataFormatter df = new DataFormatter()
  30.         String division;
  31.         String region;
  32.  
  33.         String excelFilePath = "/home/insan/Documents/SURECASH_PROJECTS/surecash-web/kb.xlsx"
  34.         FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
  35.  
  36.         Workbook workbook = new XSSFWorkbook(inputStream);
  37.         Sheet firstSheet = workbook.getSheetAt(3);
  38.  
  39.         Integer rowCount = firstSheet.getPhysicalNumberOfRows();
  40.         for (int row = 1; row < rowCount; row++) {
  41.             XSSFRow dataRow = firstSheet.getRow(row)
  42.             DtoClass dtoClass
  43.  
  44.             for (int j = 0; j < 13; j++) {
  45.                 dataRow.getCell(0).setCellType(Cell.CELL_TYPE_STRING)
  46.             }
  47.  
  48.             dtoClass = new DtoClass()
  49.             dtoClass.divisoinFromCell = dataRow.getCell(1).getRichStringCellValue().getString()
  50.             if (StringUtils.isEmpty(dtoClass.divisoinFromCell)) {
  51.                 dtoClass.divisoinFromCell = division
  52.             } else {
  53.                 division = dtoClass.divisoinFromCell
  54.             }
  55.  
  56.             dtoClass.regionFromCell = dataRow.getCell(2).getRichStringCellValue().getString()
  57.             if (StringUtils.isEmpty(dtoClass.regionFromCell)) {
  58.                 dtoClass.regionFromCell = region
  59.             } else {
  60.                 region = dtoClass.regionFromCell
  61.             }
  62.  
  63.             dtoClass.branchName = dataRow.getCell(3).getRichStringCellValue().getString()
  64.             dtoClass.branchCode = df.formatCellValue(dataRow.getCell(4))
  65.             dtoClass.district = dataRow.getCell(5).getRichStringCellValue().getString()
  66.             dtoClass.address = dataRow.getCell(6).getRichStringCellValue().getString()
  67.             dtoClass.officerName = dataRow.getCell(7).getRichStringCellValue().getString()
  68.             dtoClass.designation = dataRow.getCell(8).getRichStringCellValue().getString()
  69.             dtoClass.telephone = df.formatCellValue(dataRow.getCell(9))
  70.             dtoClass.corporateMobile = df.formatCellValue(dataRow.getCell(10))
  71.             dtoClass.dataMobile = df.formatCellValue(dataRow.getCell(11))
  72.             dtoClass.email = dataRow.getCell(12).getRichStringCellValue().getString()
  73.  
  74.             dtoClass.cleanOwn()
  75.             ScAreaForPartnerUsers insertedArea = insertArea(dtoClass)
  76.             if (insertedArea != null) {
  77.                 insertUser(dtoClass, insertedArea)
  78.             }
  79.  
  80.         }
  81.         inputStream.close();
  82.     }
  83.  
  84.     ScAreaForPartnerUsers insertArea(DtoClass dtoClass) {
  85.         ScAreaForPartnerUsers area = new ScAreaForPartnerUsers()
  86.         if (dtoClass.branchName.trim().equalsIgnoreCase("Regional Office")) {
  87.             if (ScAreaForPartnerUsers.findByAreaNameAndAreaType(dtoClass.officerName, ScAreaTypeForPartnerUsers.findByTypeName("Region"))) {
  88.                 return ScAreaForPartnerUsers.findByAreaNameAndAreaType(dtoClass.officerName, ScAreaTypeForPartnerUsers.findByTypeName("Region"))
  89.             }
  90.  
  91.             area.areaType = ScAreaTypeForPartnerUsers.findByTypeName('Region')
  92.             area.parent = ScAreaForPartnerUsers.findByAreaNameAndAreaType(dtoClass.divisoinFromCell.trim(), ScAreaTypeForPartnerUsers.findByTypeName("Division"))
  93.  
  94.             area.areaName = dtoClass.regionFromCell.trim()
  95.             area.description = area.areaName
  96.         } else {
  97.             if (ScAreaForPartnerUsers.findByAreaNameAndAreaType(dtoClass.branchName, ScAreaTypeForPartnerUsers.findByTypeName("Branch"))) {
  98.                 return ScAreaForPartnerUsers.findByAreaNameAndAreaType(dtoClass.branchName, ScAreaTypeForPartnerUsers.findByTypeName("Branch"))
  99.             }
  100.  
  101.             area.areaType = ScAreaTypeForPartnerUsers.findByTypeName('Branch')
  102.             area.areaCode = dtoClass.branchCode.trim()
  103.             Sql sql = Sql.newInstance(dataSource)
  104.             def r = sql.firstRow("""SELECT * FROM district WHERE name sounds LIKE '""" + dtoClass.district + """'""")
  105.             if (r != null) {
  106.                 area.district = District.findById(r.dId)
  107.             }
  108.             area.parent = ScAreaForPartnerUsers.findByAreaNameAndAreaType(dtoClass.regionFromCell.trim(), ScAreaTypeForPartnerUsers.findByTypeName("Region"))
  109.  
  110.             area.areaName = dtoClass.branchName.trim()
  111.             area.description = area.areaName
  112.         }
  113.  
  114.         area.createDate = area.updateDate = new Date()
  115.         if (area.save(flush: true, failOnError: true)) {
  116.             return area
  117.         } else {
  118.             return null
  119.         }
  120.     }
  121.  
  122.     void insertUser(DtoClass dtoClass, ScAreaForPartnerUsers areaForPartnerUsers) {
  123.         ScPartnerUser user = new ScPartnerUser()
  124.         user.area = areaForPartnerUsers
  125.         if (areaForPartnerUsers.areaType.id == ScAreaTypeForPartnerUsers.findByTypeName('Region').id) {
  126.             user.loginName = "kbro_" + areaForPartnerUsers.areaName.replace("[^A-Za-z0-9]", "").toLowerCase()
  127.         } else if (areaForPartnerUsers.areaType.id == ScAreaTypeForPartnerUsers.findByTypeName('Branch').id) {
  128.             user.loginName = "kbbo_" + areaForPartnerUsers.areaName.replace("[^A-Za-z0-9]", "").toLowerCase()
  129.         }
  130.         def userListByLoginName = ScPartnerUser.findAllByLoginName(user.loginName)
  131.         if (userListByLoginName != null && userListByLoginName.size() > 0) {
  132.             user.loginName += "_" + System.currentTimeMillis() %1000
  133.         }
  134.  
  135.         user.scPartnerUserType = areaForPartnerUsers.areaType.partnerUserType
  136.         user.password = "PWpr5qM7JDw="
  137.         user.name = dtoClass.officerName
  138.         user.officeAddress = dtoClass.address
  139.         user.designation = dtoClass.designation
  140.         user.telephone = dtoClass.telephone
  141.         user.mobileNoCorporate = dtoClass.corporateMobile
  142.         user.mobileNo = dtoClass.dataMobile
  143.         user.eMail = dtoClass.email
  144.  
  145.         user.createDate = user.latestUpdateDate = new Date()
  146.  
  147.         user.save(flush: true, failOnError: true)
  148.  
  149.         System.currentTimeMillis()
  150.     }
  151. }
  152.  
  153. class DtoClass {
  154.     String divisoinFromCell;
  155.     String regionFromCell
  156.     String branchName
  157.     String branchCode
  158.     String district
  159.     String address
  160.     String officerName
  161.     String designation
  162.     String dataMobile
  163.     String corporateMobile
  164.     String telephone
  165.     String email
  166.  
  167.     void cleanOwn() {
  168.         divisoinFromCell = divisoinFromCell?.replaceAll("\\P{Print}", "")?.trim()
  169.         regionFromCell = regionFromCell?.replaceAll("\\P{Print}", "")?.trim()
  170.         branchName = branchName?.replaceAll("\\P{Print}", "")?.trim()
  171.         branchCode = branchCode?.replaceAll("\\P{Print}", "")?.trim()
  172.         district = district?.replaceAll("\\P{Print}", "")?.trim()
  173.         address = address?.replaceAll("\\P{Print}", "")?.trim()
  174.         officerName = officerName?.replaceAll("\\P{Print}", "")?.trim()
  175.         designation = designation?.replaceAll("\\P{Print}", "")?.trim()
  176.         dataMobile = dataMobile?.replaceAll("\\P{Print}", "")?.trim()
  177.         corporateMobile = corporateMobile?.replaceAll("\\P{Print}", "")?.trim()
  178.         telephone = telephone?.replaceAll("\\P{Print}", "")?.trim()
  179.         email = email?.replaceAll("\\P{Print}", "")?.trim()
  180.  
  181.         if (StringUtils.isEmpty(divisoinFromCell)) {
  182.             divisoinFromCell = null
  183.         }
  184.         if (StringUtils.isEmpty(regionFromCell)) {
  185.             regionFromCell = null
  186.         }
  187.         if (StringUtils.isEmpty(branchName)) {
  188.             branchName = null
  189.         }
  190.         if (StringUtils.isEmpty(branchCode)) {
  191.             branchCode = null
  192.         }
  193.         if (StringUtils.isEmpty(district)) {
  194.             district = null
  195.         }
  196.         if (StringUtils.isEmpty(address)) {
  197.             address = null
  198.         }
  199.         if (StringUtils.isEmpty(officerName)) {
  200.             officerName = null
  201.         }
  202.         if (StringUtils.isEmpty(designation)) {
  203.             designation = null
  204.         }
  205.         if (StringUtils.isEmpty(dataMobile)) {
  206.             dataMobile = null
  207.         }
  208.         if (StringUtils.isEmpty(corporateMobile)) {
  209.             corporateMobile = null
  210.         }
  211.         if (StringUtils.isEmpty(telephone) || telephone.trim().equals("-")) {
  212.             telephone = null
  213.         }
  214.         if (StringUtils.isEmpty(email)) {
  215.             email = ""
  216.         }
  217.     }
  218. }
  219.  
  220. /*
  221. Regex rgx = new Regex("[^a-zA-Z0-9 -]");
  222. str = rgx.Replace(str, "");
  223. * */
Advertisement
Add Comment
Please, Sign In to add comment