TSK-447: code cleanup

This commit is contained in:
Mustapha Zorgati 2018-05-02 21:25:20 +02:00
parent cf0921ba8d
commit 44dfbb983e
4 changed files with 63 additions and 59 deletions

View File

@ -396,8 +396,15 @@ public class ClassificationServiceImpl implements ClassificationService {
}
private boolean isReferentialIntegrityConstraintViolation(PersistenceException e) {
return e.getCause() instanceof SQLException && ((SQLException) e.getCause()).getSQLState().equals("23503")
|| e.getCause() instanceof SQLIntegrityConstraintViolationException && e.getMessage().contains("-532");
return isH2OrPostgresIntegrityConstraintViolation(e) || isDb2IntegrityConstraintViolation(e);
}
private boolean isDb2IntegrityConstraintViolation(PersistenceException e) {
return e.getCause() instanceof SQLIntegrityConstraintViolationException && e.getMessage().contains("-532");
}
private boolean isH2OrPostgresIntegrityConstraintViolation(PersistenceException e) {
return e.getCause() instanceof SQLException && ((SQLException) e.getCause()).getSQLState().equals("23503");
}
}

View File

@ -487,10 +487,9 @@ public class TaskServiceImpl implements TaskService {
taskanaEngine.openConnection();
LOGGER.debug("entry to transferBulk(targetWbId = {}, taskIds = {})", destinationWorkbasketId, taskIds);
// Check pre-conditions with trowing Exceptions
if (destinationWorkbasketId == null || destinationWorkbasketId.isEmpty() || taskIds == null
|| taskIds.isEmpty()) {
if (destinationWorkbasketId == null || destinationWorkbasketId.isEmpty()) {
throw new InvalidArgumentException(
"DestinationWorkbasketId or TaskIds must not be null or empty.");
"DestinationWorkbasketId must not be null or empty.");
}
Workbasket destinationWorkbasket = workbasketService.getWorkbasket(destinationWorkbasketId);
@ -510,9 +509,9 @@ public class TaskServiceImpl implements TaskService {
LOGGER.debug("entry to transferBulk(targetWbKey = {}, domain = {}, taskIds = {})", destinationWorkbasketKey,
destinationWorkbasketDomain, taskIds);
// Check pre-conditions with trowing Exceptions
if (destinationWorkbasketKey == null || destinationWorkbasketDomain == null || taskIds == null) {
if (destinationWorkbasketKey == null || destinationWorkbasketDomain == null) {
throw new InvalidArgumentException(
"DestinationWorkbasketKey or domain or TaskIds can´t be used as NULL-Parameter.");
"DestinationWorkbasketKey or domain can´t be used as NULL-Parameter.");
}
Workbasket destinationWorkbasket = workbasketService.getWorkbasket(destinationWorkbasketKey,
destinationWorkbasketDomain);
@ -527,6 +526,12 @@ public class TaskServiceImpl implements TaskService {
private BulkOperationResults<String, TaskanaException> transferTasks(List<String> taskIdsToBeTransferred,
Workbasket destinationWorkbasket) throws InvalidArgumentException {
// Check pre-conditions with trowing Exceptions
if (taskIdsToBeTransferred == null
|| taskIdsToBeTransferred.isEmpty() || taskIdsToBeTransferred.contains("")) {
throw new InvalidArgumentException(
"TaskIds must not be null,empty or an empty string.");
}
BulkOperationResults<String, TaskanaException> bulkLog = new BulkOperationResults<>();
// convert to ArrayList<String> if necessary to prevent a UnsupportedOperationException while removing
@ -558,7 +563,7 @@ public class TaskServiceImpl implements TaskService {
}
// check source WB (read)+transfer
Set<String> workbasketIds = new HashSet<>();
taskSummaries.stream().forEach(t -> workbasketIds.add(t.getWorkbasketId()));
taskSummaries.forEach(t -> workbasketIds.add(t.getWorkbasketId()));
WorkbasketQueryImpl query = (WorkbasketQueryImpl) workbasketService.createWorkbasketQuery();
query.setUsedToAugmentTasks(true);
List<WorkbasketSummary> sourceWorkbaskets;
@ -585,8 +590,8 @@ public class TaskServiceImpl implements TaskService {
bulkLog.addError(currentTaskId,
new InvalidStateException("Completed task with id " + currentTaskId + " cannot be transferred."));
taskIdIterator.remove();
} else if (!sourceWorkbaskets.stream()
.anyMatch(wb -> taskSummary.getWorkbasketId().equals(wb.getId()))) {
} else if (sourceWorkbaskets.stream()
.noneMatch(wb -> taskSummary.getWorkbasketId().equals(wb.getId()))) {
bulkLog.addError(currentTaskId,
new NotAuthorizedException(
"The workbasket of this task got not TRANSFER permissions. TaskId=" + currentTaskId));
@ -1397,43 +1402,6 @@ public class TaskServiceImpl implements TaskService {
}
}
/**
* hold a pair of priority and Duration.
*
* @author bbr
*/
static class PrioDurationHolder {
private Duration duration;
private int prio;
PrioDurationHolder(Duration duration, int prio) {
super();
this.duration = duration;
this.prio = prio;
}
public Duration getDuration() {
return duration;
}
public int getPrio() {
return prio;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("PrioDurationHolder [duration=");
builder.append(duration);
builder.append(", prio=");
builder.append(prio);
builder.append("]");
return builder.toString();
}
}
BulkOperationResults<String, Exception> classificationChanged(String taskId, String classificationId)
throws TaskNotFoundException, ClassificationNotFoundException {
LOGGER.debug("entry to classificationChanged(taskId = {} , classificationId = {} )", taskId, classificationId);
@ -1501,4 +1469,41 @@ public class TaskServiceImpl implements TaskService {
return result;
}
/**
* hold a pair of priority and Duration.
*
* @author bbr
*/
static class PrioDurationHolder {
private Duration duration;
private int prio;
PrioDurationHolder(Duration duration, int prio) {
super();
this.duration = duration;
this.prio = prio;
}
public Duration getDuration() {
return duration;
}
public int getPrio() {
return prio;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("PrioDurationHolder [duration=");
builder.append(duration);
builder.append(", prio=");
builder.append(prio);
builder.append("]");
return builder.toString();
}
}
}

View File

@ -183,9 +183,7 @@ public interface TaskMapper {
@Param("referencetask") TaskSummaryImpl referencetask);
@Select("<script>SELECT ID, STATE, WORKBASKET_ID FROM TASKANA.TASK "
+ "<if test=\" taskIds != null and !taskIds.isEmpty() \">"
+ "WHERE ID IN( <foreach item='item' collection='taskIds' separator=',' >#{item}</foreach> ) "
+ "</if>"
+ "<if test=\"_databaseId == 'db2'\">with UR </if> "
+ "</script>")
@Results(value = {

View File

@ -193,9 +193,7 @@ public class TransferTaskAccTest extends AbstractAccTest {
@WithAccessId(userName = "teamlead_1", groupNames = {"group_1"})
@Test
public void testBulkTransferTaskWithExceptions()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException,
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException,
InvalidStateException, InvalidOwnerException {
throws NotAuthorizedException, InvalidArgumentException, WorkbasketNotFoundException, TaskNotFoundException {
TaskService taskService = taskanaEngine.getTaskService();
Workbasket wb = taskanaEngine.getWorkbasketService().getWorkbasket("USER_1_1", "DOMAIN_A");
Instant before = Instant.now();
@ -203,7 +201,6 @@ public class TransferTaskAccTest extends AbstractAccTest {
taskIdList.add("TKI:000000000000000000000000000000000006"); // working
taskIdList.add("TKI:000000000000000000000000000000000041"); // NotAuthorized READ
taskIdList.add("TKI:200000000000000000000000000000000006"); // NotAuthorized TRANSFER
taskIdList.add(""); // InvalidArgument
taskIdList.add(null); // InvalidArgument (added with ""), duplicate
taskIdList.add("TKI:000000000000000000000000000000000099"); // TaskNotFound
taskIdList.add("TKI:100000000000000000000000000000000006"); // already completed
@ -257,14 +254,11 @@ public class TransferTaskAccTest extends AbstractAccTest {
userName = "teamlead_1",
groupNames = {"group_1"})
@Test
public void testTransferTasksWithListNotSupportingRemove()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException,
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException,
InvalidStateException, InvalidOwnerException {
public void testTransferTasksWithListNotSupportingRemove() throws NotAuthorizedException, InvalidArgumentException,
WorkbasketNotFoundException {
TaskService taskService = taskanaEngine.getTaskService();
List<String> taskIds = Collections.singletonList("");
List<String> taskIds = Collections.singletonList("TKI:000000000000000000000000000000000006");
taskService.transferTasks("WBI:100000000000000000000000000000000006", taskIds);
}
@WithAccessId(