You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by GitBox <gi...@apache.org> on 2021/11/24 09:55:45 UTC

[GitHub] [dolphinscheduler] caishunfeng commented on a change in pull request #6970: [Improvement][API][num-8] add ProcessTaskRelationServiceImpl.createProcessTaskRelation

caishunfeng commented on a change in pull request #6970:
URL: https://github.com/apache/dolphinscheduler/pull/6970#discussion_r755632605



##########
File path: dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessTaskRelationServiceImpl.java
##########
@@ -77,9 +87,87 @@
      * @param postTaskCode postTaskCode
      * @return create result code
      */
+    @Transactional(rollbackFor = RuntimeException.class)
     @Override
     public Map<String, Object> createProcessTaskRelation(User loginUser, long projectCode, long processDefinitionCode, long preTaskCode, long postTaskCode) {
-        return null;
+        Project project = projectMapper.queryByCode(projectCode);
+        //check user access for project
+        Map<String, Object> result = projectService.checkProjectAndAuth(loginUser, project, projectCode);
+        if (result.get(Constants.STATUS) != Status.SUCCESS) {
+            return result;
+        }
+        ProcessDefinition processDefinition = processDefinitionMapper.queryByCode(processDefinitionCode);
+        if (processDefinition == null) {
+            putMsg(result, Status.PROCESS_DEFINE_NOT_EXIST, processDefinitionCode);
+            return result;
+        }
+        if (processDefinition.getProjectCode() != projectCode) {
+            putMsg(result, Status.PROJECT_PROCESS_NOT_MATCH);
+            return result;
+        }
+        List<ProcessTaskRelation> processTaskRelations = processTaskRelationMapper.queryByCode(projectCode, processDefinitionCode, 0L, postTaskCode);
+        if (!processTaskRelations.isEmpty()) {
+            Map<Long, ProcessTaskRelation> preTaskCodeMap = processTaskRelations.stream()
+                .collect(Collectors.toMap(ProcessTaskRelation::getPreTaskCode, processTaskRelation -> processTaskRelation));
+            if (preTaskCodeMap.containsKey(preTaskCode) || (!preTaskCodeMap.containsKey(0L) && preTaskCode == 0L)) {
+                putMsg(result, Status.PROCESS_TASK_RELATION_EXIST, processDefinitionCode);
+                return result;
+            }
+            if (preTaskCodeMap.containsKey(0L) && preTaskCode != 0L) {
+                ProcessTaskRelationLog processTaskRelationLog = new ProcessTaskRelationLog(preTaskCodeMap.get(0L));
+                // delete no upstream
+                int delete = processTaskRelationMapper.deleteRelation(processTaskRelationLog);
+                int deleteLog = processTaskRelationLogMapper.deleteRelation(processTaskRelationLog);
+                if ((delete & deleteLog) == 0) {
+                    putMsg(result, Status.CREATE_PROCESS_TASK_RELATION_ERROR);
+                    throw new ServiceException(Status.CREATE_PROCESS_TASK_RELATION_ERROR);
+                }
+            }
+        }
+        Date now = new Date();
+        List<ProcessTaskRelationLog> processTaskRelationLogs = new ArrayList<>();
+        if (preTaskCode != 0L) {
+            // upstream is or not exist
+            List<ProcessTaskRelation> upstreamProcessTaskRelations = processTaskRelationMapper.queryByCode(projectCode, processDefinitionCode, 0L, preTaskCode);
+            TaskDefinition preTaskDefinition = taskDefinitionMapper.queryByCode(preTaskCode);
+            if (upstreamProcessTaskRelations.isEmpty()) {
+                ProcessTaskRelationLog processTaskRelationLog = setRelationLog(processDefinition, now, loginUser.getId(), preTaskDefinition);
+                processTaskRelationLog.setPreTaskCode(0L);
+                processTaskRelationLog.setPreTaskVersion(0);
+                processTaskRelationLogs.add(processTaskRelationLog);
+            }
+            TaskDefinition postTaskDefinition = taskDefinitionMapper.queryByCode(postTaskCode);
+            ProcessTaskRelationLog processTaskRelationLog = setRelationLog(processDefinition, now, loginUser.getId(), postTaskDefinition);
+            processTaskRelationLog.setPreTaskCode(preTaskDefinition.getCode());
+            processTaskRelationLog.setPreTaskVersion(preTaskDefinition.getVersion());
+            processTaskRelationLogs.add(processTaskRelationLog);
+        } else {
+            TaskDefinition postTaskDefinition = taskDefinitionMapper.queryByCode(postTaskCode);
+            ProcessTaskRelationLog processTaskRelationLog = setRelationLog(processDefinition, now, loginUser.getId(), postTaskDefinition);
+            processTaskRelationLog.setPreTaskCode(0L);
+            processTaskRelationLog.setPreTaskVersion(0);
+            processTaskRelationLogs.add(processTaskRelationLog);
+        }
+        int insert = processTaskRelationMapper.batchInsert(processTaskRelationLogs);
+        int insertLog = processTaskRelationLogMapper.batchInsert(processTaskRelationLogs);
+        if ((insert & insertLog) > 0) {

Review comment:
       Maybe this judge condition is not always correct, such as `1&2=0`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@dolphinscheduler.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org