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;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.*;
......@@ -261,6 +262,7 @@ public class ScenarioServiceImpl implements ScenarioService {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
ByteArrayOutputStream os = new ByteArrayOutputStream();
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(2);
......@@ -270,17 +272,25 @@ public class ScenarioServiceImpl implements ScenarioService {
resultFont.setFontHeightInPoints((short) 11);
CellStyle resultStyle = workbook.createCellStyle();
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.setRightBorderColor(IndexedColors.BLACK.getIndex());
resultStyle.setBorderBottom(BorderStyle.THIN);
resultStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
resultStyle.setBorderLeft(BorderStyle.THIN);
resultStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
resultStyle.setBorderTop(BorderStyle.THIN);
resultStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
//</editor-fold>
//<editor-fold desc="Kiểm tra header của template" defaultstate="collapsed">
for (int i = 0; i < fileHeaderList.size(); i++) {
Cell cell = row.getCell(i);
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);
return result;
}
......@@ -289,8 +299,8 @@ public class ScenarioServiceImpl implements ScenarioService {
//kt file du lieu rong
if (sheet.getPhysicalNumberOfRows() == 2) {
result.put("content", Files.readAllBytes(file.toPath()));
result.put("code", Constants.FILE_UPLOAD_RESP_CODE.EMPTY);
result.put("file", file);
return result;
} else {
Cell resultCell = sheet.getRow(2).createCell(sheet.getRow(2).getPhysicalNumberOfCells());
......@@ -397,14 +407,18 @@ public class ScenarioServiceImpl implements ScenarioService {
lstQuestion.forEach(question -> {
// questionService.add(question);
});
FileOutputStream fos = new FileOutputStream(file);
workbook.write(fos);
result.put("file", file);
workbook.write(os);
os.flush();
os.close();
workbook.close();
result.put("content", os.toByteArray());
result.put("code", Constants.FILE_UPLOAD_RESP_CODE.SUCCESS);
} else {
FileOutputStream fos = new FileOutputStream(file);
workbook.write(fos);
result.put("file", file);
workbook.write(os);
os.flush();
os.close();
workbook.close();
result.put("content", os.toByteArray());
result.put("code", Constants.FILE_UPLOAD_RESP_CODE.ERROR);
}
} catch (Exception ex) {
......
......@@ -119,14 +119,11 @@ public class ScenarioController {
}
String path = saveUploadFile(file);
Map<String, Object> map = scenarioService.readAndValidateCustomer(path, scenarioId, campaignId, userSession.getCompanySiteId());
File fileExport = (File) map.get("file");
String code = (String) map.get("code");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", Constants.MIME_TYPE.EXCEL_XLSX);
headers.add("Message", code);
byte[] content = (byte[]) map.get("content");
return ResponseEntity.ok()
.headers(headers)
.body(Files.readAllBytes(fileExport.toPath()));
.header("Message", code)
.body(content);
} catch (Exception e) {
logger.error(e.getMessage());
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