Refine search
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!"); String collection = sourceDocument.getString("$ref"); MongoPersistentEntity<?> entity = getMappingContext().getPersistentEntity(targetType); if (entity != null && entity.hasIdProperty()) { id = convertId(id, entity.getIdProperty().getFieldType()); return read(targetType, sourceDocument); } else { if (!ClassUtils.isAssignable(targetType, value.getClass())) { if (getConversionService().canConvert(value.getClass(), targetType)) { return getConversionService().convert(value, targetType); return getConversionService().convert(source, targetType);
@Override public BulkOperations insert(Object document) { Assert.notNull(document, "Document must not be null!"); if (document instanceof Document) { models.add(new InsertOneModel<>((Document) document)); return this; } Document sink = new Document(); mongoOperations.getConverter().write(document, sink); models.add(new InsertOneModel<>(sink)); return this; }
private List<Object> mapEnumValues(Iterable<?> values) { List<Object> converted = new ArrayList<>(); for (Object val : values) { converted.add(converter.convertToMongoType(val)); } return converted; }
/** * Creates a new {@link QueryMapper} with the given {@link MongoConverter}. * * @param converter must not be {@literal null}. */ public QueryMapper(MongoConverter converter) { Assert.notNull(converter, "MongoConverter must not be null!"); this.conversionService = converter.getConversionService(); this.converter = converter; this.mappingContext = converter.getMappingContext(); this.exampleMapper = new MongoExampleMapper(converter); this.schemaMapper = new MongoJsonSchemaMapper(converter); }
private Object doGetConverted(Document fullDocument) { Object result = CONVERTED_UPDATER.get(this); if (result != null) { return result; } if (ClassUtils.isAssignable(Document.class, fullDocument.getClass())) { result = converter.read(targetType, fullDocument); return CONVERTED_UPDATER.compareAndSet(this, null, result) ? result : CONVERTED_UPDATER.get(this); } if (converter.getConversionService().canConvert(fullDocument.getClass(), targetType)) { result = converter.getConversionService().convert(fullDocument, targetType); return CONVERTED_UPDATER.compareAndSet(this, null, result) ? result : CONVERTED_UPDATER.get(this); } throw new IllegalArgumentException(String.format("No converter found capable of converting %s to %s", fullDocument.getClass(), targetType)); }
return getConversionService().canConvert(id.getClass(), targetType) ? getConversionService().convert(id, targetType) : convertToMongoType(id, null); } catch (ConversionException o_O) { return convertToMongoType(id, null);
/** * Create a new {@link MongoJsonSchemaMapper} facilitating the given {@link MongoConverter}. * * @param converter must not be {@literal null}. */ public MongoJsonSchemaMapper(MongoConverter converter) { Assert.notNull(converter, "Converter must not be null!"); this.converter = converter; this.mappingContext = converter.getMappingContext(); }
@Override @SuppressWarnings("unchecked") public <T> T save(T objectToSave, String collectionName) { Assert.notNull(objectToSave, "Object to save must not be null!"); Assert.hasText(collectionName, "Collection name must not be null or empty!"); AdaptibleEntity<T> source = operations.forEntity(objectToSave, mongoConverter.getConversionService()); return source.isVersionedEntity() // ? doSaveVersioned(source, collectionName) // : (T) doSave(collectionName, objectToSave, this.mongoConverter); }
/** * Returns the given {@link Example} as {@link Document} holding matching values extracted from * {@link Example#getProbe()}. * * @param example must not be {@literal null}. * @param entity must not be {@literal null}. * @return */ public Document getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) { Assert.notNull(example, "Example must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); Document reference = (Document) converter.convertToMongoType(example.getProbe()); if (entity.getIdProperty() != null && ClassUtils.isAssignable(entity.getType(), example.getProbeType())) { Object identifier = entity.getIdentifierAccessor(example.getProbe()).getIdentifier(); if (identifier == null) { reference.remove(entity.getIdProperty().getFieldName()); } } ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", reference, example.getProbeType(), matcherAccessor); Document flattened = ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, matcherAccessor.getNullHandler()) ? reference : new Document(SerializationUtils.flattenMap(reference)); Document result = example.getMatcher().isAllMatching() ? flattened : orConcatenate(flattened); return updateTypeRestrictions(result, example); }
protected <T> Collection<T> doInsertBatch(String collectionName, Collection<? extends T> batchToSave, MongoWriter<T> writer) { Assert.notNull(writer, "MongoWriter must not be null!"); T toConvert = maybeEmitEvent(event).getSource(); AdaptibleEntity<T> entity = operations.forEntity(toConvert, mongoConverter.getConversionService()); entity.assertUpdateableIdIfNotSet();
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!"); return read(targetType, sourceDocument); } else { if (!ClassUtils.isAssignable(targetType, value.getClass())) { if (getConversionService().canConvert(value.getClass(), targetType)) { return getConversionService().convert(value, targetType); return getConversionService().convert(source, targetType);
@Override public void afterPropertiesSet() { super.afterPropertiesSet(); Assert.state(operations != null, "MongoTemplate must not be null!"); if (!mappingContextConfigured) { setMappingContext(operations.getConverter().getMappingContext()); } } }
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { for (Document dbo : collection.find(dbk)) { String key = (String) dbo.get(ENTITY_FIELD_NAME); if (log.isDebugEnabled()) { log.debug("Processing key: {}", key); } if (!changeSet.getValues().containsKey(key)) { String className = (String) dbo.get(ENTITY_FIELD_CLASS); if (className == null) { throw new DataIntegrityViolationException( "Unble to convert property " + key + ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available"); } Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader()); Object value = mongoTemplate.getConverter().read(clazz, dbo); if (log.isDebugEnabled()) { log.debug("Adding to ChangeSet: {}", key); } changeSet.set(key, value); } } return null; } });
/** * Create a new {@link MongoTypeMapper} given {@link MongoConverter}. * * @param converter must not be {@literal null}. */ public MongoExampleMapper(MongoConverter converter) { this.converter = converter; this.mappingContext = converter.getMappingContext(); }
/** * Convert a given {@literal value} into a {@link Document}. * * @param value can be {@literal null}. * @return an empty {@link Document} if the source value is {@literal null}. */ protected Document toDocument(@Nullable Object value) { if (value instanceof Document) { return (Document) value; } Document document = new Document(); if (value != null) { converter.write(value, document); } return document; } }
/** * Populates the id property of the saved object, if it's not set already. * * @param savedObject * @param id */ protected <T> T populateIdIfNecessary(T savedObject, Object id) { return operations.forEntity(savedObject, mongoConverter.getConversionService()) // .populateIdIfNecessary(id); }
private Serializable readObject(DBObject dbObject) throws ClassNotFoundException { TypeInformation<?> information = mapper.getTypeMapper().readType(dbObject); if (information == null) { return null; } return (Serializable) mapper.read(information.getType(), dbObject); }
/** * Returns whether the given {@link String} is the type key. * * @param key * @return * @see MongoTypeMapper#isTypeKey(String) * @since 2.2 */ protected boolean isTypeKey(String key) { return converter.getTypeMapper().isTypeKey(key); }
/** * Creates a new {@link ReactiveMongoRepositoryFactory} with the given {@link ReactiveMongoOperations}. * * @param mongoOperations must not be {@literal null}. */ public ReactiveMongoRepositoryFactory(ReactiveMongoOperations mongoOperations) { Assert.notNull(mongoOperations, "ReactiveMongoOperations must not be null!"); this.operations = mongoOperations; this.mappingContext = mongoOperations.getConverter().getMappingContext(); }
public <T> Mono<T> save(T objectToSave, String collectionName) { Assert.notNull(objectToSave, "Object to save must not be null!"); Assert.hasText(collectionName, "Collection name must not be null or empty!"); AdaptibleEntity<T> source = operations.forEntity(objectToSave, mongoConverter.getConversionService()); return source.isVersionedEntity() ? doSaveVersioned(source, collectionName) : doSave(collectionName, objectToSave, this.mongoConverter); }