From ba6f8a6f504f504bfe8cb42245c4577fcdedad94 Mon Sep 17 00:00:00 2001 From: michaelpede Date: Wed, 12 May 2021 16:06:59 -0700 Subject: [PATCH] Adding Annotations to Enum members --- .../reso/service/data/meta/EnumFieldInfo.java | 9 ++-- .../reso/service/data/meta/EnumValueInfo.java | 37 ++++++++++++++++ .../service/edmprovider/RESOedmProvider.java | 42 +++++++++++-------- 3 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/reso/service/data/meta/EnumValueInfo.java diff --git a/src/main/java/org/reso/service/data/meta/EnumFieldInfo.java b/src/main/java/org/reso/service/data/meta/EnumFieldInfo.java index a45a034..12f0597 100644 --- a/src/main/java/org/reso/service/data/meta/EnumFieldInfo.java +++ b/src/main/java/org/reso/service/data/meta/EnumFieldInfo.java @@ -20,7 +20,7 @@ import static org.reso.service.servlet.RESOservlet.getConnection; public class EnumFieldInfo extends FieldInfo { private String lookupName; - private ArrayList values = new ArrayList<>(); + private ArrayList values = new ArrayList<>(); private static final Logger LOG = LoggerFactory.getLogger(EnumFieldInfo.class); private boolean isCollection = false; @@ -31,7 +31,7 @@ public class EnumFieldInfo extends FieldInfo super(fieldName, type); } - public void addValue(String value) + public void addValue(EnumValueInfo value) { values.add(value); } @@ -57,7 +57,8 @@ public class EnumFieldInfo extends FieldInfo { Entity ent = CommonDataProcessing.getEntityFromRow(resultSet, resource, selectLookup); Property property = ent.getProperty(LOOKUP_COLUMN_NAME); - values.add( property.getValue().toString() ); + String val = property.getValue().toString(); + values.add( new EnumValueInfo(val) ); } } catch (Exception e) @@ -68,7 +69,7 @@ public class EnumFieldInfo extends FieldInfo } } - public ArrayList getValues() + public ArrayList getValues() { if (values.size()==0) { diff --git a/src/main/java/org/reso/service/data/meta/EnumValueInfo.java b/src/main/java/org/reso/service/data/meta/EnumValueInfo.java new file mode 100644 index 0000000..38c162c --- /dev/null +++ b/src/main/java/org/reso/service/data/meta/EnumValueInfo.java @@ -0,0 +1,37 @@ +package org.reso.service.data.meta; + + +import org.apache.olingo.commons.api.edm.provider.CsdlAnnotation; +import org.apache.olingo.commons.api.edm.provider.annotation.CsdlConstantExpression; + +import java.util.ArrayList; + +public class EnumValueInfo +{ + private String value; + private ArrayList annotations = new ArrayList<>(); + + + public EnumValueInfo(String value) + { + this.value = value; + } + + public String getValue() + { + return value; + } + + public void addAnnotation(String annotation, String term) + { + CsdlAnnotation csdlAnnotation = new CsdlAnnotation().setTerm(term).setExpression(new CsdlConstantExpression(CsdlConstantExpression.ConstantExpressionType.String, annotation)); + + annotations.add( csdlAnnotation ); + } + + public ArrayList getAnnotations() + { + return annotations; + } + +} diff --git a/src/main/java/org/reso/service/edmprovider/RESOedmProvider.java b/src/main/java/org/reso/service/edmprovider/RESOedmProvider.java index fb62edc..da5f9d0 100644 --- a/src/main/java/org/reso/service/edmprovider/RESOedmProvider.java +++ b/src/main/java/org/reso/service/edmprovider/RESOedmProvider.java @@ -3,15 +3,13 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.edm.provider.*; import org.reso.service.data.meta.EnumFieldInfo; +import org.reso.service.data.meta.EnumValueInfo; import org.reso.service.data.meta.FieldInfo; import org.reso.service.data.meta.ResourceInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; public class RESOedmProvider extends CsdlAbstractEdmProvider @@ -166,6 +164,8 @@ public class RESOedmProvider extends CsdlAbstractEdmProvider // add EntityTypes List entityTypes = new ArrayList(); + HashMap enumList = new HashMap<>(); + for (ResourceInfo defn :resourceList) { ArrayList fields = defn.getFieldList(); @@ -174,24 +174,32 @@ public class RESOedmProvider extends CsdlAbstractEdmProvider if (field instanceof EnumFieldInfo) { EnumFieldInfo enumField = (EnumFieldInfo) field; + + String enumName = enumField.getLookupName(); - ArrayList values = enumField.getValues(); - - if (null!=values && values.size()>0) + if (!enumList.containsKey(enumName)) { - CsdlEnumType type = new CsdlEnumType(); - ArrayList csdlMembers = new ArrayList<>(); + enumList.put(enumName, true); + ArrayList values = enumField.getValues(); - for (String value: values) + if (null!=values && values.size()>0) { - csdlMembers.add(new CsdlEnumMember().setName(value)); + CsdlEnumType type = new CsdlEnumType(); + ArrayList csdlMembers = new ArrayList<>(); + + for (EnumValueInfo value: values) + { + CsdlEnumMember member = new CsdlEnumMember().setName(value.getValue()); + member.setAnnotations(value.getAnnotations()); + csdlMembers.add(member); + } + + type.setMembers(csdlMembers); + type.setName(enumName); + type.setUnderlyingType(EdmPrimitiveTypeKind.Int64.getFullQualifiedName()); + + enumSchema.getEnumTypes().add(type); } - - type.setMembers(csdlMembers); - type.setName(enumField.getLookupName()); - type.setUnderlyingType(EdmPrimitiveTypeKind.Int64.getFullQualifiedName()); - - enumSchema.getEnumTypes().add(type); } }