feat[storage] resource注解新增指定路径功能

This commit is contained in:
aoyu
2022-12-28 17:27:15 +08:00
parent 91393207e5
commit 4bde36ece9
7 changed files with 52 additions and 19 deletions
@@ -61,12 +61,12 @@ public abstract class CsvReader {
// 获取配置表的有效列名称,默认第一行就是字段名称
var fieldRow = iterator.next();
if (fieldRow == null) {
throw new RunException("无法获取资源[class:{}]的csv文件的属性控制列", fileName);
throw new RunException("Failed to get attribute control column from csv file of resource [class:{}]", fileName);
}
//默认第二行字段类型
var typeRow = iterator.next();
if (typeRow == null) {
throw new RunException("无法获取资源[class:{}]的csv文件的类型控制列", fileName);
throw new RunException("Failed to get type control column from csv file of resource [class:{}]", fileName);
}
// 默认第三行为描述,需要的时候再使用
var descRow = iterator.next();
@@ -75,11 +75,11 @@ public abstract class CsvReader {
for (var i = 0; i < fieldRow.size(); i++) {
var fieldName = fieldRow.get(i);
if (fieldName == null) {
throw new RunException("{}列名不能为空,第{}列没有配置名字", fileName, i + 1);
throw new RunException("The column name of {} cannot be empty, and column {} has no configured name", fileName, i + 1);
}
var filedType = typeRow.get(i);
if (filedType == null) {
throw new RunException("{}列类型不能为空,第{}列没有配置类型", fileName, i + 1);
throw new RunException("The column type of {} cannot be empty, and column {} has no configured type", fileName, i + 1);
}
headers.add(ResourceHeader.valueOf(fieldName, filedType, i));
}
@@ -90,7 +90,7 @@ public abstract class CsvReader {
try {
return CSVFormat.EXCEL.parse(new InputStreamReader(input));
} catch (IOException e) {
throw new RunException("静态资源[{}]异常,无法读取文件", fileName);
throw new RunException("Static resource [{}] is abnormal, and the file cannot be read", fileName);
}
}
}
@@ -64,12 +64,12 @@ public abstract class ExcelReader {
// 获取配置表的有效列名称,默认第一行就是字段名称
var fieldRow = iterator.next();
if (fieldRow == null) {
throw new RunException("无法获取资源[class:{}]的Excel文件的属性控制列", fileName);
throw new RunException("Failed to get attribute control column from excel file of resource [class:{}]", fileName);
}
// 默认第二行字段类型
var typeRow = iterator.next();
if (typeRow == null) {
throw new RunException("无法获取资源[class:{}]的Excel文件的类型控制列", fileName);
throw new RunException("Failed to get type control column from excel file of resource [class:{}]", fileName);
}
// 默认第三行为描述,需要的时候再使用
var desRow = iterator.next();
@@ -94,7 +94,7 @@ public abstract class ExcelReader {
}
var previousValue = cellFieldMap.put(fieldName, i);
if (Objects.nonNull(previousValue)) {
throw new RunException("资源[class:{}]的Excel文件出现重复的属性控制列[field:{}]", fileName, fieldName);
throw new RunException("There are duplicate attribute control columns [field:{}] in the Excel file of the resource [class:{}]", fieldName,fileName);
}
headerList.add(ResourceHeader.valueOf(fieldName, typeName, i));
}
@@ -105,7 +105,7 @@ public abstract class ExcelReader {
try {
return WorkbookFactory.create(input);
} catch (IOException e) {
throw new RunException("静态资源[{}]异常,无法读取文件", fileName);
throw new RunException("Static resource [{}] is abnormal, and the file cannot be read", fileName);
}
}
@@ -61,7 +61,7 @@ public class ResourceInterpreter {
} else if (resourceEnum == ResourceEnum.CSV) {
resource = CsvReader.readResourceDataFromCSV(inputStream, clazz.getSimpleName());
} else {
throw new RunException("不支持文件[{}]的配置类型[{}]", clazz.getSimpleName(), suffix);
throw new RunException("Configuration type [{}] of file [{}] is not supported", suffix, clazz.getSimpleName());
}
var result = new ArrayList<T>();
@@ -93,7 +93,7 @@ public class ResourceInterpreter {
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, instance, value);
} catch (Exception e) {
throw new RunException(e, "无法将Excel资源[class:{}]中的[content:{}]转换为属性[field:{}]", instance.getClass().getSimpleName(), content, field.getName());
throw new RunException(e, "Unable to convert [content:{}] to property [field:{}] in Excel resource [class:{}]", content, field.getName(), instance.getClass().getSimpleName());
}
}
@@ -102,13 +102,13 @@ public class ResourceInterpreter {
var fieldList = ReflectionUtils.notStaticAndTransientFields(clazz);
for (var field : fieldList) {
if (!fieldMap.containsKey(field.getName())) {
throw new RunException("资源类[class:{}]的声明属性[filed:{}]无法获取,请检查配置表的格式", clazz, field.getName());
throw new RunException("The declaration attribute [filed:{}] of the resource class [class:{}] cannot be obtained, please check the format of the configuration table", field.getName(), clazz);
}
if (field.isAnnotationPresent(Id.class)) {
var cellIndex = fieldMap.get(field.getName());
if (cellIndex != 0) {
throw new RunException("资源类[class:{}]的主键[Id:{}]必须放在Excel配置表的第一列,请检查配置表的格式", clazz, field.getName());
throw new RunException("The primary key [Id:{}] of the resource class [class:{}] must be placed in the first column of the Excel configuration table, please check the format of the configuration table", field.getName(), clazz);
}
}
}
@@ -128,7 +128,7 @@ public class ResourceInterpreter {
public static Map<String, Integer> getFieldMap(ResourceData resource, Class<?> clazz) {
var header = resource.getHeaders();
if (header == null) {
throw new RunException("无法获取资源[class:{}]的Excel文件的属性控制列", clazz.getSimpleName());
throw new RunException("Failed to get attribute control column from excel file of resource [class:{}]", clazz.getSimpleName());
}
var cellFieldMap = new HashMap<String, Integer>();
@@ -144,7 +144,7 @@ public class ResourceInterpreter {
}
var previousValue = cellFieldMap.put(name, i);
if (Objects.nonNull(previousValue)) {
throw new RunException("资源[class:{}]的Excel文件出现重复的属性控制列[field:{}]", clazz.getSimpleName(), name);
throw new RunException("There are duplicate attribute control columns [field:{}] in the Excel file of the resource [class:{}]", name, clazz.getSimpleName());
}
}
return cellFieldMap;
@@ -90,7 +90,7 @@ public class StorageManager implements IStorageManager {
throw new RuntimeException(StringUtils.format("Unable to get resource [class:{}]", clazzName));
}
var resourceFile = scanResourceFile(resourceClazz);
var resourceFile = getResourceFile(resourceClazz);
ResourceDef resourceDef = new ResourceDef(resourceClazz, resourceFile);
if (resourceDefinitionMap.containsKey(resourceClazz)) {
// 类的资源定义已经存在
@@ -290,4 +290,28 @@ public class StorageManager implements IStorageManager {
throw new RuntimeException(ExceptionUtils.getMessage(e));
}
}
private Resource getResourceFileByPath(Class<?> clazz){
var resourcePatternResolver = new PathMatchingResourcePatternResolver();
var metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
var path=clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value();
if(path.equals("")){
path=clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).path();
}
var resource= resourcePatternResolver.getResource(path);
try {
var input=resource.getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
return resource;
}
private Resource getResourceFile(Class<?> clazz){
if(clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value().equals("")&&
clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).path().equals("")){
return scanResourceFile(clazz);
}
else{
return getResourceFileByPath(clazz);
}
}
}
@@ -13,11 +13,14 @@
package com.zfoo.storage.model.anno;
import org.springframework.core.annotation.AliasFor;
import java.lang.annotation.*;
/**
* 资源注解
*
* 可以指定对应的资源路径,如果是类路径一classpath开头,如果是其它目录则以file开头
* 如果不指定路径,则默认通过扫描路径获取与类名相同的文件资源
* @author godotg
* @version 4.0
*/
@@ -25,4 +28,10 @@ import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Resource {
@AliasFor("path")
String value() default "";
@AliasFor("value")
String path() default "";
}
@@ -49,7 +49,7 @@ public class ApplicationTest {
var studentManager = context.getBean(StudentManager.class);
var studentResources = studentManager.studentResources;
var studentCsvResources = studentManager.studentCsvResources;
// 类名称和Excel名称必须完全一致,Excel的列名称必须对应对象的属性名称
// @Resource注解没指定路径,类名称和Excel名称必须完全一致,Excel的列名称必须对应对象的属性名称
for (StudentResource resource : studentResources.getAll()) {
logger.info(JsonUtils.object2String(resource));
}
@@ -21,7 +21,7 @@ import com.zfoo.storage.model.anno.Resource;
* @author godotg
* @version 4.0
*/
@Resource
@Resource(path="classpath:/excel/StudentCsvResource.csv")
public class StudentCsvResource {
@Id