简体   繁体   English

将自定义参数传递到推土机映射

[英]Passing custom parameter to Dozer mapping

I read in the documentation of custom converter that for a custom converter on a field mapping I can pass a custom parameter. 我在自定义转换器的文档中阅读到,对于字段映射上的自定义转换器,我可以传递自定义参数。 This is not good enough for me because this is specified once when building the mapper. 这对我来说还不够好,因为在构建映射器时只指定了一次。

Is there any way to pass this parameter when doing the actual mapping? 进行实际映射时,有什么方法可以传递此参数?

mapper.map(sourceObject, Destination.class, "parameter"); 

My actual problem is that I want to map from one class containing multi lingual properties and destination should only have the "choosen" language properties. 我的实际问题是,我想从一个包含多语言属性的类进行映射,而目的地应仅具有“选择的”语言属性。

Source class 源类

public class Source
{
   // Fields in default language
   private String prop1;
   private String prop2;

   // List containing all translations of properties
   private List<SourceName> sourceNames; 

}

public class SourceName
{
   private int lang_id;
   private String prop1;
   private String prop2;   
}

Destination class 目的地舱位

public class Destination
{
       // Fields translated in choosen language
       private String prop1;
       private String prop2;
}

My goal is to be able to do like this: 我的目标是能够做到这一点:

Destination destination = mapper.map(source, Destination.class, 4); // To lang_id 4

Thanks 谢谢

I have made this function (FIELDMAP var is "fieldMap"): 我做了这个函数(FIELDMAP var是“ fieldMap”):

public static <T> T mapWithParam(Object source, Class<T> destinationClass, String param) throws MappingException {

    T toReturn = null;
    DozerBeanMapper dbm = (DozerBeanMapper) MapperFactory.getMapper();
    MappingMetadata mmdt = dbm.getMappingMetadata();
    ClassMappingMetadata classMapping = mmdt.getClassMapping(source.getClass(), destinationClass);
    List<FieldMappingMetadata> fielMappingMetadata = classMapping.getFieldMappings();
    List<OriginalFieldMap> originalValues = new ArrayList<OriginalFieldMap>();
    for (FieldMappingMetadata fmmd : fielMappingMetadata) {
        if (fmmd.getCustomConverter() != null) {
            try {
                Class<?> cls = Class.forName(fmmd.getCustomConverter());
                if (cls.newInstance() instanceof ConfigurableCustomConverter) {
                    FieldMap modifieldFieldMap = (FieldMap)ReflectionHelper.executeGetMethod(fmmd, FIELDMAP);
                    originalValues.add(new OriginalFieldMap(modifieldFieldMap, modifieldFieldMap.getCustomConverterParam()));
                    modifieldFieldMap.setCustomConverterParam(param);
                    ReflectionHelper.executeSetMethod(fmmd, FIELDMAP, modifieldFieldMap);
                }
            } catch (ReflectionException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    toReturn = dbm.map(source, destinationClass);

    for (OriginalFieldMap ofp : originalValues) {
        ofp.getFieldMap().setCustomConverterParam(ofp.getOriginalValue());
    }
    return toReturn;
}

And OriginalFieldMap class: 和OriginalFieldMap类:

import org.dozer.fieldmap.FieldMap;

public class OriginalFieldMap{
    FieldMap fieldMap;
    String originalValue;

    public OriginalFieldMap(FieldMap fieldMap, String originalValue) {
        super();
        this.fieldMap = fieldMap;
        this.originalValue = originalValue;
    }
    public FieldMap getFieldMap() {
        return fieldMap;
    }
    public String getOriginalValue() {
        return originalValue;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM