Odata客户端:资源集参考导航属性只能有一个属性注释' odata.bind'数组值

时间:2017-09-12 09:08:55

标签: java odata olingo

我正在使用Olingo java客户端api发布数据。以下是我要发布的XML结构。 http://services.odata.org/V4/(S(kfu42eshy4v0da0lifqbjdgc))/TripPinServiceRW

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    <People xmlns:edm="http://schemas.magicsoftware.com/edm" xmlns:ns1="http://schemas.magicsoftware.com/edm" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <row>
            <Fields>
                <UserName>8People_UserName</UserName>
                <FirstName>Pople_FirstName</FirstName>
                <LastName>People_LastName</LastName>
                <AddressInfo>
                    <Location>
                        <Address>Address1</Address>
                        <City>
                            <CountryRegion>IN</CountryRegion>
                            <Name>ContryRegion</Name>
                            <Region>REG</Region>
                        </City>
                    </Location>
                </AddressInfo>
            </Fields>
            <Expanded_Entities>
                <Friends>
                    <row>
                        <Fields>
                            <UserName>Rajesh</UserName>
                            <FirstName>V</FirstName>
                            <LastName>Patil</LastName>
                        </Fields>
                    </row>
                </Friends>
            </Expanded_Entities>
        </row>
    </People>

我有Expanded_Entities的问题我收到错误&#34;资源集参考导航属性&#39;朋友&#39;有一个属性注释&#39; odata.bind&#39;带字符串值。资源集引用导航属性只能有一个属性注释&#39; odata.bind&#39;使用数组值&#34;

以下代码片段

private  void buildExpended(ArrayList<EntitySimpleData> xpiEntity) throws Exception {
        ArrayList<EntitySimpleData> expended = getElement(xpiEntity, EXPENDED);
        /*
         * running on first level properties
         */
        if (expended != null){
            String originalEntityName = this.entityName;
            ClientEntity expendedEntity = null;
            for (EntitySimpleData entitySimpleData : expended) {
                URI uri = URI.create(entitySimpleData.getName());
                this.entityName = entitySimpleData.getName();
                ClientLinkType type = ClientLinkType.ENTITY_NAVIGATION;
                String navigationName = entitySimpleData.getName();
                ArrayList<EntitySimpleData> rows = (ArrayList<EntitySimpleData>) entitySimpleData.getData(); // the linked entity
                EdmElement edmElement = EDMManager.getEdmElementByName(resourceName, namespace, originalEntityName, navigationName);
                if (!edmElement.isCollection()){
                    expendedEntity =  client.getObjectFactory().newEntity(new FullQualifiedName(namespace, entitySimpleData.getName()));
                    EntitySimpleData row = rows.get(0); //"row"
                    ArrayList<EntitySimpleData> fields = (ArrayList<EntitySimpleData>) row.getData();// Field
                    EntitySimpleData field = fields.get(0); //"Field"
                    ArrayList<EntitySimpleData> properties = (ArrayList<EntitySimpleData>) field.getData();// properties

                    for (EntitySimpleData entitySimpleData2 : properties) {
                        expendedEntity.getProperties().addAll( buildIt(entitySimpleData2,""));  
                    }
                    uri = null;
                    ClientInlineEntity clientLink = new ClientInlineEntity(uri, type, navigationName, expendedEntity);
                    clientEntity.getNavigationLinks().add(clientLink);
                }else{// multy

                    ClientEntitySet entitySet = client.getObjectFactory().newEntitySet();
                    for (EntitySimpleData row : rows) {
                    this.entityName = edmElement.getType().getName();

                        expendedEntity = client.getObjectFactory().newEntity(new FullQualifiedName(namespace, this.entityName));
                ArrayList<EntitySimpleData> fields = (ArrayList<EntitySimpleData>) row.getData();// Field
                        EntitySimpleData field = fields.get(0); //"Field"
                        ArrayList<EntitySimpleData> properties = (ArrayList<EntitySimpleData>) field.getData();// properties

                        for (EntitySimpleData entitySimpleData2 : properties) {
                            expendedEntity.getProperties().addAll( buildIt(entitySimpleData2,""));  

                        }
                    //  uri = null;

                    }
                entitySet.getEntities().add(expendedEntity);
                ClientInlineEntitySet clientLinks = new ClientInlineEntitySet(uri,  type, navigationName,  entitySet);
            clientEntity.getNavigationLinks().add(clientLinks);
            }
            }
        }
    }

/ *      *每个属性构建自己,如果它复杂,建立所有孩子的递归      * /

private  ArrayList<ClientProperty> buildIt( EntitySimpleData data,String parentComplex) throws Exception{
        ArrayList<ClientProperty> props = new ArrayList<ClientProperty>();
        String fullName = (parentComplex.equals("")?data.getName():parentComplex+EDMManager.COMPLEX_DELIMITED+data.getName());
        EdmElement edmElement = EDMManager.getEdmElementByName(resourceName, namespace, entityName, fullName);
        EdmPrimitiveTypeKind edmType=null;
//      if (edmElement == null)
//          throw new SDKException (ODATAstepAdaptor.ERR_INFRASTRUCTURE, "EDM element not found in resource. resourceName["+resourceName+"] namespace["+namespace+"] entityName["+entityName+"] fullName["+fullName+"]");
        if (edmElement != null)
             edmType  = (EdmPrimitiveTypeKind) EDMManager.getODataElementType(edmElement);
        if(data.getElementGeneralType().equals(EntitySimpleData.Type.Simple)){
            props.add(buildProperty(edmType,data));
        }

        else if(data.getElementGeneralType().equals(EntitySimpleData.Type.Complex )){                                                           //complex
            boolean isCollection = false;
            if (edmElement != null)
                isCollection = edmElement.isCollection();
            if (edmType!=null){

                if (isCollection)
                {
                    collectionAddressInfo=client.getObjectFactory().newCollectionValue(edmElement.getType().getFullQualifiedName().getFullQualifiedNameAsString());
                    props.add(client.getObjectFactory().newCollectionProperty(data.getName(), collectionAddressInfo));

                    ArrayList<EntitySimpleData> childrens = (ArrayList<EntitySimpleData>) data.getData();
                    for (EntitySimpleData entitySimpleData : childrens) {
                        String parentName = (parentComplex.equals("")?data.getName():parentComplex+EDMManager.COMPLEX_DELIMITED+data.getName());
                        ArrayList<ClientProperty>complexProps= buildIt(entitySimpleData,parentName);
                        for (ClientProperty clientProperty : complexProps) {
                            if(clientProperty.hasComplexValue())
                            {
                            collectionAddressInfo.add(clientProperty.getComplexValue());
                            }
                            else if(clientProperty.hasPrimitiveValue()){
                                collectionAddressInfo.add(clientProperty.getPrimitiveValue());
                            }
                            else if(clientProperty.hasCollectionValue()){
                                collectionAddressInfo.add(clientProperty.getCollectionValue());
                            }

                        }


                        }


                }
                else

                // point
                props.add(EDMGeoHelper.buildPointProperty(client,edmType,data));
            }
            else{       // regular complex

                @SuppressWarnings("unchecked")
                ArrayList<EntitySimpleData> childrens = (ArrayList<EntitySimpleData>) data.getData();
                ClientComplexValue  complex = client.getObjectFactory().newComplexValue(namespace+"."+data.getName()); 
                props.add(client.getObjectFactory().newComplexProperty(data.getName(), complex));
                for (EntitySimpleData entitySimpleData : childrens) {
                    String parentName = (parentComplex.equals("")?data.getName():parentComplex+EDMManager.COMPLEX_DELIMITED+data.getName());
                    ArrayList<ClientProperty>complexProps= buildIt(entitySimpleData,parentName);
                    for (ClientProperty clientProperty : complexProps) {
                        complex.add(clientProperty);
                    }

                }
            }
        }
        else{// Unknown
            Logger.logMessage(LogLevel.LEVEL_WARN, ODATAstepAdaptor.ODATA_CLIENT_LOG_MODULE, "Element ["+fullName+"] ignored. null value");
        }

        return props;
    }

/ **      *      * @param resourceName      * @param schemaNamespace      * @param entityName      * @param fieldName      * @param isCollection - 如果属性是集合则返回      * @return      * /

public static EdmElement getEdmElementByName(String resourceName, String schemaNamespace, 
            String entityName, String fieldName) {

        // Get current schema by namespace
        ODataSchemaImpl schema = odataResourcesMap.get(resourceName).get(schemaNamespace);

        String[] fieldHierachy = fieldName.split(COMPLEX_DELIMITED);

        EdmStructuredType edmStructured = null;

        // Check if is it complex property
        if (fieldHierachy.length > 1) 
            edmStructured = schema.getComplexTypes().get(fieldHierachy[fieldHierachy.length-2]);

        if(fieldHierachy.length == 1 || edmStructured  ==null )
            edmStructured = schema.getEntityTypes().get(entityName);    

        fieldName = fieldHierachy[fieldHierachy.length - 1];    

        if(edmStructured!=null)
          return edmStructured.getProperty(fieldName);  
        else
         return null;
    }

/ *      *在一个实体属性(EntitySimpleData)上运行,构建ClientEntity并将其发布到服务器。      * /

public void postEntity(String namespace, String entityName,EntitySimpleData data,String serviceName) throws Exception {
        ODataEntityCreateRequest<ClientEntity> req = null;
        ClientEntityBuilder helper = null;
        ClientEntity clientEntity = null;
        URI entityURI = null;

        try{
            Logger.logMessage(LogLevel.LEVEL_DEBUG, ODATAstepAdaptor.ODATA_CLIENT_LOG_MODULE, "postEntity. entityName ["+entityName+"] data["+data+"] serviceName [" + serviceName + "]");

            helper = new ClientEntityBuilder(data,client,namespace,SERVICE_ROOT,entityName);
            clientEntity = helper.buildClientEntityParameters();
            entityURI = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment(serviceName).expand("Friends").build();

            Logger.logMessage(LogLevel.LEVEL_DEBUG, ODATAstepAdaptor.ODATA_CLIENT_LOG_MODULE, "postEntity. entityName ["+entityName+"] data["+data+"] serviceName [" + serviceName + "] entityURI[ " + entityURI + "] clientEntity [" + clientEntity + "]");
            req = client.getCUDRequestFactory().getEntityCreateRequest(entityURI, clientEntity);
            setHeaders(req, headers);
            ODataEntityCreateResponse<ClientEntity> res = req.execute();
            clientEntity = res.getBody();
        }catch(Exception e){
            Logger.logMessage(LogLevel.LEVEL_ERROR, ODATAstepAdaptor.ODATA_CLIENT_LOG_MODULE, "postEntity. entityName ["+entityName+"] data["+data+"] serviceName [" + serviceName + "] entityURI [ " + entityURI + "] clientEntity [" + clientEntity + "]",e,1);
            throw(e);
        }
    }

ODataEntityCreateResponse<ClientEntity> res = req.execute(); this line I got 
above error 

0 个答案:

没有答案