Search in sources :

Example 1 with ExcelBindField

use of com.diboot.file.excel.annotation.ExcelBindField in project diboot by dibo-software.

the class ReadExcelListener method validateOrConvertDictAndRefField.

/**
 * 检查或转换字典和关联字段
 */
protected void validateOrConvertDictAndRefField(List<T> dataList, boolean preview) {
    Class<T> tClass = getExcelModelClass();
    Map<String, Annotation> fieldName2BindAnnoMap = ExcelBindAnnoHandler.getField2BindAnnoMap(tClass);
    if (fieldName2BindAnnoMap.isEmpty()) {
        return;
    }
    for (Map.Entry<String, Annotation> entry : fieldName2BindAnnoMap.entrySet()) {
        List nameList = (entry.getValue() instanceof ExcelBindField) ? BeanUtils.collectToList(dataList, entry.getKey()) : null;
        Map<String, List> map = ExcelBindAnnoHandler.convertToNameValueMap(entry.getValue(), nameList);
        Field field = BeanUtils.extractField(tClass, entry.getKey());
        boolean valueNotNull = (field.getAnnotation(NotNull.class) != null);
        for (T data : dataList) {
            String name = BeanUtils.getStringProperty(data, entry.getKey());
            if (S.isEmpty(name)) {
                continue;
            }
            List valList = map.get(name);
            if (entry.getValue() instanceof ExcelBindField) {
                ExcelBindField excelBindField = (ExcelBindField) entry.getValue();
                String setFieldName = S.defaultIfEmpty(excelBindField.setIdField(), entry.getKey());
                if (V.isEmpty(valList)) {
                    if (excelBindField.empty().equals(EmptyStrategy.SET_0)) {
                        // 非预览时 赋值
                        if (!preview) {
                            BeanUtils.setProperty(data, setFieldName, 0);
                        }
                    } else if (excelBindField.empty().equals(EmptyStrategy.WARN)) {
                        data.addComment(entry.getKey(), name + " 值不存在");
                    } else if (excelBindField.empty().equals(EmptyStrategy.IGNORE) && valueNotNull) {
                        log.warn("非空字段 {} 不应设置 EmptyStrategy.IGNORE.", entry.getKey());
                    }
                } else if (valList.size() == 1) {
                    // 非预览时 赋值
                    if (!preview) {
                        BeanUtils.setProperty(data, setFieldName, valList.get(0));
                    }
                } else {
                    if (excelBindField.duplicate().equals(DuplicateStrategy.WARN)) {
                        data.addComment(entry.getKey(), name + " 匹配到多个值");
                    } else if (excelBindField.duplicate().equals(DuplicateStrategy.FIRST)) {
                        // 非预览时 赋值
                        if (!preview) {
                            BeanUtils.setProperty(data, setFieldName, valList.get(0));
                        }
                    }
                }
            } else if (entry.getValue() instanceof ExcelBindDict || entry.getValue() instanceof BindDict) {
                if (V.isEmpty(valList)) {
                    if (name.contains(S.SEPARATOR)) {
                        valList = new LinkedList<>();
                        for (String item : name.split(S.SEPARATOR)) {
                            valList.addAll(map.get(item));
                        }
                        if (valList.size() > 0) {
                            valList.add(0, S.join(valList));
                        }
                    }
                    // 非空才报错
                    if (valueNotNull && V.isEmpty(valList)) {
                        data.addComment(entry.getKey(), name + " 无匹配字典");
                        continue;
                    }
                }
                // 非预览时 赋值
                if (!preview && V.notEmpty(valList)) {
                    BeanUtils.setProperty(data, entry.getKey(), valList.get(0));
                }
            }
        }
    }
}
Also used : ExcelBindDict(com.diboot.file.excel.annotation.ExcelBindDict) BindDict(com.diboot.core.binding.annotation.BindDict) Annotation(java.lang.annotation.Annotation) Field(java.lang.reflect.Field) ExcelBindField(com.diboot.file.excel.annotation.ExcelBindField) ExcelBindDict(com.diboot.file.excel.annotation.ExcelBindDict) ExcelBindField(com.diboot.file.excel.annotation.ExcelBindField)

Example 2 with ExcelBindField

use of com.diboot.file.excel.annotation.ExcelBindField in project diboot by dibo-software.

the class ExcelBindAnnoHandler method convertToNameValueMap.

/**
 * 转换为name-value map
 * @param annotation
 * @param nameList
 * @return
 */
public static Map<String, List> convertToNameValueMap(Annotation annotation, List<String> nameList) {
    // 字典
    if (annotation instanceof ExcelBindDict || annotation instanceof BindDict) {
        String dictType = null;
        if (annotation instanceof ExcelBindDict) {
            dictType = ((ExcelBindDict) annotation).type();
        } else {
            dictType = ((BindDict) annotation).type();
        }
        DictionaryServiceExtProvider bindDictService = ContextHelper.getBean(DictionaryServiceExtProvider.class);
        if (bindDictService == null) {
            throw new InvalidUsageException("DictionaryService未实现,无法使用ExcelBindDict注解!");
        }
        List<LabelValue> list = bindDictService.getLabelValueList(dictType);
        return convertLabelValueListToMap(list);
    } else if (annotation instanceof ExcelBindField) {
        ExcelBindField bindField = (ExcelBindField) annotation;
        return executeBindField(bindField, nameList);
    } else {
        return Collections.emptyMap();
    }
}
Also used : ExcelBindDict(com.diboot.file.excel.annotation.ExcelBindDict) BindDict(com.diboot.core.binding.annotation.BindDict) DictionaryServiceExtProvider(com.diboot.core.service.DictionaryServiceExtProvider) LabelValue(com.diboot.core.vo.LabelValue) ExcelBindDict(com.diboot.file.excel.annotation.ExcelBindDict) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) ExcelBindField(com.diboot.file.excel.annotation.ExcelBindField)

Example 3 with ExcelBindField

use of com.diboot.file.excel.annotation.ExcelBindField in project diboot by dibo-software.

the class ExcelBindAnnoHandler method getField2BindAnnoMap.

/**
 * 获取字段-绑定注解之间的map
 * @param modelClass
 * @return
 */
public static Map<String, Annotation> getField2BindAnnoMap(Class modelClass) {
    String modelClassName = modelClass.getName();
    Map<String, Annotation> field2AnnoMap = MODEL_BINDANNO_CACHE.get(modelClassName);
    if (field2AnnoMap == null) {
        List<Field> fieldList = BeanUtils.extractAllFields(modelClass);
        for (Field field : fieldList) {
            ExcelBindDict excelBindDict = field.getAnnotation(ExcelBindDict.class);
            BindDict bindDict = field.getAnnotation(BindDict.class);
            if (excelBindDict != null) {
                if (field2AnnoMap == null) {
                    field2AnnoMap = new HashMap<>(8);
                }
                field2AnnoMap.put(field.getName(), excelBindDict);
            } else if (bindDict != null) {
                if (field2AnnoMap == null) {
                    field2AnnoMap = new HashMap<>(8);
                }
                field2AnnoMap.put(field.getName(), bindDict);
            }
            ExcelBindField bindField = field.getAnnotation(ExcelBindField.class);
            if (bindField != null) {
                if (field2AnnoMap == null) {
                    field2AnnoMap = new HashMap<>(8);
                }
                field2AnnoMap.put(field.getName(), bindField);
            }
        }
        if (field2AnnoMap == null) {
            field2AnnoMap = Collections.emptyMap();
        }
        MODEL_BINDANNO_CACHE.put(modelClassName, field2AnnoMap);
    }
    return field2AnnoMap;
}
Also used : Field(java.lang.reflect.Field) ExcelBindField(com.diboot.file.excel.annotation.ExcelBindField) ExcelBindDict(com.diboot.file.excel.annotation.ExcelBindDict) BindDict(com.diboot.core.binding.annotation.BindDict) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ExcelBindDict(com.diboot.file.excel.annotation.ExcelBindDict) Annotation(java.lang.annotation.Annotation) ExcelBindField(com.diboot.file.excel.annotation.ExcelBindField)

Aggregations

BindDict (com.diboot.core.binding.annotation.BindDict)3 ExcelBindDict (com.diboot.file.excel.annotation.ExcelBindDict)3 ExcelBindField (com.diboot.file.excel.annotation.ExcelBindField)3 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 InvalidUsageException (com.diboot.core.exception.InvalidUsageException)1 DictionaryServiceExtProvider (com.diboot.core.service.DictionaryServiceExtProvider)1 LabelValue (com.diboot.core.vo.LabelValue)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1