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());
}
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;
}
Aggregations