| import org.springframework.data.jpa.repository.JpaRepository; | import org.springframework.data.jpa.repository.JpaRepository; | ||||
| import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||||
| import org.springframework.data.jpa.repository.Query; | |||||
| import org.springframework.data.repository.query.Param; | |||||
| import org.springframework.stereotype.Repository; | import org.springframework.stereotype.Repository; | ||||
| import vn.azteam.tpf.domain.TBCodeDetails; | import vn.azteam.tpf.domain.TBCodeDetails; | ||||
| import java.util.List; | |||||
| import java.util.Optional; | import java.util.Optional; | ||||
| @Repository | @Repository | ||||
| // @Query("select tb_code_details from TBCodeDetails tb_code_details where tb_code_details.code =:code") | // @Query("select tb_code_details from TBCodeDetails tb_code_details where tb_code_details.code =:code") | ||||
| Optional<TBCodeDetails> findByCode(String code); | Optional<TBCodeDetails> findByCode(String code); | ||||
| @Query(value = "SELECT dt FROM TBCodeDetails dt INNER JOIN TBCode tc ON (dt.tbCodeId = tc.id) \n" + | |||||
| " WHERE tc.code= :tbCodeValue") | |||||
| List<TBCodeDetails> findByTBCode(@Param("tbCodeValue") String tbCodeValue); | |||||
| } | } |
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
| import org.springframework.transaction.annotation.Transactional; | import org.springframework.transaction.annotation.Transactional; | ||||
| import vn.azteam.tpf.domain.*; | import vn.azteam.tpf.domain.*; | ||||
| import vn.azteam.tpf.repository.TBCodeDetailsRepository; | |||||
| import vn.azteam.tpf.repository.TBCodeRepository; | import vn.azteam.tpf.repository.TBCodeRepository; | ||||
| import vn.azteam.tpf.service.dto.ExportTBCodeDTO; | |||||
| import vn.azteam.tpf.service.dto.TBCodeCriteria; | import vn.azteam.tpf.service.dto.TBCodeCriteria; | ||||
| import vn.azteam.tpf.service.dto.TBCodeDTO; | import vn.azteam.tpf.service.dto.TBCodeDTO; | ||||
| import vn.azteam.tpf.service.mapper.TBCodeMapper; | import vn.azteam.tpf.service.mapper.TBCodeMapper; | ||||
| import vn.azteam.tpf.service.util.ExcelWriteUtility; | |||||
| import vn.azteam.tpf.web.rest.errors.BadRequestAlertException; | |||||
| import javax.persistence.criteria.Join; | import javax.persistence.criteria.Join; | ||||
| import javax.persistence.criteria.JoinType; | import javax.persistence.criteria.JoinType; | ||||
| import java.io.IOException; | |||||
| import java.util.ArrayList; | |||||
| import java.util.List; | |||||
| import java.util.Optional; | |||||
| @Service | @Service | ||||
| @Transactional(readOnly = true) | @Transactional(readOnly = true) | ||||
| private final Logger log = LoggerFactory.getLogger(TBCodeQueryService.class); | private final Logger log = LoggerFactory.getLogger(TBCodeQueryService.class); | ||||
| private final TBCodeRepository tBCodeRepository; | private final TBCodeRepository tBCodeRepository; | ||||
| private final TBCodeDetailsRepository tbCodeDetailsRepository; | |||||
| private final TBCodeMapper tBCodeMapper; | private final TBCodeMapper tBCodeMapper; | ||||
| public TBCodeQueryService(TBCodeRepository tBCodeRepository, TBCodeMapper tBCodeMapper) { | |||||
| public TBCodeQueryService(TBCodeRepository tBCodeRepository, TBCodeDetailsRepository tbCodeDetailsRepository, TBCodeMapper tBCodeMapper) { | |||||
| this.tBCodeRepository = tBCodeRepository; | this.tBCodeRepository = tBCodeRepository; | ||||
| this.tbCodeDetailsRepository = tbCodeDetailsRepository; | |||||
| this.tBCodeMapper = tBCodeMapper; | this.tBCodeMapper = tBCodeMapper; | ||||
| } | } | ||||
| return specification; | return specification; | ||||
| } | } | ||||
| public byte[] exportInfoTBCode(String tbCodeValue) { | |||||
| try { | |||||
| Optional<TBCode> tbCode = tBCodeRepository.findByCode(tbCodeValue); | |||||
| if (!tbCode.isPresent()) { | |||||
| throw new BadRequestAlertException("1047", "TB Code export could found", "1047"); | |||||
| } | |||||
| log.info("exportInfoTBCode: start query"); | |||||
| List<ExportTBCodeDTO> queryResult = | |||||
| this.getInfoTbCodeDetailByTbCode(tbCodeValue); | |||||
| log.info("exportInfoTBCode: end query, result {}", queryResult.size()); | |||||
| return this.createExcelFile(queryResult, tbCodeValue); | |||||
| } catch (Exception exception) { | |||||
| log.error("exportInfoCampaignCalling exception", exception); | |||||
| } | |||||
| return new byte[0]; | |||||
| } | |||||
| private List<ExportTBCodeDTO> getInfoTbCodeDetailByTbCode(String tbCodeValue) { | |||||
| List<TBCodeDetails> tbCodeDetails = tbCodeDetailsRepository.findByTBCode(tbCodeValue); | |||||
| List<ExportTBCodeDTO> callingCampaignExportDTOList = new ArrayList<>(); | |||||
| if (!tbCodeDetails.isEmpty()) { | |||||
| tbCodeDetails.forEach(c -> { | |||||
| ExportTBCodeDTO exportTBCodeDTO = new ExportTBCodeDTO(); | |||||
| exportTBCodeDTO.setDetailCode(c.getCode()); | |||||
| exportTBCodeDTO.setStatus(c.getStatus().getName()); | |||||
| callingCampaignExportDTOList.add(exportTBCodeDTO); | |||||
| }); | |||||
| } | |||||
| return callingCampaignExportDTOList; | |||||
| } | |||||
| public byte[] createExcelFile(List<ExportTBCodeDTO> contents, String tbCodeValue) throws IOException { | |||||
| log.info("createExcelFile"); | |||||
| List<List<Object>> data = new ArrayList<>(); | |||||
| data.add(createReportHeader()); | |||||
| contents.forEach( | |||||
| cicBadDebtExportDTO -> { | |||||
| List<Object> rowData = convertRecordToArray(cicBadDebtExportDTO, tbCodeValue); | |||||
| data.add(rowData); | |||||
| } | |||||
| ); | |||||
| return ExcelWriteUtility.writeExcelToByteArray(data, null); | |||||
| } | |||||
| public List<Object> createReportHeader() { | |||||
| log.info("createReportHeader"); | |||||
| List<Object> header = new ArrayList<>(); | |||||
| header.add("Code"); | |||||
| header.add("Status"); | |||||
| // header.add("User"); | |||||
| // header.add("Completed"); | |||||
| // header.add("Completed (Another reason)"); | |||||
| // header.add("Partial completed"); | |||||
| // header.add("Follow"); | |||||
| // header.add("No Answer"); | |||||
| // header.add("Drop Call"); | |||||
| return header; | |||||
| } | |||||
| public List<Object> convertRecordToArray(ExportTBCodeDTO content, String campaignName) { | |||||
| return getObjects(content, campaignName); | |||||
| } | |||||
| public List<Object> getObjects(ExportTBCodeDTO content, String campaignName) { | |||||
| //log.info("convertRecordToArray"); | |||||
| List<Object> row = new ArrayList<>(); | |||||
| //row.add(campaignName); | |||||
| row.add(content.getDetailCode()); | |||||
| row.add(content.getStatus()); | |||||
| return row; | |||||
| } | |||||
| } | } |
| package vn.azteam.tpf.service.dto; | |||||
| import java.io.Serializable; | |||||
| public class ExportTBCodeDTO implements Serializable { | |||||
| private String detailCode; | |||||
| private String status; | |||||
| public String getDetailCode() { | |||||
| return detailCode; | |||||
| } | |||||
| public void setDetailCode(String detailCode) { | |||||
| this.detailCode = detailCode; | |||||
| } | |||||
| public String getStatus() { | |||||
| return status; | |||||
| } | |||||
| public void setStatus(String status) { | |||||
| this.status = status; | |||||
| } | |||||
| } |
| package vn.azteam.tpf.service.dto; | |||||
| import java.io.Serializable; | |||||
| public class TBExampleStampCreationDTO implements Serializable { | |||||
| private String exampleStampName; | |||||
| private String description; | |||||
| private Integer width; | |||||
| private Integer height; | |||||
| private Integer x; | |||||
| private Integer y; | |||||
| private Integer size; | |||||
| private String exampleStampImage; | |||||
| } |
| package vn.azteam.tpf.service.util; | |||||
| import org.apache.commons.io.output.ByteArrayOutputStream; | |||||
| import org.apache.poi.ss.usermodel.*; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import java.io.File; | |||||
| import java.io.FileOutputStream; | |||||
| import java.io.IOException; | |||||
| import java.io.OutputStream; | |||||
| import java.util.*; | |||||
| import java.util.function.Function; | |||||
| public class ExcelWriteUtility { | |||||
| private static final Logger logger = LoggerFactory.getLogger(ExcelWriteUtility.class); | |||||
| private static final DataFormatter dataFormatter = new DataFormatter(); | |||||
| public ExcelWriteUtility() { | |||||
| } | |||||
| public static byte[] writeExcelToByteArray(List<List<Object>> inputData, Function<Object, String> dataFormatter) throws IOException { | |||||
| ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); | |||||
| writeExcel(inputData, byteOutputStream, dataFormatter); | |||||
| return byteOutputStream.toByteArray(); | |||||
| } | |||||
| public static void writeExcelToFile(List<List<Object>> inputData, File outFile, Function<Object, String> dataFormatter) throws IOException { | |||||
| OutputStream outputStream = null; | |||||
| try { | |||||
| outputStream = new FileOutputStream(outFile); | |||||
| writeExcel(inputData, outputStream, dataFormatter); | |||||
| } finally { | |||||
| if (outputStream != null) { | |||||
| outputStream.close(); | |||||
| } | |||||
| } | |||||
| } | |||||
| public static void writeExcel(List<List<Object>> inputData, OutputStream outputStream, Function<Object, String> dataFormatter) throws IOException { | |||||
| new ArrayList(); | |||||
| Workbook workbook = null; | |||||
| try { | |||||
| workbook = WorkbookFactory.create(true); | |||||
| Sheet sheet = getOrCreateSheet(workbook); | |||||
| for (int rowNum = 0; rowNum < inputData.size(); ++rowNum) { | |||||
| List<Object> rowData = (List) inputData.get(rowNum); | |||||
| writeRowData(sheet, rowNum, rowData, dataFormatter); | |||||
| } | |||||
| workbook.write(outputStream); | |||||
| } finally { | |||||
| if (workbook != null) { | |||||
| workbook.close(); | |||||
| } | |||||
| } | |||||
| } | |||||
| private static void writeRowData(Sheet sheet, int rowNum, List<Object> rowData, Function<Object, String> dataFormatter) { | |||||
| Row row = getOrCreateRow(sheet, rowNum); | |||||
| for (int col = 0; col < rowData.size(); ++col) { | |||||
| Cell cell = getOrCreateCell(row, col); | |||||
| if (dataFormatter != null) { | |||||
| setCellValue(cell, dataFormatter.apply(rowData.get(col))); | |||||
| } else { | |||||
| setCellValue(cell, rowData.get(col)); | |||||
| } | |||||
| } | |||||
| } | |||||
| private static Sheet getOrCreateSheet(Workbook workbook) { | |||||
| int numOfSheet = workbook.getNumberOfSheets(); | |||||
| return numOfSheet <= 0 ? workbook.createSheet() : workbook.getSheetAt(0); | |||||
| } | |||||
| private static Row getOrCreateRow(Sheet sheet, int rowNum) { | |||||
| Row r = sheet.getRow(rowNum); | |||||
| if (r == null) { | |||||
| r = sheet.createRow(rowNum); | |||||
| } | |||||
| return r; | |||||
| } | |||||
| private static Cell getOrCreateCell(Row row, int colNum) { | |||||
| Cell c = row.getCell(colNum); | |||||
| if (c == null) { | |||||
| c = row.createCell(colNum); | |||||
| } | |||||
| return c; | |||||
| } | |||||
| private static void setCellValue(Cell cell, Object value) { | |||||
| if (value instanceof String) { | |||||
| cell.setCellValue((String) value); | |||||
| } else if (value instanceof RichTextString) { | |||||
| cell.setCellValue((RichTextString) value); | |||||
| } else if (value instanceof Number) { | |||||
| cell.setCellValue(((Number) value).doubleValue()); | |||||
| } else if (value instanceof Date) { | |||||
| cell.setCellValue((Date) value); | |||||
| } else if (value instanceof Calendar) { | |||||
| cell.setCellValue((Calendar) value); | |||||
| } else { | |||||
| cell.setCellValue(Objects.toString(value, "")); | |||||
| } | |||||
| } | |||||
| } |
| import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| import org.springframework.core.io.InputStreamResource; | |||||
| import org.springframework.data.domain.Page; | import org.springframework.data.domain.Page; | ||||
| import org.springframework.data.domain.Pageable; | import org.springframework.data.domain.Pageable; | ||||
| import org.springframework.http.HttpHeaders; | import org.springframework.http.HttpHeaders; | ||||
| import org.springframework.http.MediaType; | |||||
| import org.springframework.http.ResponseEntity; | import org.springframework.http.ResponseEntity; | ||||
| import org.springframework.web.bind.annotation.*; | import org.springframework.web.bind.annotation.*; | ||||
| import vn.azteam.tpf.domain.TBCode; | import vn.azteam.tpf.domain.TBCode; | ||||
| import vn.azteam.tpf.web.rest.util.PaginationUtil; | import vn.azteam.tpf.web.rest.util.PaginationUtil; | ||||
| import vn.azteam.tpf.web.rest.util.RandomStringUtil; | import vn.azteam.tpf.web.rest.util.RandomStringUtil; | ||||
| import java.io.ByteArrayInputStream; | |||||
| import java.net.URI; | import java.net.URI; | ||||
| import java.net.URISyntaxException; | import java.net.URISyntaxException; | ||||
| import java.text.ParseException; | |||||
| import java.time.Instant; | import java.time.Instant; | ||||
| import java.util.Comparator; | import java.util.Comparator; | ||||
| import java.util.List; | import java.util.List; | ||||
| } | } | ||||
| } | } | ||||
| // @PostMapping("/uploadJsonAndMultipartData") | |||||
| // public ResponseEntity<String> handleJsonAndMultipartInput(@RequestPart("data") JsonRequest json, @RequestPart("file") MultipartFile file) { | |||||
| // return ResponseEntity.ok() | |||||
| // .body(json.getId() + json.getName()); | |||||
| // } | |||||
| @GetMapping(value = "/tb-codes/export") | |||||
| public ResponseEntity<InputStreamResource> exportTbCode( | |||||
| @RequestParam(value="tbCodeValue") String tbCodeValue) { | |||||
| try { | |||||
| if (StringUtils.isBlank(tbCodeValue)) { | |||||
| throw new BadRequestAlertException("1047", "TB Code export could not empty", "1047"); | |||||
| } | |||||
| byte[] out = tBCodeQueryService.exportInfoTBCode(tbCodeValue); | |||||
| String fileName = "Export_detail_code_by_tb_code" + tbCodeValue + ".xlsx"; | |||||
| InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(out)); | |||||
| return ResponseEntity.ok() | |||||
| .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename= " + fileName) | |||||
| .contentLength(out.length) | |||||
| .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) | |||||
| .body(resource); | |||||
| } catch (Exception e) { | |||||
| throw e; | |||||
| } | |||||
| } | |||||
| @GetMapping("/tb-codes/{id}") | @GetMapping("/tb-codes/{id}") | ||||
| @Timed | @Timed | ||||
| public ResponseEntity<TBCodeDTO> getTBCodes(@PathVariable Long id) { | public ResponseEntity<TBCodeDTO> getTBCodes(@PathVariable Long id) { |
| import com.codahale.metrics.annotation.Timed; | import com.codahale.metrics.annotation.Timed; | ||||
| import org.apache.commons.lang3.StringUtils; | |||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| import org.springframework.data.domain.Page; | import org.springframework.data.domain.Page; | ||||
| import org.springframework.data.domain.Pageable; | import org.springframework.data.domain.Pageable; | ||||
| import org.springframework.http.HttpHeaders; | import org.springframework.http.HttpHeaders; | ||||
| import org.springframework.http.ResponseEntity; | import org.springframework.http.ResponseEntity; | ||||
| import org.springframework.web.bind.annotation.GetMapping; | |||||
| import org.springframework.web.bind.annotation.RequestMapping; | |||||
| import org.springframework.web.bind.annotation.RestController; | |||||
| import org.springframework.web.bind.annotation.*; | |||||
| import vn.azteam.tpf.domain.TBCodeStatusEnum; | |||||
| import vn.azteam.tpf.service.TBExampleStampQueryService; | import vn.azteam.tpf.service.TBExampleStampQueryService; | ||||
| import vn.azteam.tpf.service.dto.TBExampleStampCriteria; | |||||
| import vn.azteam.tpf.service.dto.TBExampleStampDTO; | |||||
| import vn.azteam.tpf.service.UserService; | |||||
| import vn.azteam.tpf.service.dto.*; | |||||
| import vn.azteam.tpf.service.util.PageableUtil; | import vn.azteam.tpf.service.util.PageableUtil; | ||||
| import vn.azteam.tpf.service.util.UserRoleUtil; | import vn.azteam.tpf.service.util.UserRoleUtil; | ||||
| import vn.azteam.tpf.web.rest.errors.BadRequestAlertException; | |||||
| import vn.azteam.tpf.web.rest.util.HeaderUtil; | |||||
| import vn.azteam.tpf.web.rest.util.PaginationUtil; | import vn.azteam.tpf.web.rest.util.PaginationUtil; | ||||
| import vn.azteam.tpf.web.rest.util.RandomStringUtil; | |||||
| import java.net.URI; | |||||
| import java.net.URISyntaxException; | |||||
| import java.time.Instant; | |||||
| import java.util.Comparator; | import java.util.Comparator; | ||||
| import java.util.List; | import java.util.List; | ||||
| import java.util.Optional; | |||||
| import java.util.stream.Collectors; | import java.util.stream.Collectors; | ||||
| @RestController | @RestController | ||||
| private final UserRoleUtil userRoleUtil; | private final UserRoleUtil userRoleUtil; | ||||
| private final UserService userService; | |||||
| private final PageableUtil pageableUtil; | private final PageableUtil pageableUtil; | ||||
| public TBExampleStampResource(TBExampleStampQueryService tBExampleStampQueryService, UserRoleUtil userRoleUtil, PageableUtil pageableUtil){ | |||||
| public TBExampleStampResource(TBExampleStampQueryService tBExampleStampQueryService, UserRoleUtil userRoleUtil, UserService userService, PageableUtil pageableUtil){ | |||||
| this.tBExampleStampQueryService = tBExampleStampQueryService; | this.tBExampleStampQueryService = tBExampleStampQueryService; | ||||
| this.userRoleUtil = userRoleUtil; | this.userRoleUtil = userRoleUtil; | ||||
| this.userService = userService; | |||||
| this.pageableUtil = pageableUtil; | this.pageableUtil = pageableUtil; | ||||
| } | } | ||||