Runtime Discovery of the Entity Data Model¶
Use the Metadata API for runtime discovery of the entity data model of a service provider as follows:
EntityContainerMap containerMap = csdlDocument.getEntityContainers();
// Iterate through all entity containers
for (EntityContainer container : containerMap.values()) {
EntitySetMap setMap = container.getEntitySets();
// Iterate through all entity sets within the container
for (EntitySet entitySet : setMap.values()) {
// Get the entity type associated with the entity set
EntityType entityType = entitySet.getEntityType();
// Get all the properties associated with the entity type
PropertyMap propertyMap = entityType.getPropertyMap();
// Form a query to retrive all instances of the entity set
DataQuery query = new DataQuery().from(entitySet);
EntityValueList entities = dataService.executeQuery(query).getEntityList();
ArrayList<HashMap<String, String>> entityCollection =
new ArrayList<HashMap<String, String>>();
// Iterate through each instance of the entity set retrieved
for (EntityValue entityValue : entities) {
HashMap<String, String> entityValues = new HashMap<String, String>();
// Iterate through all the properties of the instance
for (Property property: propertyMap.values()) {
if (property.isNavigation()) {
// Skip navigation properties
continue;
}
// For simplicity, our instances only have 4 data types
String typeName = property.getType().getName();
String stringValue;
// use the type information to determine how to get the value of the property
// in this example, we convert all property values to string for UI binding
switch (typeName) {
case "long":
Long longValue = null;
if (property.isNullable()) {
longValue = property.getNullableLong(entityValue);
if (longValue == null) {
entityValues.put(property.getName(), null);
continue;
}
} else {
longValue = property.getLong(entityValue);
}
stringValue = longValue.toString();
entityValues.put(property.getName(), stringValue);
continue;
case "string":
if (property.isNullable()) {
stringValue = property.getNullableString(entityValue);
if (stringValue == null) {
entityValues.put(property.getName(), null);
continue;
}
} else {
stringValue = property.getString(entityValue);
}
entityValues.put(property.getName(), stringValue);
continue;
case "LocalDateTime":
DataValue dataValue = null;
dataValue = property.getValue(entityValue);
if (dataValue == null) {
entityValues.put(property.getName(), null);
continue;
}
stringValue = LocalDateTime.castRequired(dataValue).toString();
entityValues.put(property.getName(), stringValue);
continue;
case "int":
Integer intValue = null;
if (property.isNullable()) {
intValue = property.getNullableInt(entityValue);
if (intValue == null) {
entityValues.put(property.getName(), null);
continue;
}
} else {
intValue = property.getInt(entityValue);
}
stringValue = intValue.toString();
entityValues.put(property.getName(), stringValue);
continue;
default:
// we only recognize the 4 data types
assertTrue(false);
}
}
// entityValues (hashtable) now as all non-null properties as name-value pair
entityCollection.add(entityValues);
}
// entityCollection now has an array of HashMap representing all the instances
}
}
Last update: April 14, 2021