Search code examples
javaapacheodataolingoodata-v4

How to use String enumerations with Apache Olingo V4 Java API


I just can't figure out how to use the CsdlEnumType of Apaches Olingo V4 Java API during the data creation.

Here is what I've done so far with as less code as possible:

1) In my EdmODataProvider.java class I've created an entity type and added the FQDN of the enum entity to the properties. Furthermore I've instantiated the CsdlEnumType in the schema provider class. I guess this works, because if I'm using only numbers in the setValue() part I'm getting the expected results. :

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) In my data provider class I'm creating an entity like this:

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"));

If I'm trying to call the entity I'm finally receiving the error:

<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>

My $metadata contains:

<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>

I guess the problem is in Part 2) where I'm adding the attribute DISPNAME as String. Any idea how to fix this issue?


Solution

  • I don't know if you still facing this issue. But maybe my answer will be useful for someone. According to the OASIS OData Documentation:

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

    You simple cannot create an enum in Olingo, which has String underlying type. My suggestion is to skip .setValue("") part from your code and rely on a default values (0, 1, 2 etc.). Don't be afraid - you can operate on an enum value in your requests by providing either name or value. So just rely on the name.

    And in the code you can use valueOfString(name, null, null, null, null, null, Long) method from EdmEnumType.