Search in sources :

Example 6 with Nullable

use of org.springframework.lang.Nullable in project spring-data-mongodb by spring-projects.

the class MongoConverter method mapValueToTargetType.

/**
 * Mapping function capable of converting values into a desired target type by eg. extracting the actual java type
 * from a given {@link BsonValue}.
 *
 * @param targetType must not be {@literal null}.
 * @param dbRefResolver must not be {@literal null}.
 * @param <S>
 * @param <T>
 * @return new typed {@link java.util.function.Function}.
 * @throws IllegalArgumentException if {@literal targetType} is {@literal null}.
 * @since 2.1
 */
@SuppressWarnings("unchecked")
@Nullable
default <S, T> T mapValueToTargetType(S source, Class<T> targetType, DbRefResolver dbRefResolver) {
    Assert.notNull(targetType, "TargetType must not be null!");
    Assert.notNull(dbRefResolver, "DbRefResolver must not be null!");
    if (targetType != Object.class && ClassUtils.isAssignable(targetType, source.getClass())) {
        return (T) source;
    }
    if (source instanceof BsonValue) {
        Object value = BsonUtils.toJavaType((BsonValue) source);
        if (value instanceof Document) {
            Document sourceDocument = (Document) value;
            if (sourceDocument.containsKey("$ref") && sourceDocument.containsKey("$id")) {
                sourceDocument = dbRefResolver.fetch(new DBRef(sourceDocument.getString("$ref"), sourceDocument.get("$id")));
                if (sourceDocument == null) {
                    return null;
                }
            }
            return read(targetType, sourceDocument);
        } else {
            if (!ClassUtils.isAssignable(targetType, value.getClass())) {
                if (getConversionService().canConvert(value.getClass(), targetType)) {
                    return getConversionService().convert(value, targetType);
                }
            }
        }
        return (T) value;
    }
    return getConversionService().convert(source, targetType);
}
Also used : DBRef(com.mongodb.DBRef) Document(org.bson.Document) BsonValue(org.bson.BsonValue) Nullable(org.springframework.lang.Nullable)

Example 7 with Nullable

use of org.springframework.lang.Nullable in project spring-data-mongodb by spring-projects.

the class QueryMapper method convertAssociation.

/**
 * Converts the given source assuming it's actually an association to another object.
 *
 * @param source
 * @param property
 * @return
 */
@Nullable
protected Object convertAssociation(@Nullable Object source, @Nullable MongoPersistentProperty property) {
    if (property == null || source == null || source instanceof Document || source instanceof DBObject) {
        return source;
    }
    if (source instanceof DBRef) {
        DBRef ref = (DBRef) source;
        return new DBRef(ref.getCollectionName(), convertId(ref.getId()));
    }
    if (source instanceof Iterable) {
        BasicDBList result = new BasicDBList();
        for (Object element : (Iterable<?>) source) {
            result.add(createDbRefFor(element, property));
        }
        return result;
    }
    if (property.isMap()) {
        Document result = new Document();
        Document dbObject = (Document) source;
        for (String key : dbObject.keySet()) {
            result.put(key, createDbRefFor(dbObject.get(key), property));
        }
        return result;
    }
    return createDbRefFor(source, property);
}
Also used : BasicDBList(com.mongodb.BasicDBList) DBRef(com.mongodb.DBRef) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Document(org.bson.Document) NestedDocument(org.springframework.data.mongodb.core.convert.MappingMongoConverter.NestedDocument) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Nullable(org.springframework.lang.Nullable)

Example 8 with Nullable

use of org.springframework.lang.Nullable in project spring-data-mongodb by spring-projects.

the class BasicMongoPersistentEntity method detectExpression.

/**
 * Returns a SpEL {@link Expression} frór the collection String expressed in the given {@link Document} annotation if
 * present or {@literal null} otherwise. Will also return {@literal null} it the collection {@link String} evaluates
 * to a {@link LiteralExpression} (indicating that no subsequent evaluation is necessary).
 *
 * @param document can be {@literal null}
 * @return
 */
@Nullable
private static Expression detectExpression(Document document) {
    String collection = document.collection();
    if (!StringUtils.hasText(collection)) {
        return null;
    }
    Expression expression = PARSER.parseExpression(document.collection(), ParserContext.TEMPLATE_EXPRESSION);
    return expression instanceof LiteralExpression ? null : expression;
}
Also used : LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) LiteralExpression(org.springframework.expression.common.LiteralExpression) Nullable(org.springframework.lang.Nullable)

Example 9 with Nullable

use of org.springframework.lang.Nullable in project spring-data-mongodb by spring-projects.

the class ExpressionEvaluatingParameterBinder method evaluateExpression.

/**
 * Evaluates the given {@code expressionString}.
 *
 * @param expressionString must not be {@literal null} or empty.
 * @param parameters must not be {@literal null}.
 * @param parameterValues must not be {@literal null}.
 * @return
 */
@Nullable
private Object evaluateExpression(String expressionString, MongoParameters parameters, Object[] parameterValues) {
    EvaluationContext evaluationContext = evaluationContextProvider.getEvaluationContext(parameters, parameterValues);
    Expression expression = expressionParser.parseExpression(expressionString);
    return expression.getValue(evaluationContext, Object.class);
}
Also used : Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) Nullable(org.springframework.lang.Nullable)

Example 10 with Nullable

use of org.springframework.lang.Nullable in project spring-data-jdbc by spring-projects.

the class IterableOfEntryToMapConverter method convert.

@Nullable
@Override
public Map convert(Iterable source) {
    Map result = new HashMap();
    source.forEach(element -> {
        if (!(element instanceof Entry)) {
            throw new IllegalArgumentException(String.format("Cannot convert %s to Map.Entry", element.getClass()));
        }
        Entry entry = (Entry) element;
        result.put(entry.getKey(), entry.getValue());
    });
    return result;
}
Also used : Entry(java.util.Map.Entry) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Nullable(org.springframework.lang.Nullable)

Aggregations

Nullable (org.springframework.lang.Nullable)18 Document (org.bson.Document)11 BasicDBObject (com.mongodb.BasicDBObject)5 Entry (java.util.Map.Entry)5 BsonValue (org.bson.BsonValue)5 MappingException (org.springframework.data.mapping.MappingException)5 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)5 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)5 DBObject (com.mongodb.DBObject)4 DeleteResult (com.mongodb.client.result.DeleteResult)4 UpdateResult (com.mongodb.client.result.UpdateResult)4 ClientSession (com.mongodb.session.ClientSession)4 JSONParseException (com.mongodb.util.JSONParseException)4 Bson (org.bson.conversions.Bson)4 BasicDBList (com.mongodb.BasicDBList)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 NonNull (lombok.NonNull)3 RequiredArgsConstructor (lombok.RequiredArgsConstructor)3 Codec (org.bson.codecs.Codec)3