use of com.mongodb.BasicDBList in project spring-data-mongodb by spring-projects.
the class MappingMongoConverter method createCollection.
/**
* Writes the given {@link Collection} using the given {@link MongoPersistentProperty} information.
*
* @param collection must not be {@literal null}.
* @param property must not be {@literal null}.
* @return
*/
protected List<Object> createCollection(Collection<?> collection, MongoPersistentProperty property) {
if (!property.isDbReference()) {
return writeCollectionInternal(collection, property.getTypeInformation(), new BasicDBList());
}
List<Object> dbList = new ArrayList<>(collection.size());
for (Object element : collection) {
if (element == null) {
continue;
}
DBRef dbRef = createDBRef(element, property);
dbList.add(dbRef);
}
return dbList;
}
use of com.mongodb.BasicDBList in project spring-data-mongodb by spring-projects.
the class MappingMongoConverter method writeMapInternal.
/**
* Writes the given {@link Map} to the given {@link Document} considering the given {@link TypeInformation}.
*
* @param obj must not be {@literal null}.
* @param bson must not be {@literal null}.
* @param propertyType must not be {@literal null}.
* @return
*/
protected Bson writeMapInternal(Map<Object, Object> obj, Bson bson, TypeInformation<?> propertyType) {
for (Map.Entry<Object, Object> entry : obj.entrySet()) {
Object key = entry.getKey();
Object val = entry.getValue();
if (conversions.isSimpleType(key.getClass())) {
String simpleKey = prepareMapKey(key);
if (val == null || conversions.isSimpleType(val.getClass())) {
writeSimpleInternal(val, bson, simpleKey);
} else if (val instanceof Collection || val.getClass().isArray()) {
addToMap(bson, simpleKey, writeCollectionInternal(asCollection(val), propertyType.getMapValueType(), new BasicDBList()));
} else {
Document document = new Document();
TypeInformation<?> valueTypeInfo = propertyType.isMap() ? propertyType.getMapValueType() : ClassTypeInformation.OBJECT;
writeInternal(val, document, valueTypeInfo);
addToMap(bson, simpleKey, document);
}
} else {
throw new MappingException("Cannot use a complex object as a key value.");
}
}
return bson;
}
use of com.mongodb.BasicDBList in project spring-data-mongodb by spring-projects.
the class MappingMongoConverter method removeTypeInfo.
/**
* Removes the type information from the entire conversion result.
*
* @param object
* @param recursively whether to apply the removal recursively
* @return
*/
@SuppressWarnings("unchecked")
private Object removeTypeInfo(Object object, boolean recursively) {
if (!(object instanceof Document)) {
return object;
}
Document document = (Document) object;
String keyToRemove = null;
for (String key : document.keySet()) {
if (recursively) {
Object value = document.get(key);
if (value instanceof BasicDBList) {
for (Object element : (BasicDBList) value) {
removeTypeInfo(element, recursively);
}
} else if (value instanceof List) {
for (Object element : (List<Object>) value) {
removeTypeInfo(element, recursively);
}
} else {
removeTypeInfo(value, recursively);
}
}
if (typeMapper.isTypeKey(key)) {
keyToRemove = key;
if (!recursively) {
break;
}
}
}
if (keyToRemove != null) {
document.remove(keyToRemove);
}
return document;
}
use of com.mongodb.BasicDBList 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 com.mongodb.BasicDBList in project spring-data-mongodb by spring-projects.
the class MappingMongoConverterUnitTests method readsListOfMapsCorrectly.
// DATAMONGO-259
@Test
public void readsListOfMapsCorrectly() {
org.bson.Document map = new org.bson.Document("Foo", "en");
BasicDBList list = new BasicDBList();
list.add(map);
org.bson.Document wrapperSource = new org.bson.Document("listOfMaps", list);
CollectionWrapper wrapper = converter.read(CollectionWrapper.class, wrapperSource);
assertThat(wrapper.listOfMaps, is(notNullValue()));
assertThat(wrapper.listOfMaps.size(), is(1));
assertThat(wrapper.listOfMaps.get(0), is(notNullValue()));
assertThat(wrapper.listOfMaps.get(0).get("Foo"), is(Locale.ENGLISH));
}
Aggregations