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);
}
}
}
}
}
}
}
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;
}
Aggregations