/** * Returns a copy of the {@link ObjectPath} with the given {@link Object} as current object. * * @param object must not be {@literal null}. * @param entity must not be {@literal null}. * @param id must not be {@literal null}. * @return new instance of {@link ObjectPath}. */ ObjectPath push(Object object, MongoPersistentEntity<?> entity, @Nullable Object id) { Assert.notNull(object, "Object must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); return new ObjectPath(this, object, id, entity.getCollection()); }
/** * Returns the object with the given id and stored in the given collection if it's contained in the * {@link ObjectPath}. * * @param id must not be {@literal null}. * @param collection must not be {@literal null} or empty. * @return * @deprecated use {@link #getPathItem(Object, String, Class)}. */ @Nullable @Deprecated Object getPathItem(Object id, String collection) { Assert.notNull(id, "Id must not be null!"); Assert.hasText(collection, "Collection name must not be null!"); for (ObjectPath current = this; current != null; current = current.parent) { Object object = current.getObject(); if (object == null || current.getIdValue() == null) { continue; } if (collection.equals(current.getCollection()) && id.equals(current.getIdValue())) { return object; } } return null; }
private ParameterValueProvider<MongoPersistentProperty> getParameterProvider(MongoPersistentEntity<?> entity, DocumentAccessor source, SpELExpressionEvaluator evaluator, ObjectPath path) { AssociationAwareMongoDbPropertyValueProvider provider = new AssociationAwareMongoDbPropertyValueProvider(source, evaluator, path); PersistentEntityParameterValueProvider<MongoPersistentProperty> parameterProvider = new PersistentEntityParameterValueProvider<>( entity, provider, path.getCurrentObject()); return new ConverterAwareSpELExpressionParameterValueProvider(evaluator, conversionService, parameterProvider, path); }
/** * Returns the current object of the {@link ObjectPath} or {@literal null} if the path is empty. * * @return */ @Nullable Object getCurrentObject() { return getObject(); }
@Override public Object populateId(MongoPersistentProperty property, @Nullable DBRef source, Object proxy) { if (source == null) { return proxy; } MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(property); MongoPersistentProperty idProperty = entity.getRequiredIdProperty(); if (idProperty.usePropertyAccess()) { return proxy; } SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(proxy, spELContext); PersistentPropertyAccessor accessor = entity.getPropertyAccessor(proxy); Document object = new Document(idProperty.getFieldName(), source.getId()); ObjectPath objectPath = ObjectPath.ROOT.push(proxy, entity, null); accessor.setProperty(idProperty, resolver.getValueInternal(idProperty, object, evaluator, objectPath)); return proxy; } }
@Nullable @SuppressWarnings("unchecked") private <T> T potentiallyReadOrResolveDbRef(@Nullable DBRef dbref, TypeInformation<?> type, ObjectPath path, Class<?> rawType) { if (rawType.equals(DBRef.class)) { return (T) dbref; } T object = dbref == null ? null : path.getPathItem(dbref.getId(), dbref.getCollectionName(), (Class<T>) rawType); return object != null ? object : readAndConvertDBRef(dbref, type, path, rawType); }
@Override public String toString() { if (parent == null) { return "[empty]"; } List<String> strings = new ArrayList<>(); for (ObjectPath current = this; current != null; current = current.parent) { strings.add(ObjectUtils.nullSafeToString(current.getObject())); } return StringUtils.collectionToDelimitedString(strings, " -> "); } }
private <S> S populateProperties(MongoPersistentEntity<S> entity, DocumentAccessor documentAccessor, ObjectPath path, SpELExpressionEvaluator evaluator, S instance) { PersistentPropertyAccessor<S> accessor = new ConvertingPropertyAccessor<>(entity.getPropertyAccessor(instance), conversionService); // Make sure id property is set before all other properties Object rawId = readAndPopulateIdentifier(accessor, documentAccessor, entity, path, evaluator); ObjectPath currentPath = path.push(accessor.getBean(), entity, rawId); MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(documentAccessor, evaluator, currentPath); readProperties(entity, accessor, documentAccessor, valueProvider, currentPath, evaluator); return accessor.getBean(); }
@Nullable @SuppressWarnings("unchecked") private <T> T potentiallyReadOrResolveDbRef(@Nullable DBRef dbref, TypeInformation<?> type, ObjectPath path, Class<?> rawType) { if (rawType.equals(DBRef.class)) { return (T) dbref; } T object = dbref == null ? null : path.getPathItem(dbref.getId(), dbref.getCollectionName(), (Class<T>) rawType); return object != null ? object : readAndConvertDBRef(dbref, type, path, rawType); }
/** * Get the object with given {@literal id}, stored in the {@literal collection} that is assignable to the given * {@literal type} or {@literal null} if no match found. * * @param id must not be {@literal null}. * @param collection must not be {@literal null} or empty. * @param type must not be {@literal null}. * @return {@literal null} when no match found. * @since 2.0 */ @Nullable <T> T getPathItem(Object id, String collection, Class<T> type) { Assert.notNull(id, "Id must not be null!"); Assert.hasText(collection, "Collection name must not be null!"); Assert.notNull(type, "Type must not be null!"); for (ObjectPath current = this; current != null; current = current.parent) { Object object = current.getObject(); if (object == null || current.getIdValue() == null) { continue; } if (collection.equals(current.getCollection()) && id.equals(current.getIdValue()) && ClassUtils.isAssignable(type, object.getClass())) { return type.cast(object); } } return null; }
/** * Returns the current object of the {@link ObjectPath} or {@literal null} if the path is empty. * * @return */ @Nullable Object getCurrentObject() { return getObject(); }
@Override public Object populateId(MongoPersistentProperty property, @Nullable DBRef source, Object proxy) { if (source == null) { return proxy; } MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(property); MongoPersistentProperty idProperty = entity.getRequiredIdProperty(); if (idProperty.usePropertyAccess()) { return proxy; } SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(proxy, spELContext); PersistentPropertyAccessor accessor = entity.getPropertyAccessor(proxy); Document object = new Document(idProperty.getFieldName(), source.getId()); ObjectPath objectPath = ObjectPath.ROOT.push(proxy, entity, null); accessor.setProperty(idProperty, resolver.getValueInternal(idProperty, object, evaluator, objectPath)); return proxy; } }
private ParameterValueProvider<MongoPersistentProperty> getParameterProvider(MongoPersistentEntity<?> entity, DocumentAccessor source, SpELExpressionEvaluator evaluator, ObjectPath path) { AssociationAwareMongoDbPropertyValueProvider provider = new AssociationAwareMongoDbPropertyValueProvider(source, evaluator, path); PersistentEntityParameterValueProvider<MongoPersistentProperty> parameterProvider = new PersistentEntityParameterValueProvider<>( entity, provider, path.getCurrentObject()); return new ConverterAwareSpELExpressionParameterValueProvider(evaluator, conversionService, parameterProvider, path); }
/** * Returns a copy of the {@link ObjectPath} with the given {@link Object} as current object. * * @param object must not be {@literal null}. * @param entity must not be {@literal null}. * @param id must not be {@literal null}. * @return new instance of {@link ObjectPath}. */ ObjectPath push(Object object, MongoPersistentEntity<?> entity, @Nullable Object id) { Assert.notNull(object, "Object must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); return new ObjectPath(this, object, id, entity.getCollection()); }
/** * Returns the object with the given id and stored in the given collection if it's contained in the * {@link ObjectPath}. * * @param id must not be {@literal null}. * @param collection must not be {@literal null} or empty. * @return * @deprecated use {@link #getPathItem(Object, String, Class)}. */ @Nullable @Deprecated Object getPathItem(Object id, String collection) { Assert.notNull(id, "Id must not be null!"); Assert.hasText(collection, "Collection name must not be null!"); for (ObjectPath current = this; current != null; current = current.parent) { Object object = current.getObject(); if (object == null || current.getIdValue() == null) { continue; } if (collection.equals(current.getCollection()) && id.equals(current.getIdValue())) { return object; } } return null; }
@Override public String toString() { if (parent == null) { return "[empty]"; } List<String> strings = new ArrayList<>(); for (ObjectPath current = this; current != null; current = current.parent) { strings.add(ObjectUtils.nullSafeToString(current.getObject())); } return StringUtils.collectionToDelimitedString(strings, " -> "); } }
private <S> S populateProperties(MongoPersistentEntity<S> entity, DocumentAccessor documentAccessor, ObjectPath path, SpELExpressionEvaluator evaluator, S instance) { PersistentPropertyAccessor<S> accessor = new ConvertingPropertyAccessor<>(entity.getPropertyAccessor(instance), conversionService); // Make sure id property is set before all other properties Object rawId = readAndPopulateIdentifier(accessor, documentAccessor, entity, path, evaluator); ObjectPath currentPath = path.push(accessor.getBean(), entity, rawId); MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(documentAccessor, evaluator, currentPath); readProperties(entity, accessor, documentAccessor, valueProvider, currentPath, evaluator); return accessor.getBean(); }
/** * Get the object with given {@literal id}, stored in the {@literal collection} that is assignable to the given * {@literal type} or {@literal null} if no match found. * * @param id must not be {@literal null}. * @param collection must not be {@literal null} or empty. * @param type must not be {@literal null}. * @return {@literal null} when no match found. * @since 2.0 */ @Nullable <T> T getPathItem(Object id, String collection, Class<T> type) { Assert.notNull(id, "Id must not be null!"); Assert.hasText(collection, "Collection name must not be null!"); Assert.notNull(type, "Type must not be null!"); for (ObjectPath current = this; current != null; current = current.parent) { Object object = current.getObject(); if (object == null || current.getIdValue() == null) { continue; } if (collection.equals(current.getCollection()) && id.equals(current.getIdValue()) && ClassUtils.isAssignable(type, object.getClass())) { return type.cast(object); } } return null; }