linyanbin666 commented on a change in pull request #7026:
URL: https://github.com/apache/dolphinscheduler/pull/7026#discussion_r775112166
##########
File path:
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java
##########
@@ -1356,4 +1363,108 @@ void listAllChildren(int resourceId,List<Integer>
childList) {
return CollectionUtils.isEmpty(resIds) ? new ArrayList<>() :
resourcesMapper.queryResourceListById(resIds);
}
+ /**
+ * query all resource component list created by user
+ *
+ * @param userId user id
+ * @return resource component list
+ */
+ @Override
+ public List<ResourceComponent> queryCreatedByUser(int userId) {
+ List<Resource> resourceList = resourcesMapper.queryResourceList(null,
userId, -1);
+ if (CollectionUtils.isEmpty(resourceList)) {
+ return Collections.emptyList();
+ }
+ Visitor visitor = new ResourceTreeVisitor(resourceList);
+ return visitor.visit().getChildren();
+ }
+
+ /**
+ * transfer resource list owned by the user
+ *
+ * @param transferredUserId transferred user id
+ * @param receivedUserId received user id
+ * @param transferredIds transferred ids
+ * @return transfer result code
+ */
+ @Transactional(rollbackFor = Exception.class)
+ @Override
+ public Map<String, Object> transferOwnedData(int transferredUserId, int
receivedUserId, List<Integer> transferredIds) {
+ Map<String, Object> result = new HashMap<>();
+
+ List<Resource> resources =
resourcesMapper.selectList(Wrappers.<Resource>lambdaQuery()
+ .eq(Resource::getUserId, transferredUserId)
+ .in(Resource::getId, transferredIds)
+ );
+ Set<Integer> realResourceIds =
resources.stream().map(Resource::getId).collect(Collectors.toSet());
+ // update resource owner
+ int updatedResourceNum = resourcesMapper.update(null,
Wrappers.<Resource>lambdaUpdate()
+ .set(Resource::getUserId, receivedUserId)
+ .set(Resource::getUpdateTime, new Date())
+ .in(Resource::getId, realResourceIds)
+ );
+ if (updatedResourceNum != realResourceIds.size()) {
+ putMsg(result, Status.TRANSFER_RESOURCE_ERROR);
+ return result;
+ }
+ // delete project user relation if exist
+ resourceUserMapper.delete(Wrappers.<ResourcesUser>lambdaQuery()
+ .eq(ResourcesUser::getUserId, receivedUserId)
+ .in(ResourcesUser::getResourcesId, realResourceIds)
+ );
+
+ // copy resource file
+ copyResourceFiles(resources, transferredUserId, receivedUserId);
+
+ putMsg(result, Status.SUCCESS);
+ return result;
+ }
+
+ /**
+ * return the resource type
+ *
+ * @return transfer data type
+ */
+ @Override
+ public TransferDataType transferDataType() {
+ return TransferDataType.RESOURCE;
+ }
+
+ /**
+ * copy resource files
+ *
+ * @param resourceList resource list
+ * @param transferredUserId transferred user id
+ * @param receivedUserId received user id
+ */
+ private void copyResourceFiles(List<Resource> resourceList, int
transferredUserId, int receivedUserId) {
+ if (CollectionUtils.isEmpty(resourceList)) {
+ return;
+ }
+ Map<Integer, User> userById =
userMapper.selectByIds(Lists.newArrayList(transferredUserId, receivedUserId))
+ .stream().collect(Collectors.toMap(User::getId,
Function.identity()));
+ if (!userById.containsKey(transferredUserId)) {
+ throw new ServiceException(Status.USER_NOT_EXIST,
transferredUserId);
+ }
+ if (!userById.containsKey(receivedUserId)) {
+ throw new ServiceException(Status.USER_NOT_EXIST, receivedUserId);
+ }
+ Map<Integer, String> tenantCodeById =
tenantMapper.selectBatchIds(userById.values().stream().map(User::getTenantId).collect(Collectors.toSet())).stream()
+ .collect(Collectors.toMap(Tenant::getId,
Tenant::getTenantCode));
+ String transferredUserTenantCode =
tenantCodeById.get(userById.get(transferredUserId).getTenantId());
+ String receivedUserTenantCode =
tenantCodeById.get(userById.get(receivedUserId).getTenantId());
+ if (StringUtils.isBlank(transferredUserTenantCode) ||
StringUtils.isBlank(receivedUserTenantCode) ||
+ Objects.equals(transferredUserTenantCode,
receivedUserTenantCode)) {
+ return;
+ }
+ try {
+ ResourceUtils.copyResourceFiles(resourceList,
HadoopUtils.getHdfsResDir(transferredUserTenantCode),
Review comment:
This method already exists before this pr. Because it's needed here, I
pulled it into a new class. Maybe we can discuss this in another issue.
--
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]