سؤال

I've two classes I need to map (one way at the moment).

class Order1 {
   int number;
   String status;
}

class Order2 {
    int number;
    boolean canceled;
    boolean confirmed;
}

I would like to do something like:

mapperFactory.classMap(Order1.class, Order2.class)
    .map("status == 'Canceled'", "canceled")
    .map("status == 'Confirmed'", "confirmed")
    .byDefault().register();

Is there a way to do something like this?

Edit: I tried using a CustomMapper like this:

.customize(new CustomMapper<Order1, Order1>() {
    @Override
    public void mapAtoB(final Order1 a, final Order1 b, final MappingContext context) {
        String status = a.getStatus();
        b.setCanceled("Canceled".equals(status));
        b.setConfirmed("Confirmed".equals(status));
    }
})

This works, but it doesn't seem possible to add many customized mappers for the same pair of classes. Instead, I used custom converters as Sidi's answer explains.

Thanks.

هل كانت مفيدة؟

المحلول

Can you just add public boolean isCanceled() and boolean isConfirmed() to class Order1 ? Orika will auto map it

Or

Use you can create a converter StatusConverter taking String parameter, register it

converterFactory.registerConverter("canceled", new StatusConverter("Canceled"));
converterFactory.registerConverter("confirmed", new StatusConverter("Confirmed"));

Then

mapperFactory.classMap(Order1.class, Order2.class)
    .fieldMap("status", "canceled").converter("canceled").add()
    .fieldMap("status", "confirmed") .converter("confirmed").add()
    .byDefault().register();

this converter should convert String to Boolean, return true if the given parameter is equals to the value.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top