2016-03-18 65 views
1

我只是無法弄清楚在數據創建過程中如何使用Apaches Olingo V4 Java API的CsdlEnumType如何在Apache Olingo V4 Java API中使用字符串枚舉

以下是我已經與爲更少的代碼儘可能迄今所做的:

1)在我EdmODataProvider.java類我創建了一個實體類型,並添加枚舉實體的FQDN的性能。此外,我已經在模式提供者類中實例化了CsdlEnumType。我猜這是有效的,因爲如果我只使用setValue()部分中的數字,我會得到預期結果。 :

public CsdlEntityType getEntityType(FullQualifiedName entityTypeName) throws ODataException { 
    CsdlEntityType entityType = new CsdlEntityType(); 
    List<CsdlProperty> properties = new ArrayList<CsdlProperty>(); 
    properties.add(new CsdlProperty().setName("Attributes").setType(new FullQualifiedName("Namespace", "Attributes"))); 
    entityType.setName("Langs").setProperties(properties); 
    return entityType; 
} 

public List<CsdlSchema> getSchemas() throws ODataException { 
    CsdlSchema schema = new CsdlSchema(); 
    schema.setNamespace("Namespace"); 
    List<CsdlEnumType> enumTypes = new ArrayList<CsdlEnumType>(); 
    enumTypes.add(
     new CsdlEnumType() 
      .setName("LangAttributes") 
      .setMembers(Arrays.asList(
       // if I use setValue("0") ... setValue("1") everything works fine 
       new CsdlEnumMember().setName("DISPNAME").setValue("DISPNAME"), 
       new CsdlEnumMember().setName("DESC").setValue("DESC") 
     )) 
    ) 
    // ... add entity type and set the other stuff 
} 

2)在我的數據提供程序類我創建一個這樣的實體:

Entity e = new Entity(); 
// again: it would work if I would use 0 instead of "DISPNAME" here 
e.addProperty(new Property(null, "LangAttributes", ValueType.Enum, "DISPNAME")); 

如果我想打電話給我終於收到錯誤的實體:

<error xmlns="http://docs.oasis-open.org/odata/ns/metadata"> 
<code>400</code> 
<message>The value 'DISPNAME' is not valid for property 'LangAttributes'.</message> 
</error> 

$metadata包含:

<EnumType Name="Attribute" IsFlags="false" UnderlyingType="Edm.Int32"> 
    <Member Name="DISPNAME" Value="DISPNAME"/> 
    <Member Name="DESC" Value="DESC"/> 
</EnumType> 
.... 
<EntityType Name="Attributes"> 
    <Property Name="LangAttributes" Type="Namespace.Attribute"/> 
</EntityType> 

我想問題是在第2部分)我在那裏添加屬性DISPNAME作爲字符串。任何想法如何解決這個問題?

回答

1

我不知道你是否仍然面臨這個問題。但是也許我的答案對某個人有用。按照OASIS OData Documentation

Enumeration types are named primitive types whose values are named constants with underlying integer values.

您簡單的不能創建在Olingo一個枚舉,其中有字符串基本類型。我的建議是從代碼中跳過.setValue("")部分,並依靠默認值(0,1,2等)。不要害怕 - 您可以通過提供名稱或值來對請求中的枚舉值進行操作。所以只是依靠名稱

而在代碼中,您可以使用valueOfString(name, null, null, null, null, null, Long)方法從EdmEnumType