Orika - map two fields only if third field matches a string

java, mapping, orika

Solution

Yes it is possible. Simply add a custom mapper after regular field mapping.

    factory.classMap(ClazzA.class, ClazzB.class)
                .field("name", "sname")
                .byDefault()
                .customize(new CustomMapper<ClazzA, ClazzB>() {
                    @Override
                    public void mapBtoA(ClazzB clazzB, ClazzA clazzA, MappingContext context) {
                        if ("stk".equals(clazzB.getType())) {
                            clazzA.setName(clazzB.getSName());
                        }
                    }                   
                }
                .register(); 

Problem

I have two fields that I want to map using Orika ``` @Override public void configure(MapperFactory factory) { factory.classMap(ClazzA.class, ClazzB.class) .byDefault() .field("name", "sname") .register(); ``` I want to map that field name to the value from sname only if a third field "type" in ClazzB matches a string "stk". Is it possible using Orika?

Original source