From ab23e03e0433f25c5eb5f17db9a43f8fce1e29d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Mon, 27 Sep 2021 13:47:07 +0800 Subject: [PATCH] feat($Quartz): support batch import --- .../function/BooleanCheckUtil.java | 44 +++++++++++++++++++ .../QuartzJobConfigurationController.java | 38 +++++++++++++++- .../entity/QuartzJobConfigurationExcel.java | 32 ++++++++++++++ .../mapper/QuartzJobConfigurationMapper.java | 11 +++++ .../QuartzJobConfigurationService.java | 29 ++++++++++++ .../QuartzJobConfigurationServiceImpl.java | 42 ++++++++++++++++++ .../quartz/QuartzJobConfigurationMapper.xml | 25 ++++++++++- 7 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/BooleanCheckUtil.java create mode 100644 spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/entity/QuartzJobConfigurationExcel.java diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/BooleanCheckUtil.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/BooleanCheckUtil.java new file mode 100644 index 00000000..c90498c5 --- /dev/null +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/function/BooleanCheckUtil.java @@ -0,0 +1,44 @@ +package com.jmsoftware.maf.springcloudstarter.function; + +import cn.hutool.core.util.BooleanUtil; + +import java.util.function.Consumer; +import java.util.function.Supplier; + +import static java.util.Objects.nonNull; + +/** + * Description: BooleanCheckUtil, change description here. + * + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 9/27/2021 12:03 PM + **/ +public class BooleanCheckUtil { + /** + * Require true or else throw exception. + * + * @param aBoolean the aBoolean + * @param after the after + * @return the throw exception function + */ + public static ThrowExceptionFunction requireTrue(Boolean aBoolean, Consumer after) { + if (nonNull(after)) { + after.accept(aBoolean); + } + return exceptionSupplier -> { + if (BooleanUtil.isFalse(aBoolean)) { + throw exceptionSupplier.get(); + } + }; + } + + @FunctionalInterface + public interface ThrowExceptionFunction { + /** + * Or else throw. + * + * @param exceptionSupplier the exception supplier + * @throws Throwable the throwable + */ + void orElseThrow(Supplier exceptionSupplier) throws Throwable; + } +} diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/controller/QuartzJobConfigurationController.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/controller/QuartzJobConfigurationController.java index 0453f400..64a5af4d 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/controller/QuartzJobConfigurationController.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/controller/QuartzJobConfigurationController.java @@ -1,17 +1,23 @@ package com.jmsoftware.maf.springcloudstarter.quartz.controller; import com.jmsoftware.maf.common.bean.PageResponseBodyBean; +import com.jmsoftware.maf.springcloudstarter.poi.AbstractExcelDataController; import com.jmsoftware.maf.springcloudstarter.quartz.entity.GetQuartzJobConfigurationPageListItem; import com.jmsoftware.maf.springcloudstarter.quartz.entity.GetQuartzJobConfigurationPageListPayload; +import com.jmsoftware.maf.springcloudstarter.quartz.entity.QuartzJobConfigurationExcel; import com.jmsoftware.maf.springcloudstarter.quartz.service.QuartzJobConfigurationService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; +import java.time.Instant; +import java.util.List; /** *

QuartzJobConfigurationController

@@ -20,11 +26,13 @@ * * @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 9/26/21 11:58 PM **/ +@Slf4j @Validated @RestController @RequiredArgsConstructor +@RequestMapping("/quartz-job") @Api(tags = {"Quartz Job Configuration API"}) -public class QuartzJobConfigurationController { +public class QuartzJobConfigurationController extends AbstractExcelDataController { private final QuartzJobConfigurationService service; @GetMapping("/quartz-job-configurations") @@ -34,4 +42,32 @@ public PageResponseBodyBean getPageList( ) { return this.service.getPageList(payload); } + + @Override + protected void onExceptionOccurred() { + log.error("Exception occurred when uploading excel. Excel class: {}", QuartzJobConfigurationExcel.class); + this.fileName.set(String.format("quartz-job-configuration-stat-%s.xlsx", Instant.now())); + } + + @Override + protected void validateBeforeAddToBeanList(List beanList, + QuartzJobConfigurationExcel bean, int index) + throws IllegalArgumentException { + this.service.validateBeforeAddToBeanList(beanList, bean, index); + } + + @Override + protected void executeDatabaseOperation(List beanList) { + this.service.save(beanList); + } + + @Override + protected String getTemplateFileName() { + return QuartzJobConfigurationService.TEMPLATE_EXCEL; + } + + @Override + protected List getListForExporting() { + return this.service.getListForExporting(); + } } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/entity/QuartzJobConfigurationExcel.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/entity/QuartzJobConfigurationExcel.java new file mode 100644 index 00000000..2877b034 --- /dev/null +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/entity/QuartzJobConfigurationExcel.java @@ -0,0 +1,32 @@ +package com.jmsoftware.maf.springcloudstarter.quartz.entity; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.time.LocalDateTime; + +/** + * Description: QuartzJobConfigurationExcel, change description here. + * + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 9/27/2021 11:26 AM + **/ +@Data +public class QuartzJobConfigurationExcel { + @NotBlank + private String name; + private String group; + private String serviceName; + @NotBlank + private String invokeTarget; + @NotBlank + private String cronExpression; + private Byte misfirePolicy; + private Byte concurrent; + @NotBlank + private String description; + private Byte status; + private String createdBy; + private LocalDateTime createdTime; + private String modifiedBy; + private LocalDateTime modifiedTime; +} diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/mapper/QuartzJobConfigurationMapper.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/mapper/QuartzJobConfigurationMapper.java index 5f8a6bb2..0ed00dd4 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/mapper/QuartzJobConfigurationMapper.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/mapper/QuartzJobConfigurationMapper.java @@ -4,10 +4,13 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.jmsoftware.maf.springcloudstarter.quartz.entity.GetQuartzJobConfigurationPageListItem; import com.jmsoftware.maf.springcloudstarter.quartz.entity.GetQuartzJobConfigurationPageListPayload; +import com.jmsoftware.maf.springcloudstarter.quartz.entity.QuartzJobConfigurationExcel; import com.jmsoftware.maf.springcloudstarter.quartz.entity.persistence.QuartzJobConfiguration; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; +import java.util.List; + /** * Description: QuartzJobConfigurationMapper * @@ -26,4 +29,12 @@ Page selectPageList( Page page, @Param("payload") GetQuartzJobConfigurationPageListPayload payload ); + + /** + * Select list for exporting list. + * + * @param serviceName the service name + * @return the list + */ + List selectListForExporting(String serviceName); } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/service/QuartzJobConfigurationService.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/service/QuartzJobConfigurationService.java index 1451f21c..70e48791 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/service/QuartzJobConfigurationService.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/service/QuartzJobConfigurationService.java @@ -4,10 +4,13 @@ import com.jmsoftware.maf.common.bean.PageResponseBodyBean; import com.jmsoftware.maf.springcloudstarter.quartz.entity.GetQuartzJobConfigurationPageListItem; import com.jmsoftware.maf.springcloudstarter.quartz.entity.GetQuartzJobConfigurationPageListPayload; +import com.jmsoftware.maf.springcloudstarter.quartz.entity.QuartzJobConfigurationExcel; import com.jmsoftware.maf.springcloudstarter.quartz.entity.persistence.QuartzJobConfiguration; import org.springframework.validation.annotation.Validated; import javax.validation.Valid; +import javax.validation.constraints.NotEmpty; +import java.util.List; /** * Description: QuartzJobConfigurationService @@ -16,6 +19,8 @@ */ @Validated public interface QuartzJobConfigurationService extends IService { + String TEMPLATE_EXCEL = "quartz-job-configuration-stat.xlsx"; + /** * Gets page list. * @@ -25,4 +30,28 @@ public interface QuartzJobConfigurationService extends IService getPageList( @Valid GetQuartzJobConfigurationPageListPayload payload ); + + /** + * Validate before add to bean list. + * + * @param beanList the bean list + * @param bean the bean + * @param index the index + */ + void validateBeforeAddToBeanList(List beanList, QuartzJobConfigurationExcel bean, + int index); + + /** + * Save. + * + * @param beanList the bean list + */ + void save(@NotEmpty List<@Valid QuartzJobConfigurationExcel> beanList); + + /** + * Gets list for exporting. + * + * @return the list for exporting + */ + List getListForExporting(); } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/service/impl/QuartzJobConfigurationServiceImpl.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/service/impl/QuartzJobConfigurationServiceImpl.java index 823a82ea..868440ab 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/service/impl/QuartzJobConfigurationServiceImpl.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/quartz/service/impl/QuartzJobConfigurationServiceImpl.java @@ -1,5 +1,7 @@ package com.jmsoftware.maf.springcloudstarter.quartz.service.impl; +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.extra.validation.ValidationUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -7,21 +9,28 @@ import com.jmsoftware.maf.springcloudstarter.configuration.MafProjectProperty; import com.jmsoftware.maf.springcloudstarter.quartz.entity.GetQuartzJobConfigurationPageListItem; import com.jmsoftware.maf.springcloudstarter.quartz.entity.GetQuartzJobConfigurationPageListPayload; +import com.jmsoftware.maf.springcloudstarter.quartz.entity.QuartzJobConfigurationExcel; import com.jmsoftware.maf.springcloudstarter.quartz.entity.persistence.QuartzJobConfiguration; import com.jmsoftware.maf.springcloudstarter.quartz.mapper.QuartzJobConfigurationMapper; import com.jmsoftware.maf.springcloudstarter.quartz.service.QuartzJobConfigurationService; import com.jmsoftware.maf.springcloudstarter.quartz.util.ScheduleUtil; import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.quartz.SchedulerException; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.PostConstruct; import javax.validation.Valid; +import javax.validation.constraints.NotEmpty; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; + +import static com.jmsoftware.maf.springcloudstarter.function.BooleanCheckUtil.requireTrue; /** * Description: QuartzJobConfigurationServiceImpl @@ -66,4 +75,37 @@ public PageResponseBodyBean getPageList( this.getBaseMapper().selectPageList(page, payload); return PageResponseBodyBean.ofSuccess(page.getRecords(), page.getTotal()); } + + @Override + @SneakyThrows + public void validateBeforeAddToBeanList(List beanList, + QuartzJobConfigurationExcel bean, int index) { + val beanValidationResult = ValidationUtil.warpValidate(bean); + requireTrue( + beanValidationResult.isSuccess(), + success -> log.info("QuartzJobConfigurationExcel validation result: {}", success) + ).orElseThrow( + () -> new IllegalStateException("Invalid data. " + beanValidationResult.getErrorMessages().get(0)) + ); + } + + @Override + @SneakyThrows + @Transactional(rollbackFor = Throwable.class) + public void save(@NotEmpty List<@Valid QuartzJobConfigurationExcel> beanList) { + val quartzJobConfigurationList = beanList.stream().map(quartzJobConfigurationExcel -> { + val quartzJobConfiguration = new QuartzJobConfiguration(); + BeanUtil.copyProperties(quartzJobConfigurationExcel, quartzJobConfiguration); + return quartzJobConfiguration; + }).collect(Collectors.toList()); + requireTrue( + this.saveBatch(quartzJobConfigurationList), + saved -> log.info("Saved quartzJobConfigurationList, saved: {}", saved) + ).orElseThrow(() -> new IllegalStateException("Failed to save batch quartzJobConfigurationList")); + } + + @Override + public List getListForExporting() { + return this.getBaseMapper().selectListForExporting(this.mafProjectProperty.getProjectArtifactId()); + } } diff --git a/spring-cloud-starter/src/main/resources/mapper/quartz/QuartzJobConfigurationMapper.xml b/spring-cloud-starter/src/main/resources/mapper/quartz/QuartzJobConfigurationMapper.xml index 3e9b144f..a7ad522c 100644 --- a/spring-cloud-starter/src/main/resources/mapper/quartz/QuartzJobConfigurationMapper.xml +++ b/spring-cloud-starter/src/main/resources/mapper/quartz/QuartzJobConfigurationMapper.xml @@ -50,8 +50,31 @@ qjc.deleted = 0 - AND qjc.service_name LIKE concat('%', #{payload.serviceName}, '%') + AND qjc.service_name LIKE CONCAT('%', #{payload.serviceName}, '%') + +