3

I have created an enumeration:

public enum ROLECATEGORY {
    LOW ("Low Risk", 0),
    MEDIUM ("Medium Risk", 1),

    public final String attrname;
    public final int value;

    ROLECATEGORY(String attrname, int value) {
        this.attrname = attrname;
        this.value = value;
    }
    public static ROLECATEGORY valueOf(int val){
        switch(val){
        case 0: return LOW; 
        case 1: return MEDIUM;
        default: throw new IllegalArgumentException("blablabla");
        }
    }
    public int toInt() { return value; }
}

According to the starter tutorial I've created the normal ODataProvider Class. All I'm missing is a peace of code to get the enum as FQDN type for the property instantiation:

CsdlProperty p = new CsdlProperty().setName("MYENUM").setType( ?getEnumType("MYENUM")? )
Tim Malich
  • 1,301
  • 14
  • 22

1 Answers1

3

OK, I found a simple solution myself. But it's probably not the best one:

1.) I've added a new static FullQualifiedName:

public static final FullQualifiedName CET_ROLECAT = new FullQualifiedName(NAMESPACE, "RoleCategory");

2.) I've created the member getEnumType()

public CsdlEnumType getEnumType(final FullQualifiedName enmuTypeName){
    if (CET_ROLECAT.equals(enmuTypeName)) {
        return new CsdlEnumType()
            .setName(CET_ROLECAT.getName())
            .setMembers(Arrays.asList(
                    new CsdlEnumMember().setName("LOW").setValue("0"),
                    new CsdlEnumMember().setName("MEDIUM").setValue("1")
            ))
            .setUnderlyingType(EdmPrimitiveTypeKind.Int32.getFullQualifiedName())
        ;
    }
    return null;
}

3.) I've added the FQDN from 1.) to my Entity Property:

// ...    
CsdlProperty p = new CsdlProperty().setName("RoleCategory").setType(CET_ROLECAT));
//...

4.) Finally I've added the EnumType the my schema:

public List<CsdlSchema> getSchemas() throws ODataException {
    CsdlSchema schema = new CsdlSchema();
    // ...
    List<CsdlEnumType> enumTypes = new ArrayList<CsdlEnumType>();
    enumTypes.add(getEnumType(CET_ROLECAT));
    schema.setEnumTypes(enumTypes);
    // ...
    List<CsdlSchema> schemas = new ArrayList<CsdlSchema>();
    schemas.add(schema);
    return schemas;
}

FYI: 'NAMESPACE' is just a public static final String member in my EdmODataProvider class.

Unfortunately it was only possible for me to add Strings in the name and value parts in 2.) at the CsdlEnumMember. Neither I'm sure what's setUnderlyningType() for.

Tim Malich
  • 1,301
  • 14
  • 22
  • 1
    This is the correct way to achive this. Olingo has a lazy loading concept for the EDM which means that the EDMProvider will only be called if the object is needed for URI parsing and/or de-/serialization. This is why you need to set the types as Strings. The underlying type is specified here: http://docs.oasis-open.org/odata/odata/v4.0/errata02/os/complete/part3-csdl/odata-v4.0-errata02-os-part3-csdl-complete.html#_Toc406397994 – chrisam Apr 06 '16 at 13:27