From f71b38b73145232711910f886164184d23f33642 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 25 Aug 2017 09:45:14 +0200 Subject: [PATCH] DATAMONGO-1768 - Polishing. Extend javadocs. Make methods static/reorder methods where possible. Formatting. Original pull request: #496. --- .../core/convert/MongoExampleMapper.java | 191 +++++++++--------- .../convert/MongoExampleMapperUnitTests.java | 9 +- 2 files changed, 105 insertions(+), 95 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java index 55a2b8217..f802d7cb3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java @@ -50,9 +50,13 @@ import com.mongodb.BasicDBObject; import com.mongodb.DBObject; /** + * Mapper from {@link Example} to a query {@link DBObject}. + * * @author Christoph Strobl * @author Mark Paluch * @since 1.8 + * @see Example + * @see org.springframework.data.domain.ExampleMatcher */ public class MongoExampleMapper { @@ -60,6 +64,11 @@ public class MongoExampleMapper { private final MongoConverter converter; private final Map stringMatcherPartMapping = new HashMap(); + /** + * Create a new {@link MongoTypeMapper} given {@link MongoConverter}. + * + * @param converter must not be {@literal null}. + */ public MongoExampleMapper(MongoConverter converter) { this.converter = converter; @@ -101,8 +110,10 @@ public class MongoExampleMapper { DBObject reference = (DBObject) converter.convertToMongoType(example.getProbe()); - if (entity.hasIdProperty() && ClassUtils.isAssignable(entity.getType(), example.getProbeType())) { - if (entity.getIdentifierAccessor(example.getProbe()).getIdentifier() == null) {reference.removeField(entity.getIdProperty().getFieldName());} + if (entity.hasIdProperty() && ClassUtils.isAssignable(entity.getType(), example.getProbeType())) { + if (entity.getIdentifierAccessor(example.getProbe()).getIdentifier() == null) { + reference.removeField(entity.getIdProperty().getFieldName()); + } } ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); @@ -116,77 +127,6 @@ public class MongoExampleMapper { return updateTypeRestrictions(result, example); } - private static DBObject orConcatenate(DBObject source) { - - List foo = new ArrayList(source.keySet().size()); - - for (String key : source.keySet()) { - foo.add(new BasicDBObject(key, source.get(key))); - } - - return new BasicDBObject("$or", foo); - } - - private Set> getTypesToMatch(Example example) { - - Set> types = new HashSet>(); - - for (TypeInformation reference : mappingContext.getManagedTypes()) { - if (example.getProbeType().isAssignableFrom(reference.getType())) { - types.add(reference.getType()); - } - } - - return types; - } - - private String getMappedPropertyPath(String path, Class probeType) { - - MongoPersistentEntity entity = mappingContext.getPersistentEntity(probeType); - - Iterator parts = Arrays.asList(path.split("\\.")).iterator(); - - final Stack stack = new Stack(); - - List resultParts = new ArrayList(); - - while (parts.hasNext()) { - - final String part = parts.next(); - MongoPersistentProperty prop = entity.getPersistentProperty(part); - - if (prop == null) { - - entity.doWithProperties(new PropertyHandler() { - - @Override - public void doWithPersistentProperty(MongoPersistentProperty property) { - - if (property.getFieldName().equals(part)) { - stack.push(property); - } - } - }); - - if (stack.isEmpty()) { - return ""; - } - prop = stack.pop(); - } - - resultParts.add(prop.getName()); - - if (prop.isEntity() && mappingContext.hasPersistentEntityFor(prop.getActualType())) { - entity = mappingContext.getPersistentEntity(prop.getActualType()); - } else { - break; - } - } - - return StringUtils.collectionToDelimitedString(resultParts, "."); - - } - private void applyPropertySpecs(String path, DBObject source, Class probeType, ExampleMatcherAccessor exampleSpecAccessor) { @@ -246,31 +186,51 @@ public class MongoExampleMapper { } } - private boolean isEmptyIdProperty(Entry entry) { - return entry.getKey().equals("_id") && entry.getValue() == null; - } + private String getMappedPropertyPath(String path, Class probeType) { - private void applyStringMatcher(Map.Entry entry, StringMatcher stringMatcher, boolean ignoreCase) { + MongoPersistentEntity entity = mappingContext.getPersistentEntity(probeType); - BasicDBObject dbo = new BasicDBObject(); + Iterator parts = Arrays.asList(path.split("\\.")).iterator(); - if (ObjectUtils.nullSafeEquals(StringMatcher.DEFAULT, stringMatcher)) { + final Stack stack = new Stack(); - if (ignoreCase) { - dbo.put("$regex", Pattern.quote((String) entry.getValue())); - entry.setValue(dbo); + List resultParts = new ArrayList(); + + while (parts.hasNext()) { + + final String part = parts.next(); + MongoPersistentProperty prop = entity.getPersistentProperty(part); + + if (prop == null) { + + entity.doWithProperties(new PropertyHandler() { + + @Override + public void doWithPersistentProperty(MongoPersistentProperty property) { + + if (property.getFieldName().equals(part)) { + stack.push(property); + } + } + }); + + if (stack.isEmpty()) { + return ""; + } + + prop = stack.pop(); } - } else { - Type type = stringMatcherPartMapping.get(stringMatcher); - String expression = MongoRegexCreator.INSTANCE.toRegularExpression((String) entry.getValue(), type); - dbo.put("$regex", expression); - entry.setValue(dbo); + resultParts.add(prop.getName()); + + if (prop.isEntity() && mappingContext.hasPersistentEntityFor(prop.getActualType())) { + entity = mappingContext.getPersistentEntity(prop.getActualType()); + } else { + break; + } } - if (ignoreCase) { - dbo.put("$options", "i"); - } + return StringUtils.collectionToDelimitedString(resultParts, "."); } private DBObject updateTypeRestrictions(DBObject query, Example example) { @@ -307,4 +267,55 @@ public class MongoExampleMapper { return true; } + + private Set> getTypesToMatch(Example example) { + + Set> types = new HashSet>(); + + for (TypeInformation reference : mappingContext.getManagedTypes()) { + if (example.getProbeType().isAssignableFrom(reference.getType())) { + types.add(reference.getType()); + } + } + + return types; + } + + private static boolean isEmptyIdProperty(Entry entry) { + return entry.getKey().equals("_id") && entry.getValue() == null; + } + + private void applyStringMatcher(Map.Entry entry, StringMatcher stringMatcher, boolean ignoreCase) { + + BasicDBObject dbo = new BasicDBObject(); + + if (ObjectUtils.nullSafeEquals(StringMatcher.DEFAULT, stringMatcher)) { + + if (ignoreCase) { + dbo.put("$regex", Pattern.quote((String) entry.getValue())); + entry.setValue(dbo); + } + } else { + + Type type = stringMatcherPartMapping.get(stringMatcher); + String expression = MongoRegexCreator.INSTANCE.toRegularExpression((String) entry.getValue(), type); + dbo.put("$regex", expression); + entry.setValue(dbo); + } + + if (ignoreCase) { + dbo.put("$options", "i"); + } + } + + private static DBObject orConcatenate(DBObject source) { + + List or = new ArrayList(source.keySet().size()); + + for (String key : source.keySet()) { + or.add(new BasicDBObject(key, source.get(key))); + } + + return new BasicDBObject("$or", or); + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java index 470c9b75f..4d7cf0bcd 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java @@ -289,7 +289,7 @@ public class MongoExampleMapperUnitTests { DBObject dbo = mapper.getMappedExample(of(probe), context.getPersistentEntity(WithDBRef.class)); com.mongodb.DBRef reference = getTypedValue(dbo, "referenceDocument", com.mongodb.DBRef.class); - assertThat(reference.getId(), Is.is("200")); + assertThat(reference.getId(), Is. is("200")); assertThat(reference.getCollectionName(), is("refDoc")); } @@ -312,8 +312,8 @@ public class MongoExampleMapperUnitTests { DBObject dbo = mapper.getMappedExample(of(probe), context.getPersistentEntity(WithDBRef.class)); - assertThat(dbo.get("legacyPoint.x"), Is.is(10D)); - assertThat(dbo.get("legacyPoint.y"), Is.is(20D)); + assertThat(dbo.get("legacyPoint.x"), Is. is(10D)); + assertThat(dbo.get("legacyPoint.y"), Is. is(20D)); } @Test // DATAMONGO-1245 @@ -434,8 +434,7 @@ public class MongoExampleMapperUnitTests { probe.flatDoc = new FlatDocument(); probe.flatDoc.stringValue = "conflux"; - DBObject dbo = mapper - .getMappedExample(Example.of(probe, ExampleMatcher.matching().withIgnorePaths("_class"))); + DBObject dbo = mapper.getMappedExample(Example.of(probe, ExampleMatcher.matching().withIgnorePaths("_class"))); assertThat(dbo, isBsonObject().notContaining("_class")); }