Skip to content

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:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
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
    }
}