Search code examples
javas4sdksap-cloud-sdk

Perform filter on expanded entity with SAP Cloud SDK


So this might be an easy one, but I somehow can't wrap my head around it. I am trying to find a business partner by its e-mail address using the SAP Cloud SDK.

This is what I have so far:

final List<BusinessPartner> businessPartners = new DefaultBusinessPartnerService()
                        .getAllBusinessPartner()
                        .select(BusinessPartner.BUSINESS_PARTNER,
                                BusinessPartner.TO_BUSINESS_PARTNER_ADDRESS
                                        .select(BusinessPartnerAddress.TO_EMAIL_ADDRESS
                                                .select(AddressEmailAddress.SEARCH_EMAIL_ADDRESS)))
                        .filter(AddressEmailAddress.SEARCH_EMAIL_ADDRESS.eq(email)) //something like this?
                        .top(10)
                        .execute();

Now, how can I perform a filter operation on the expanded entity AddressEmailAddress? The one I came up with obviously doesn't. I have troubles navigating to the expanded entity using the fluent API.

Any ideas?


Solution

  • Since my proposal worked for you. Let me rephrase the comment as answer.

    Unfortunately it's not possible to filter on expanded entities for SAP OData V2 service endpoints. A fallback strategy would be to directly query for the "filtered" item in the entity collection (leaf) and run additional OData requests to traverse to your original entity (root).

    In your case, I would recommend querying directly for the AddressEmailAddress, to resolve BusinessPartnerAddress by the AddressId. Next step is to resolve BusinessPartner by the businessPartner field, e.g.

    public List<BusinessPartner> getBusinessPartnersByEmail(
        @Nonnull BusinessPartnerService service,
        @Nonnull String email
    )
        throws ODataException
    {
        List<AddressEmailAddress> emailAddresses = service
            .getAllAddressEmailAddress()
            .filter(AddressEmailAddress.SEARCH_EMAIL_ADDRESS.eq(email))
            .execute();
    
        List<BusinessPartnerAddress> addresses = new LinkedList<>();
        for( AddressEmailAddress emailAddress : emailAddresses ) {
            addresses.addAll(
                service
                    .getAllBusinessPartnerAddress()
                    .filter(BusinessPartnerAddress.ADDRESS_ID.eq(emailAddress.getAddressID()))
                    .execute());
        }
    
        List<BusinessPartner> businessPartners = new LinkedList<>();
        for( BusinessPartnerAddress address : addresses ) {
            businessPartners.add(service.getBusinessPartnerByKey(address.getBusinessPartner()).execute());
        }
    
        return businessPartners;
    }