如何在 olingo 中创建复杂类型集合

goo*_*ier 3 odata olingo

我尝试按照 olingo 的以下文档创建 odata 服务。

注释处理器

但是我无法创建一个具有类型为 ComplexType 列表的属性的实体。任何人都有这样的例子。或者只是不支持?

Tar*_*hut 6

也许当时Utsav 的回答是正确的,但 Odata v4 确实支持具有类型为 " List of ComplexType"属性的实体。
证明:这个示例中, entityType Person有属性AddressInfo,它的类型是 ComplexType 的集合:

<ComplexType Name="Location" OpenType="true">
  <Property Name="Address" Type="Edm.String" Nullable="false"/>
  <Property Name="City" Type="Microsoft.OData.SampleService.Models.TripPin.City" Nullable="false"/>
</ComplexType>
...
<EntityType Name="Person" OpenType="true">
  <Property Name="AddressInfo" Type="Collection(Microsoft.OData.SampleService.Models.TripPin.Location)"/>
...
</EntityType>
Run Code Online (Sandbox Code Playgroud)

关于 Olingo 实现,在您的 CSDL 提供程序中,您必须正确定义您的复杂类型实体,然后定义您希望成为此复杂类型集合的属性。然后你必须正确处理结果。

1. 在 CSDL provider 中定义元数据

在您的提供者的getSchemas()方法中,您必须声明您的复杂类型:

    List <CsdlComplexType> complexTypes = new ArrayList<>();
    //...initialization of complexTypes list...
    schema.setComplexTypes(complexTypes);
Run Code Online (Sandbox Code Playgroud)

getEntityType()方法中,您必须将属性创建为复杂类型对象的集合:

    //...initialization of entityType...
    List<CsdlProperty> properties = new ArrayList<>();
    FullQualifiedName type;
    //...initialization of the type as a complex type...
    properties.add(new  CsdlProperty().setName("propertyName").setType(type).setCollection(true));
    entityType.setProperties(properties);
    //...
Run Code Online (Sandbox Code Playgroud)

2.处理结果

在您的处理器的实现中,您必须正确构建您的实体:例如,在实现readEntityCollection()方法中,EntityCollectionProcessor您应该有类似的东西:

    EntityCollection entities = new EntityCollection();
    List<Entity> eList = entities.getEntities();
    Entity e = new Entity();
    List<Map> data;
    //...initialization of complex type's data...
    List<ComplexValue> properties = new ArrayList<>();
    for (Object complexObject : data) {
        ComplexValue complexValue = new ComplexValue();
        for (Map.Entry<String, Object> entry : complexObject.entrySet()) {
                complexValue.getValue().add(new Property(null, entry.getKey(), ValueType.PRIMITIVE, entry.getValue()));
            }
        properties.add(complexValue);
    }
    e.addProperty(new Property(null, "propertyName", ValueType.COLLECTION_COMPLEX, properties););
    eList.add(e);

//...serialize and set the result to response
Run Code Online (Sandbox Code Playgroud)