public String convert(MongoPersistentProperty source) { return source.getFieldName(); } }
@Override public String convert(MongoPersistentProperty source) { if (associationFound) { return null; } if (property.equals(source)) { associationFound = true; } return source.getFieldName(); } }
private Document getMappedTextScoreField(MongoPersistentProperty property) { return new Document(property.getFieldName(), META_TEXT_SCORE); }
private String pathAwareIndexName(String indexName, String dotPath, @Nullable MongoPersistentProperty property) { String nameToUse = StringUtils.hasText(indexName) ? indexName : ""; if (!StringUtils.hasText(dotPath) || (property != null && dotPath.equals(property.getFieldName()))) { return StringUtils.hasText(nameToUse) ? nameToUse : dotPath; } if (StringUtils.hasText(dotPath)) { nameToUse = StringUtils.hasText(nameToUse) ? (property != null ? dotPath.replace("." + property.getFieldName(), "") : dotPath) + "." + nameToUse : dotPath; } return nameToUse; }
private String getFieldName(@Nullable PersistentEntity<?, MongoPersistentProperty> entity, String sourceField) { if (entity == null) { return sourceField; } MongoPersistentProperty property = entity.getPersistentProperty(sourceField); return property != null ? property.getFieldName() : sourceField; } }
private void assertUniqueness(MongoPersistentProperty property) { String fieldName = property.getFieldName(); MongoPersistentProperty existingProperty = properties.get(fieldName); if (existingProperty != null) { throw new MappingException( String.format(AMBIGUOUS_FIELD_MAPPING, property.toString(), existingProperty.toString(), fieldName)); } properties.put(fieldName, property); } }
@Override public String getIdFieldName() { return entity.getRequiredIdProperty().getFieldName(); }
@Override public boolean isIdField() { MongoPersistentProperty idProperty = (property != null && property.isIdProperty()) ? property : entity.getIdProperty(); if (idProperty != null) { return name.equals(idProperty.getName()) || name.equals(idProperty.getFieldName()) || name.endsWith("." + idProperty.getName()) || name.endsWith("." + idProperty.getFieldName()); } return DEFAULT_ID_NAMES.contains(name); }
private boolean containsVersionProperty(Document document, @Nullable MongoPersistentEntity<?> persistentEntity) { if (persistentEntity == null || !persistentEntity.hasVersionProperty()) { return false; } return document.containsKey(persistentEntity.getRequiredIdProperty().getFieldName()); }
private void increaseVersionForUpdateIfNecessary(@Nullable MongoPersistentEntity<?> persistentEntity, UpdateDefinition update) { if (persistentEntity != null && persistentEntity.hasVersionProperty()) { String versionFieldName = persistentEntity.getRequiredVersionProperty().getFieldName(); if (!update.modifies(versionFieldName)) { update.inc(versionFieldName); } } }
private void increaseVersionForUpdateIfNecessary(@Nullable MongoPersistentEntity<?> persistentEntity, UpdateDefinition update) { if (persistentEntity != null && persistentEntity.hasVersionProperty()) { String versionFieldName = persistentEntity.getRequiredVersionProperty().getFieldName(); if (!update.modifies(versionFieldName)) { update.inc(versionFieldName); } } }
private void resolveAndAddIndexesForAssociation(Association<MongoPersistentProperty> association, List<IndexDefinitionHolder> indexes, String path, String collection) { MongoPersistentProperty property = association.getInverse(); String propertyDotPath = (StringUtils.hasText(path) ? path + "." : "") + property.getFieldName(); if (property.isAnnotationPresent(GeoSpatialIndexed.class) || property.isAnnotationPresent(TextIndexed.class)) { throw new MappingException( String.format("Cannot create geospatial-/text- index on DBRef in collection '%s' for path '%s'.", collection, propertyDotPath)); } IndexDefinitionHolder indexDefinitionHolder = createIndexDefinitionHolderForProperty(propertyDotPath, collection, property); if (indexDefinitionHolder != null) { indexes.add(indexDefinitionHolder); } }
private String createMapKey(MongoPersistentProperty property) { return ClassUtils.getShortName(property.getOwner().getType()) + ":" + property.getFieldName(); }
private void potentiallyAddIndexForProperty(MongoPersistentEntity<?> root, MongoPersistentProperty persistentProperty, List<IndexDefinitionHolder> indexes, CycleGuard guard) { try { if (persistentProperty.isEntity()) { indexes.addAll(resolveIndexForClass(persistentProperty.getTypeInformation().getActualType(), persistentProperty.getFieldName(), Path.of(persistentProperty), root.getCollection(), guard)); } IndexDefinitionHolder indexDefinitionHolder = createIndexDefinitionHolderForProperty( persistentProperty.getFieldName(), root.getCollection(), persistentProperty); if (indexDefinitionHolder != null) { indexes.add(indexDefinitionHolder); } } catch (CyclicPropertyReferenceException e) { LOGGER.info(e.getMessage()); } }
private void guradAndPotentiallyAddIndexForProperty(MongoPersistentProperty persistentProperty, String dotPath, Path path, String collection, List<IndexDefinitionHolder> indexes, CycleGuard guard) { String propertyDotPath = (StringUtils.hasText(dotPath) ? dotPath + "." : "") + persistentProperty.getFieldName(); Path propertyPath = path.append(persistentProperty); guard.protect(persistentProperty, propertyPath); if (persistentProperty.isEntity()) { try { indexes.addAll(resolveIndexForClass(persistentProperty.getTypeInformation().getActualType(), propertyDotPath, propertyPath, collection, guard)); } catch (CyclicPropertyReferenceException e) { LOGGER.info(e.getMessage()); } } IndexDefinitionHolder indexDefinitionHolder = createIndexDefinitionHolderForProperty(propertyDotPath, collection, persistentProperty); if (indexDefinitionHolder != null) { indexes.add(indexDefinitionHolder); } }
/** * Detect a cycle in a property path if the property was seen at least once. * * @param property The property to inspect * @param path The type path under which the property can be reached. * @throws CyclicPropertyReferenceException in case a potential cycle is detected. * @see Path#isCycle() */ void protect(MongoPersistentProperty property, Path path) throws CyclicPropertyReferenceException { String propertyTypeKey = createMapKey(property); if (!seenProperties.add(propertyTypeKey)) { if (path.isCycle()) { throw new CyclicPropertyReferenceException(property.getFieldName(), property.getOwner().getType(), path.toCyclePath()); } } }
private void mapMetaAttributes(Document source, @Nullable MongoPersistentEntity<?> entity, MetaMapping metaMapping) { if (entity == null) { return; } if (entity.hasTextScoreProperty() && !MetaMapping.IGNORE.equals(metaMapping)) { MongoPersistentProperty textScoreProperty = entity.getTextScoreProperty(); if (MetaMapping.FORCE.equals(metaMapping) || (MetaMapping.WHEN_PRESENT.equals(metaMapping) && source.containsKey(textScoreProperty.getFieldName()))) { source.putAll(getMappedTextScoreField(textScoreProperty)); } } }
@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; } }
@Override protected String getKeyForPath(Path<?> expr, PathMetadata metadata) { if (!metadata.getPathType().equals(PathType.PROPERTY)) { return super.getKeyForPath(expr, metadata); } Path<?> parent = metadata.getParent(); MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(parent.getType()); MongoPersistentProperty property = entity.getPersistentProperty(metadata.getName()); return property == null ? super.getKeyForPath(expr, metadata) : property.getFieldName(); }
/** * 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); }