org.springframework.data.repository.support.Repositories Java Examples

The following examples show how to use org.springframework.data.repository.support.Repositories. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JpaTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@TestFactory
public Collection<DynamicTest> dynamicTestsForRepositorySelectCount() {
    ArrayList<DynamicTest> tests = new ArrayList<>();
    Repositories repositories = new Repositories(applicationContext);
    for (Class domainType : repositories) {
        Class<?> repoInterface = repositories.getRepositoryInformationFor(domainType).get().getRepositoryInterface();
        Object repositoryInstance = repositories.getRepositoryFor(domainType).get();
        Method[] methods = repoInterface.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().startsWith("find")) {
                tests.add(
                        DynamicTest.dynamicTest(
                                repoInterface.getSimpleName() + "." + method.getName(),
                                callRepositoryMethod(repositoryInstance, method)));
            }
        }
    }
    return tests;
}
 
Example #2
Source File: AbstractContentPropertyController.java    From spring-content with Apache License 2.0 6 votes vote down vote up
public static Object save(Repositories repositories, Object domainObj)
		throws HttpRequestMethodNotSupportedException {

	RepositoryInformation ri = RepositoryUtils.findRepositoryInformation(repositories,
			domainObj.getClass());

	if (ri == null) {
		throw new ResourceNotFoundException();
	}

	Class<?> domainObjClazz = ri.getDomainType();

	if (domainObjClazz != null) {
		Optional<Method> saveMethod = ri.getCrudMethods().getSaveMethod();
		if (!saveMethod.isPresent()) {
			throw new HttpRequestMethodNotSupportedException("save");
		}
		domainObj = ReflectionUtils.invokeMethod(saveMethod.get(),
				repositories.getRepositoryFor(domainObjClazz).get(), domainObj);
	}

	return domainObj;
}
 
Example #3
Source File: JpaTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@TestFactory
public Collection<DynamicTest> dynamicTestsForRepositorySelectCount() {
    ArrayList<DynamicTest> tests = new ArrayList<>();
    Repositories repositories = new Repositories(applicationContext);
    for (Class domainType : repositories) {
        Class<?> repoInterface = repositories.getRepositoryInformationFor(domainType).get().getRepositoryInterface();
        Object repositoryInstance = repositories.getRepositoryFor(domainType).get();
        Method[] methods = repoInterface.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().startsWith("find")) {
                tests.add(
                        DynamicTest.dynamicTest(
                                repoInterface.getSimpleName() + "." + method.getName(),
                                callRepositoryMethod(repositoryInstance, method)));
            }
        }
    }
    return tests;
}
 
Example #4
Source File: RepositoryUtils.java    From spring-content with Apache License 2.0 6 votes vote down vote up
public static RepositoryInformation findRepositoryInformation(
		Repositories repositories, String repository) {
	RepositoryInformation ri = null;
	for (Class<?> clazz : repositories) {
		Optional<RepositoryInformation> candidate = repositories
				.getRepositoryInformationFor(clazz);
		if (candidate.isPresent() == false) {
			continue;
		}
		if (repository.equals(repositoryPath(candidate.get()))) {
			ri = candidate.get();
			break;
		}
	}
	return ri;
}
 
Example #5
Source File: AbstractContentPropertyController.java    From spring-content with Apache License 2.0 6 votes vote down vote up
public static Object findOne(Repositories repositories, Class<?> domainObjClass,
		String id) throws HttpRequestMethodNotSupportedException {

	Optional<Object> domainObj = null;

	RepositoryInformation ri = RepositoryUtils.findRepositoryInformation(repositories,
			domainObjClass);

	if (ri == null) {
		throw new ResourceNotFoundException();
	}

	Class<?> domainObjClazz = ri.getDomainType();
	Class<?> idClazz = ri.getIdType();

	Optional<Method> findOneMethod = ri.getCrudMethods().getFindOneMethod();
	if (!findOneMethod.isPresent()) {
		throw new HttpRequestMethodNotSupportedException("fineOne");
	}

	Object oid = new DefaultConversionService().convert(id, idClazz);
	domainObj = (Optional<Object>) ReflectionUtils.invokeMethod(findOneMethod.get(),
			repositories.getRepositoryFor(domainObjClazz).get(), oid);

	return domainObj.orElseThrow(ResourceNotFoundException::new);
}
 
Example #6
Source File: ApplicationStructureController.java    From moserp with Apache License 2.0 5 votes vote down vote up
@Autowired
public ApplicationStructureController(ApplicationStructureBuilder applicationStructureBuilder, RepositoryRestConfiguration configuration, Repositories repositories, ResourceMappings mappings) {
    Assert.notNull(applicationStructureBuilder, "ApplicationStructureBuilder must not be null!");
    Assert.notNull(configuration, "RepositoryRestConfiguration must not be null!");
    Assert.notNull(repositories, "Repositories must not be null!");
    Assert.notNull(mappings, "ResourceMappings must not be null!");

    this.applicationStructureBuilder = applicationStructureBuilder;
    this.configuration = configuration;
    this.repositories = repositories;
    this.mappings = mappings;
}
 
Example #7
Source File: ContentPropertyRestController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
private void replaceContentInternal(HttpHeaders headers, Repositories repositories,
		ContentStoreService stores, String repository, String id,
		String contentProperty, String contentId, String mimeType,
		String originalFileName, InputStream stream)
		throws HttpRequestMethodNotSupportedException {

	Object domainObj = findOne(repositories, repository, id);

	String etag = (BeanUtils.getFieldWithAnnotation(domainObj, Version.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, Version.class).toString() : null);
	Object lastModifiedDate = (BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) != null ? BeanUtils.getFieldWithAnnotation(domainObj, LastModifiedDate.class) : null);
	HeaderUtils.evaluateHeaderConditions(headers, etag, lastModifiedDate);

	PersistentProperty<?> property = this.getContentPropertyDefinition(repositories.getPersistentEntity(domainObj.getClass()), contentProperty);

	Object contentPropertyValue = this.getContentProperty(domainObj, property, contentId);

	if (BeanUtils.hasFieldWithAnnotation(contentPropertyValue, MimeType.class)) {
		BeanUtils.setFieldWithAnnotation(contentPropertyValue, MimeType.class,
				mimeType);
	}

	if (originalFileName != null && StringUtils.hasText(originalFileName)) {
		if (BeanUtils.hasFieldWithAnnotation(contentPropertyValue,
				OriginalFileName.class)) {
			BeanUtils.setFieldWithAnnotation(contentPropertyValue,
					OriginalFileName.class, originalFileName);
		}
	}

	Class<?> contentEntityClass = ContentPropertyUtils.getContentPropertyType(property);

	ContentStoreInfo info = ContentStoreUtils.findContentStore(storeService, contentEntityClass);

	info.getImpementation().setContent(contentPropertyValue, stream);

	save(repositories, domainObj);
}
 
Example #8
Source File: ContentPropertyRestController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
@Autowired
public ContentPropertyRestController(ApplicationContext context,
		ContentStoreService storeService, StoreByteRangeHttpRequestHandler handler) {
	super();
	try {
		this.repositories = context.getBean(Repositories.class);
	}
	catch (BeansException be) {
		this.repositories = new Repositories(context);
	}
	this.storeService = storeService;
	this.handler = handler;
}
 
Example #9
Source File: AbstractContentPropertyController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
public static Iterable findAll(Repositories repositories, String repository)
		throws HttpRequestMethodNotSupportedException {

	Iterable entities = null;

	RepositoryInformation ri = RepositoryUtils.findRepositoryInformation(repositories, repository);

	if (ri == null) {
		throw new ResourceNotFoundException();
	}

	Class<?> domainObjClazz = ri.getDomainType();
	Class<?> idClazz = ri.getIdType();

	Optional<Method> findAllMethod = ri.getCrudMethods().getFindAllMethod();
	if (!findAllMethod.isPresent()) {
		throw new HttpRequestMethodNotSupportedException("fineAll");
	}

	entities = (Iterable) ReflectionUtils.invokeMethod(findAllMethod.get(),
			repositories.getRepositoryFor(domainObjClazz));

	if (null == entities) {
		throw new ResourceNotFoundException();
	}

	return entities;
}
 
Example #10
Source File: AbstractContentPropertyController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
public static Object findOne(Repositories repositories, String repository, String id)
		throws HttpRequestMethodNotSupportedException {

	Optional<Object> domainObj = null;

	RepositoryInformation ri = RepositoryUtils.findRepositoryInformation(repositories, repository);

	if (ri == null) {
		throw new ResourceNotFoundException();
	}

	Class<?> domainObjClazz = ri.getDomainType();
	Class<?> idClazz = ri.getIdType();

	Optional<Method> findOneMethod = ri.getCrudMethods().getFindOneMethod();
	if (!findOneMethod.isPresent()) {
		throw new HttpRequestMethodNotSupportedException("fineOne");
	}

	Object oid = new DefaultConversionService().convert(id, idClazz);
	domainObj = (Optional<Object>) ReflectionUtils.invokeMethod(findOneMethod.get(),
			repositories.getRepositoryFor(domainObjClazz).get(), oid);

	if (null == domainObj) {
		throw new ResourceNotFoundException();
	}

	return domainObj.orElseThrow(ResourceNotFoundException::new);
}
 
Example #11
Source File: ContentEntityRestController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
@Autowired
public ContentEntityRestController(ApplicationContext context, ContentStoreService storeService, StoreByteRangeHttpRequestHandler handler) {
	try {
		this.repositories = context.getBean(Repositories.class);
	}
	catch (BeansException be) {
		this.repositories = new Repositories(context);
	}
	this.storeService = storeService;
	this.handler = handler;
}
 
Example #12
Source File: ContentPropertyCollectionRestController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
@Autowired
public ContentPropertyCollectionRestController(ApplicationContext context, ContentStoreService stores, StoreByteRangeHttpRequestHandler handler) {
	super();
	try {
		this.repositories = context.getBean(Repositories.class);
	}
	catch (BeansException be) {
		this.repositories = new Repositories(context);
	}
	this.storeService = stores;
	this.handler = handler;
}
 
Example #13
Source File: ApplicationConfigurationTest.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void repositoriesAreAssignedToAppropriateStores() {

	Repositories repositories = new Repositories(context);

	assertThat(repositories.getEntityInformationFor(Customer.class), is(instanceOf(JpaEntityInformation.class)));
	assertThat(repositories.getEntityInformationFor(Order.class), is(instanceOf(MongoEntityInformation.class)));
}
 
Example #14
Source File: RepositoryUtils.java    From spring-content with Apache License 2.0 5 votes vote down vote up
public static RepositoryInformation findRepositoryInformation(
		Repositories repositories, Class<?> domainObjectClass) {
	RepositoryInformation ri = null;
	for (Class<?> clazz : repositories) {
		if (clazz.equals(domainObjectClass)) {
			return repositories.getRepositoryInformationFor(clazz).get();
		}
	}
	return ri;
}
 
Example #15
Source File: ContentSearchRestController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
@Autowired
public ContentSearchRestController(Repositories repositories,
		ContentStoreService stores, PagedResourcesAssembler<Object> assembler) {
	// super(assembler);

	this.repositories = repositories;
	this.stores = stores;
	this.pagedResourcesAssembler = assembler;

	this.reflectionService = new ReflectionServiceImpl();
}
 
Example #16
Source File: LockingAndVersioningRestController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
@Autowired
public LockingAndVersioningRestController(Repositories repositories, PagedResourcesAssembler<Object> assembler) {
	this.repositories = repositories;
	this.pagedResourcesAssembler = assembler;

	this.reflectionService = new ReflectionServiceImpl();
}
 
Example #17
Source File: RepoBasedConverter.java    From spring-data-jpa-extra with Apache License 2.0 5 votes vote down vote up
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
    Class<?>[] classes = GenericTypeResolver.resolveTypeArguments(this.getClass(), RepoBasedConverter.class);
    Class<?> clazz = classes[0];
    this.repositories = new Repositories(context);
    this.entityInformation = repositories.getEntityInformationFor(clazz);
    this.genericJpaRepository = (GenericJpaRepository<S, ID>) repositories.getRepositoryFor(clazz).orElse(null);
    this.useCache = genericJpaRepository instanceof CachingJpaRepository;
}
 
Example #18
Source File: DemoRepositoryEntityLinks.java    From spring-data-dynamodb-demo with Apache License 2.0 4 votes vote down vote up
public DemoRepositoryEntityLinks(Repositories repositories,
		ResourceMappings mappings, RepositoryRestConfiguration config, HateoasPageableHandlerMethodArgumentResolver resolver,org.springframework.plugin.core.PluginRegistry<BackendIdConverter,Class<?>> idConverters) {
	super(repositories, mappings, config, resolver,idConverters);
	this.resourceMappings = mappings;
	this.config = config;
}
 
Example #19
Source File: ContentLinksResourceProcessor.java    From spring-content with Apache License 2.0 4 votes vote down vote up
public ContentLinksResourceProcessor(Repositories repos, ContentStoreService stores, RestConfiguration config, RepositoryResourceMappings mappings) {
	this.stores = stores;
	this.config = config;
	this.mappings = mappings;
}
 
Example #20
Source File: HypermediaConfiguration.java    From spring-content with Apache License 2.0 4 votes vote down vote up
@Bean
public ResourceProcessor<PersistentEntityResource> contentLinksProcessor(Repositories repos, ContentStoreService stores, RestConfiguration config, RepositoryResourceMappings mappings) {
	return new ContentLinksResourceProcessor(repos, stores, config, mappings);
}
 
Example #21
Source File: SpringDocDataRestConfiguration.java    From springdoc-openapi with Apache License 2.0 3 votes vote down vote up
/**
 * Spring repository rest resource provider spring repository rest resource provider.
 *
 * @param mappings the mappings 
 * @param repositories the repositories 
 * @param associations the associations 
 * @param delegatingHandlerMapping the delegating handler mapping 
 * @param dataRestRouterOperationBuilder the data rest router operation builder 
 * @return the spring repository rest resource provider
 */
@Bean
@ConditionalOnMissingBean
SpringRepositoryRestResourceProvider springRepositoryRestResourceProvider(ResourceMappings mappings,
		Repositories repositories, Associations associations, DelegatingHandlerMapping delegatingHandlerMapping,
		DataRestRouterOperationBuilder dataRestRouterOperationBuilder) {
	return new SpringRepositoryRestResourceProvider(mappings, repositories, associations,
			delegatingHandlerMapping, dataRestRouterOperationBuilder);
}
 
Example #22
Source File: SpringRepositoryRestResourceProvider.java    From springdoc-openapi with Apache License 2.0 3 votes vote down vote up
/**
 * Instantiates a new Spring repository rest resource provider.
 *
 * @param mappings the mappings
 * @param repositories the repositories
 * @param associations the associations
 * @param delegatingHandlerMapping the delegating handler mapping
 * @param dataRestRouterOperationBuilder the data rest router operation builder
 */
public SpringRepositoryRestResourceProvider(ResourceMappings mappings, Repositories repositories, Associations associations,
		DelegatingHandlerMapping delegatingHandlerMapping, DataRestRouterOperationBuilder dataRestRouterOperationBuilder) {
	this.mappings = mappings;
	this.repositories = repositories;
	this.associations = associations;
	this.delegatingHandlerMapping = delegatingHandlerMapping;
	this.dataRestRouterOperationBuilder = dataRestRouterOperationBuilder;
}