Search in sources :

Example 1 with BeanProperty

use of com.att.cdp.pal.util.BeanProperty in project AJSC by att.

the class Unmarshaller method unmarshallMapObject.

/**
 * Unmarshalls a map into a non-scalar model object
 *
 * @param model
 *            The non-scalar model object
 * @param map
 *            The map to be unmarshalled
 * @throws UnmarshallException
 *             If the map does not match the model object
 */
private <T extends ModelObject> void unmarshallMapObject(T model, Map<String, Object> map) throws UnmarshallException {
    Class<? extends ModelObject> implClass = null;
    /*
         * The data we are processing is a map. In that case we iterate the map keys and use that to locate properties
         * that correspond to the key
         */
    for (String key : map.keySet()) {
        try {
            Object obj = map.get(key);
            BeanProperty beanProperty = BeanProperty.getBeanProperty(model, key);
            /*
                 * If we can't find a property, then check if it's ok to ignore unknown properties. If not, its an
                 * exception.
                 */
            if (beanProperty == null) {
                if (!ObjectHelper.isJsonIgnoreUnknownProperty(model.getClass(), key)) {
                    throw new UnmarshallException(String.format("Expected property %s was not found on %s", key, model.getClass().getSimpleName()));
                }
                continue;
            }
            /*
                 * Determine the implementation class of the object we need to map into
                 */
            implClass = determineImplementationClass(beanProperty, obj);
            if (beanProperty.isMap()) {
                /*
                     * If the property is a map, then we are creating a map of the implementation objects.
                     */
                Map<String, Object> values = (Map<String, Object>) obj;
                Map<String, Object> collection = (Map<String, Object>) beanProperty.get();
                if (collection == null) {
                    collection = new HashMap<String, Object>();
                    beanProperty.set(collection);
                }
                for (String valueKey : values.keySet()) {
                    collection.put(valueKey, unmarshall(implClass, values.get(valueKey)));
                }
            } else if (beanProperty.isList()) {
                /*
                     * If the property is a list, then we are creating a list of the implementation objects
                     */
                List<Object> values = (List<Object>) obj;
                List<Object> collection = (List<Object>) beanProperty.get();
                if (collection == null) {
                    collection = new ArrayList<Object>();
                    beanProperty.set(collection);
                }
                for (Object value : values) {
                    collection.add(unmarshall(implClass, value));
                }
            } else {
                /*
                     * Otherwise we are creating a scalar object
                     */
                beanProperty.set(unmarshall(Scalar.class, obj));
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new UnmarshallException(String.format("Exception instantiating model object of class %s", implClass.getSimpleName()), e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) BeanProperty(com.att.cdp.pal.util.BeanProperty) ModelObject(com.att.cdp.openstack.heat.model.ModelObject) ArrayList(java.util.ArrayList) List(java.util.List) UnmarshallException(com.att.cdp.openstack.exception.UnmarshallException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

UnmarshallException (com.att.cdp.openstack.exception.UnmarshallException)1 ModelObject (com.att.cdp.openstack.heat.model.ModelObject)1 BeanProperty (com.att.cdp.pal.util.BeanProperty)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1