Commit b5d0ef35 authored by Vu Duy Anh's avatar Vu Duy Anh

anhvd commit import scenario

parent 0fd8d25d
...@@ -26,6 +26,7 @@ import java.io.ByteArrayOutputStream; ...@@ -26,6 +26,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
...@@ -261,6 +262,7 @@ public class ScenarioServiceImpl implements ScenarioService { ...@@ -261,6 +262,7 @@ public class ScenarioServiceImpl implements ScenarioService {
File file = new File(path); File file = new File(path);
FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(fis); XSSFWorkbook workbook = new XSSFWorkbook(fis);
ByteArrayOutputStream os = new ByteArrayOutputStream();
Sheet sheet = workbook.getSheetAt(0); Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(2); Row row = sheet.getRow(2);
...@@ -270,17 +272,25 @@ public class ScenarioServiceImpl implements ScenarioService { ...@@ -270,17 +272,25 @@ public class ScenarioServiceImpl implements ScenarioService {
resultFont.setFontHeightInPoints((short) 11); resultFont.setFontHeightInPoints((short) 11);
CellStyle resultStyle = workbook.createCellStyle(); CellStyle resultStyle = workbook.createCellStyle();
resultStyle.setFont(resultFont); resultStyle.setFont(resultFont);
resultStyle.setBorderLeft(BorderStyle.THIN); resultStyle.setAlignment(HorizontalAlignment.CENTER);
resultStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
resultStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
resultStyle.setBorderRight(BorderStyle.THIN); resultStyle.setBorderRight(BorderStyle.THIN);
resultStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
resultStyle.setBorderBottom(BorderStyle.THIN); resultStyle.setBorderBottom(BorderStyle.THIN);
resultStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
resultStyle.setBorderLeft(BorderStyle.THIN);
resultStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
resultStyle.setBorderTop(BorderStyle.THIN); resultStyle.setBorderTop(BorderStyle.THIN);
resultStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
//</editor-fold> //</editor-fold>
//<editor-fold desc="Kiểm tra header của template" defaultstate="collapsed"> //<editor-fold desc="Kiểm tra header của template" defaultstate="collapsed">
for (int i = 0; i < fileHeaderList.size(); i++) { for (int i = 0; i < fileHeaderList.size(); i++) {
Cell cell = row.getCell(i); Cell cell = row.getCell(i);
if (!cell.getStringCellValue().equals(fileHeaderList.get(i).split("#")[0])) { if (!cell.getStringCellValue().equals(fileHeaderList.get(i).split("#")[0])) {
result.put("file", file); result.put("content", Files.readAllBytes(file.toPath()));
result.put("code", Constants.FILE_UPLOAD_RESP_CODE.INVALID_FORMAT); result.put("code", Constants.FILE_UPLOAD_RESP_CODE.INVALID_FORMAT);
return result; return result;
} }
...@@ -289,8 +299,8 @@ public class ScenarioServiceImpl implements ScenarioService { ...@@ -289,8 +299,8 @@ public class ScenarioServiceImpl implements ScenarioService {
//kt file du lieu rong //kt file du lieu rong
if (sheet.getPhysicalNumberOfRows() == 2) { if (sheet.getPhysicalNumberOfRows() == 2) {
result.put("content", Files.readAllBytes(file.toPath()));
result.put("code", Constants.FILE_UPLOAD_RESP_CODE.EMPTY); result.put("code", Constants.FILE_UPLOAD_RESP_CODE.EMPTY);
result.put("file", file);
return result; return result;
} else { } else {
Cell resultCell = sheet.getRow(2).createCell(sheet.getRow(2).getPhysicalNumberOfCells()); Cell resultCell = sheet.getRow(2).createCell(sheet.getRow(2).getPhysicalNumberOfCells());
...@@ -397,14 +407,18 @@ public class ScenarioServiceImpl implements ScenarioService { ...@@ -397,14 +407,18 @@ public class ScenarioServiceImpl implements ScenarioService {
lstQuestion.forEach(question -> { lstQuestion.forEach(question -> {
// questionService.add(question); // questionService.add(question);
}); });
FileOutputStream fos = new FileOutputStream(file); workbook.write(os);
workbook.write(fos); os.flush();
result.put("file", file); os.close();
workbook.close();
result.put("content", os.toByteArray());
result.put("code", Constants.FILE_UPLOAD_RESP_CODE.SUCCESS); result.put("code", Constants.FILE_UPLOAD_RESP_CODE.SUCCESS);
} else { } else {
FileOutputStream fos = new FileOutputStream(file); workbook.write(os);
workbook.write(fos); os.flush();
result.put("file", file); os.close();
workbook.close();
result.put("content", os.toByteArray());
result.put("code", Constants.FILE_UPLOAD_RESP_CODE.ERROR); result.put("code", Constants.FILE_UPLOAD_RESP_CODE.ERROR);
} }
} catch (Exception ex) { } catch (Exception ex) {
......
...@@ -119,14 +119,11 @@ public class ScenarioController { ...@@ -119,14 +119,11 @@ public class ScenarioController {
} }
String path = saveUploadFile(file); String path = saveUploadFile(file);
Map<String, Object> map = scenarioService.readAndValidateCustomer(path, scenarioId, campaignId, userSession.getCompanySiteId()); Map<String, Object> map = scenarioService.readAndValidateCustomer(path, scenarioId, campaignId, userSession.getCompanySiteId());
File fileExport = (File) map.get("file");
String code = (String) map.get("code"); String code = (String) map.get("code");
HttpHeaders headers = new HttpHeaders(); byte[] content = (byte[]) map.get("content");
headers.add("Content-Type", Constants.MIME_TYPE.EXCEL_XLSX);
headers.add("Message", code);
return ResponseEntity.ok() return ResponseEntity.ok()
.headers(headers) .header("Message", code)
.body(Files.readAllBytes(fileExport.toPath())); .body(content);
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment