Skip to content

Commit

Permalink
perf($starter): refine Excel data abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Aug 23, 2021
1 parent fc14c03 commit 9d5de27
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.jmsoftware.maf.authcenter.role.controller;

import com.jmsoftware.maf.authcenter.role.entity.RoleExcelImport;
import com.jmsoftware.maf.authcenter.role.entity.RoleExcelBean;
import com.jmsoftware.maf.authcenter.role.service.RoleService;
import com.jmsoftware.maf.springcloudstarter.controller.AbstractExcelImportController;
import com.jmsoftware.maf.springcloudstarter.controller.AbstractExcelDataController;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.servlet.mvc.method.annotation.StreamingResponseBody;

import java.time.Instant;
import java.util.List;
Expand All @@ -23,33 +20,38 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/roles")
public class RoleController extends AbstractExcelImportController<RoleExcelImport> {
public class RoleController extends AbstractExcelDataController<RoleExcelBean> {
private final RoleService roleService;

@GetMapping("/stat/excel")
public ResponseEntity<StreamingResponseBody> downloadRoleStat() {
return this.roleService.downloadRoleStat();
}

@Override
public void onExceptionOccurred() {
log.error("Exception occurred when uploading excel for role.");
this.fileName.set("role-stat" + Instant.now() + ".xlsx");
}

@Override
protected void beforeDatabaseOperation(List<RoleExcelImport> beanList) {
protected void beforeDatabaseOperation(List<RoleExcelBean> beanList) {
log.info("BeforeDatabaseOperation: {}", beanList);
}

@Override
protected void executeDatabaseOperation(List<RoleExcelImport> beanList) throws Exception {
protected void executeDatabaseOperation(List<RoleExcelBean> beanList) throws Exception {
log.info("ExecuteDatabaseOperation: {}", beanList);
this.roleService.save(beanList);
}

@Override
protected void validateBeforeAddToBeanList(List<RoleExcelImport> beanList, RoleExcelImport bean, int index) throws IllegalArgumentException {
protected String getTemplateFileName() {
return RoleService.ROLE_TEMPLATE_EXCEL;
}

@Override
protected List<RoleExcelBean> getListForExporting() {
return this.roleService.getListForExporting();
}

@Override
protected void validateBeforeAddToBeanList(List<RoleExcelBean> beanList, RoleExcelBean bean, int index) throws IllegalArgumentException {
this.roleService.validateBeforeAddToBeanList(beanList, bean, index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import javax.validation.constraints.NotBlank;

/**
* Description: RoleExcelImport, change description here.
* Description: RoleExcelBean, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 8/22/2021 10:52 AM
**/
@Data
public class RoleExcelImport {
public class RoleExcelBean {
/**
* Role name
*/
Expand All @@ -33,8 +33,8 @@ public class RoleExcelImport {
* @param role the role
* @return the role excel import
*/
public static RoleExcelImport transformBy(Role role) {
val roleExcelImport = new RoleExcelImport();
public static RoleExcelBean transformBy(Role role) {
val roleExcelImport = new RoleExcelBean();
roleExcelImport.setName(role.getName());
roleExcelImport.setDescription(role.getDescription());
return roleExcelImport;
Expand All @@ -43,13 +43,13 @@ public static RoleExcelImport transformBy(Role role) {
/**
* Transform by role.
*
* @param roleExcelImport the role excel import
* @param roleExcelBean the role excel import
* @return the role excel import
*/
public static Role transformTo(RoleExcelImport roleExcelImport) {
public static Role transformTo(RoleExcelBean roleExcelBean) {
val role = new Role();
role.setName(roleExcelImport.getName());
role.setDescription(roleExcelImport.getDescription());
role.setName(roleExcelBean.getName());
role.setDescription(roleExcelBean.getDescription());
return role;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.jmsoftware.maf.authcenter.role.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.jmsoftware.maf.authcenter.role.entity.RoleExcelImport;
import com.jmsoftware.maf.authcenter.role.entity.RoleExcelBean;
import com.jmsoftware.maf.authcenter.role.entity.persistence.Role;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdSingleResponse;
import lombok.NonNull;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
Expand Down Expand Up @@ -52,11 +50,11 @@ public interface RoleService extends IService<Role> {
boolean checkAdmin(@NotEmpty List<@NotNull Long> roleIdList);

/**
* Download role stat response entity.
* Gets list for exporting.
*
* @return the response entity
* @return the list for exporting
*/
ResponseEntity<StreamingResponseBody> downloadRoleStat();
List<RoleExcelBean> getListForExporting();

/**
* Validate before add to bean list boolean.
Expand All @@ -66,12 +64,12 @@ public interface RoleService extends IService<Role> {
* @param index the index
* @throws IllegalArgumentException the illegal argument exception
*/
void validateBeforeAddToBeanList(List<RoleExcelImport> beanList, RoleExcelImport bean, int index) throws IllegalArgumentException;
void validateBeforeAddToBeanList(List<RoleExcelBean> beanList, RoleExcelBean bean, int index) throws IllegalArgumentException;

/**
* Save.
*
* @param beanList the bean list
*/
void save(@NotEmpty List<@Valid RoleExcelImport> beanList);
void save(@NotEmpty List<@Valid RoleExcelBean> beanList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,32 @@
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.validation.ValidationUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jmsoftware.maf.authcenter.role.entity.RoleExcelImport;
import com.jmsoftware.maf.authcenter.role.entity.RoleExcelBean;
import com.jmsoftware.maf.authcenter.role.entity.constant.RoleRedisKey;
import com.jmsoftware.maf.authcenter.role.entity.persistence.Role;
import com.jmsoftware.maf.authcenter.role.mapper.RoleMapper;
import com.jmsoftware.maf.authcenter.role.service.RoleService;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdSingleResponse;
import com.jmsoftware.maf.springcloudstarter.annotation.ExcelColumn;
import com.jmsoftware.maf.springcloudstarter.configuration.MafConfiguration;
import com.jmsoftware.maf.springcloudstarter.configuration.MafProjectProperty;
import lombok.*;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.lang.reflect.Field;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -95,31 +91,18 @@ public boolean checkAdmin(@NotEmpty List<@NotNull Long> roleIdList) {
}

@Override
public ResponseEntity<StreamingResponseBody> downloadRoleStat() {
public List<RoleExcelBean> getListForExporting() {
val rolePage = new Page<Role>(1, 500);
this.page(rolePage);
val roleExcelImportList = rolePage
return rolePage
.getRecords()
.stream()
.map(RoleExcelImport::transformBy)
.map(RoleExcelBean::transformBy)
.collect(Collectors.toList());
@Cleanup val excelWriter = new ExcelWriter(true);
Field[] declaredFields = RoleExcelImport.class.getDeclaredFields();
for (Field declaredField : declaredFields) {
ExcelColumn annotation = declaredField.getAnnotation(ExcelColumn.class);
excelWriter.addHeaderAlias(declaredField.getName(), annotation.name());
}
excelWriter.write(roleExcelImportList);
excelWriter.setFreezePane(1);
excelWriter.autoSizeColumnAll();
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION,
ContentDisposition.builder("attachment").filename(ROLE_TEMPLATE_EXCEL).build().toString())
.body(outputStream -> excelWriter.flush(outputStream, true));
}

@Override
public void validateBeforeAddToBeanList(List<RoleExcelImport> beanList, RoleExcelImport bean, int index) throws IllegalArgumentException {
public void validateBeforeAddToBeanList(List<RoleExcelBean> beanList, RoleExcelBean bean, int index) throws IllegalArgumentException {
val beanValidationResult = ValidationUtil.warpValidate(bean);
if (!beanValidationResult.isSuccess()) {
log.warn("Validation failed! beanList: {}, bean: {}, index: {}", beanList, bean, index);
Expand All @@ -131,8 +114,8 @@ public void validateBeforeAddToBeanList(List<RoleExcelImport> beanList, RoleExce

@Override
@Transactional(rollbackFor = Throwable.class)
public void save(@NotEmpty List<@Valid RoleExcelImport> beanList) {
val roleList = beanList.stream().map(RoleExcelImport::transformTo).collect(Collectors.toList());
public void save(@NotEmpty List<@Valid RoleExcelBean> beanList) {
val roleList = beanList.stream().map(RoleExcelBean::transformTo).collect(Collectors.toList());
val saved = this.saveBatch(roleList);
if (!saved) {
log.error("Cannot save batch role list. {}", roleList);
Expand Down
Loading

0 comments on commit 9d5de27

Please sign in to comment.