Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't create new repositories if they already exist on the ApplicationContext #230

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.lightadmin.core.config.bootstrap;

import org.apache.tika.metadata.Geographic;
import org.lightadmin.core.config.bootstrap.parsing.validation.CompositeConfigurationUnitsValidator;
import org.lightadmin.core.config.bootstrap.scanning.AdministrationClassScanner;
import org.lightadmin.core.config.bootstrap.scanning.ClassScanner;
Expand All @@ -33,6 +34,7 @@
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.event.AbstractRepositoryEventListener;
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
import org.springframework.orm.jpa.SharedEntityManagerCreator;
Expand All @@ -44,6 +46,7 @@
import javax.persistence.EntityManagerFactory;
import javax.persistence.metamodel.EntityType;
import javax.servlet.ServletContext;

import java.util.Set;

import static com.google.common.collect.Sets.newHashSet;
Expand Down Expand Up @@ -89,10 +92,13 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) t
registry.registerBeanDefinition(JPA_MAPPPING_CONTEXT_BEAN, mappingContext(entityManager));

registry.registerBeanDefinition(CONFIGURATION_UNITS_VALIDATOR_BEAN, configurationUnitsValidator(resourceLoader));


Repositories repositories = new Repositories(rootContext);
for (Class<?> managedEntityType : managedEntities(entityManager)) {
Class repoInterface = createDynamicRepositoryClass(managedEntityType, entityManager);
registry.registerBeanDefinition(beanName(repoInterface), repositoryFactory(repoInterface, entityManager));
if(!repositories.hasRepositoryFor(managedEntityType)){
Class repoInterface = createDynamicRepositoryClass(managedEntityType, entityManager);
registry.registerBeanDefinition(beanName(repoInterface), repositoryFactory(repoInterface, entityManager));
}
}

registerRepositoryEventListeners(configurationUnits, registry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,30 @@
*/
package org.lightadmin.core.config.context;

import com.fasterxml.jackson.databind.ObjectMapper;
import static com.google.common.collect.Lists.newLinkedList;
import static org.springframework.beans.PropertyAccessorFactory.forDirectFieldAccess;
import static org.springframework.util.ClassUtils.isAssignableValue;

import java.util.List;

import org.lightadmin.core.config.LightAdminConfiguration;
import org.lightadmin.core.config.bootstrap.RepositoriesFactoryBean;
import org.lightadmin.core.config.domain.GlobalAdministrationConfiguration;
import org.lightadmin.core.persistence.repository.event.FileManipulationRepositoryEventListener;
import org.lightadmin.core.persistence.repository.invoker.DynamicRepositoryInvokerFactory;
import org.lightadmin.core.persistence.support.DynamicDomainObjectMerger;
import org.lightadmin.core.storage.FileResourceStorage;
import org.lightadmin.core.web.json.DomainTypeToJsonMetadataConverter;
import org.lightadmin.core.web.json.LightAdminJacksonModule;
import org.lightadmin.core.web.support.*;
import org.springframework.beans.BeanInstantiationException;
import org.lightadmin.core.web.support.ConfigurationHandlerMethodArgumentResolver;
import org.lightadmin.core.web.support.DomainEntityLinks;
import org.lightadmin.core.web.support.DynamicPersistentEntityResourceAssemblerArgumentResolver;
import org.lightadmin.core.web.support.DynamicPersistentEntityResourceProcessor;
import org.lightadmin.core.web.support.DynamicRepositoryEntityLinks;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.data.rest.core.invoke.RepositoryInvokerFactory;
Expand All @@ -44,11 +50,7 @@
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.util.List;

import static com.google.common.collect.Lists.newLinkedList;
import static org.springframework.beans.PropertyAccessorFactory.forDirectFieldAccess;
import static org.springframework.util.ClassUtils.isAssignableValue;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
@ComponentScan(basePackages = {"org.lightadmin.core.web"},
Expand Down Expand Up @@ -78,15 +80,6 @@ public DomainTypeToJsonMetadataConverter domainTypeToJsonMetadataConverter() {
return new DomainTypeToJsonMetadataConverter(globalAdministrationConfiguration(), entityLinks());
}

@Bean
public Repositories repositories() {
try {
return new RepositoriesFactoryBean(beanFactory).getObject();
} catch (Exception e) {
throw new BeanInstantiationException(Repositories.class, "Repositories bean instantiation problem!", e);
}
}

@Bean
public DomainObjectMerger domainObjectMerger() throws Exception {
return new DynamicDomainObjectMerger(repositories(), defaultConversionService(), globalAdministrationConfiguration());
Expand Down