Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map multiple source fields to same type target fields with Mapstruct

Consider the following POJOs:

public class SchedulePayload {
    public String name;
    public String scheduler;
    public PeriodPayload notificationPeriod;
    public PeriodPayload schedulePeriod;
}

private class Lecture {
    public ZonedDateTime start;
    public ZonedDateTime end;
}

public class XmlSchedule {
    public String scheduleName;
    public String schedulerName;
    public DateTime notificationFrom;
    public DateTime notificationTo;
    public DateTime scheduleFrom;
    public DateTime scheduleTo;
}

public class PeriodPayload {
    public DateTime start;
    public DateTime finish;
}

Using MapStruct, I created a mapper that maps XmlSchedule to a SchedulePayload. Due to "business" "logic", I need to constrain notificationPeriod and schedulePeriod to a Lecture's start and end field values. Here is what I've come up to, using another class:

@Mapper(imports = { NotificationPeriodHelper.class })
public interface ISchedulePayloadMapper
{
    @Mappings({
        @Mapping(target = "name", source = "scheduleName"),
        @Mapping(target = "scheduler", source = "schedulerName"),
        @Mapping(target = "notificationPeriod", expression = "java(NotificationPeriodHelper.getConstrainedPeriod(xmlSchedule, notificationFrom, notificationTo))"),
        @Mapping(target = "schedulePeriod", expression = "java(NotificationPeriodHelper.getConstrainedPeriod(xmlSchedule, scheduleFrom, scheduleTo))")
    })
    SchedulePayload map(XmlSchedule xmlSchedule, Lecture lecture);

}

Is there any way this can be achieved in another way (i.e. another mapper, decorators, etc.)? How can I pass multiple values (xmlSchedule, lecture) to a mapper?

like image 661
giannoug Avatar asked Jun 08 '16 14:06

giannoug


1 Answers

What you can do is create an @AfterMapping method to populate those parts manually:

@Mapper
public abstract class SchedulePayloadMapper
{
    @Mappings({
        @Mapping(target = "name", source = "scheduleName"),
        @Mapping(target = "scheduler", source = "schedulerName"),
        @Mapping(target = "notificationPeriod", expression = "java(NotificationPeriodHelper.getConstrainedPeriod(xmlSchedule, notificationFrom, notificationTo))"),
        @Mapping(target = "schedulePeriod", expression = "java(NotificationPeriodHelper.getConstrainedPeriod(xmlSchedule, scheduleFrom, scheduleTo))")
    })
    public abstract SchedulePayload map(XmlSchedule xmlSchedule, Lecture lecture);

    @AfterMapping
    protected void addPeriods(@MappingTarget SchedulePayload result, XmlSchedule xmlSchedule, Lecture lecture) {
        result.setNotificationPeriod(..);
        result.setSchedulePeriod(..);
    }
}

Alternatively, you can place the @AfterMapping method in another class that is referenced in @Mapper(uses = ..) or you can use a Decorator (using the mechanisms MapStruct provides, or of your dependency injection framework if you use one).

like image 82
agudian Avatar answered Oct 11 '22 09:10

agudian