Skip to content

Commit

Permalink
feat($auth-center): import role data from Excel
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Aug 22, 2021
1 parent a70434f commit 3468082
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 149 deletions.
8 changes: 8 additions & 0 deletions auth-center/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,13 @@
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>

<!-- OSS Dependencies -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>${minio.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.Date;
import java.time.LocalDateTime;

import static com.baomidou.mybatisplus.annotation.FieldFill.INSERT;
import static com.baomidou.mybatisplus.annotation.FieldFill.UPDATE;
Expand All @@ -21,109 +21,83 @@
@Data
@TableName(value = Permission.TABLE_NAME)
public class Permission {
public static final String TABLE_NAME = "permission";
public static final String COL_ID = "id";
public static final String COL_URL = "url";
public static final String COL_DESCRIPTION = "description";
public static final String COL_TYPE = "type";
public static final String COL_PERMISSION_EXPRESSION = "permission_expression";
public static final String COL_METHOD = "method";
public static final String COL_SORT = "sort";
public static final String COL_PARENT_ID = "parent_id";
public static final String COL_CREATED_BY = "created_by";
public static final String COL_CREATED_TIME = "created_time";
public static final String COL_MODIFIED_BY = "modified_by";
public static final String COL_MODIFIED_TIME = "modified_time";
public static final String COL_DELETED = "deleted";
/**
* Primary key
*/
@TableId(value = COL_ID, type = IdType.AUTO)
private Long id;

/**
* URL. If type of record is page (1), URL stands for route; if type of record is button (2), URL stands for API
*/
@TableField(value = COL_URL)
private String url;

/**
* Permission description
*/
@TableField(value = COL_DESCRIPTION)
private String description;

/**
* Permission type. 1 - page; 2 - button
*/
@TableField(value = COL_TYPE)
private Byte type;

/**
* Permission expression
*/
@TableField(value = COL_PERMISSION_EXPRESSION)
private String permissionExpression;

/**
* HTTP method of API
*/
@TableField(value = COL_METHOD)
private Object method;

/**
* Sort number
*/
@TableField(value = COL_SORT)
private Integer sort;

/**
* Primary key of parent
*/
@TableField(value = COL_PARENT_ID)
private Long parentId;

/**
* Created by
*/
@TableField(value = COL_CREATED_BY, fill = INSERT)
private Long createdBy;

/**
* Created time
*/
@TableField(value = COL_CREATED_TIME, fill = INSERT)
private Date createdTime;

private LocalDateTime createdTime;
/**
* Modified by
*/
@TableField(value = COL_MODIFIED_BY, fill = UPDATE)
private Long modifiedBy;

/**
* Modified time
*/
@TableField(value = COL_MODIFIED_TIME, fill = UPDATE)
private Date modifiedTime;

private LocalDateTime modifiedTime;
/**
* Deleted flag
*/
@TableField(value = COL_DELETED, fill = INSERT)
private Byte deleted;

public static final String TABLE_NAME = "permission";

public static final String COL_ID = "id";

public static final String COL_URL = "url";

public static final String COL_DESCRIPTION = "description";

public static final String COL_TYPE = "type";

public static final String COL_PERMISSION_EXPRESSION = "permission_expression";

public static final String COL_METHOD = "method";

public static final String COL_SORT = "sort";

public static final String COL_PARENT_ID = "parent_id";

public static final String COL_CREATED_BY = "created_by";

public static final String COL_CREATED_TIME = "created_time";

public static final String COL_MODIFIED_BY = "modified_by";

public static final String COL_MODIFIED_TIME = "modified_time";

public static final String COL_DELETED = "deleted";
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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 @@ -33,15 +34,22 @@ public ResponseEntity<StreamingResponseBody> 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 executeDatabaseOperation(List<RoleExcelImport> beanList) throws Exception {
protected void beforeDatabaseOperation(List<RoleExcelImport> beanList) {
log.info("BeforeDatabaseOperation: {}", beanList);
}

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

@Override
protected boolean validateBeforeAddToBeanList(List<RoleExcelImport> beanList, RoleExcelImport bean, int index) throws IllegalArgumentException {
return false;
return this.roleService.validateBeforeAddToBeanList(beanList, bean, index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@ public static RoleExcelImport transformBy(Role role) {
roleExcelImport.setDescription(role.getDescription());
return roleExcelImport;
}

/**
* Transform by role.
*
* @param roleExcelImport the role excel import
* @return the role excel import
*/
public static Role transformTo(RoleExcelImport roleExcelImport) {
val role = new Role();
role.setName(roleExcelImport.getName());
role.setDescription(roleExcelImport.getDescription());
return role;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.Date;
import java.time.LocalDateTime;

import static com.baomidou.mybatisplus.annotation.FieldFill.INSERT;
import static com.baomidou.mybatisplus.annotation.FieldFill.UPDATE;
Expand All @@ -21,69 +21,53 @@
@Data
@TableName(value = Role.TABLE_NAME)
public class Role {
public static final String TABLE_NAME = "role";
public static final String COL_ID = "id";
public static final String COL_NAME = "name";
public static final String COL_DESCRIPTION = "description";
public static final String COL_CREATED_BY = "created_by";
public static final String COL_CREATED_TIME = "created_time";
public static final String COL_MODIFIED_BY = "modified_by";
public static final String COL_MODIFIED_TIME = "modified_time";
public static final String COL_DELETED = "deleted";
/**
* Primary key
*/
@TableId(value = COL_ID, type = IdType.AUTO)
private Long id;

/**
* Role name
*/
@TableField(value = COL_NAME)
private String name;

/**
* Role description
*/
@TableField(value = COL_DESCRIPTION)
private String description;

/**
* Created by
*/
@TableField(value = COL_CREATED_BY, fill = INSERT)
private Long createdBy;

/**
* Created time
*/
@TableField(value = COL_CREATED_TIME, fill = INSERT)
private Date createdTime;

private LocalDateTime createdTime;
/**
* Modified by
*/
@TableField(value = COL_MODIFIED_BY, fill = UPDATE)
private Long modifiedBy;

/**
* Modified time
*/
@TableField(value = COL_MODIFIED_TIME, fill = UPDATE)
private Date modifiedTime;

private LocalDateTime modifiedTime;
/**
* Deleted flag.
*/
@TableField(value = COL_DELETED, fill = INSERT)
private Byte deleted;

public static final String TABLE_NAME = "role";

public static final String COL_ID = "id";

public static final String COL_NAME = "name";

public static final String COL_DESCRIPTION = "description";

public static final String COL_CREATED_BY = "created_by";

public static final String COL_CREATED_TIME = "created_time";

public static final String COL_MODIFIED_BY = "modified_by";

public static final String COL_MODIFIED_TIME = "modified_time";

public static final String COL_DELETED = "deleted";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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.persistence.Role;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import com.jmsoftware.maf.common.domain.authcenter.role.GetRoleListByUserIdSingleResponse;
Expand All @@ -9,6 +10,7 @@
import org.springframework.validation.annotation.Validated;
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.util.List;
Expand All @@ -23,6 +25,8 @@
*/
@Validated
public interface RoleService extends IService<Role> {
String ROLE_TEMPLATE_EXCEL = "role-stat.xlsx";

/**
* Gets role list by user id.
*
Expand Down Expand Up @@ -53,4 +57,21 @@ public interface RoleService extends IService<Role> {
* @return the response entity
*/
ResponseEntity<StreamingResponseBody> downloadRoleStat();

/**
* Validate before add to bean list boolean.
*
* @param beanList the bean list
* @param bean the bean
* @param index the index
* @return the boolean
*/
boolean validateBeforeAddToBeanList(List<RoleExcelImport> beanList, RoleExcelImport bean, int index);

/**
* Save.
*
* @param beanList the bean list
*/
void save(@NotEmpty List<@Valid RoleExcelImport> beanList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.hutool.core.util.BooleanUtil;
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;
Expand All @@ -23,6 +24,7 @@
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;
Expand All @@ -31,6 +33,7 @@
import org.springframework.stereotype.Service;
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;
Expand All @@ -46,10 +49,10 @@
* @author Johnny Miller (锺俊)
* @date 2020-05-10 22:39:50
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
private static final String ROLE_TEMPLATE_EXCEL = "role-stat.xlsx";
private final MafProjectProperty mafProjectProperty;
private final MafConfiguration mafConfiguration;
private final RedisTemplate<String, String> redisTemplate;
Expand Down Expand Up @@ -116,4 +119,25 @@ public ResponseEntity<StreamingResponseBody> downloadRoleStat() {
ContentDisposition.builder("attachment").filename(ROLE_TEMPLATE_EXCEL).build().toString())
.body(outputStream -> excelWriter.flush(outputStream, true));
}

@Override
public boolean validateBeforeAddToBeanList(List<RoleExcelImport> beanList, RoleExcelImport bean, int index) {
val beanValidationResult = ValidationUtil.warpValidate(bean);
if (!beanValidationResult.isSuccess()) {
log.warn("Validation failed! beanList: {}, bean: {}, index: {}", beanList, bean, index);
val firstErrorMessage = CollUtil.getFirst(beanValidationResult.getErrorMessages());
throw new IllegalArgumentException(
String.format("%s %s", firstErrorMessage.getPropertyName(), firstErrorMessage.getMessage()));
}
return true;
}

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

0 comments on commit 3468082

Please sign in to comment.