/** * 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); }
String mappedPropertyPath = getMappedPropertyPath(propertyPath, probeType); if (isEmptyIdProperty(entry)) { iter.remove(); continue; : getMappedPropertyPath(propertyPath, probeType); applyStringMatcher(entry, stringMatcher, ignoreCase); } else if (entry.getValue() instanceof Document) { applyPropertySpecs(propertyPath, (Document) entry.getValue(), probeType, exampleSpecAccessor);
/** * Returns the given {@link Example} as {@link Document} holding matching values extracted from * {@link Example#getProbe()}. * * @param example must not be {@literal null}. * @return */ public Document getMappedExample(Example<?> example) { Assert.notNull(example, "Example must not be null!"); return getMappedExample(example, mappingContext.getRequiredPersistentEntity(example.getProbeType())); }
private Document updateTypeRestrictions(Document query, Example example) { Document result = new Document(); if (isTypeRestricting(example)) { result.putAll(query); this.converter.getTypeMapper().writeTypeRestrictions(result, getTypesToMatch(example)); return result; } for (Map.Entry<String, Object> entry : query.entrySet()) { if (!this.converter.getTypeMapper().isTypeKey(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } } return result; }
/** * 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); }
private static void applyStringMatcher(Map.Entry<String, Object> entry, StringMatcher stringMatcher, boolean ignoreCase) { Document document = new Document(); if (StringMatcher.DEFAULT == stringMatcher) { if (ignoreCase) { document.put("$regex", Pattern.quote((String) entry.getValue())); entry.setValue(document); } } else { String expression = MongoRegexCreator.INSTANCE.toRegularExpression((String) entry.getValue(), toMatchMode(stringMatcher)); document.put("$regex", expression); entry.setValue(document); } if (ignoreCase) { document.put("$options", "i"); } }
/** * 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); }
private Document updateTypeRestrictions(Document query, Example example) { Document result = new Document(); if (isTypeRestricting(example)) { result.putAll(query); this.converter.getTypeMapper().writeTypeRestrictions(result, getTypesToMatch(example)); return result; } for (Map.Entry<String, Object> entry : query.entrySet()) { if (!this.converter.getTypeMapper().isTypeKey(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } } return result; }
private static void applyStringMatcher(Map.Entry<String, Object> entry, StringMatcher stringMatcher, boolean ignoreCase) { Document document = new Document(); if (StringMatcher.DEFAULT == stringMatcher) { if (ignoreCase) { document.put("$regex", Pattern.quote((String) entry.getValue())); entry.setValue(document); } } else { String expression = MongoRegexCreator.INSTANCE.toRegularExpression((String) entry.getValue(), toMatchMode(stringMatcher)); document.put("$regex", expression); entry.setValue(document); } if (ignoreCase) { document.put("$options", "i"); } }
String mappedPropertyPath = getMappedPropertyPath(propertyPath, probeType); if (isEmptyIdProperty(entry)) { iter.remove(); continue; : getMappedPropertyPath(propertyPath, probeType); applyStringMatcher(entry, stringMatcher, ignoreCase); } else if (entry.getValue() instanceof Document) { applyPropertySpecs(propertyPath, (Document) entry.getValue(), probeType, exampleSpecAccessor);
/** * 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)); }
/** * 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); }
/** * Returns the given {@link Example} as {@link Document} holding matching values extracted from * {@link Example#getProbe()}. * * @param example must not be {@literal null}. * @return */ public Document getMappedExample(Example<?> example) { Assert.notNull(example, "Example must not be null!"); return getMappedExample(example, mappingContext.getRequiredPersistentEntity(example.getProbeType())); }
/** * 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)); }