Refine search
/** * @param entity * @param key * @param mappingContext * @return */ protected Field createPropertyField(@Nullable MongoPersistentEntity<?> entity, String key, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) { if (entity == null) { return new Field(key); } if (Field.ID_KEY.equals(key)) { return new MetadataBackedField(key, entity, mappingContext, entity.getIdProperty()); } return new MetadataBackedField(key, entity, mappingContext); }
private String getMappedPropertyPath(String path, Class<?> probeType) { MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(probeType); Iterator<String> parts = Arrays.asList(path.split("\\.")).iterator(); final Stack<MongoPersistentProperty> stack = new Stack<>(); List<String> resultParts = new ArrayList<>(); while (parts.hasNext()) { String part = parts.next(); MongoPersistentProperty prop = entity.getPersistentProperty(part); if (prop == null) { entity.doWithProperties((PropertyHandler<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.getRequiredPersistentEntity(prop.getActualType()); } else { break; } } return StringUtils.collectionToDelimitedString(resultParts, "."); }
/** * Resolve the {@link IndexDefinition}s for given {@literal root} entity by traversing {@link MongoPersistentProperty} * scanning for index annotations {@link Indexed}, {@link CompoundIndex} and {@link GeospatialIndex}. The given * {@literal root} has therefore to be annotated with {@link Document}. * * @param root must not be null. * @return List of {@link IndexDefinitionHolder}. Will never be {@code null}. * @throws IllegalArgumentException in case of missing {@link Document} annotation marking root entities. */ public List<IndexDefinitionHolder> resolveIndexForEntity(final MongoPersistentEntity<?> root) { Assert.notNull(root, "Index cannot be resolved for given 'null' entity."); Document document = root.findAnnotation(Document.class); Assert.notNull(document, "Given entity is not collection root."); final List<IndexDefinitionHolder> indexInformation = new ArrayList<>(); indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions("", root.getCollection(), root)); indexInformation.addAll(potentiallyCreateTextIndexDefinition(root)); root.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> this .potentiallyAddIndexForProperty(root, property, indexInformation, new CycleGuard())); indexInformation.addAll(resolveIndexesForDbrefs("", root.getCollection(), root)); return indexInformation; }
/** * Returns the name of the identifier property. Considers mapping information but falls back to the MongoDB default of * {@code _id} if no identifier property can be found. * * @param type must not be {@literal null}. * @return */ public String getIdPropertyName(Class<?> type) { Assert.notNull(type, "Type must not be null!"); MongoPersistentEntity<?> persistentEntity = context.getPersistentEntity(type); if (persistentEntity != null && persistentEntity.getIdProperty() != null) { return persistentEntity.getRequiredIdProperty().getName(); } return ID_FIELD; }
private static <T> MappedEntity<T> of(T bean, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> context) { MongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(bean.getClass()); IdentifierAccessor identifierAccessor = entity.getIdentifierAccessor(bean); PersistentPropertyAccessor<T> propertyAccessor = entity.getPropertyAccessor(bean); return new MappedEntity<>(entity, identifierAccessor, propertyAccessor); }
private void assertUpdateableIdIfNotSet(Object value) { MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(value.getClass()); if (entity != null && entity.hasIdProperty()) { MongoPersistentProperty property = entity.getRequiredIdProperty(); Object propertyValue = entity.getPropertyAccessor(value).getProperty(property); if (propertyValue != null) { return; } if (!MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(property.getType())) { throw new InvalidDataAccessApiUsageException( String.format("Cannot autogenerate id of type %s for entity of type %s!", property.getType().getName(), value.getClass().getName())); } } }
public String determineCollectionName(@Nullable Class<?> entityClass) { if (entityClass == null) { throw new InvalidDataAccessApiUsageException( "No class parameter provided, entity collection can't be determined!"); } return context.getRequiredPersistentEntity(entityClass).getCollection(); }
/** * Returns a copy of the {@link ObjectPath} with the given {@link Object} as current object. * * @param object must not be {@literal null}. * @param entity must not be {@literal null}. * @param id must not be {@literal null}. * @return new instance of {@link ObjectPath}. */ ObjectPath push(Object object, MongoPersistentEntity<?> entity, @Nullable Object id) { Assert.notNull(object, "Object must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); return new ObjectPath(this, object, id, entity.getCollection()); }
@Nullable private MongoPersistentProperty getPropertyFor(Path<?> path) { Path<?> parent = path.getMetadata().getParent(); if (parent == null || !PATH_TYPES.contains(path.getMetadata().getPathType())) { return null; } MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(parent.getType()); return entity != null ? entity.getPersistentProperty(path.getMetadata().getName()) : null; }
@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; } }
@Nullable private MongoPersistentEntity<?> lookupPersistentEntity(@Nullable Class<?> entityType, String collection) { if (entityType != null) { return mapper.getMappingContext().getRequiredPersistentEntity(entityType); } Collection<? extends MongoPersistentEntity<?>> entities = mapper.getMappingContext().getPersistentEntities(); for (MongoPersistentEntity<?> entity : entities) { if (entity.getCollection().equals(collection)) { return entity; } } return null; }
@Nullable private MongoPersistentEntity<?> lookupPersistentEntity(String collection) { Collection<? extends MongoPersistentEntity<?>> entities = queryMapper.getMappingContext().getPersistentEntities(); return entities.stream() // .filter(entity -> entity.getCollection().equals(collection)) // .findFirst() // .orElse(null); }
protected void writeInternal(@Nullable Object obj, Bson bson, @Nullable MongoPersistentEntity<?> entity) { if (obj == null) { return; } if (null == entity) { throw new MappingException("No mapping metadata found for entity of type " + obj.getClass().getName()); } PersistentPropertyAccessor<?> accessor = entity.getPropertyAccessor(obj); DocumentAccessor dbObjectAccessor = new DocumentAccessor(bson); MongoPersistentProperty idProperty = entity.getIdProperty(); if (idProperty != null && !dbObjectAccessor.hasValue(idProperty)) { Object value = idMapper.convertId(accessor.getProperty(idProperty), idProperty.getFieldType()); if (value != null) { dbObjectAccessor.put(idProperty, value); } } writeProperties(bson, entity, accessor, dbObjectAccessor, idProperty); }
@Nullable default <S, T> T mapValueToTargetType(S source, Class<T> targetType, DbRefResolver dbRefResolver) { String collection = sourceDocument.getString("$ref"); MongoPersistentEntity<?> entity = getMappingContext().getPersistentEntity(targetType); if (entity != null && entity.hasIdProperty()) { id = convertId(id, entity.getIdProperty().getFieldType());
/** * 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)); }
protected void writeInternal(@Nullable Object obj, Bson bson, @Nullable MongoPersistentEntity<?> entity) { if (obj == null) { return; } if (null == entity) { throw new MappingException("No mapping metadata found for entity of type " + obj.getClass().getName()); } PersistentPropertyAccessor<?> accessor = entity.getPropertyAccessor(obj); DocumentAccessor dbObjectAccessor = new DocumentAccessor(bson); MongoPersistentProperty idProperty = entity.getIdProperty(); if (idProperty != null && !dbObjectAccessor.hasValue(idProperty)) { Object value = idMapper.convertId(accessor.getProperty(idProperty)); if (value != null) { dbObjectAccessor.put(idProperty, value); } } writeProperties(bson, entity, accessor, dbObjectAccessor, idProperty); }
@Override @SuppressWarnings("unchecked") public MongoEntityMetadata<?> getEntityInformation() { if (metadata == null) { Class<?> returnedObjectType = getReturnedObjectType(); Class<?> domainClass = getDomainClass(); if (ClassUtils.isPrimitiveOrWrapper(returnedObjectType)) { this.metadata = new SimpleMongoEntityMetadata<Object>((Class<Object>) domainClass, mappingContext.getRequiredPersistentEntity(domainClass)); } else { MongoPersistentEntity<?> returnedEntity = mappingContext.getPersistentEntity(returnedObjectType); MongoPersistentEntity<?> managedEntity = mappingContext.getRequiredPersistentEntity(domainClass); returnedEntity = returnedEntity == null || returnedEntity.getType().isInterface() ? managedEntity : returnedEntity; MongoPersistentEntity<?> collectionEntity = domainClass.isAssignableFrom(returnedObjectType) ? returnedEntity : managedEntity; this.metadata = new SimpleMongoEntityMetadata<>((Class<Object>) returnedEntity.getType(), collectionEntity); } } return this.metadata; }
protected DBRef createDBRef(Object target, MongoPersistentProperty property) { Assert.notNull(target, "Target object must not be null!"); if (target instanceof DBRef) { return (DBRef) target; } MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass()); targetEntity = targetEntity != null ? targetEntity : mappingContext.getPersistentEntity(property); if (null == targetEntity) { throw new MappingException("No mapping metadata found for " + target.getClass()); } MongoPersistentEntity<?> entity = targetEntity; MongoPersistentProperty idProperty = entity.getIdProperty(); if (idProperty != null) { Object id = target.getClass().equals(idProperty.getType()) ? target : entity.getPropertyAccessor(target).getProperty(idProperty); if (null == id) { throw new MappingException("Cannot create a reference to an object with a NULL id."); } return dbRefResolver.createDbRef(property == null ? null : property.getDBRef(), entity, idMapper.convertId(id, idProperty != null ? idProperty.getFieldType() : ObjectId.class)); } throw new MappingException("No id property found on class " + entity.getType()); }
/** * Get the basic services from mongo template */ private synchronized void fetchMappingContextAndConversionService() { if(mappingContext == null) { MongoConverter mongoConverter = this.mongoTemplate.getConverter(); mappingContext = mongoConverter.getMappingContext(); conversionService = mongoConverter.getConversionService(); MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entityClass); MongoPersistentProperty idProperty = persistentEntity.getIdProperty(); this.idKey = idProperty == null ? "_id" : idProperty.getName(); } }
IPropertyDescriptor propertyDescriptor = entityDescriptor.getPropertyDescriptor(propertyName); if (propertyDescriptor != null && !propertyDescriptor.isComputed() && !IEntity.ID.equals(propertyName)) { String convertedPropertyName = getConverter().getMappingContext().getPersistentEntity(componentContract) .getPersistentProperty(PropertyHelper.toJavaBeanPropertyName(propertyName)) .getFieldName(); if (propertyValue instanceof IComponent) { if (propertyValue instanceof IEntity) {