Skip to content

Commit

Permalink
Merge pull request #190 from croz-ltd/feature_addDomainClassToReposit…
Browse files Browse the repository at this point in the history
…ories

Add support for resolving domain class from repository method to make AOP on repositories easier to work with
  • Loading branch information
jzrilic authored Jan 24, 2024
2 parents ae1392b + f48178b commit fc8808f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,10 @@ public interface SearchExecutor<T> {
*/
<R, P> boolean exists(R request, SearchConfiguration<T, P, R> searchConfiguration);

/**
* Returns repository domain class.
*
* @return repository domain class
*/
Class<T> getDomainClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@ public interface StringSearchExecutor<T> {
*/
<P> boolean exists(String searchTerm, List<String> propertyToSearchList, SearchConfiguration<T, P, Map<String, Object>> searchConfiguration);

/**
* Returns repository domain class.
*
* @return repository domain class
*/
Class<T> getDomainClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ public class JpaSearchExecutor<T> implements SearchExecutor<T> {

private final EntityManager entityManager;

private final Class<T> domainClass;

private final JpaQueryBuilder<T> queryBuilder;

public JpaSearchExecutor(EntityManager entityManager, JpaEntityInformation<T, ?> entityInformation) {
this.entityManager = entityManager;
this.queryBuilder = new JpaQueryBuilder<>(entityManager, entityInformation.getJavaType());
domainClass = entityInformation.getJavaType();
queryBuilder = new JpaQueryBuilder<>(entityManager, entityInformation.getJavaType());
}

@Override
Expand Down Expand Up @@ -101,6 +104,11 @@ public <R, P> boolean exists(R request, SearchConfiguration<T, P, R> searchConfi
return entityManager.createQuery(query).setMaxResults(1).getResultList().size() == 1;
}

@Override
public Class<T> getDomainClass() {
return domainClass;
}

private <R, P> long executeCountQuery(R request, SearchConfiguration<T, P, R> searchConfiguration) {
CriteriaQuery<Long> countQuery = queryBuilder.buildCountQuery(request, searchConfiguration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ public class JpaStringSearchExecutor<T> implements StringSearchExecutor<T> {

private final EntityManager entityManager;

private final Class<T> domainClass;

private final JpaQueryBuilder<T> queryBuilder;

private final ManagedType<?> managedType;

public JpaStringSearchExecutor(StringToEntityPropertyMapConverter stringToEntityPropertyMapConverter, EntityManager entityManager, JpaEntityInformation<T, ?> jpaEntityInformation) {
this.stringToEntityPropertyMapConverter = stringToEntityPropertyMapConverter;
this.entityManager = entityManager;
this.queryBuilder = new JpaQueryBuilder<>(entityManager, jpaEntityInformation.getJavaType());
this.managedType = jpaEntityInformation.getRequiredIdAttribute().getDeclaringType();
domainClass = jpaEntityInformation.getJavaType();
queryBuilder = new JpaQueryBuilder<>(entityManager, jpaEntityInformation.getJavaType());
managedType = jpaEntityInformation.getRequiredIdAttribute().getDeclaringType();
}

@Override
Expand Down Expand Up @@ -121,6 +124,11 @@ public <P> boolean exists(String searchTerm, List<String> propertyToSearchList,
return entityManager.createQuery(query).setMaxResults(1).getResultList().size() == 1;
}

@Override
public Class<T> getDomainClass() {
return domainClass;
}

private Map<String, Object> convertToMap(String searchTerm, List<String> propertyToSearchList, SearchConfiguration<T, ?, Map<String, Object>> searchConfiguration) {
return stringToEntityPropertyMapConverter.convert(searchTerm, propertyToSearchList, managedType, searchConfiguration.getSearchPropertyConfiguration());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,13 @@ void shouldReturnFalseWhenEntityDoesntExist() {
// then
assertThat(result).isFalse();
}

@Test
void shouldReturnDomainClass() {
// when
Class<?> result = testEntitySearchRepository.getDomainClass();

// then
assertThat(result).isEqualTo(TestEntity.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,13 @@ void shouldFindByRangeQuery() {
// then
assertThat(result).isEqualTo(3);
}

@Test
void shouldReturnDomainClass() {
// when
Class<?> result = testEntityStringSearchRepository.getDomainClass();

// then
assertThat(result).isEqualTo(TestStringSearchEntity.class);
}
}

0 comments on commit fc8808f

Please sign in to comment.