private void checkForIndexes(final MongoPersistentEntity<?> entity) { Class<?> type = entity.getType(); if (!classesSeen.containsKey(type)) { this.classesSeen.put(type, Boolean.TRUE); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Analyzing class " + type + " for index information."); } checkForAndCreateIndexes(entity); } }
/** * Inspect entities for index creation. * * @return a {@link Mono} that completes without value after indexes were created. */ public Mono<Void> checkForIndexes(MongoPersistentEntity<?> entity) { Class<?> type = entity.getType(); if (!classesSeen.containsKey(type)) { if (this.classesSeen.put(type, Boolean.TRUE) == null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Analyzing class " + type + " for index information."); } return checkForAndCreateIndexes(entity); } } return Mono.empty(); }
/** * Creates a new {@link AbstractMongoQuery} from the given {@link MongoQueryMethod} and {@link MongoOperations}. * * @param method must not be {@literal null}. * @param operations must not be {@literal null}. */ public AbstractMongoQuery(MongoQueryMethod method, MongoOperations operations) { Assert.notNull(operations, "MongoOperations must not be null!"); Assert.notNull(method, "MongoQueryMethod must not be null!"); this.method = method; this.operations = operations; MongoEntityMetadata<?> metadata = method.getEntityInformation(); Class<?> type = metadata.getCollectionEntity().getType(); this.executableFind = operations.query(type); }
private Document getMappedFieldsObject(Document fields, MongoPersistentEntity<?> entity, Class<?> targetType) { Document projectedFields = propertyOperations.computeFieldsForProjection(projectionFactory, fields, entity.getType(), targetType); if (ObjectUtils.nullSafeEquals(fields, projectedFields)) { return queryMapper.getMappedFields(projectedFields, entity); } return queryMapper.getMappedFields(projectedFields, mappingContext.getRequiredPersistentEntity(targetType)); }
private Document getMappedFieldsObject(Document fields, MongoPersistentEntity<?> entity, Class<?> targetType) { Document projectedFields = propertyOperations.computeFieldsForProjection(projectionFactory, fields, entity.getType(), targetType); if (ObjectUtils.nullSafeEquals(fields, projectedFields)) { return queryMapper.getMappedFields(projectedFields, entity); } return queryMapper.getMappedFields(projectedFields, mappingContext.getRequiredPersistentEntity(targetType)); }
public void assertUpdateableIdIfNotSet() { if (!entity.hasIdProperty()) { return; } MongoPersistentProperty property = entity.getRequiredIdProperty(); Object propertyValue = idAccessor.getIdentifier(); 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(), entity.getType().getName())); } }
/** * Creates a new {@link AbstractReactiveMongoQuery} from the given {@link MongoQueryMethod} and * {@link MongoOperations}. * * @param method must not be {@literal null}. * @param operations must not be {@literal null}. */ public AbstractReactiveMongoQuery(ReactiveMongoQueryMethod method, ReactiveMongoOperations operations) { Assert.notNull(method, "MongoQueryMethod must not be null!"); Assert.notNull(operations, "ReactiveMongoOperations must not be null!"); this.method = method; this.operations = operations; this.instantiators = new EntityInstantiators(); MongoEntityMetadata<?> metadata = method.getEntityInformation(); Class<?> type = metadata.getCollectionEntity().getType(); this.findOperationWithProjection = operations.query(type); }
@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; }
private Collection<? extends IndexDefinitionHolder> potentiallyCreateTextIndexDefinition( MongoPersistentEntity<?> root) { String name = root.getType().getSimpleName() + "_TextIndex"; if (name.getBytes().length > 127) { String[] args = ClassUtils.getShortNameAsProperty(root.getType()).split("\\."); name = ""; Iterator<String> it = Arrays.asList(args).iterator();
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()); }
/** * 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)); }
@Override public Query getByIdQuery() { if (!entity.hasIdProperty()) { throw new MappingException("No id property found for object of type " + entity.getType() + "!"); } MongoPersistentProperty idProperty = entity.getRequiredIdProperty(); return Query.query(Criteria.where(idProperty.getName()).is(getId())); }
/** * 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 void checkForIndexes(final MongoPersistentEntity<?> entity) { Class<?> type = entity.getType(); if (!classesSeen.containsKey(type)) { this.classesSeen.put(type, Boolean.TRUE); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Analyzing class " + type + " for index information."); } checkForAndCreateIndexes(entity); } }
/** * Inspect entities for index creation. * * @return a {@link Mono} that completes without value after indexes were created. */ public Mono<Void> checkForIndexes(MongoPersistentEntity<?> entity) { Class<?> type = entity.getType(); if (!classesSeen.containsKey(type)) { if (this.classesSeen.put(type, Boolean.TRUE) == null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Analyzing class " + type + " for index information."); } return checkForAndCreateIndexes(entity); } } return Mono.empty(); }
/** * Creates a new {@link AbstractMongoQuery} from the given {@link MongoQueryMethod} and {@link MongoOperations}. * * @param method must not be {@literal null}. * @param operations must not be {@literal null}. */ public AbstractMongoQuery(MongoQueryMethod method, MongoOperations operations) { Assert.notNull(operations, "MongoOperations must not be null!"); Assert.notNull(method, "MongoQueryMethod must not be null!"); this.method = method; this.operations = operations; MongoEntityMetadata<?> metadata = method.getEntityInformation(); Class<?> type = metadata.getCollectionEntity().getType(); this.executableFind = operations.query(type); }
private Document getMappedFieldsObject(Document fields, MongoPersistentEntity<?> entity, Class<?> targetType) { Document projectedFields = propertyOperations.computeFieldsForProjection(projectionFactory, fields, entity.getType(), targetType); if (ObjectUtils.nullSafeEquals(fields, projectedFields)) { return queryMapper.getMappedFields(projectedFields, entity); } return queryMapper.getMappedFields(projectedFields, mappingContext.getRequiredPersistentEntity(targetType)); }
private Document getMappedFieldsObject(Document fields, MongoPersistentEntity<?> entity, Class<?> targetType) { Document projectedFields = propertyOperations.computeFieldsForProjection(projectionFactory, fields, entity.getType(), targetType); if (ObjectUtils.nullSafeEquals(fields, projectedFields)) { return queryMapper.getMappedFields(projectedFields, entity); } return queryMapper.getMappedFields(projectedFields, mappingContext.getRequiredPersistentEntity(targetType)); }
public void assertUpdateableIdIfNotSet() { if (!entity.hasIdProperty()) { return; } MongoPersistentProperty property = entity.getRequiredIdProperty(); Object propertyValue = idAccessor.getIdentifier(); 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(), entity.getType().getName())); } }
@Override public Query getByIdQuery() { if (!entity.hasIdProperty()) { throw new MappingException("No id property found for object of type " + entity.getType() + "!"); } MongoPersistentProperty idProperty = entity.getRequiredIdProperty(); return Query.query(Criteria.where(idProperty.getName()).is(getId())); }