TSK-926: Refactor internal taskana engine
This commit is contained in:
parent
35ae1ea52f
commit
25668e1324
|
@ -1,5 +1,7 @@
|
|||
package pro.taskana.impl;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
|
||||
import pro.taskana.TaskanaEngine;
|
||||
|
@ -24,6 +26,15 @@ public interface InternalTaskanaEngine {
|
|||
*/
|
||||
void returnConnection();
|
||||
|
||||
/**
|
||||
*
|
||||
* Executes the supplier after openConnection is called and then returns the connection.
|
||||
* @param supplier a function that returns something of type T
|
||||
* @param <T> any type
|
||||
* @return the result of the supplier
|
||||
*/
|
||||
<T> T openAndReturnConnection(Supplier<T> supplier);
|
||||
|
||||
/**
|
||||
* Initializes the SqlSessionManager.
|
||||
*/
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.util.Deque;
|
|||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.apache.ibatis.mapping.Environment;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
|
@ -353,6 +354,17 @@ public class TaskanaEngineImpl implements TaskanaEngine {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T openAndReturnConnection(Supplier<T> supplier) {
|
||||
try {
|
||||
openConnection();
|
||||
return supplier.get();
|
||||
} finally {
|
||||
// will be called before return & in case of exceptions
|
||||
returnConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean domainExists(String domain) {
|
||||
return getConfiguration().getDomains().contains(domain);
|
||||
|
|
|
@ -102,20 +102,13 @@ public class WorkbasketAccessItemQueryImpl implements WorkbasketAccessItemQuery
|
|||
@Override
|
||||
public List<WorkbasketAccessItem> list() {
|
||||
LOGGER.debug("entry to list(), this = {}", this);
|
||||
List<WorkbasketAccessItem> result = new ArrayList<>();
|
||||
try {
|
||||
taskanaEngine.openConnection();
|
||||
List<WorkbasketAccessItemImpl> foundAccessItms = taskanaEngine.getSqlSession()
|
||||
.selectList(LINK_TO_MAPPER, this);
|
||||
result.addAll(foundAccessItms);
|
||||
return result;
|
||||
} finally {
|
||||
taskanaEngine.returnConnection();
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("exit from list(). Returning {} resulting Objects: {} ", result.size(),
|
||||
LoggerUtils.listToString(result));
|
||||
}
|
||||
List<WorkbasketAccessItem> result = taskanaEngine.openAndReturnConnection(
|
||||
() -> new ArrayList<>(taskanaEngine.getSqlSession().selectList(LINK_TO_MAPPER, this)));
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("exit from list(). Returning {} resulting Objects: {} ", result.size(),
|
||||
LoggerUtils.listToString(result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -35,8 +35,7 @@ public class WorkbasketAccessItemQueryImplTest {
|
|||
|
||||
@Test
|
||||
public void should_ReturnList_when_BuilderIsUsed() {
|
||||
when(internalTaskanaEngine.getSqlSession()).thenReturn(sqlSession);
|
||||
when(sqlSession.selectList(any(), any())).thenReturn(new ArrayList<>());
|
||||
when(internalTaskanaEngine.openAndReturnConnection(any())).thenReturn(new ArrayList<>());
|
||||
|
||||
List<WorkbasketAccessItem> result = workbasketAccessItemQueryImpl.accessIdIn("test", "asd")
|
||||
.list();
|
||||
|
|
Loading…
Reference in New Issue