/** * 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 Document mapProperties(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, Document source) { Document sink = new Document(); for (String fieldName : source.keySet()) { String mappedFieldName = getFieldName(entity, fieldName); Document mappedProperty = mapProperty(entity, fieldName, source.get(fieldName, Document.class)); sink.append(mappedFieldName, mappedProperty); } return sink; }
private Document mapEnumValuesIfNecessary(Document source) { Document sink = new Document(source); if (source.containsKey(ENUM_FIELD)) { sink.replace(ENUM_FIELD, mapEnumValues(source.get(ENUM_FIELD, Iterable.class))); } return sink; }
@SuppressWarnings("unchecked") private Document mapSchemaObject(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, Document source) { Document sink = new Document(source); if (source.containsKey(REQUIRED_FIELD)) { sink.replace(REQUIRED_FIELD, mapRequiredProperties(entity, source.get(REQUIRED_FIELD, Collection.class))); } if (source.containsKey(PROPERTIES_FIELD)) { sink.replace(PROPERTIES_FIELD, mapProperties(entity, source.get(PROPERTIES_FIELD, Document.class))); } mapEnumValuesIfNecessary(sink); return sink; }
private Document mapProperty(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, String sourceFieldName, Document source) { Document sink = new Document(source); if (entity != null && sink.containsKey(Type.objectType().representation())) { MongoPersistentProperty property = entity.getPersistentProperty(sourceFieldName); if (property != null && property.isEntity()) { sink = mapSchemaObject(mappingContext.getPersistentEntity(property.getActualType()), source); } } return mapEnumValuesIfNecessary(sink); }
private List<String> mapRequiredProperties(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, Collection<String> sourceFields) { return sourceFields.stream() /// .map(fieldName -> getFieldName(entity, fieldName)) // .collect(Collectors.toList()); }
public Document mapSchema(Document jsonSchema, Class<?> type) { Assert.notNull(jsonSchema, "Schema must not be null!"); Assert.notNull(type, "Type must not be null! Please consider Object.class."); Assert.isTrue(jsonSchema.containsKey($JSON_SCHEMA), () -> String.format("Document does not contain $jsonSchema field. Found %s.", jsonSchema)); if (Object.class.equals(type)) { return new Document(jsonSchema); } return new Document($JSON_SCHEMA, mapSchemaObject(mappingContext.getPersistentEntity(type), jsonSchema.get($JSON_SCHEMA, Document.class))); }
/** * Returns the given {@link Document} representing a keyword by mapping the keyword's value. * * @param keyword the {@link Document} representing a keyword (e.g. {@code $ne : … } ) * @param entity * @return */ protected Document getMappedKeyword(Keyword keyword, @Nullable MongoPersistentEntity<?> entity) { // $or/$nor if (keyword.isOrOrNor() || (keyword.hasIterableValue() && !keyword.isGeometry())) { Iterable<?> conditions = keyword.getValue(); List<Object> newConditions = new ArrayList<Object>(); for (Object condition : conditions) { newConditions.add(isDocument(condition) ? getMappedObject((Document) condition, entity) : convertSimpleOrDocument(condition, entity)); } return new Document(keyword.getKey(), newConditions); } if (keyword.isSample()) { return exampleMapper.getMappedExample(keyword.<Example<?>> getValue(), entity); } if (keyword.isJsonSchema()) { return schemaMapper.mapSchema(new Document(keyword.getKey(), keyword.getValue()), entity.getType()); } return new Document(keyword.getKey(), convertSimpleOrDocument(keyword.getValue(), entity)); }
@SuppressWarnings("unchecked") private Document mapSchemaObject(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, Document source) { Document sink = new Document(source); if (source.containsKey(REQUIRED_FIELD)) { sink.replace(REQUIRED_FIELD, mapRequiredProperties(entity, source.get(REQUIRED_FIELD, Collection.class))); } if (source.containsKey(PROPERTIES_FIELD)) { sink.replace(PROPERTIES_FIELD, mapProperties(entity, source.get(PROPERTIES_FIELD, Document.class))); } mapEnumValuesIfNecessary(sink); return sink; }
private Document mapProperty(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, String sourceFieldName, Document source) { Document sink = new Document(source); if (entity != null && sink.containsKey(Type.objectType().representation())) { MongoPersistentProperty property = entity.getPersistentProperty(sourceFieldName); if (property != null && property.isEntity()) { sink = mapSchemaObject(mappingContext.getPersistentEntity(property.getActualType()), source); } } return mapEnumValuesIfNecessary(sink); }
private List<String> mapRequiredProperties(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, Collection<String> sourceFields) { return sourceFields.stream() /// .map(fieldName -> getFieldName(entity, fieldName)) // .collect(Collectors.toList()); }
public Document mapSchema(Document jsonSchema, Class<?> type) { Assert.notNull(jsonSchema, "Schema must not be null!"); Assert.notNull(type, "Type must not be null! Please consider Object.class."); Assert.isTrue(jsonSchema.containsKey($JSON_SCHEMA), () -> String.format("Document does not contain $jsonSchema field. Found %s.", jsonSchema)); if (Object.class.equals(type)) { return new Document(jsonSchema); } return new Document($JSON_SCHEMA, mapSchemaObject(mappingContext.getPersistentEntity(type), jsonSchema.get($JSON_SCHEMA, Document.class))); }
/** * Returns the given {@link Document} representing a keyword by mapping the keyword's value. * * @param keyword the {@link Document} representing a keyword (e.g. {@code $ne : … } ) * @param entity * @return */ protected Document getMappedKeyword(Keyword keyword, @Nullable MongoPersistentEntity<?> entity) { // $or/$nor if (keyword.isOrOrNor() || (keyword.hasIterableValue() && !keyword.isGeometry())) { Iterable<?> conditions = keyword.getValue(); List<Object> newConditions = new ArrayList<Object>(); for (Object condition : conditions) { newConditions.add(isDocument(condition) ? getMappedObject((Document) condition, entity) : convertSimpleOrDocument(condition, entity)); } return new Document(keyword.getKey(), newConditions); } if (keyword.isSample()) { return exampleMapper.getMappedExample(keyword.<Example<?>> getValue(), entity); } if (keyword.isJsonSchema()) { return schemaMapper.mapSchema(new Document(keyword.getKey(), keyword.getValue()), entity.getType()); } return new Document(keyword.getKey(), convertSimpleOrDocument(keyword.getValue(), entity)); }
private Document mapProperties(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, Document source) { Document sink = new Document(); for (String fieldName : source.keySet()) { String mappedFieldName = getFieldName(entity, fieldName); Document mappedProperty = mapProperty(entity, fieldName, source.get(fieldName, Document.class)); sink.append(mappedFieldName, mappedProperty); } return sink; }
/** * Constructor used for a basic template configuration. * * @param mongoDbFactory must not be {@literal null}. * @param mongoConverter */ public MongoTemplate(MongoDbFactory mongoDbFactory, @Nullable MongoConverter mongoConverter) { Assert.notNull(mongoDbFactory, "MongoDbFactory must not be null!"); this.mongoDbFactory = mongoDbFactory; this.exceptionTranslator = mongoDbFactory.getExceptionTranslator(); this.mongoConverter = mongoConverter == null ? getDefaultMongoConverter(mongoDbFactory) : mongoConverter; this.queryMapper = new QueryMapper(this.mongoConverter); this.updateMapper = new UpdateMapper(this.mongoConverter); this.schemaMapper = new MongoJsonSchemaMapper(this.mongoConverter); this.projectionFactory = new SpelAwareProxyProjectionFactory(); this.operations = new EntityOperations(this.mongoConverter.getMappingContext()); this.propertyOperations = new PropertyOperations(this.mongoConverter.getMappingContext()); // We always have a mapping context in the converter, whether it's a simple one or not mappingContext = this.mongoConverter.getMappingContext(); // We create indexes based on mapping events if (mappingContext instanceof MongoMappingContext) { MongoMappingContext mappingContext = (MongoMappingContext) this.mappingContext; if (mappingContext.isAutoIndexCreation()) { indexCreator = new MongoPersistentEntityIndexCreator(mappingContext, this); eventPublisher = new MongoMappingEventPublisher(indexCreator); mappingContext.setApplicationEventPublisher(eventPublisher); } } }
private Document mapEnumValuesIfNecessary(Document source) { Document sink = new Document(source); if (source.containsKey(ENUM_FIELD)) { sink.replace(ENUM_FIELD, mapEnumValues(source.get(ENUM_FIELD, Iterable.class))); } return sink; }
this.queryMapper = new QueryMapper(this.mongoConverter); this.updateMapper = new UpdateMapper(this.mongoConverter); this.schemaMapper = new MongoJsonSchemaMapper(this.mongoConverter); this.projectionFactory = new SpelAwareProxyProjectionFactory(); this.indexCreatorListener = new IndexCreatorEventListener(subscriptionExceptionHandler);
/** * 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); }
this.queryMapper = new QueryMapper(this.mongoConverter); this.updateMapper = new UpdateMapper(this.mongoConverter); this.schemaMapper = new MongoJsonSchemaMapper(this.mongoConverter); this.projectionFactory = new SpelAwareProxyProjectionFactory(); this.indexCreatorListener = new IndexCreatorEventListener(subscriptionExceptionHandler);
/** * Constructor used for a basic template configuration. * * @param mongoDbFactory must not be {@literal null}. * @param mongoConverter */ public MongoTemplate(MongoDbFactory mongoDbFactory, @Nullable MongoConverter mongoConverter) { Assert.notNull(mongoDbFactory, "MongoDbFactory must not be null!"); this.mongoDbFactory = mongoDbFactory; this.exceptionTranslator = mongoDbFactory.getExceptionTranslator(); this.mongoConverter = mongoConverter == null ? getDefaultMongoConverter(mongoDbFactory) : mongoConverter; this.queryMapper = new QueryMapper(this.mongoConverter); this.updateMapper = new UpdateMapper(this.mongoConverter); this.schemaMapper = new MongoJsonSchemaMapper(this.mongoConverter); this.projectionFactory = new SpelAwareProxyProjectionFactory(); this.operations = new EntityOperations(this.mongoConverter.getMappingContext()); this.propertyOperations = new PropertyOperations(this.mongoConverter.getMappingContext()); // We always have a mapping context in the converter, whether it's a simple one or not mappingContext = this.mongoConverter.getMappingContext(); // We create indexes based on mapping events if (mappingContext instanceof MongoMappingContext) { indexCreator = new MongoPersistentEntityIndexCreator((MongoMappingContext) mappingContext, this); eventPublisher = new MongoMappingEventPublisher(indexCreator); if (mappingContext instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) mappingContext).setApplicationEventPublisher(eventPublisher); } } }