I'm creating a poc for using Mapstruct in my future projects.
Now I have one question how to map custom methods to a special target.
For example I have following interface mapper:
@Mapper
public interface ItemMapper {
static ItemMapper INSTANCE = Mappers.getMapper(ItemMapper.class);
@Mappings({ @Mapping(source = "number", target = "itemnumber"),
@Mapping(source = "description", target = "description"),
@Mapping(source = "itemClass.name", target = "ic"), @Mapping(source = "optionPart", target = "option"),
@Mapping(source = "plannerCode.code", target = "plannercode"),
@Mapping(source = "plannerCode.name", target = "planner"),
@Mapping(source = "vendor.buyerCode.name", target = "buyer"),
@Mapping(source = "vendor.buyerCode.code", target = "buyerCode"),
@Mapping(source = "vendor.number", target = "vendor"),
@Mapping(source = "vendor.name", target = "vendorName"), @Mapping(source = "pcsItem", target = "pcs"),
@Mapping(source = "specialColourVariant", target = "specialColors"),
@Mapping(source = "qtyBufferGreen", target = "greenLine"),
@Mapping(source = "qtyBufferRed", target = "redine"), @Mapping(source = "leadtime", target = "leadTime"),
@Mapping(source = "qtyStock", target = "stockAC"),
@Mapping(source = "qtyStockSupplier", target = "stockSupplier"),
@Mapping(source = "qtyPurchaseOrder", target = "qtyPo"),
@Mapping(source = "qtyShopOrder", target = "qtySo"),
@Mapping(source = "qtyFirmPlannedOrder", target = "qtyFpo"),
@Mapping(source = "qtyForecast", target = "qtyForecast"),
@Mapping(source = "standardCost", target = "stdCost"),
@Mapping(source = "itemCategory.name", target = "category") })
ItemViewModel itemToDto(Item item);
default String locationToLocationDto(Item item) {
return item.getItemsOnDetailedLocations().iterator().next().getLocation().getLocation();
}
default double locationToBinType(Item item) {
return item.getItemsOnDetailedLocations().iterator().next().getBinType();
}
default double itemToLotsize(Item item) {
double lotSize = 0;
if (item.getLotsize() != null) {
lotSize = item.getLotsize();
} else if (item.getItemsOnDetailedLocations() != null && !item.getItemsOnDetailedLocations().isEmpty()) {
ItemsOnDetailedLocation location = item.getItemsOnDetailedLocations().iterator().next();
lotSize = location.getLotSize();
} else {
lotSize = 0.0;
}
return lotSize;
}
default double stockRails(Item item) {
double value = 0;
for (ItemsOnDetailedLocation detailedLocation : item.getItemsOnDetailedLocations()) {
if (detailedLocation.getId().getSource().equals("RAILS")) {
long lotSize2 = detailedLocation.getLotSize();
long binInStock = detailedLocation.getBinInStock();
if (binInStock != 0) {
value += lotSize2 * (binInStock - 0.5);
}
}
}
return value;
}
}
In the code you can see the mapping and some default methods with other mapping in it. How can I use those methods in the Mapstruct mappings so that mapstruct uses those methods to fillin values in the fields?