Skip to content

Commit

Permalink
feat: refacatoring document repository
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Dec 28, 2023
1 parent 89c4be4 commit 905f3c8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,84 +40,50 @@
* @param <T> the entity type
* @param <K> the key type
*/
public abstract class AbstractDocumentRepositoryProxy<T, K> extends BaseDocumentRepository<T> implements InvocationHandler {
public abstract class AbstractDocumentRepositoryProxy<T, K> extends BaseDocumentRepository<T, K> {

protected abstract AbstractRepository<T, K> repository();

protected abstract Class<?> repositoryType();
@Override
protected Object executeQuery(Object instance, Method method, Object[] params) {
Class<?> type = entityMetadata().type();
DynamicQueryMethodReturn methodReturn = DynamicQueryMethodReturn.builder()
.withArgs(params)
.withMethod(method)
.withTypeClass(type)
.withPrepareConverter(q -> template().prepare(q))
.withQueryConverter(q -> template().query(q)).build();
return methodReturn.execute();
}

@Override
public Object invoke(Object instance, Method method, Object[] args) throws Throwable {
protected Object executeDeleteByAll(Object instance, Method method, Object[] params) {
DocumentDeleteQuery documentDeleteQuery = deleteQuery(method, params);
template().delete(documentDeleteQuery);
return null;
}

RepositoryType type = RepositoryType.of(method, repositoryType());
@Override
protected Object executeFindAll(Object instance, Method method, Object[] params) {
Class<?> typeClass = entityMetadata().type();
DocumentQuery queryFindAll = select().from(entityMetadata().name()).build();
return executeFindByQuery(method, params, typeClass, updateQueryDynamically(params, queryFindAll));
}

@Override
protected Object executeExistByQuery(Object instance, Method method, Object[] params) {
return executeExistsByQuery(query(method, params));
}

switch (type) {
case DEFAULT -> {
return unwrapInvocationTargetException(() -> method.invoke(repository(), args));
}
case FIND_BY -> {
return executeFindByQuery(method, args, typeClass, query(method, args));
}
case COUNT_BY -> {
return executeCountByQuery(query(method, args));
}
case EXISTS_BY -> {
return executeExistsByQuery(query(method, args));
}
case FIND_ALL -> {
DocumentQuery queryFindAll = select().from(entityMetadata().name()).build();
return executeFindByQuery(method, args, typeClass, updateQueryDynamically(args, queryFindAll));
}
case DELETE_BY -> {
DocumentDeleteQuery documentDeleteQuery = deleteQuery(method, args);
template().delete(documentDeleteQuery);
return null;
}
case OBJECT_METHOD -> {
return unwrapInvocationTargetException(() -> method.invoke(this, args));
}
case DEFAULT_METHOD -> {
return unwrapInvocationTargetException(() -> InvocationHandler.invokeDefault(instance, method, args));
}
case ORDER_BY ->
throw new MappingException("Eclipse JNoSQL has not support for method that has OrderBy annotation");
case QUERY -> {
DynamicQueryMethodReturn methodReturn = DynamicQueryMethodReturn.builder()
.withArgs(args)
.withMethod(method)
.withTypeClass(typeClass)
.withPrepareConverter(q -> template().prepare(q))
.withQueryConverter(q -> template().query(q)).build();
return methodReturn.execute();
}
case CUSTOM_REPOSITORY -> {
Object customRepository = CDI.current().select(method.getDeclaringClass()).get();
return unwrapInvocationTargetException(() -> method.invoke(customRepository, args));
}
case SAVE -> {
return unwrapInvocationTargetException(() -> SAVE.invoke(new AnnotationOperation.Operation(method, args, repository())));
}
case INSERT -> {
return unwrapInvocationTargetException(() -> INSERT.invoke(new AnnotationOperation.Operation(method, args, repository())));
}
case DELETE -> {
return unwrapInvocationTargetException(() -> DELETE.invoke(new AnnotationOperation.Operation(method, args, repository())));
}
case UPDATE -> {
return unwrapInvocationTargetException(() -> UPDATE.invoke(new AnnotationOperation.Operation(method, args, repository())));
}
default -> {
return Void.class;
}
}
@Override
protected Object executeCountByQuery(Object instance, Method method, Object[] params) {
return executeCountByQuery(query(method, params));
}

private Object unwrapInvocationTargetException(ThrowingSupplier<Object> supplier) throws Throwable {
try {
return supplier.get();
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
@Override
protected Object executeFindByQuery(Object instance, Method method, Object[] params) {
Class<?> type = entityMetadata().type();
return executeFindByQuery(method, params, type, query(method, params));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.jnosql.communication.query.method.SelectMethodProvider;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.core.NoSQLPage;
import org.eclipse.jnosql.mapping.core.query.AbstractRepositoryProxy;
import org.eclipse.jnosql.mapping.document.JNoSQLDocumentTemplate;
import org.eclipse.jnosql.mapping.document.MappingDocumentQuery;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
Expand All @@ -47,7 +48,7 @@
import java.util.function.Function;
import java.util.stream.Stream;

public abstract class BaseDocumentRepository<T> {
public abstract class BaseDocumentRepository<T, K> extends AbstractRepositoryProxy<T, K> {

private static final SelectQueryParser SELECT_PARSER = new SelectQueryParser();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ void shouldSaveUsingAnnotation(){
}


interface BaseQuery<T> {
public interface BaseQuery<T> {

List<T> findByNameLessThan(String name);

Expand All @@ -881,7 +881,7 @@ default List<T> ada() {
}
}

interface PersonRepository extends CrudRepository<Person, Long>, BaseQuery<Person> {
public interface PersonRepository extends CrudRepository<Person, Long>, BaseQuery<Person> {

List<Person> findBySalary_Currency(String currency);

Expand Down

0 comments on commit 905f3c8

Please sign in to comment.