Dozer String to enum mapping

madhead picture madhead · Nov 25, 2011 · Viewed 21.8k times · Source

I have such enum:

public enum PartnershipIndicator {
    VENDOR("VENDOR"), COPARTNER("COPARTNER"), BUYER("BUYER");

    String code;

    private PartnershipIndicator(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public static PartnershipIndicator valueOfCode(String code) {
        for (PartnershipIndicator status : values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        throw new IllegalArgumentException(
            "Partnership status cannot be resolved for code " + code);
    }

    @Override
    public String toString() {
        return code;
    }
}

I need to convert it to String and vice versa. Now, it is done by custom converter. But i want to do it via dozer mappings (if it is possible). If i do not write any mappings to the dozer confing, i get

org.dozer.MappingException: java.lang.NoSuchMethodException: by.dev.madhead.demo.test_java.model.PartnershipIndicator.<init>()

exception. I cannot add default public constructor to enum, as it is not possible. So, i wrote a trick with internal code and valueOfCode() / toString(). It does not work. Then, i've mapped it in dozer config:

<mapping>
    <class-a>java.lang.String</class-a>
    <class-b create-method="valueOfCode">by.dev.madhead.demo.test_java.model.PartnershipIndicator</class-b>
</mapping>

It does not work. I tried valueOfCode(), one-way mappings. Nothing works. Enum to String conversion does not work too, i get empty Strings. Any ideas?

Answer

Christopher picture Christopher · May 15, 2012

Not sure if this is still an issue, but maybe help for anyone searching. But here is implemented solution to this:

@Override
public Object convert(Object destination, Object source, Class<?> destinationClass,    Class<?> sourceClass) {
    if(source == null)
        return null;
    if(destinationClass != null){
        if(destinationClass.getSimpleName().equalsIgnoreCase("String")){
            return this.getString(source);
        }else if( destinationClass.isEnum()){

            return this.getEnum(destinationClass, source);

        }else{
            throw new MappingException(new StrBuilder("Converter ").append(this.getClass().getSimpleName())
                       .append(" was used incorrectly. Arguments were: ")
                       .append(destinationClass.getClass().getName())
                       .append(" and ")
                       .append(source).toString());
        }
    }
    return null;
}

private Object getString(Object object){
    String value = object.toString();
    return value;
}
private Object getEnum(Class<?> destinationClass, Object source){
    Object enumeration = null;

    Method [] ms = destinationClass.getMethods();
    for(Method m : ms){
        if(m.getName().equalsIgnoreCase("valueOf")){
            try {
                enumeration = m.invoke( destinationClass.getClass(), (String)source);
            }
            catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return enumeration;
        }
    }
    return null;
}

The StrBuilder class when building the exception message is from the apaches common-lang libs. But other than that a simple reflection to solve this issue. Just add to a class that implements CustomConverter and then in your dozer mapping xml file add the following configuration:

<configuration>
    <custom-converters>
        <converter type="com.yourcompany.manager.utils.dozer.converters.EnumStringBiDirectionalDozerConverter">
            <class-a>java.lang.Enum</class-a>
            <class-b>java.lang.String</class-b>
        </converter>
    </custom-converters>
</configuration>

Note that you can only list a configuration once between all of your mapping files (if you have multiple) otherwise dozer will complain. What I typically do is place my custom converter configurations in one file for simplicity. Hope this helps!