Search in sources :

Example 1 with SimpleExportXmEntityDto

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));
    }
}
Also used : XmEntityService(com.icthh.xm.ms.entity.service.XmEntityService) StorageService(com.icthh.xm.ms.entity.service.StorageService) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) XmEntityPermittedRepository(com.icthh.xm.ms.entity.repository.XmEntityPermittedRepository) LogicExtensionPoint(com.icthh.xm.commons.lep.LogicExtensionPoint) Collectors.toMap(java.util.stream.Collectors.toMap) Vote(com.icthh.xm.ms.entity.domain.Vote) Arrays.asList(java.util.Arrays.asList) FileFormatEnum(com.icthh.xm.ms.entity.domain.FileFormatEnum) EntityToCsvConverterUtils(com.icthh.xm.ms.entity.domain.converter.EntityToCsvConverterUtils) Map(java.util.Map) EntityToExcelConverterUtils(com.icthh.xm.ms.entity.domain.converter.EntityToExcelConverterUtils) Pageable(org.springframework.data.domain.Pageable) FindWithPermission(com.icthh.xm.commons.permission.annotation.FindWithPermission) URI(java.net.URI) Resource(org.springframework.core.io.Resource) Link(com.icthh.xm.ms.entity.domain.Link) LifecycleLepStrategy(com.icthh.xm.ms.entity.service.LifecycleLepStrategy) BusinessException(com.icthh.xm.commons.exceptions.BusinessException) Collections.emptyList(java.util.Collections.emptyList) PageRequest(org.springframework.data.domain.PageRequest) Set(java.util.Set) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec) LinkService(com.icthh.xm.ms.entity.service.LinkService) XmEntitySearchRepository(com.icthh.xm.ms.entity.repository.search.XmEntitySearchRepository) Page(org.springframework.data.domain.Page) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) HttpEntity(org.springframework.http.HttpEntity) List(java.util.List) ProfileService(com.icthh.xm.ms.entity.service.ProfileService) XmEntitySpecService(com.icthh.xm.ms.entity.service.XmEntitySpecService) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) Specification(org.springframework.data.jpa.domain.Specification) Comment(com.icthh.xm.ms.entity.domain.Comment) Optional(java.util.Optional) XmAuthenticationContextHolder(com.icthh.xm.commons.security.XmAuthenticationContextHolder) CustomCollectionUtils.nullSafe(com.icthh.xm.ms.entity.util.CustomCollectionUtils.nullSafe) Constants(com.icthh.xm.ms.entity.config.Constants) XmEntityTypeKeyResolver(com.icthh.xm.ms.entity.lep.keyresolver.XmEntityTypeKeyResolver) XmEntityIdKeyTypeKey(com.icthh.xm.ms.entity.projection.XmEntityIdKeyTypeKey) Callable(java.util.concurrent.Callable) LepService(com.icthh.xm.commons.lep.spring.LepService) ModelMapper(org.modelmapper.ModelMapper) XmEntityStateProjection(com.icthh.xm.ms.entity.projection.XmEntityStateProjection) Propagation(org.springframework.transaction.annotation.Propagation) SimpleExportXmEntityDto(com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto) EntityNotFoundException(com.icthh.xm.commons.exceptions.EntityNotFoundException) IdOrKey(com.icthh.xm.ms.entity.domain.ext.IdOrKey) AttachmentService(com.icthh.xm.ms.entity.service.AttachmentService) SEARCH_BUILDER_TYPE(com.icthh.xm.ms.entity.domain.spec.LinkSpec.SEARCH_BUILDER_TYPE) Rating(com.icthh.xm.ms.entity.domain.Rating) Iterator(java.util.Iterator) Optional.ofNullable(java.util.Optional.ofNullable) ErrorConstants(com.icthh.xm.commons.exceptions.ErrorConstants) LifecycleLepStrategyFactory(com.icthh.xm.ms.entity.service.LifecycleLepStrategyFactory) ObjectUtils(org.springframework.util.ObjectUtils) LinkSpec(com.icthh.xm.ms.entity.domain.spec.LinkSpec) Attachment(com.icthh.xm.ms.entity.domain.Attachment) Tag(com.icthh.xm.ms.entity.domain.Tag) Location(com.icthh.xm.ms.entity.domain.Location) Consumer(java.util.function.Consumer) StateSpec(com.icthh.xm.ms.entity.domain.spec.StateSpec) XmEntityPermittedSearchRepository(com.icthh.xm.ms.entity.repository.search.XmEntityPermittedSearchRepository) XmEntityRepository(com.icthh.xm.ms.entity.repository.XmEntityRepository) Calendar(com.icthh.xm.ms.entity.domain.Calendar) MultipartFile(org.springframework.web.multipart.MultipartFile) NEW_BUILDER_TYPE(com.icthh.xm.ms.entity.domain.spec.LinkSpec.NEW_BUILDER_TYPE) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) PageRequest(org.springframework.data.domain.PageRequest) BusinessException(com.icthh.xm.commons.exceptions.BusinessException) SimpleExportXmEntityDto(com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) ModelMapper(org.modelmapper.ModelMapper) LogicExtensionPoint(com.icthh.xm.commons.lep.LogicExtensionPoint)

Example 2 with SimpleExportXmEntityDto

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");
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) SimpleExportXmEntityDto(com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCreationHelper(org.apache.poi.xssf.usermodel.XSSFCreationHelper) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) Row(org.apache.poi.ss.usermodel.Row) IOException(java.io.IOException) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

SimpleExportXmEntityDto (com.icthh.xm.ms.entity.domain.SimpleExportXmEntityDto)2 BusinessException (com.icthh.xm.commons.exceptions.BusinessException)1 EntityNotFoundException (com.icthh.xm.commons.exceptions.EntityNotFoundException)1 ErrorConstants (com.icthh.xm.commons.exceptions.ErrorConstants)1 LogicExtensionPoint (com.icthh.xm.commons.lep.LogicExtensionPoint)1 LepService (com.icthh.xm.commons.lep.spring.LepService)1 FindWithPermission (com.icthh.xm.commons.permission.annotation.FindWithPermission)1 XmAuthenticationContextHolder (com.icthh.xm.commons.security.XmAuthenticationContextHolder)1 Constants (com.icthh.xm.ms.entity.config.Constants)1 Attachment (com.icthh.xm.ms.entity.domain.Attachment)1 Calendar (com.icthh.xm.ms.entity.domain.Calendar)1 Comment (com.icthh.xm.ms.entity.domain.Comment)1 FileFormatEnum (com.icthh.xm.ms.entity.domain.FileFormatEnum)1 Link (com.icthh.xm.ms.entity.domain.Link)1 Location (com.icthh.xm.ms.entity.domain.Location)1 Rating (com.icthh.xm.ms.entity.domain.Rating)1 Tag (com.icthh.xm.ms.entity.domain.Tag)1 Vote (com.icthh.xm.ms.entity.domain.Vote)1 XmEntity (com.icthh.xm.ms.entity.domain.XmEntity)1 EntityToCsvConverterUtils (com.icthh.xm.ms.entity.domain.converter.EntityToCsvConverterUtils)1