use of com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto in project xm-ms-entity by xm-online.
the class XmEntityServiceImpl method exportEntities.
@LogicExtensionPoint("Export")
@Override
public byte[] exportEntities(String fileFormat, String typeKey) {
Set<String> typeKeys = xmEntitySpecService.findNonAbstractTypesByPrefix(typeKey).stream().map(TypeSpec::getKey).collect(Collectors.toSet());
List<XmEntity> xmEntities = xmEntityRepository.findAllByTypeKeyIn(new PageRequest(0, Integer.MAX_VALUE), typeKeys).getContent();
ModelMapper modelMapper = new ModelMapper();
List<SimpleExportXmEntityDto> simpleEntities = xmEntities.stream().map(entity -> modelMapper.map(entity, SimpleExportXmEntityDto.class)).collect(Collectors.toList());
switch(FileFormatEnum.valueOf(fileFormat.toUpperCase())) {
case CSV:
return EntityToCsvConverterUtils.toCsv(simpleEntities, SimpleExportXmEntityDto.class);
case XLSX:
return EntityToExcelConverterUtils.toExcel(simpleEntities, typeKey);
default:
throw new BusinessException(ErrorConstants.ERR_VALIDATION, String.format("Converter doesn't support '%s' file format", fileFormat));
}
}
use of com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto in project xm-ms-entity by xm-online.
the class EntityToExcelConverterUtils method toExcel.
/**
* Writes entities to excel file.
* @param entities the entities list
* @return byte array of excel file
*/
public static byte[] toExcel(List<SimpleExportXmEntityDto> entities, String sheetName) {
if (CollectionUtils.isEmpty(entities)) {
log.warn("Passed empty object for serialize, therefore return empty byte array which represents excel file");
return new byte[0];
}
try (XSSFWorkbook workbook = new XSSFWorkbook();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
XSSFSheet sheet = workbook.createSheet(sheetName);
XSSFCreationHelper creationHelper = workbook.getCreationHelper();
XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat(DateFormatConverter.getJavaDateTimePattern(0, Locale.US)));
int rowCount = 0;
XSSFRow headerRow = sheet.createRow(rowCount);
IntStream.range(0, headers.length).forEach(i -> headerRow.createCell(i).setCellValue(headers[i]));
for (SimpleExportXmEntityDto entity : entities) {
Row row = sheet.createRow(++rowCount);
int columnCount = 0;
Cell cell = row.createCell(columnCount);
cell.setCellValue(entity.getOrElseId(0L));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseKey(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseTypeKey(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseStateKey(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseName(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(Date.from(entity.getOrElseStartDate(Instant.now())));
cell.setCellStyle(cellStyle);
cell = row.createCell(++columnCount);
cell.setCellValue(Date.from(entity.getOrElseStartDate(Instant.now())));
cell.setCellStyle(cellStyle);
cell = row.createCell(++columnCount);
cell.setCellValue(Date.from(entity.getOrElseEndDate(Instant.now())));
cell.setCellStyle(cellStyle);
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseAvatarUrl(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseDescription(StringUtils.EMPTY));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.isOrElseRemoved(false));
cell = row.createCell(++columnCount);
cell.setCellValue(entity.getOrElseCreatedBy(StringUtils.EMPTY));
}
workbook.write(outputStream);
return outputStream.toByteArray();
} catch (IOException e) {
throw new IllegalStateException("Exception while writing data to excel file");
}
}
Aggregations