Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an enum entity type in Olingo OData V4 java API

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")? )
like image 344
Tim Malich Avatar asked Feb 25 '26 01:02

Tim Malich


1 Answers

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.

like image 107
Tim Malich Avatar answered Feb 27 '26 15:02

Tim Malich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!