1

I want to use MapStruct framework and to extend the Java Class that I map. Currently I use this:

// @Mapper(config = BaseMapperConfig.class)
public interface MerchantsMapper {

    MerchantNewDTO toNewDTO(Merchants merchant);
}

Custom implementation:

public MerchantNewDTO toNewDTO(Merchants merchant)
  {
    MerchantNewDTO merchantNewDTO = new MerchantNewDTO();

    merchantNewDTO.setId(Integer.valueOf(merchant.getId()));
    ......

    MerchantConfigurationUtils merchant_config = new MerchantConfigurationUtils();
    Map<MerchantConfigurationFeatureBitString, Boolean> features = merchant_config.initFromDatabaseValue(merchant.getSupported_features());

    merchantNewDTO.setSupports_api(features.get(MerchantConfigurationFeatureBitString.Supports_api));

    return merchantNewDTO;
  }

As you can see I want to get getSupported_features and to populate the Supports_api value.

But it's very painful process to add new values. Is there some way to create adapter which extends the mapped interface and set/get the values?

Can you recommend some solution?

2
  • can you show us the classes please? Commented Aug 9, 2019 at 17:06
  • MerchantNewDTO and Merchants? Commented Aug 9, 2019 at 19:00

1 Answer 1

1

You could do this with @AfterMapping or @BeforeMapping.

@Mapper
public interface MerchantsMapper {

    @Mapping(target = "supports_api", ignore = "true")
    MerchantNewDTO toNewDTO(Merchant merchant);

    @AfterMapping
    default applyFeatures(@MappingTarget MerchatNewDTO merchantNewDTO, Merchant merchant) {

        MerchantConfigurationUtils merchant_config = new MerchantConfigurationUtils();
        Map<MerchantConfigurationFeatureBitString, Boolean> features = merchant_config.initFromDatabaseValue(merchant.getSupported_features());

        merchatNewDTO.setSupports_api(features.get(MerchantConfigurationFeatureBitString.Supports_api));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.