Search in sources :

Example 56 with BasicDBList

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;
}
Also used : BasicDBList(com.mongodb.BasicDBList) ArrayList(java.util.ArrayList) DBRef(com.mongodb.DBRef) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 57 with BasicDBList

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;
}
Also used : BasicDBList(com.mongodb.BasicDBList) Collection(java.util.Collection) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Document(org.bson.Document) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) TypeInformation(org.springframework.data.util.TypeInformation) MappingException(org.springframework.data.mapping.MappingException)

Example 58 with BasicDBList

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;
}
Also used : BasicDBList(com.mongodb.BasicDBList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document)

Example 59 with BasicDBList

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());
}
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 60 with BasicDBList

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));
}
Also used : BasicDBList(com.mongodb.BasicDBList) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.Test)

Aggregations

BasicDBList (com.mongodb.BasicDBList)69 BasicDBObject (com.mongodb.BasicDBObject)40 DBObject (com.mongodb.DBObject)33 Test (org.junit.Test)27 Document (org.springframework.data.mongodb.core.mapping.Document)16 ArrayList (java.util.ArrayList)13 List (java.util.List)9 DBCollection (com.mongodb.DBCollection)8 Document (org.bson.Document)7 DBCursor (com.mongodb.DBCursor)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 DBRef (com.mongodb.DBRef)5 Collection (java.util.Collection)4 NotFoundException (org.graylog2.database.NotFoundException)4 ImmutableList (com.google.common.collect.ImmutableList)3 MongoClient (com.mongodb.MongoClient)3 MongoException (com.mongodb.MongoException)3 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)3 LinkedHashMap (java.util.LinkedHashMap)3