zhongjiajie commented on code in PR #10823:
URL: https://github.com/apache/dolphinscheduler/pull/10823#discussion_r918683517
##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/python/PythonGateway.java:
##########
@@ -547,6 +547,21 @@ public Map<String, Object> queryResourcesFileInfo(String
userName, String fullNa
return result;
}
+ /**
+ * create or update resource.
+ * If the folder is not already created, it will be
+ *
+ * @param userName user who create or update resource
+ * @param fullName The fullname of resource.Includes path and suffix.
+ * @param description description of resource
+ * @param resourceContent content of resource
+ * @return id of resource
+ */
+ public Integer createOrUpdateResource(
+ String userName, String fullName, String description, String
resourceContent) {
+ return resourceService.createOrUpdateResource(userName, fullName,
description, resourceContent);
+ }
Review Comment:
I looks perfect now, thanks 👍
##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java:
##########
@@ -1117,6 +1118,96 @@ public Result<Object> onlineCreateResource(User
loginUser, ResourceType type, St
return result;
}
+ /**
+ * create or update resource.
+ * If the folder is not already created, it will be
+ *
+ * @param loginUser user who create or update resource
+ * @param fileFullName The full name of resource.Includes path and suffix.
+ * @param desc description of resource
+ * @param content content of resource
+ * @return create result code
+ */
+ @Override
+ @Transactional
+ public Result<Object> onlineCreateOrUpdateResourceWithDir(User loginUser,
String fileFullName, String desc, String content) {
+ if (checkResourceExists(fileFullName, ResourceType.FILE.ordinal())) {
+ Resource resource = resourcesMapper.queryResource(fileFullName,
ResourceType.FILE.ordinal()).get(0);
+ Result<Object> result = this.updateResourceContent(loginUser,
resource.getId(), content);
+ if (result.getCode() == Status.SUCCESS.getCode()) {
+ resource.setDescription(desc);
+ 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());
+ }
+ }
+ result.setData(resultMap);
+ }
+ return result;
+ } else {
+ String resourceSuffix =
fileFullName.substring(fileFullName.indexOf(".") + 1);
+ String fileNameWithSuffix =
fileFullName.substring(fileFullName.lastIndexOf("/") + 1);
+ String resourceDir = fileFullName.replace(fileNameWithSuffix, "");
+ String resourceName = fileNameWithSuffix.replace("." +
resourceSuffix, EMPTY_STRING);
+ String[] dirNames = resourceDir.split("/");
+ int pid = -1;
+ StringBuilder currDirPath = new StringBuilder();
+ for (String dirName : dirNames) {
+ if (StringUtils.isNotEmpty(dirName)) {
+ pid = queryOrCreateDirId(loginUser, pid,
currDirPath.toString(), dirName);
+ currDirPath.append("/").append(dirName);
+ }
+ }
+ return this.onlineCreateResource(
+ loginUser, ResourceType.FILE, resourceName,
resourceSuffix, desc, content, pid, currDirPath.toString());
+ }
+ }
+
+ @Override
+ @Transactional
+ public Integer createOrUpdateResource(String userName, String fullName,
String description, String resourceContent) {
+ User user = userMapper.queryByUserNameAccurately(userName);
+ int suffixLabelIndex = fullName.indexOf(".");
Review Comment:
it is better to add `.` to `Constants` too
##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java:
##########
@@ -1117,6 +1118,96 @@ public Result<Object> onlineCreateResource(User
loginUser, ResourceType type, St
return result;
}
+ /**
+ * create or update resource.
+ * If the folder is not already created, it will be
+ *
+ * @param loginUser user who create or update resource
+ * @param fileFullName The full name of resource.Includes path and suffix.
+ * @param desc description of resource
+ * @param content content of resource
+ * @return create result code
+ */
+ @Override
+ @Transactional
+ public Result<Object> onlineCreateOrUpdateResourceWithDir(User loginUser,
String fileFullName, String desc, String content) {
+ if (checkResourceExists(fileFullName, ResourceType.FILE.ordinal())) {
+ Resource resource = resourcesMapper.queryResource(fileFullName,
ResourceType.FILE.ordinal()).get(0);
+ Result<Object> result = this.updateResourceContent(loginUser,
resource.getId(), content);
+ if (result.getCode() == Status.SUCCESS.getCode()) {
+ resource.setDescription(desc);
+ 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());
+ }
+ }
+ result.setData(resultMap);
+ }
+ return result;
+ } else {
+ String resourceSuffix =
fileFullName.substring(fileFullName.indexOf(".") + 1);
+ String fileNameWithSuffix =
fileFullName.substring(fileFullName.lastIndexOf("/") + 1);
+ String resourceDir = fileFullName.replace(fileNameWithSuffix, "");
+ String resourceName = fileNameWithSuffix.replace("." +
resourceSuffix, EMPTY_STRING);
+ String[] dirNames = resourceDir.split("/");
+ int pid = -1;
+ StringBuilder currDirPath = new StringBuilder();
+ for (String dirName : dirNames) {
+ if (StringUtils.isNotEmpty(dirName)) {
+ pid = queryOrCreateDirId(loginUser, pid,
currDirPath.toString(), dirName);
+ currDirPath.append("/").append(dirName);
+ }
+ }
+ return this.onlineCreateResource(
+ loginUser, ResourceType.FILE, resourceName,
resourceSuffix, desc, content, pid, currDirPath.toString());
+ }
+ }
+
+ @Override
+ @Transactional
+ public Integer createOrUpdateResource(String userName, String fullName,
String description, String resourceContent) {
+ User user = userMapper.queryByUserNameAccurately(userName);
+ int suffixLabelIndex = fullName.indexOf(".");
+ if (suffixLabelIndex == -1) {
+ String msg = String.format("The suffix of file can not be empty :
%s", fullName);
+ logger.error(msg);
+ throw new IllegalArgumentException(msg);
+ }
+ if (!fullName.startsWith("/")) {
Review Comment:
same as other place
##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java:
##########
@@ -1117,6 +1118,96 @@ public Result<Object> onlineCreateResource(User
loginUser, ResourceType type, St
return result;
}
+ /**
+ * create or update resource.
+ * If the folder is not already created, it will be
+ *
+ * @param loginUser user who create or update resource
+ * @param fileFullName The full name of resource.Includes path and suffix.
+ * @param desc description of resource
+ * @param content content of resource
+ * @return create result code
+ */
+ @Override
+ @Transactional
+ public Result<Object> onlineCreateOrUpdateResourceWithDir(User loginUser,
String fileFullName, String desc, String content) {
+ if (checkResourceExists(fileFullName, ResourceType.FILE.ordinal())) {
+ Resource resource = resourcesMapper.queryResource(fileFullName,
ResourceType.FILE.ordinal()).get(0);
+ Result<Object> result = this.updateResourceContent(loginUser,
resource.getId(), content);
+ if (result.getCode() == Status.SUCCESS.getCode()) {
+ resource.setDescription(desc);
+ 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());
+ }
+ }
+ result.setData(resultMap);
+ }
+ return result;
+ } else {
+ String resourceSuffix =
fileFullName.substring(fileFullName.indexOf(".") + 1);
+ String fileNameWithSuffix =
fileFullName.substring(fileFullName.lastIndexOf("/") + 1);
+ String resourceDir = fileFullName.replace(fileNameWithSuffix, "");
+ String resourceName = fileNameWithSuffix.replace("." +
resourceSuffix, EMPTY_STRING);
+ String[] dirNames = resourceDir.split("/");
+ int pid = -1;
+ StringBuilder currDirPath = new StringBuilder();
+ for (String dirName : dirNames) {
+ if (StringUtils.isNotEmpty(dirName)) {
+ pid = queryOrCreateDirId(loginUser, pid,
currDirPath.toString(), dirName);
+ currDirPath.append("/").append(dirName);
+ }
+ }
+ return this.onlineCreateResource(
+ loginUser, ResourceType.FILE, resourceName,
resourceSuffix, desc, content, pid, currDirPath.toString());
+ }
+ }
+
+ @Override
+ @Transactional
+ public Integer createOrUpdateResource(String userName, String fullName,
String description, String resourceContent) {
+ User user = userMapper.queryByUserNameAccurately(userName);
+ int suffixLabelIndex = fullName.indexOf(".");
+ if (suffixLabelIndex == -1) {
+ String msg = String.format("The suffix of file can not be empty :
%s", fullName);
+ logger.error(msg);
+ throw new IllegalArgumentException(msg);
+ }
+ if (!fullName.startsWith("/")) {
Review Comment:
please use constant `FOLDER_SEPARATOR` instead of bare `"/"`
```suggestion
if (!fullName.startsWith(Coonstants.FOLDER_SEPARATOR)) {
```
--
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]