diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessTaskRelationServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessTaskRelationServiceImpl.java index 9a476c8e8b..d4e79bf350 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessTaskRelationServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessTaskRelationServiceImpl.java @@ -22,6 +22,7 @@ import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.ProcessTaskRelationService; import org.apache.dolphinscheduler.api.service.ProjectService; import org.apache.dolphinscheduler.common.Constants; +import org.apache.dolphinscheduler.common.enums.TaskType; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation; import org.apache.dolphinscheduler.dao.entity.ProcessTaskRelationLog; @@ -191,15 +192,57 @@ public class ProcessTaskRelationServiceImpl extends BaseServiceImpl implements P /** * delete process task relation * - * @param loginUser login user - * @param projectCode project code + * @param loginUser login user + * @param projectCode project code * @param processDefinitionCode process definition code - * @param taskCode the post task code + * @param taskCode the post task code * @return delete result code */ @Override public Map deleteTaskProcessRelation(User loginUser, long projectCode, long processDefinitionCode, long taskCode) { - return null; + Project project = projectMapper.queryByCode(projectCode); + //check user access for project + Map result = projectService.checkProjectAndAuth(loginUser, project, projectCode); + if (result.get(Constants.STATUS) != Status.SUCCESS) { + return result; + } + if (taskCode == 0) { + putMsg(result, Status.DELETE_TASK_PROCESS_RELATION_ERROR); + return result; + } + List downstreamList = processTaskRelationMapper.queryByCode(projectCode, processDefinitionCode, taskCode, 0L); + if (CollectionUtils.isNotEmpty(downstreamList)) { + Set postTaskCodes = downstreamList + .stream() + .map(ProcessTaskRelation::getPostTaskCode) + .collect(Collectors.toSet()); + putMsg(result, Status.TASK_HAS_DOWNSTREAM, org.apache.commons.lang.StringUtils.join(postTaskCodes, ",")); + return result; + } + + ProcessTaskRelationLog processTaskRelationLog = new ProcessTaskRelationLog(); + processTaskRelationLog.setProjectCode(projectCode); + processTaskRelationLog.setPreTaskCode(taskCode); + processTaskRelationLog.setProcessDefinitionCode(processDefinitionCode); + int deleteRelation = processTaskRelationMapper.deleteRelation(processTaskRelationLog); + if (0 == deleteRelation) { + putMsg(result, Status.DELETE_TASK_PROCESS_RELATION_ERROR); + } + + TaskDefinition taskDefinition = taskDefinitionMapper.queryByCode(taskCode); + if (null == taskDefinition) { + putMsg(result, Status.DATA_IS_NULL, "taskDefinition"); + return result; + } + if (TaskType.CONDITIONS.getDesc().equals(taskDefinition.getTaskType()) + || TaskType.DEPENDENT.getDesc().equals(taskDefinition.getTaskType()) + || TaskType.SUB_PROCESS.getDesc().equals(taskDefinition.getTaskType())) { + int deleteTaskDefinition = taskDefinitionMapper.deleteByCode(taskCode); + if (0 == deleteTaskDefinition) { + putMsg(result, Status.DELETE_TASK_PROCESS_RELATION_ERROR); + } + } + return result; } /** diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessTaskRelationServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessTaskRelationServiceTest.java index 55efbfe088..2b7b5a7fe9 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessTaskRelationServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessTaskRelationServiceTest.java @@ -21,6 +21,7 @@ import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.impl.ProcessTaskRelationServiceImpl; import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl; import org.apache.dolphinscheduler.common.Constants; +import org.apache.dolphinscheduler.common.enums.TaskType; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation; @@ -461,4 +462,34 @@ public class ProcessTaskRelationServiceTest { Map result1 = processTaskRelationService.deleteUpstreamRelation(loginUser, projectCode, "123", taskCode); Assert.assertEquals(Status.SUCCESS, result1.get(Constants.STATUS)); } + + @Test + public void testDeleteTaskProcessRelation() { + long projectCode = 1L; + long taskCode = 2L; + long processDefinitionCode = 3L; + long preTaskCode = 4L; + long postTaskCode = 5L; + Project project = getProject(projectCode); + Mockito.when(projectMapper.queryByCode(projectCode)).thenReturn(project); + + User loginUser = new User(); + loginUser.setId(-1); + loginUser.setUserType(UserType.GENERAL_USER); + Map result = new HashMap<>(); + putMsg(result, Status.SUCCESS, projectCode); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectCode)).thenReturn(result); + Mockito.when(processTaskRelationMapper.queryByCode(projectCode, processDefinitionCode, preTaskCode, postTaskCode)).thenReturn(Lists.newArrayList()); + ProcessTaskRelationLog processTaskRelationLog = new ProcessTaskRelationLog(); + processTaskRelationLog.setProjectCode(projectCode); + processTaskRelationLog.setPreTaskCode(taskCode); + processTaskRelationLog.setProcessDefinitionCode(processDefinitionCode); + Mockito.when(processTaskRelationMapper.deleteRelation(processTaskRelationLog)).thenReturn(1); + TaskDefinition taskDefinition = new TaskDefinition(); + taskDefinition.setTaskType(TaskType.CONDITIONS.getDesc()); + Mockito.when(taskDefinitionMapper.queryByCode(taskCode)).thenReturn(taskDefinition); + Mockito.when(taskDefinitionMapper.deleteByCode(taskCode)).thenReturn(1); + result = processTaskRelationService.deleteTaskProcessRelation(loginUser, projectCode, processDefinitionCode, taskCode); + Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + } }