I'm trying to map following source classes to target class using MapStruct.
Target Classes :
public class Response {
private List<Customer> customer = new ArrayList<Customer>();
}
public class Customer {
private String customerId;
private List<Product> products = new ArrayList<Product>();
}
public class CustProduct {
private String CustProductId;
private String CustPdtName;
private List<productDetail> CustProductDetails = new ArrayList<productDetail>();
}
Source Classes :
public class UserList {
protected List<User> user;
}
public class User {
protected String userId;
protected List<String> productRefId; //List of products for that particular user
}
public class ProductList {
protected List<Product> product;
}
public class Product {
protected String productId; //Reference to productRefId
protected String productName;
protected List<Details> productDetails;
}
Mapper Interface :
List<Customer> mapUser(List<User> user);
@Mappings({
@Mapping(target = "customerId", source = "userId”),
@Mapping(target = "products", ignore = true)
})
Customer mapUser(User user);
@Mappings({
@Mapping(target = "CustProductId", source = "productId"),
@Mapping(target = "CustPdtName", source = "productName"),
@Mapping(target = "CustProductDetails", source = "productDetails")
})
CustProduct mapUser(Product product);
My problem is, I want to connect CustProduct with Customer For that, I tried AfterMapping like :
default void findProducts(User user, @MappingTarget Customer customer) {
List<String> productIds = user.getproductRefId();
List<CustProduct> custProducts = new ArrayList<>();
for(int i=0; i<productIds.size();i++){
CustProduct custProduct = new CustProduct();
custProduct.setCustProductId(productIds.get(i));
//Here I want set productName and productDetails to custProduct Object(Iterating through ProductList and get from Product)
custProducts.add(custProduct);
}
}
customer.setCustProducts(custProducts);
}
Can anyone please help to fill out the comment section above? Or is there any other option to map these objects?
Edited : I tried the below solution but the interface implementation class itself changed.
You need to use @Context annotation to bring ProductList object into the context.
Change the mapper method to below definition and pass ProductList object when calling mapUser
:
@Mappings({
@Mapping(target = "customerId", source = "paxJourneyType.paxJourneyID”),
@Mapping(target = "products", ignore = true)
})
Customer mapUser(User user, @Context ProductList productList);
and then you can use the same ProductList object in @AfterMapping
method :
default void findProducts(User user, @Context ProductList productList @MappingTarget Customer customer) {
List<String> productIds = user.getproductRefId();
List<CustProduct> custProducts = new ArrayList<>();
for(int i=0; i<productIds.size();i++){
CustProduct custProduct = new CustProduct();
custProduct.setCustProductId(productIds.get(i));
Product product = getProduct(ProductList productList,productIds.get(i));
custProduct.setCustPdtName(product.getProductName);
custProducts.add(custProduct);
}
}
customer.setCustProducts(custProducts);
}
private Product getProduct(ProductList productList,String productId){
//Iterate through ProductList and get from Product
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With