Search in sources :

Example 1 with AbstractMongoConfiguration

use of io.micronaut.configuration.mongo.core.AbstractMongoConfiguration in project micronaut-data by micronaut-projects.

the class AbstractMongoCollectionsCreator method initialize.

/**
 * Initialize the collections.
 *
 * @param runtimeEntityRegistry      The entity registry
 * @param mongoConfigurations        The configuration
 * @param databaseOperationsProvider The database provider
 */
protected void initialize(RuntimeEntityRegistry runtimeEntityRegistry, List<AbstractMongoConfiguration> mongoConfigurations, DatabaseOperationsProvider<Dtbs> databaseOperationsProvider) {
    for (AbstractMongoConfiguration mongoConfiguration : mongoConfigurations) {
        // TODO: different initializer per conf
        Collection<BeanIntrospection<Object>> introspections = BeanIntrospector.SHARED.findIntrospections(MappedEntity.class);
        PersistentEntity[] entities = introspections.stream().filter(i -> !i.getBeanType().getName().contains("$")).filter(i -> !java.lang.reflect.Modifier.isAbstract(i.getBeanType().getModifiers())).map(e -> runtimeEntityRegistry.getEntity(e.getBeanType())).toArray(PersistentEntity[]::new);
        DatabaseOperations<Dtbs> databaseOperations = databaseOperationsProvider.get(mongoConfiguration);
        for (PersistentEntity entity : entities) {
            Dtbs database = databaseOperations.find(entity);
            Set<String> collections = databaseOperations.listCollectionNames(database);
            String persistedName = entity.getPersistedName();
            if (collections.add(persistedName)) {
                if (LOG.isInfoEnabled()) {
                    LOG.info("Creating collection: {} in database: {}", persistedName, databaseOperations.getDatabaseName(database));
                }
                databaseOperations.createCollection(database, persistedName);
            }
            for (PersistentProperty persistentProperty : entity.getPersistentProperties()) {
                if (persistentProperty instanceof Association) {
                    Association association = (Association) persistentProperty;
                    Optional<Association> inverseSide = association.getInverseSide().map(Function.identity());
                    if (association.getKind() == Relation.Kind.MANY_TO_MANY || association.isForeignKey() && !inverseSide.isPresent()) {
                        Association owningAssociation = inverseSide.orElse(association);
                        NamingStrategy namingStrategy = association.getOwner().getNamingStrategy();
                        String joinCollectionName = namingStrategy.mappedName(owningAssociation);
                        if (collections.add(joinCollectionName)) {
                            if (LOG.isInfoEnabled()) {
                                LOG.info("Creating collection: {} in database: {}", persistedName, databaseOperations.getDatabaseName(database));
                            }
                            databaseOperations.createCollection(database, joinCollectionName);
                        }
                    }
                }
            }
        }
    }
}
Also used : PersistentProperty(io.micronaut.data.model.PersistentProperty) NamingStrategy(io.micronaut.data.model.naming.NamingStrategy) Logger(org.slf4j.Logger) Context(io.micronaut.context.annotation.Context) Qualifier(io.micronaut.context.Qualifier) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) MappedEntity(io.micronaut.data.annotation.MappedEntity) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Set(java.util.Set) RuntimeEntityRegistry(io.micronaut.data.model.runtime.RuntimeEntityRegistry) Internal(io.micronaut.core.annotation.Internal) Function(java.util.function.Function) List(java.util.List) Association(io.micronaut.data.model.Association) AbstractMongoConfiguration(io.micronaut.configuration.mongo.core.AbstractMongoConfiguration) NamedMongoConfiguration(io.micronaut.configuration.mongo.core.NamedMongoConfiguration) PersistentEntity(io.micronaut.data.model.PersistentEntity) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) Optional(java.util.Optional) BeanLocator(io.micronaut.context.BeanLocator) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector) DefaultMongoConfiguration(io.micronaut.configuration.mongo.core.DefaultMongoConfiguration) Relation(io.micronaut.data.annotation.Relation) PersistentEntity(io.micronaut.data.model.PersistentEntity) PersistentProperty(io.micronaut.data.model.PersistentProperty) NamingStrategy(io.micronaut.data.model.naming.NamingStrategy) Association(io.micronaut.data.model.Association) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) AbstractMongoConfiguration(io.micronaut.configuration.mongo.core.AbstractMongoConfiguration)

Example 2 with AbstractMongoConfiguration

use of io.micronaut.configuration.mongo.core.AbstractMongoConfiguration in project micronaut-data by micronaut-projects.

the class MongoAutomaticClassModelsDisabler method onCreated.

@Override
public AbstractMongoConfiguration onCreated(BeanCreatedEvent<AbstractMongoConfiguration> event) {
    AbstractMongoConfiguration bean = event.getBean();
    // Disable automatic class models
    bean.setAutomaticClassModels(false);
    return bean;
}
Also used : AbstractMongoConfiguration(io.micronaut.configuration.mongo.core.AbstractMongoConfiguration)

Aggregations

AbstractMongoConfiguration (io.micronaut.configuration.mongo.core.AbstractMongoConfiguration)2 DefaultMongoConfiguration (io.micronaut.configuration.mongo.core.DefaultMongoConfiguration)1 NamedMongoConfiguration (io.micronaut.configuration.mongo.core.NamedMongoConfiguration)1 BeanLocator (io.micronaut.context.BeanLocator)1 Qualifier (io.micronaut.context.Qualifier)1 Context (io.micronaut.context.annotation.Context)1 Internal (io.micronaut.core.annotation.Internal)1 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)1 BeanIntrospector (io.micronaut.core.beans.BeanIntrospector)1 MappedEntity (io.micronaut.data.annotation.MappedEntity)1 Relation (io.micronaut.data.annotation.Relation)1 Association (io.micronaut.data.model.Association)1 PersistentEntity (io.micronaut.data.model.PersistentEntity)1 PersistentProperty (io.micronaut.data.model.PersistentProperty)1 NamingStrategy (io.micronaut.data.model.naming.NamingStrategy)1 RuntimeEntityRegistry (io.micronaut.data.model.runtime.RuntimeEntityRegistry)1 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)1 Collection (java.util.Collection)1 List (java.util.List)1 Optional (java.util.Optional)1