Adding Annotations to Enum members

This commit is contained in:
michaelpede 2021-05-12 16:06:59 -07:00
parent 6c7b161e18
commit ba6f8a6f50
3 changed files with 67 additions and 21 deletions

View File

@ -20,7 +20,7 @@ import static org.reso.service.servlet.RESOservlet.getConnection;
public class EnumFieldInfo extends FieldInfo public class EnumFieldInfo extends FieldInfo
{ {
private String lookupName; private String lookupName;
private ArrayList<String> values = new ArrayList<>(); private ArrayList<EnumValueInfo> values = new ArrayList<>();
private static final Logger LOG = LoggerFactory.getLogger(EnumFieldInfo.class); private static final Logger LOG = LoggerFactory.getLogger(EnumFieldInfo.class);
private boolean isCollection = false; private boolean isCollection = false;
@ -31,7 +31,7 @@ public class EnumFieldInfo extends FieldInfo
super(fieldName, type); super(fieldName, type);
} }
public void addValue(String value) public void addValue(EnumValueInfo value)
{ {
values.add(value); values.add(value);
} }
@ -57,7 +57,8 @@ public class EnumFieldInfo extends FieldInfo
{ {
Entity ent = CommonDataProcessing.getEntityFromRow(resultSet, resource, selectLookup); Entity ent = CommonDataProcessing.getEntityFromRow(resultSet, resource, selectLookup);
Property property = ent.getProperty(LOOKUP_COLUMN_NAME); 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) catch (Exception e)
@ -68,7 +69,7 @@ public class EnumFieldInfo extends FieldInfo
} }
} }
public ArrayList<String> getValues() public ArrayList<EnumValueInfo> getValues()
{ {
if (values.size()==0) if (values.size()==0)
{ {

View File

@ -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<CsdlAnnotation> 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<CsdlAnnotation> getAnnotations()
{
return annotations;
}
}

View File

@ -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.FullQualifiedName;
import org.apache.olingo.commons.api.edm.provider.*; import org.apache.olingo.commons.api.edm.provider.*;
import org.reso.service.data.meta.EnumFieldInfo; 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.FieldInfo;
import org.reso.service.data.meta.ResourceInfo; import org.reso.service.data.meta.ResourceInfo;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class RESOedmProvider extends CsdlAbstractEdmProvider public class RESOedmProvider extends CsdlAbstractEdmProvider
@ -166,6 +164,8 @@ public class RESOedmProvider extends CsdlAbstractEdmProvider
// add EntityTypes // add EntityTypes
List<CsdlEntityType> entityTypes = new ArrayList<CsdlEntityType>(); List<CsdlEntityType> entityTypes = new ArrayList<CsdlEntityType>();
HashMap<String, Boolean> enumList = new HashMap<>();
for (ResourceInfo defn :resourceList) for (ResourceInfo defn :resourceList)
{ {
ArrayList<FieldInfo> fields = defn.getFieldList(); ArrayList<FieldInfo> fields = defn.getFieldList();
@ -175,23 +175,31 @@ public class RESOedmProvider extends CsdlAbstractEdmProvider
{ {
EnumFieldInfo enumField = (EnumFieldInfo) field; EnumFieldInfo enumField = (EnumFieldInfo) field;
ArrayList<String> values = enumField.getValues(); String enumName = enumField.getLookupName();
if (null!=values && values.size()>0) if (!enumList.containsKey(enumName))
{ {
CsdlEnumType type = new CsdlEnumType(); enumList.put(enumName, true);
ArrayList<CsdlEnumMember> csdlMembers = new ArrayList<>(); ArrayList<EnumValueInfo> values = enumField.getValues();
for (String value: values) if (null!=values && values.size()>0)
{ {
csdlMembers.add(new CsdlEnumMember().setName(value)); CsdlEnumType type = new CsdlEnumType();
ArrayList<CsdlEnumMember> 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);
} }
} }