All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ludii.excel.parse.AnnotationExcelFieldConfigParse Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.ludii.excel.parse;

import com.ludii.excel.annotation.ExcelField;
import com.ludii.excel.annotation.ExcelFields;
import com.ludii.excel.annotation.ExcelImportParseMode;
import com.ludii.excel.utils.ExcelUtils;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 陆迪
 * @date 2022/3/24
 */
public class AnnotationExcelFieldConfigParse implements ExcelFieldConfigParse {

    private final Class itemClazz;
    private final String[] groups;

    public AnnotationExcelFieldConfigParse(Class itemClazz, String... groups) {
        this.itemClazz = itemClazz;
        this.groups = groups;
    }

    @Override
    public ExcelImportConfigDefined getExcelImportConfigDefined() {
        //读取方法上的注解
        ExcelFieldParseMode excelFieldParseMode = getExcelFieldParseMode(itemClazz);
        List excelFieldConfigDefinedItemList = getDataClazzAnnotationItemList(itemClazz, ExcelField.Type.IMPORT, groups);

        return new DefaultExcelImportConfigDefined<>(itemClazz, excelFieldParseMode, excelFieldConfigDefinedItemList);
    }

    private ExcelFieldParseMode getExcelFieldParseMode(Class clazz) {
        ExcelImportParseMode excelImportParseMode = clazz.getAnnotation(ExcelImportParseMode.class);
        if (excelImportParseMode != null) {
            return excelImportParseMode.parseMode();
        } else {
            return ExcelFieldParseMode.sort;
        }

    }

    public List getDataClazzAnnotationItemList(Class listItemClazz, ExcelField.Type type, String... groups) {
        List annotationList = new ArrayList<>();
        // 获取字段上的注解
        Field[] declaredFields = ExcelUtils.getAllDeclaredFields(listItemClazz);
        for (Field field : declaredFields) {
            List excelFieldList = getAnnotationExcelField(field);
            for (ExcelField excelField : excelFieldList) {
                AnnotationExcelFieldDefined annotationExcelFieldDefined = new AnnotationExcelFieldDefined(excelField);
                addAnnotation(annotationList, annotationExcelFieldDefined, field, type, groups);
            }
        }
        // 获取方法上的注解
        Method[] declaredMethods = listItemClazz.getDeclaredMethods();
        for (Method method : declaredMethods) {
            List excelFieldList = getAnnotationExcelField(method);
            for (ExcelField excelField : excelFieldList) {
                AnnotationExcelFieldDefined annotationExcelFieldDefined = new AnnotationExcelFieldDefined(excelField);
                addAnnotation(annotationList, annotationExcelFieldDefined, method, type, groups);
            }
        }

        this.validatedAnnotationList(annotationList);
        return annotationList;
    }

    private List getAnnotationExcelField(AnnotatedElement annotatedElement) {
        List excelFieldList = new ArrayList<>();
        ExcelFields excelFields = annotatedElement.getAnnotation(ExcelFields.class);
        if (excelFields != null && excelFields.value() != null) {
            Collections.addAll(excelFieldList, excelFields.value());
        }
        ExcelField excelField = annotatedElement.getAnnotation(ExcelField.class);
        // 若无注解,则导入解析忽略此字段
        if (excelField != null) {
            excelFieldList.add(excelField);
        }
        return excelFieldList;
    }

    /**
     * 添加到 annotationList
     */
    private void addAnnotation(List annotationList, ExcelFieldDefined excelField, Field field, ExcelField.Type excelFieldType, String... groups) {
        if (excelField == null) {
            return;
        }
        // 非导入字段
        if (!ExcelField.Type.ALL.equals(excelField.type()) && !excelFieldType.equals(excelField.type())) {
            return;
        }

        // 不存在groups时
        if (groups == null || groups.length == 0) {
            ExcelFieldConfigDefinedItem excelFieldConfigDefinedItem = new ExcelFieldConfigDefinedItem(excelField, field);
            annotationList.add(excelFieldConfigDefinedItem);
            return;
        }

        boolean inGroup = false;
        for (String group : groups) {
            if (inGroup) {
                break;
            }
            for (String excelFieldGroup : excelField.groups()) {
                if (ExcelUtils.equals(group, excelFieldGroup)) {
                    inGroup = true;
                    ExcelFieldConfigDefinedItem excelFieldConfigDefinedItem = new ExcelFieldConfigDefinedItem(excelField, field);
                    annotationList.add(excelFieldConfigDefinedItem);
                    break;
                }
            }
        }
    }

    private void addAnnotation(List annotationList, ExcelFieldDefined excelField, Method method, ExcelField.Type excelFieldType, String... groups) {
        if (excelField == null) {
            return;
        }
        // 非导入字段
        if (!ExcelField.Type.ALL.equals(excelField.type()) && !excelFieldType.equals(excelField.type())) {
            return;
        }

        // 不存在groups时
        if (groups == null || groups.length == 0) {
            ExcelFieldConfigDefinedItem excelFieldConfigDefinedItem = new ExcelFieldConfigDefinedItem(excelField, method);
            annotationList.add(excelFieldConfigDefinedItem);
            return;
        }

        boolean inGroup = false;
        for (String group : groups) {
            if (inGroup) {
                break;
            }
            for (String excelFieldGroup : excelField.groups()) {
                if (ExcelUtils.equals(group, excelFieldGroup)) {
                    inGroup = true;
                    ExcelFieldConfigDefinedItem excelFieldConfigDefinedItem = new ExcelFieldConfigDefinedItem(excelField, method);
                    annotationList.add(excelFieldConfigDefinedItem);
                    break;
                }
            }
        }
    }

    /**
     * 校验注解是否正确配置
     *
     * @param annotationList 注解元数据列表
     */
    private void validatedAnnotationList(List annotationList) {
        //todo 以后再完善
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy