Search in sources :

Example 11 with MappingException

use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.

the class MappingMongoConverter method readCollectionOrArray.

/**
 * Reads the given {@link BasicDBList} into a collection of the given {@link TypeInformation}.
 *
 * @param targetType must not be {@literal null}.
 * @param source must not be {@literal null}.
 * @param path must not be {@literal null}.
 * @return the converted {@link Collection} or array, will never be {@literal null}.
 */
@SuppressWarnings("unchecked")
private Object readCollectionOrArray(TypeInformation<?> targetType, Collection<?> source, ObjectPath path) {
    Assert.notNull(targetType, "Target type must not be null!");
    Assert.notNull(path, "Object path must not be null!");
    Class<?> collectionType = targetType.getType();
    collectionType = // 
    Collection.class.isAssignableFrom(collectionType) ? // 
    collectionType : List.class;
    TypeInformation<?> componentType = // 
    targetType.getComponentType() != null ? // 
    targetType.getComponentType() : ClassTypeInformation.OBJECT;
    Class<?> rawComponentType = componentType.getType();
    Collection<Object> items = // 
    targetType.getType().isArray() ? // 
    new ArrayList<>(source.size()) : CollectionFactory.createCollection(collectionType, rawComponentType, source.size());
    if (source.isEmpty()) {
        return getPotentiallyConvertedSimpleRead(items, targetType.getType());
    }
    if (!DBRef.class.equals(rawComponentType) && isCollectionOfDbRefWhereBulkFetchIsPossible(source)) {
        List<Object> objects = bulkReadAndConvertDBRefs((List<DBRef>) source, componentType, path, rawComponentType);
        return getPotentiallyConvertedSimpleRead(objects, targetType.getType());
    }
    for (Object element : source) {
        if (element instanceof DBRef) {
            items.add(DBRef.class.equals(rawComponentType) ? element : readAndConvertDBRef((DBRef) element, componentType, path, rawComponentType));
        } else if (element instanceof Document) {
            items.add(read(componentType, (Document) element, path));
        } else if (element instanceof BasicDBObject) {
            items.add(read(componentType, (BasicDBObject) element, path));
        } else {
            if (element instanceof Collection) {
                if (!rawComponentType.isArray() && !ClassUtils.isAssignable(Iterable.class, rawComponentType)) {
                    throw new MappingException(String.format(INCOMPATIBLE_TYPES, element, element.getClass(), rawComponentType, path));
                }
            }
            if (element instanceof List) {
                items.add(readCollectionOrArray(componentType, (Collection<Object>) element, path));
            } else {
                items.add(getPotentiallyConvertedSimpleRead(element, rawComponentType));
            }
        }
    }
    return getPotentiallyConvertedSimpleRead(items, targetType.getType());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBRef(com.mongodb.DBRef) Collection(java.util.Collection) BasicDBList(com.mongodb.BasicDBList) List(java.util.List) ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Document(org.bson.Document) MappingException(org.springframework.data.mapping.MappingException)

Example 12 with MappingException

use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.

the class MappingMongoConverter method createMap.

/**
 * Writes the given {@link Map} using the given {@link MongoPersistentProperty} information.
 *
 * @param map must not {@literal null}.
 * @param property must not be {@literal null}.
 * @return
 */
protected Bson createMap(Map<Object, Object> map, MongoPersistentProperty property) {
    Assert.notNull(map, "Given map must not be null!");
    Assert.notNull(property, "PersistentProperty must not be null!");
    if (!property.isDbReference()) {
        return writeMapInternal(map, new Document(), property.getTypeInformation());
    }
    Document document = new Document();
    for (Map.Entry<Object, Object> entry : map.entrySet()) {
        Object key = entry.getKey();
        Object value = entry.getValue();
        if (conversions.isSimpleType(key.getClass())) {
            String simpleKey = prepareMapKey(key.toString());
            document.put(simpleKey, value != null ? createDBRef(value, property) : null);
        } else {
            throw new MappingException("Cannot use a complex object as a key value.");
        }
    }
    return document;
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Document(org.bson.Document) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) MappingException(org.springframework.data.mapping.MappingException)

Aggregations

MappingException (org.springframework.data.mapping.MappingException)12 Document (org.bson.Document)8 BasicDBObject (com.mongodb.BasicDBObject)6 DBObject (com.mongodb.DBObject)5 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)5 BasicDBList (com.mongodb.BasicDBList)3 Collection (java.util.Collection)3 DBRef (com.mongodb.DBRef)2 ReturnDocument (com.mongodb.client.model.ReturnDocument)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 JSONParseException (com.mongodb.util.JSONParseException)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Field (java.lang.reflect.Field)1 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1 ClassTypeInformation (org.springframework.data.util.ClassTypeInformation)1 TypeInformation (org.springframework.data.util.TypeInformation)1 Nullable (org.springframework.lang.Nullable)1