Skip to content

Commit

Permalink
update implementation of column repository 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 beb0ff4 commit 4dff750
Showing 1 changed file with 36 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,71 +33,48 @@
* @param <T> the entity type
* @param <K> the K entity
*/
public abstract class AbstractColumnRepositoryProxy<T, K> extends BaseColumnRepository<T> implements InvocationHandler {
public abstract class AbstractColumnRepositoryProxy<T, K> extends BaseColumnRepository<T, K> {

protected abstract AbstractRepository<T, K> repository();
@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();
}

protected abstract Class<?> repositoryType();
@Override
protected Object executeDeleteByAll(Object instance, Method method, Object[] params) {
ColumnDeleteQuery deleteQuery = deleteQuery(method, params);
template().delete(deleteQuery);
return Void.class;
}

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

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 -> {
ColumnQuery queryFindAll = ColumnQuery.select().from(entityMetadata().name()).build();
return executeFindByQuery(method, args, typeClass, updateQueryDynamically(args, queryFindAll));
}
case DELETE_BY -> {
ColumnDeleteQuery deleteQuery = deleteQuery(method, args);
template().delete(deleteQuery);
return Void.class;
}
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));
}
default -> {
return Void.class;
}
}
@Override
protected Object executeExistByQuery(Object instance, Method method, Object[] params) {
return executeExistsByQuery(query(method, params));
}

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

@Override
protected Object executeFindByQuery(Object instance, Method method, Object[] params) {
Class<?> type = entityMetadata().type();
return executeFindByQuery(method, params, type, query(method, params));
}

}

0 comments on commit 4dff750

Please sign in to comment.