EricPyZhou commented on code in PR #12076:
URL: https://github.com/apache/dolphinscheduler/pull/12076#discussion_r996368313


##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java:
##########
@@ -506,63 +441,90 @@ public Result<Object> updateResource(User loginUser,
             resource.setSize(file.getSize());
         }
 
-        try {
-            resourcesMapper.updateById(resource);
-            if (resource.isDirectory()) {
-                List<Integer> childrenResource = listAllChildren(resource, 
false);
-                if (CollectionUtils.isNotEmpty(childrenResource)) {
-                    String matcherFullName = 
Matcher.quoteReplacement(fullName);
-                    List<Resource> childResourceList;
-                    Integer[] childResIdArray = childrenResource.toArray(new 
Integer[childrenResource.size()]);
-                    List<Resource> resourceList = 
resourcesMapper.listResourceByIds(childResIdArray);
-                    childResourceList = resourceList.stream().map(t -> {
-                        
t.setFullName(t.getFullName().replaceFirst(originFullName, matcherFullName));
-                        t.setUpdateTime(now);
-                        return t;
-                    }).collect(Collectors.toList());
-                    resourcesMapper.batchUpdateResource(childResourceList);
-
-                    if (ResourceType.UDF.equals(resource.getType())) {
-                        List<UdfFunc> udfFuncs = 
udfFunctionMapper.listUdfByResourceId(childResIdArray);
-                        if (CollectionUtils.isNotEmpty(udfFuncs)) {
-                            udfFuncs = udfFuncs.stream().map(t -> {
-                                
t.setResourceName(t.getResourceName().replaceFirst(originFullName, 
matcherFullName));
-                                t.setUpdateTime(now);
-                                return t;
-                            }).collect(Collectors.toList());
-                            udfFunctionMapper.batchUpdateUdfFunc(udfFuncs);
-                        }
-                    }
-                }
-            } else if (ResourceType.UDF.equals(resource.getType())) {
-                List<UdfFunc> udfFuncs = 
udfFunctionMapper.listUdfByResourceId(new Integer[]{resourceId});
-                if (CollectionUtils.isNotEmpty(udfFuncs)) {
-                    udfFuncs = udfFuncs.stream().map(t -> {
-                        t.setResourceName(fullName);
-                        t.setUpdateTime(now);
-                        return t;
-                    }).collect(Collectors.toList());
-                    udfFunctionMapper.batchUpdateUdfFunc(udfFuncs);
-                }
+        // if name unchanged, return directly without moving on HDFS
+        if (originResourceName.equals(name) && file == null) {
+            return result;
+        }
 
+        List<ResourcesTask> existResourcesList;
+        if (resource.isDirectory()) {
+            existResourcesList = 
resourceTaskMapper.selectSubfoldersFullNames(originFullName + FOLDER_SEPARATOR);
+        } else {
+            existResourcesList = resourceTaskMapper.selectByMap(
+                    Collections.singletonMap("full_name", originFullName));
+        }
+
+        if (existResourcesList.size() > 0 && !fullName.equals(originFullName)) 
{
+            // check if any related task is online. If it is, it can not be 
updated.
+            for (ResourcesTask existResource : existResourcesList) {
+                int taskId = existResource.getTaskId();
+                if 
(processService.isTaskOnline(taskDefinitionMapper.selectById(taskId).getCode()))
 {
+                    logger.error("can't be updated,because it is used of 
process definition that's online");
+                    logger.error("resource task relation id:{} is used of task 
code {}", existResource.getId(),
+                            taskDefinitionMapper.selectById(taskId).getCode());
+                    putMsg(result, Status.RESOURCE_IS_USED);
+                    return result;
+                }
             }
 
-            putMsg(result, Status.SUCCESS);
-            Map<String, Object> resultMap = new HashMap<>();
-            for (Map.Entry<Object, Object> entry : new 
BeanMap(resource).entrySet()) {
-                if 
(!Constants.CLASS.equalsIgnoreCase(entry.getKey().toString())) {
-                    resultMap.put(entry.getKey().toString(), entry.getValue());
+            for (ResourcesTask existResource : existResourcesList) {
+                int taskId = existResource.getTaskId();
+                long taskCode = 
taskDefinitionMapper.selectById(taskId).getCode();
+
+                List<ProcessTaskRelation> processTaskRelation = 
processTaskRelationMapper.selectByMap(
+                        Collections.singletonMap("post_task_code", taskCode));
+                if (processTaskRelation.size() > 0) {
+                    long processDefinitionCode = 
processTaskRelation.get(0).getProcessDefinitionCode();
+                    int processDefinitionVersion = 
processTaskRelation.get(0).getProcessDefinitionVersion();
+                    List<ProcessTaskRelation> taskRelationList = 
processTaskRelationMapper.queryByProcessCode(
+                            processTaskRelation.get(0).getProjectCode(),
+                            processDefinitionCode);
+
+                    List<TaskDefinition> taskDefinitionLogList = new 
ArrayList<>();
+
+                    if (taskRelationList.size() > 0) {
+                        ProcessDefinitionLog processDefinition =
+                                
processDefinitionLogMapper.queryByDefinitionCodeAndVersion(
+                                        processDefinitionCode, 
processDefinitionVersion);
+                        for (ProcessTaskRelation taskRelation : 
taskRelationList) {
+                            long taskCodeInProcess = 
taskRelation.getPostTaskCode();
+                            TaskDefinition taskDefinition = 
taskDefinitionMapper.queryByCode(taskCodeInProcess);
+                            if (taskCodeInProcess == taskCode) {
+                                // originFullName is a prefix if isDirectory 
is true
+                                
taskDefinition.setTaskParams(RemoveResourceFromResourceList(originFullName,
+                                        taskDefinition.getTaskParams(),
+                                        resource.isDirectory()));
+                                // if isDirectory is true, fullName is the new 
prefix. we replace old prefix
+                                // of resource fullname with the new prefix.
+                                // if isDirectory is false, fullName is the 
new path.
+                                
taskDefinition.setTaskParams(AddResourceToResourceList(originFullName,
+                                        fullName,
+                                        existResource.getFullName(),
+                                        taskDefinition.getTaskParams(),
+                                        resource.isDirectory()));

Review Comment:
   > Why call `taskDefinition.setTaskParams` twice?
   
   The first setTaskParams removes resources deselected by users. (it was 
selected as first, and then get deselected)
   The second setTaskParams add resources get selected by users.
   
   I used two setTaskParams to add code readability.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to