Petar Tahchiev
Petar Tahchiev

Reputation: 4386

Instruct orika when should map

Is there a way to specifically tell Orika when to map a given item? For example I want Orika to map my items only if A.getLastModifiedTime() > B.getLastModifiedTime. Is there a way to configure this somehow?

Upvotes: 2

Views: 846

Answers (2)

Vilius
Vilius

Reputation: 77

This should do the trick:

mapperFactory.classMap(A.class, B.class)
        .customize(new CustomMapper<A,B>() { 
                if(A.getLastModifiedTime() > B.getLastModifiedTime()){
                        A.getLastModifiedTime() == B.getLastModifiedTime();
                }
         }).register();

Upvotes: 1

Manolo Santos
Manolo Santos

Reputation: 1913

You can use Orika filters in order to map or not a field depending on a runtime condition. The easiest way is by means of extending Orika's NullFilter.

class MyFilter<A,B> extends NullFilter<A,B> {

    private myCondition(S source, D dest) {
      ...
    }

    public <S extends A, D extends B> boolean shouldMap(final Type<S> sourceType, final String sourceName, final S source, final Type<D> destType, final String destName,
                                                        final MappingContext mappingContext) {
        if (destName.equals("lastModifiedTime") && myCondition(source, dest)) {
          return false;
        } 

        return true;    
    }
}

Upvotes: 0

Related Questions