Java Code Examples for org.springframework.data.repository.core.RepositoryMetadata

The following examples show how to use org.springframework.data.repository.core.RepositoryMetadata. These examples are extracted from open source projects. 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 Project: sdn-rx   Source File: Neo4jQueryLookupStrategy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
	NamedQueries namedQueries) {

	Neo4jQueryMethod queryMethod = new Neo4jQueryMethod(method, metadata, factory);
	String namedQueryName = queryMethod.getNamedQueryName();

	if (namedQueries.hasQuery(namedQueryName)) {
		return StringBasedNeo4jQuery.create(neo4jOperations, mappingContext, evaluationContextProvider, queryMethod,
			namedQueries.getQuery(namedQueryName));
	} else if (queryMethod.hasQueryAnnotation()) {
		return StringBasedNeo4jQuery.create(neo4jOperations, mappingContext, evaluationContextProvider, queryMethod);
	} else {
		return PartTreeNeo4jQuery.create(neo4jOperations, mappingContext, queryMethod);
	}
}
 
Example 2
Source Project: sdn-rx   Source File: ReactiveNeo4jQueryLookupStrategy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
	NamedQueries namedQueries) {

	Neo4jQueryMethod queryMethod = new ReactiveNeo4jQueryMethod(method, metadata, factory);
	String namedQueryName = queryMethod.getNamedQueryName();

	if (namedQueries.hasQuery(namedQueryName)) {
		return ReactiveStringBasedNeo4jQuery
			.create(neo4jOperations, mappingContext, evaluationContextProvider, queryMethod,
			namedQueries.getQuery(namedQueryName));
	} else if (queryMethod.hasQueryAnnotation()) {
		return ReactiveStringBasedNeo4jQuery
			.create(neo4jOperations, mappingContext, evaluationContextProvider, queryMethod);
	} else {
		return ReactivePartTreeNeo4jQuery.create(neo4jOperations, mappingContext, queryMethod);
	}
}
 
Example 3
Source Project: ignite   Source File: ExpressionBasedStringQuery.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param qry,     the query expression potentially containing a SpEL expression. Must not be {@literal null}.}
 * @param metadata the {@link RepositoryMetadata} for the given entity. Must not be {@literal null}.
 * @param parser   Must not be {@literal null}.
 * @return rendered query
 */
private static String renderQueryIfExpressionOrReturnQuery(String qry,
    RepositoryMetadata metadata,
    SpelExpressionParser parser) {

    Assert.notNull(qry, "query must not be null!");
    Assert.notNull(metadata, "metadata must not be null!");
    Assert.notNull(parser, "parser must not be null!");

    if (!containsExpression(qry))
        return qry;

    StandardEvaluationContext evalCtx = new StandardEvaluationContext();
    evalCtx.setVariable(ENTITY_NAME, metadata.getDomainType().getSimpleName());

    qry = potentiallyQuoteExpressionsParameter(qry);

    Expression expr = parser.parseExpression(qry, ParserContext.TEMPLATE_EXPRESSION);

    String result = expr.getValue(evalCtx, String.class);

    if (result == null)
        return qry;

    return potentiallyUnquoteParameterExpressions(result);
}
 
Example 4
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata,
		ProjectionFactory factory, NamedQueries namedQueries) {
	SpannerQueryMethod queryMethod = createQueryMethod(method, metadata, factory);
	Class<?> entityType = getEntityType(queryMethod);
	boolean isDml = queryMethod.getQueryAnnotation() != null && queryMethod.getQueryAnnotation().dmlStatement();

	if (queryMethod.hasAnnotatedQuery()) {
		Query query = queryMethod.getQueryAnnotation();
		return createSqlSpannerQuery(entityType, queryMethod, query.value(), isDml);
	}
	else if (namedQueries.hasQuery(queryMethod.getNamedQueryName())) {
		String sql = namedQueries.getQuery(queryMethod.getNamedQueryName());
		return createSqlSpannerQuery(entityType, queryMethod, sql, isDml);
	}

	return createPartTreeSpannerQuery(entityType, queryMethod);
}
 
Example 5
private static SpelCriteria createQueryForMethodWithArgs(String methodName, Object... args) throws Exception {

		List<Class<?>> types = new ArrayList<>(args.length);

		for (Object arg : args) {
			types.add(arg.getClass());
		}

		Method method = PersonRepository.class.getMethod(methodName, types.toArray(new Class<?>[types.size()]));
		RepositoryMetadata metadata = mock(RepositoryMetadata.class);
		doReturn(method.getReturnType()).when(metadata).getReturnedDomainClass(method);

		PartTree partTree = new PartTree(method.getName(), method.getReturnType());
		SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(
				new QueryMethod(method, metadata, new SpelAwareProxyProjectionFactory()).getParameters(), args));

		return new SpelCriteria(creator.createQuery().getCriteria(),
				SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().withRootObject(args).build());
	}
 
Example 6
Source Project: spring-data-mybatis   Source File: MybatisQueryMethod.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@link QueryMethod} from the given parameters. Looks up the correct
 * query to use for following invocations of the method given.
 * @param method must not be {@literal null}.
 * @param metadata must not be {@literal null}.
 * @param factory must not be {@literal null}.
 */
public MybatisQueryMethod(Method method, RepositoryMetadata metadata,
		ProjectionFactory factory) {

	super(method, metadata, factory);

	Assert.notNull(method, "Method must not be null!");

	this.method = method;

	Assert.isTrue(!(isModifyingQuery() && getParameters().hasSpecialParameter()),
			String.format("Modifying method must not contain %s!", Parameters.TYPES));

	this.metadata = metadata;
	String namespace = getAnnotationValue("namespace", String.class);
	String statementName = getAnnotationValue("statement", String.class);
	this.namespace = StringUtils.hasText(namespace) ? namespace
			: metadata.getRepositoryInterface().getName();
	this.statementName = StringUtils.hasText(statementName) ? statementName
			: (method.getName() + UUID.randomUUID().toString().replace("-", ""));

}
 
Example 7
@Override
protected Class<?> getRepositoryBaseClass(final RepositoryMetadata metadata) {

    if (isQueryDslExecutor(metadata.getRepositoryInterface())) {
        return QueryDslJpaRepository.class;
    } else {
        return GenericRepositoryImpl.class;
    }
}
 
Example 8
Source Project: spring-data-simpledb   Source File: SimpleDbQueryMethod.java    License: MIT License 5 votes vote down vote up
/**
 * Creates a new {@link org.springframework.data.simpledb.query.SimpleDbQueryMethod}
 * 
 * @param method
 *            must not be {@literal null}
 * @param metadata
 *            must not be {@literal null}
 * @param simpleDbDomain
 */
public SimpleDbQueryMethod(Method method, RepositoryMetadata metadata, SimpleDbDomain simpleDbDomain) {
	super(method, metadata);
	
	this.method = method;
	this.simpleDbDomain = simpleDbDomain;

	Assert.isTrue(!(isModifyingQuery() && getParameters().hasSpecialParameter()),
               String.format("Modifying method must not contain %s!", Parameters.TYPES));
}
 
Example 9
@Override
protected <T, ID extends Serializable> RepositoryQuery createDynamoDBQuery(Method method, RepositoryMetadata metadata,
		Class<T> entityClass, Class<ID> idClass, NamedQueries namedQueries) {
	try {
		return new PartTreeDynamoDBQuery<T, ID>(dynamoDBOperations, new DynamoDBQueryMethod<T, ID>(method, metadata));
	} catch (IllegalArgumentException e) {
		throw new IllegalArgumentException(String.format("Could not create query metamodel for method %s!",
				method.toString()), e);
	}
}
 
Example 10
Source Project: moduliths   Source File: ArchitecturallyEvidentType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean isAggregateRoot() {
	return isEntity() && beanTypes.that(SpringDataTypes.isSpringDataRepository()).stream() //
			.map(JavaClass::reflect) //
			.map(AbstractRepositoryMetadata::getMetadata) //
			.map(RepositoryMetadata::getDomainType) //
			.anyMatch(it -> getType().isAssignableTo(it));
}
 
Example 11
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
    Class<?> repositoryInterface = metadata.getRepositoryInterface();
    if (DataTablesRepository.class.isAssignableFrom(repositoryInterface)) {
        return DataTablesRepositoryImpl.class;
    } else {
        return super.getRepositoryBaseClass(metadata);
    }
}
 
Example 12
Source Project: spring-data   Source File: ArangoRepositoryFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected RepositoryMetadata getRepositoryMetadata(final Class<?> repositoryInterface) {
	Assert.notNull(repositoryInterface, "Repository interface must not be null!");

	return Repository.class.isAssignableFrom(repositoryInterface)
			? new DefaultArangoRepositoryMetadata(repositoryInterface)
			: new AnnotationArangoRepositoryMetadata(repositoryInterface);
}
 
Example 13
Source Project: base-framework   Source File: BasicJpaRepositoryFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
	if (isQueryDslExecutor(metadata.getRepositoryInterface())) {
		return QueryDslJpaRepository.class;
	} else {
		return JpaSupportRepository.class;
	}
}
 
Example 14
Source Project: dubbox   Source File: SolrRepositoryFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
	if (isQueryDslRepository(metadata.getRepositoryInterface())) {
		throw new IllegalArgumentException("QueryDsl Support has not been implemented yet.");
	}
	return SimpleSolrRepository.class;
}
 
Example 15
Source Project: dubbox   Source File: SolrRepositoryFactory.java    License: Apache License 2.0 5 votes vote down vote up
private SolrOperations selectSolrOperations(RepositoryMetadata metadata) {
	SolrOperations ops = templateHolder.getSolrOperations(metadata.getDomainType());
	if (ops == null) {
		ops = solrOperations;
	}
	return ops;
}
 
Example 16
Source Project: blog-examples   Source File: JooqJpaRepositoryFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {

	if (JooqQueryExecutor.class.isAssignableFrom(metadata.getRepositoryInterface())) {
		return JooqJpaRepository.class;
	} else {
		return SimpleJpaRepository.class;
	}
}
 
Example 17
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata,
                                    ProjectionFactory factory, NamedQueries namedQueries) {
    final ReactiveCosmosQueryMethod queryMethod = new ReactiveCosmosQueryMethod(method,
        metadata, factory);

    Assert.notNull(queryMethod, "queryMethod must not be null!");
    Assert.notNull(cosmosOperations, "dbOperations must not be null!");
    return new PartTreeReactiveCosmosQuery(queryMethod, cosmosOperations);

}
 
Example 18
Source Project: es   Source File: SimpleBaseRepositoryFactoryBean.java    License: Apache License 2.0 5 votes vote down vote up
protected Object getTargetRepository(RepositoryMetadata metadata) {
    Class<?> repositoryInterface = metadata.getRepositoryInterface();

    if (isBaseRepository(repositoryInterface)) {

        JpaEntityInformation<M, ID> entityInformation = getEntityInformation((Class<M>) metadata.getDomainType());
        SimpleBaseRepository repository = new SimpleBaseRepository<M, ID>(entityInformation, entityManager);

        SearchableQuery searchableQuery = AnnotationUtils.findAnnotation(repositoryInterface, SearchableQuery.class);
        if (searchableQuery != null) {
            String countAllQL = searchableQuery.countAllQuery();
            if (!StringUtils.isEmpty(countAllQL)) {
                repository.setCountAllQL(countAllQL);
            }
            String findAllQL = searchableQuery.findAllQuery();
            if (!StringUtils.isEmpty(findAllQL)) {
                repository.setFindAllQL(findAllQL);
            }
            Class<? extends SearchCallback> callbackClass = searchableQuery.callbackClass();
            if (callbackClass != null && callbackClass != SearchCallback.class) {
                repository.setSearchCallback(BeanUtils.instantiate(callbackClass));
            }

            repository.setJoins(searchableQuery.joins());

        }

        return repository;
    }
    return super.getTargetRepository(metadata);
}
 
Example 19
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
                                    NamedQueries namedQueries) {
    if (method.getAnnotation(TemplateQuery.class) == null) {
        return jpaQueryLookupStrategy.resolveQuery(method, metadata, factory, namedQueries);
    } else {
        return new FreemarkerTemplateQuery(new JpaQueryMethod(method, metadata, factory, extractor), entityManager);
    }
}
 
Example 20
@SuppressWarnings({ "rawtypes", "unchecked" })
private SimpleDbQueryMethod prepareQueryMethodToTest(String methodName, Class<?> entityClass) throws Exception {
	RepositoryMetadata repositoryMetadata = Mockito.mock(RepositoryMetadata.class);
	when(repositoryMetadata.getDomainType()).thenReturn((Class) entityClass);

	Method testMethod = AnnotatedQueryRepository.class.getMethod(methodName);
	when(repositoryMetadata.getReturnedDomainClass(testMethod)).thenReturn((Class) entityClass);
	SimpleDbDomain simpleDbDomain = new SimpleDbDomain();
	return new SimpleDbQueryMethod(testMethod, repositoryMetadata, simpleDbDomain);
}
 
Example 21
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata,
                                    ProjectionFactory projectionFactory, NamedQueries namedQueries) {
  if (!isAggregateQueryAnnotated(method)) {
    return parentQueryLookupStrategy.resolveQuery(method, repositoryMetadata, projectionFactory, namedQueries);
  }
  else {
    return new AggregateMongoQuery(method, repositoryMetadata, mongoOperations, projectionFactory, queryExecutor);
  }
}
 
Example 22
@Test(dataProvider = "queryMethods", expectedExceptions = {JsonParseException.class})
public void testCreateAggregateQuery(String methodName) throws Exception {
  AggregateQuerySupportingRepositoryFactory factory = new AggregateQuerySupportingRepositoryFactory(mongoOperations,
                                                                                                    queryExecutor);
  QueryLookupStrategy lookupStrategy = factory.getQueryLookupStrategy(CREATE_IF_NOT_FOUND, evaluationContextProvider);

  Method method = TestAggregateRepository22.class.getMethod(methodName);
  RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(TestAggregateRepository22.class);
  ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
  RepositoryQuery query = lookupStrategy.resolveQuery(method, repositoryMetadata, projectionFactory, null);
  assertNotNull(query);
  Object object = query.execute(new Object[0]);
  assertNull(object);
}
 
Example 23
/**
 * Creates a new {@link AbstractMongoQuery} from the given {@link MongoQueryMethod} and {@link MongoOperations}.
 *
 * @param method must not be {@literal null}.
 * @param projectionFactory - the projection factory
 */
@Autowired
public NonReactiveAggregateMongoQuery(Method method, RepositoryMetadata metadata, MongoOperations mongoOperations,
                                      ProjectionFactory projectionFactory, MongoQueryExecutor queryExecutor) {
  super(new MongoQueryMethod(method, metadata, projectionFactory, mongoOperations.getConverter().getMappingContext()),
        mongoOperations,
        new SpelExpressionParser(),
        CONTEXT_PROVIDER);
  this.mongoOperations = mongoOperations;
  this.method = method;
  this.queryExecutor = queryExecutor;
}
 
Example 24
/**
 * Creates a new {@link AbstractMongoQuery} from the given {@link MongoQueryMethod} and {@link MongoOperations}.
 *
 * @param method must not be {@literal null}.
 * @param projectionFactory - the projection factory
 */
@Autowired
public ReactiveAggregateMongoQuery(Method method, RepositoryMetadata metadata, ReactiveMongoOperations mongoOperations,
                                   ProjectionFactory projectionFactory, MongoQueryExecutor queryExecutor) {
  super(new ReactiveMongoQueryMethod(method, metadata, projectionFactory,
                                     mongoOperations.getConverter().getMappingContext()),
        mongoOperations,
        new SpelExpressionParser(),
        CONTEXT_PROVIDER);
  this.mongoOperations = mongoOperations;
  this.method = method;
  this.queryExecutor = queryExecutor;
}
 
Example 25
Source Project: ignite   Source File: IgniteRepositoryFactory.java    License: Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected QueryLookupStrategy getQueryLookupStrategy(final QueryLookupStrategy.Key key,
    EvaluationContextProvider evaluationCtxProvider) {

    return new QueryLookupStrategy() {
        @Override public RepositoryQuery resolveQuery(final Method mtd, final RepositoryMetadata metadata,
            final ProjectionFactory factory, NamedQueries namedQueries) {

            final Query annotation = mtd.getAnnotation(Query.class);

            if (annotation != null) {
                String qryStr = annotation.value();

                if (key != Key.CREATE && StringUtils.hasText(qryStr))
                    return new IgniteRepositoryQuery(metadata,
                        new IgniteQuery(qryStr, isFieldQuery(qryStr), IgniteQueryGenerator.getOptions(mtd)),
                        mtd, factory, ignite.getOrCreateCache(repoToCache.get(metadata.getRepositoryInterface())));
            }

            if (key == QueryLookupStrategy.Key.USE_DECLARED_QUERY)
                throw new IllegalStateException("To use QueryLookupStrategy.Key.USE_DECLARED_QUERY, pass " +
                    "a query string via org.apache.ignite.springdata.repository.config.Query annotation.");

            return new IgniteRepositoryQuery(metadata, IgniteQueryGenerator.generateSql(mtd, metadata), mtd,
                factory, ignite.getOrCreateCache(repoToCache.get(metadata.getRepositoryInterface())));
        }
    };
}
 
Example 26
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repositoryMetadata,
                                    ProjectionFactory projectionFactory, NamedQueries namedQueries) {
  if (!isAggregateQueryAnnotated(method)) {
    return parentQueryLookupStrategy.resolveQuery(method, repositoryMetadata, projectionFactory, namedQueries);
  }
  else {
    return new ReactiveAggregateMongoQuery(method, repositoryMetadata, mongoOperations, projectionFactory, queryExecutor);
  }
}
 
Example 27
Source Project: spring-data-simpledb   Source File: SimpleDbQueryLookupStrategy.java    License: MIT License 5 votes vote down vote up
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) {
	SimpleDbQueryMethod queryMethod;
	
	if(SimpleDbQueryMethod.isAnnotatedQuery(method)) {
		queryMethod = new SimpleDbQueryMethod(method, metadata, simpleDbOperations.getSimpleDb()
				.getSimpleDbDomain());
	} else {
		queryMethod = new SimpleDbPartTreeQueryMethod(method, metadata, simpleDbOperations.getSimpleDb()
				.getSimpleDbDomain());
	}
	
	return SimpleDbRepositoryQuery.fromQueryAnnotation(queryMethod, simpleDbOperations);
}
 
Example 28
@Test(dataProvider = "queryMethods", expectedExceptions = {JsonParseException.class})
public void testCreateAggregateQuery(String methodName) throws Exception {
  NonReactiveAggregateQuerySupportingRepositoryFactory factory =
      new NonReactiveAggregateQuerySupportingRepositoryFactory(mongoOperations, queryExecutor);
  Optional<QueryLookupStrategy> lookupStrategy = factory.getQueryLookupStrategy(CREATE_IF_NOT_FOUND, evaluationContextProvider);
  assertTrue(lookupStrategy.isPresent(), "Expecting non null lookupStrategy");
  Method method = TestAggregateRepository22.class.getMethod(methodName);
  RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(TestAggregateRepository22.class);
  ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
  RepositoryQuery query = lookupStrategy.get().resolveQuery(method, repositoryMetadata, projectionFactory, null);
  assertNotNull(query);
  Object object = query.execute(new Object[0]);
  assertNull(object);
}
 
Example 29
@SuppressWarnings({ "unchecked", "rawtypes" })
private SimpleDbQueryMethod prepareQueryMethodToTest(String methodName, Class<?> entityClass) throws Exception {
	RepositoryMetadata repositoryMetadata = Mockito.mock(RepositoryMetadata.class);
	when(repositoryMetadata.getDomainType()).thenReturn((Class) entityClass);

	Method testMethod = AnnotatedQueryRepository.class.getMethod(methodName);
	when(repositoryMetadata.getReturnedDomainClass(testMethod)).thenReturn((Class) entityClass);
	SimpleDbDomain simpleDbDomain = new SimpleDbDomain();
	return new SimpleDbQueryMethod(testMethod, repositoryMetadata, simpleDbDomain);
}
 
Example 30
Source Project: spring-data-dynamodb   Source File: DynamoDBQueryMethod.java    License: Apache License 2.0 5 votes vote down vote up
public DynamoDBQueryMethod(Method method, RepositoryMetadata metadata) {
	super(method, metadata);
	this.method = method;
	this.scanEnabledForRepository = metadata.getRepositoryInterface().isAnnotationPresent(EnableScan.class);
	this.scanCountEnabledForRepository = metadata.getRepositoryInterface().isAnnotationPresent(EnableScanCount.class);

}