@Override protected RepositoryInvoker postProcess(MethodParameter parameter, RepositoryInvoker invoker, Class<?> domainType, Map<String, String[]> parameters) { if (!parameter.hasParameterAnnotation(QuerydslPredicate.class)) { return invoker; } return repositories.getRepositoryFor(domainType)// .filter(it -> QuerydslPredicateExecutor.class.isInstance(it))// .map(it -> QuerydslPredicateExecutor.class.cast(it))// .flatMap(it -> getRepositoryAndPredicate(it, domainType, parameters))// .map(it -> getQuerydslAdapter(invoker, it.getFirst(), it.getSecond()))// .orElse(invoker); }
@Bean public Repositories repositories() { return new Repositories(applicationContext); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { return !sourceType.equals(URI_TYPE) ? false : repositories.getRepositoryInformationFor(targetType.getType()).isPresent(); }
/** * Creates a new {@link RepositoriesEntityLookup} for the given {@link Repositories} and {@link LookupInformation}. * * @param repositories must not be {@literal null}. * @param lookupInformation must not be {@literal null}. */ @SuppressWarnings("unchecked") public RepositoriesEntityLookup(Repositories repositories, LookupInformation<Object, Object, Repository<? extends T, ?>> lookupInformation) { Assert.notNull(repositories, "Repositories must not be null!"); Assert.notNull(lookupInformation, "LookupInformation must not be null!"); RepositoryInformation information = repositories.getRepositoryInformation(lookupInformation.repositoryType)// .orElseThrow(() -> new IllegalStateException( "No repository found for type " + lookupInformation.repositoryType.getName() + "!")); this.domainType = information.getDomainType(); this.lookupInfo = lookupInformation; this.repository = (Repository<? extends T, ?>) repositories.getRepositoryFor(information.getDomainType())// .orElseThrow(() -> new IllegalStateException( "No repository found for type " + information.getDomainType().getName() + "!")); }
/** * Creates a {@link RepositoryInvoker} for the repository managing the given domain type. * * @param domainType * @return */ private RepositoryInvoker prepareInvokers(Class<?> domainType) { Optional<RepositoryInformation> information = repositories.getRepositoryInformationFor(domainType); Optional<Object> repository = repositories.getRepositoryFor(domainType); return mapIfAllPresent(information, repository, this::createInvoker)// .orElseThrow( () -> new IllegalArgumentException(String.format("No repository found for domain type: %s", domainType))); }
@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; } }
public void registerCustomEditors(PropertyEditorRegistry registry) { for (Class<?> domainClass : repositories) { RepositoryInformation repositoryInformation = repositories.getRepositoryInformationFor(domainClass); CrudRepository<Object, Serializable> repository = repositories.getRepositoryFor(domainClass); DomainClassPropertyEditor<Object, Serializable> editor = new DomainClassPropertyEditor<Object, Serializable>( repository, repositories.getEntityInformationFor(repositoryInformation.getDomainType()), registry); registry.registerCustomEditor(repositoryInformation.getDomainType(), editor); } }
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { if (!repositories.hasRepositoryFor(targetType.getType())) { return false; } return conversionService.canConvert(sourceType.getType(), repositories.getRepositoryInformationFor(targetType.getType()).getIdType()); }
@Override public boolean supports(Class<?> delimiter) { return repositories.hasRepositoryFor(delimiter); }
private ReferencedProperty(PersistentProperty<?> property, Object propertyValue, PersistentPropertyAccessor wrapper) { this.property = property; this.propertyValue = propertyValue; this.accessor = wrapper; this.propertyType = property.getActualType(); this.entity = repositories.getPersistentEntity(propertyType); }
private final void populateCache(Repositories repositories, RepositoryRestConfiguration configuration) { for (Class<?> type : repositories) { RepositoryInformation repositoryInformation = repositories.getRequiredRepositoryInformation(type); Class<?> repositoryInterface = repositoryInformation.getRepositoryInterface(); PersistentEntity<?, ?> entity = repositories.getPersistentEntity(type); RepositoryDetectionStrategy strategy = configuration.getRepositoryDetectionStrategy(); RelProvider provider = configuration.getRelProvider(); CollectionResourceMapping mapping = new RepositoryCollectionResourceMapping(repositoryInformation, strategy, provider); RepositoryAwareResourceMetadata information = new RepositoryAwareResourceMetadata(entity, mapping, this, repositoryInformation); addToCache(repositoryInterface, information); if (!hasMetadataFor(type) || information.isPrimary()) { addToCache(type, information); } } }
@Override public SearchResourceMappings getSearchResourceMappings(Class<?> domainType) { Assert.notNull(domainType, "Type must not be null!"); if (searchCache.containsKey(domainType)) { return searchCache.get(domainType); } RepositoryInformation repositoryInformation = repositories.getRequiredRepositoryInformation(domainType); List<MethodResourceMapping> mappings = new ArrayList<MethodResourceMapping>(); ResourceMetadata resourceMapping = getMetadataFor(domainType); if (resourceMapping.isExported()) { for (Method queryMethod : repositoryInformation.getQueryMethods()) { RepositoryMethodResourceMapping methodMapping = new RepositoryMethodResourceMapping(queryMethod, resourceMapping, repositoryInformation, exposeMethodsByDefault()); if (methodMapping.isExported()) { mappings.add(methodMapping); } } } SearchResourceMappings searchResourceMappings = new SearchResourceMappings(mappings); searchCache.put(domainType, searchResourceMappings); return searchResourceMappings; }
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null || !StringUtils.hasText(source.toString())) { return null; } RepositoryInformation info = repositories.getRepositoryInformationFor(targetType.getType()); CrudRepository<?, Serializable> repository = repositories.getRepositoryFor(targetType.getType()); Serializable id = conversionService.convert(source, info.getIdType()); return repository.findOne(id); }
@Override public boolean hasMappingFor(Class<?> type) { if (super.hasMappingFor(type)) { return true; } if (repositories.hasRepositoryFor(type)) { return true; } return false; }
/** * Translates Jackson field names within a {@link Sort} to {@link PersistentProperty} property names. * * @param input must not be {@literal null}. * @param parameter must not be {@literal null}. * @param webRequest must not be {@literal null}. * @return a {@link Sort} containing translated property names or {@literal null} the resulting {@link Sort} contains * no properties. */ protected Sort translateSort(Sort input, MethodParameter parameter, NativeWebRequest webRequest) { Assert.notNull(input, "Sort must not be null!"); Assert.notNull(parameter, "MethodParameter must not be null!"); Assert.notNull(webRequest, "NativeWebRequest must not be null!"); Class<?> domainClass = domainClassResolver.resolve(parameter.getMethod(), webRequest); if (domainClass == null) { return input; } PersistentEntity<?, ?> persistentEntity = repositories.getPersistentEntity(domainClass); return sortTranslator.translateSort(input, persistentEntity); }
/** * Creates the {@link Descriptor}s for pagination parameters. * * @param type * @return */ private List<Descriptor> getPaginationDescriptors(Class<?> type, HttpMethod method) { RepositoryInformation information = repositories.getRequiredRepositoryInformation(type); if (!information.isPagingRepository() || !getType(method).equals(Type.SAFE)) { return Collections.emptyList(); } Link linkToCollectionResource = entityLinks.linkToCollectionResource(type); List<TemplateVariable> variables = linkToCollectionResource.getVariables(); List<Descriptor> descriptors = new ArrayList<Descriptor>(variables.size()); ProjectionDefinitionConfiguration projectionConfiguration = configuration.getProjectionConfiguration(); for (TemplateVariable variable : variables) { // Skip projection parameter if (projectionConfiguration.getParameterName().equals(variable.getName())) { continue; } ResourceDescription description = SimpleResourceDescription.defaultFor(variable.getDescription()); descriptors.add(// descriptor().// name(variable.getName()).// type(Type.SEMANTIC).// doc(getDocFor(description)).// build()); } return descriptors; }
@SuppressWarnings("unchecked") @Override public <T> PagingAndSortingRepository<T, ?> getRepositoryFor(Class<?> clazz) { return (PagingAndSortingRepository<T, ?>) repositories.getRepositoryFor(clazz); }
public EntityModelAttributeMethodProcessor(ConversionService conversionService, boolean annotationNotRequired, ApplicationContext applicationContext) { super(annotationNotRequired); this.conversionService = conversionService; this.repositories = new Repositories(applicationContext); }
Optional<CorsConfiguration> findCorsConfiguration(String lookupPath) { return getResourceMetadata(getRepositoryBasePath(lookupPath))// .flatMap(it -> repositories.flatMap(foo -> foo.getRepositoryInformationFor(it.getDomainType())))// .map(it -> it.getRepositoryInterface())// .map(it -> createConfiguration(it)); }
@Override public boolean isMapped(PersistentProperty<?> property) { return repositories.hasRepositoryFor(property.getActualType()) && super.isMapped(property); }