Search in sources :

Example 1 with InvalidPersistentPropertyPath

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

the class QueryMapper method getMappedObject.

/**
 * Replaces the property keys used in the given {@link Document} with the appropriate keys by using the
 * {@link PersistentEntity} metadata.
 *
 * @param query must not be {@literal null}.
 * @param entity can be {@literal null}.
 * @return
 */
@SuppressWarnings("deprecation")
public Document getMappedObject(Bson query, @Nullable MongoPersistentEntity<?> entity) {
    if (isNestedKeyword(query)) {
        return getMappedKeyword(new Keyword(query), entity);
    }
    Document result = new Document();
    for (String key : BsonUtils.asMap(query).keySet()) {
        // TODO: remove one once QueryMapper can work with Query instances directly
        if (Query.isRestrictedTypeKey(key)) {
            Set<Class<?>> restrictedTypes = BsonUtils.get(query, key);
            this.converter.getTypeMapper().writeTypeRestrictions(result, restrictedTypes);
            continue;
        }
        if (isTypeKey(key)) {
            result.put(key, BsonUtils.get(query, key));
            continue;
        }
        if (isKeyword(key)) {
            result.putAll(getMappedKeyword(new Keyword(query, key), entity));
            continue;
        }
        try {
            Field field = createPropertyField(entity, key, mappingContext);
            // TODO: move to dedicated method
            if (field.getProperty() != null && field.getProperty().isUnwrapped()) {
                Object theNestedObject = BsonUtils.get(query, key);
                Document mappedValue = (Document) getMappedValue(field, theNestedObject);
                if (!StringUtils.hasText(field.getMappedKey())) {
                    result.putAll(mappedValue);
                } else {
                    result.put(field.getMappedKey(), mappedValue);
                }
            } else {
                Entry<String, Object> entry = getMappedObjectForField(field, BsonUtils.get(query, key));
                result.put(entry.getKey(), entry.getValue());
            }
        } catch (InvalidPersistentPropertyPath invalidPathException) {
            // in case the object has not already been mapped
            if (!(BsonUtils.get(query, key) instanceof Document)) {
                throw invalidPathException;
            }
            result.put(key, BsonUtils.get(query, key));
        }
    }
    return result;
}
Also used : InvalidPersistentPropertyPath(org.springframework.data.mapping.context.InvalidPersistentPropertyPath) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Document(org.bson.Document) NestedDocument(org.springframework.data.mongodb.core.convert.MappingMongoConverter.NestedDocument)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 Document (org.bson.Document)1 InvalidPersistentPropertyPath (org.springframework.data.mapping.context.InvalidPersistentPropertyPath)1 NestedDocument (org.springframework.data.mongodb.core.convert.MappingMongoConverter.NestedDocument)1