[ 
https://issues.apache.org/jira/browse/OLINGO-811?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14987852#comment-14987852
 ] 

Riccardo Mariani edited comment on OLINGO-811 at 11/4/15 11:54 AM:
-------------------------------------------------------------------

The following solution should solve both the problems:
- $expand=NavigationPropertySet($count=true)
- $expand=NavigationPropertySet/$count

{code:title=ODataJsonSerializer.java|borderStyle=solid}
                        @Override
        protected void writeNavigationProperties(final ServiceMetadata 
metadata, final EdmStructuredType type,
                        final Linked linked, final ExpandOption expand, final 
JsonGenerator json)
                                        throws SerializerException, IOException 
{
                if (ExpandSelectHelper.hasExpand(expand)) {
                        final boolean expandAll = 
ExpandSelectHelper.isExpandAll(expand);
                        final Set<String> expanded = expandAll ? new 
HashSet<String>()
                                        : 
ExpandSelectHelper.getExpandedPropertyNames(expand.getExpandItems());
                        for (final String propertyName : 
type.getNavigationPropertyNames()) {
                                if (expandAll || 
expanded.contains(propertyName)) {
                                        final EdmNavigationProperty property = 
type.getNavigationProperty(propertyName);
                                        final Link navigationLink = 
linked.getNavigationLink(property.getName());
                                        final ExpandItem innerOptions = 
expandAll ? null
                                                        : 
ExpandSelectHelper.getExpandItem(expand.getExpandItems(), propertyName);
                                        if (innerOptions != null && 
innerOptions.getLevelsOption() != null) {
                                                throw new 
SerializerException("Expand option $levels is not supported.",
                                                                
SerializerException.MessageKeys.NOT_IMPLEMENTED);
                                        }

                                        boolean isNavigationPropertyCountOnly = 
false;
                                        if (innerOptions != null) {
                                                final UriInfoResource 
uriInfoResource = innerOptions.getResourcePath();
                                                final List<UriResource> 
uriResourceParts = uriInfoResource.getUriResourceParts();
                                                if (uriResourceParts.size() == 
2 && uriResourceParts.get(0) instanceof UriResourceNavigation
                                                                && 
uriResourceParts.get(1) instanceof UriResourceCount) {
                                                        
isNavigationPropertyCountOnly = true;
                                                }
                                        }

                                        if (isNavigationPropertyCountOnly) {
                                                final LinkCustom linkCustom = 
(LinkCustom) navigationLink;
                                                
writeNavigationPropertyCount(property, linkCustom.getInlineCount(), json);
                                        } else {
                                                if (innerOptions != null && 
innerOptions.getCountOption() != null
                                                                && 
innerOptions.getCountOption().getValue()) {
                                                        
writeNavigationPropertyCount(property, 
navigationLink.getInlineEntitySet().getCount(),
                                                                        json);
                                                }

                                                
writeExpandedNavigationProperty(metadata, property, navigationLink,
                                                                innerOptions == 
null ? null : innerOptions.getExpandOption(),
                                                                innerOptions == 
null ? null : innerOptions.getSelectOption(), json);
                                        }
                                }
                        }
                }
        }

        private void writeNavigationPropertyCount(final EdmNavigationProperty 
property, final int count,
                        final JsonGenerator json) throws IOException {
                if (isIEEE754Compatible) {
                        json.writeStringField(property.getName() + 
Constants.JSON_COUNT, String.valueOf(count));
                } else {
                        json.writeNumberField(property.getName() + 
Constants.JSON_COUNT, count);
                }
        }
{code}


was (Author: riccardomariani):
A possible solution:
{code:title=ODataJsonSerializer.java|borderStyle=solid}
                @Override
        protected void writeNavigationProperties(final ServiceMetadata 
metadata, final EdmStructuredType type,
                        final Linked linked, final ExpandOption expand, final 
JsonGenerator json)
                                        throws SerializerException, IOException 
{
                if (ExpandSelectHelper.hasExpand(expand)) {
                        final boolean expandAll = 
ExpandSelectHelper.isExpandAll(expand);
                        final Set<String> expanded = expandAll ? new 
HashSet<String>()
                                        : 
ExpandSelectHelper.getExpandedPropertyNames(expand.getExpandItems());
                        for (final String propertyName : 
type.getNavigationPropertyNames()) {
                                if (expandAll || 
expanded.contains(propertyName)) {
                                        final EdmNavigationProperty property = 
type.getNavigationProperty(propertyName);
                                        final Link navigationLink = 
linked.getNavigationLink(property.getName());
                                        final ExpandItem innerOptions = 
expandAll ? null
                                                        : 
ExpandSelectHelper.getExpandItem(expand.getExpandItems(), propertyName);
                                        if (innerOptions != null && 
innerOptions.getLevelsOption() != null) {
                                                throw new 
SerializerException("Expand option $levels is not supported.",
                                                                
SerializerException.MessageKeys.NOT_IMPLEMENTED);
                                        }

                                        if (innerOptions != null && 
innerOptions.getCountOption() != null
                                                        && 
innerOptions.getCountOption().getValue()) {
                                                
writeNavigationPropertyCount(property, navigationLink.getInlineEntitySet(), 
json);
                                        }

                                        
writeExpandedNavigationProperty(metadata, property, navigationLink,
                                                        innerOptions == null ? 
null : innerOptions.getExpandOption(),
                                                        innerOptions == null ? 
null : innerOptions.getSelectOption(), json);
                                }
                        }
                }
        }

        private void writeNavigationPropertyCount(final EdmNavigationProperty 
property,
                        final EntityCollection entityCollection, final 
JsonGenerator json) throws IOException {
                if (isIEEE754Compatible) {
                        json.writeStringField(property.getName() + 
Constants.JSON_COUNT, entityCollection.getCount().toString());
                } else {
                        json.writeNumberField(property.getName() + 
Constants.JSON_COUNT, entityCollection.getCount());
                }
        }
{code}

> Use of $count for expanded items does not work
> ----------------------------------------------
>
>                 Key: OLINGO-811
>                 URL: https://issues.apache.org/jira/browse/OLINGO-811
>             Project: Olingo
>          Issue Type: Bug
>          Components: odata4-server
>    Affects Versions: (Java) V4 4.0.0
>            Reporter: Michael Bolz
>
> The {{$count}} query option within the {{$expand}} clause does not work 
> correctly (see in 
> [Specification|http://docs.oasis-open.org/odata/odata/v4.0/errata02/os/complete/part1-protocol/odata-v4.0-errata02-os-part1-protocol-complete.html#_The_$inlinecount_System]
> Example URL (for *TecSvc*):
> http://localhost:8080/odata.svc/ESAllPrim/?$expand=NavPropertyETTwoPrimMany($count=true)&$format=json&$count=true
> And example for OData-TripPin Service:
> [http://services.odata.org/V4/(S(kb3m31znaqv0xd4oqq0esxfv))/TripPinServiceRW/People?$expand=Trips($count=true)]



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to