Skip to content

Commit 88a4090

Browse files
authored
Merge 61b4dcf into c30cca9
2 parents c30cca9 + 61b4dcf commit 88a4090

File tree

31 files changed

+1302
-19
lines changed

31 files changed

+1302
-19
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.api.controller;
19+
20+
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_PROJECT_PREFERENCE_ERROR;
21+
import static org.apache.dolphinscheduler.api.enums.Status.UPDATE_PROJECT_PREFERENCE_ERROR;
22+
23+
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
24+
import org.apache.dolphinscheduler.api.exceptions.ApiException;
25+
import org.apache.dolphinscheduler.api.service.ProjectPreferenceService;
26+
import org.apache.dolphinscheduler.api.utils.Result;
27+
import org.apache.dolphinscheduler.common.constants.Constants;
28+
import org.apache.dolphinscheduler.dao.entity.User;
29+
30+
import lombok.extern.slf4j.Slf4j;
31+
32+
import org.springframework.beans.factory.annotation.Autowired;
33+
import org.springframework.http.HttpStatus;
34+
import org.springframework.web.bind.annotation.GetMapping;
35+
import org.springframework.web.bind.annotation.PathVariable;
36+
import org.springframework.web.bind.annotation.PutMapping;
37+
import org.springframework.web.bind.annotation.RequestAttribute;
38+
import org.springframework.web.bind.annotation.RequestMapping;
39+
import org.springframework.web.bind.annotation.RequestParam;
40+
import org.springframework.web.bind.annotation.ResponseStatus;
41+
import org.springframework.web.bind.annotation.RestController;
42+
43+
import io.swagger.v3.oas.annotations.Operation;
44+
import io.swagger.v3.oas.annotations.Parameter;
45+
import io.swagger.v3.oas.annotations.Parameters;
46+
import io.swagger.v3.oas.annotations.media.Schema;
47+
import io.swagger.v3.oas.annotations.tags.Tag;
48+
49+
@Tag(name = "PROJECT_PREFERENCE_TAG")
50+
@RestController
51+
@RequestMapping("projects/{projectCode}/project-preference")
52+
@Slf4j
53+
public class ProjectPreferenceController extends BaseController {
54+
55+
@Autowired
56+
private ProjectPreferenceService projectPreferenceService;
57+
58+
@Operation(summary = "updateProjectPreference", description = "UPDATE_PROJECT_PREFERENCE_NOTES")
59+
@Parameters({
60+
@Parameter(name = "projectPreferences", description = "PROJECT_PREFERENCES", schema = @Schema(implementation = String.class)),
61+
})
62+
@PutMapping
63+
@ResponseStatus(HttpStatus.CREATED)
64+
@ApiException(UPDATE_PROJECT_PREFERENCE_ERROR)
65+
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
66+
public Result updateProjectPreference(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
67+
@Parameter(name = "projectCode", description = "PROJECT_CODE", required = true) @PathVariable long projectCode,
68+
@RequestParam(value = "projectPreferences", required = true) String projectPreferences) {
69+
return projectPreferenceService.updateProjectPreference(loginUser, projectCode, projectPreferences);
70+
}
71+
72+
@Operation(summary = "queryProjectPreferenceByProjectCode", description = "QUERY_PROJECT_PREFERENCE_NOTES")
73+
@GetMapping()
74+
@ResponseStatus(HttpStatus.OK)
75+
@ApiException(QUERY_PROJECT_PREFERENCE_ERROR)
76+
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
77+
public Result queryProjectPreferenceByProjectCode(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
78+
@Parameter(name = "projectCode", description = "PROJECT_CODE", required = true) @PathVariable long projectCode) {
79+
return projectPreferenceService.queryProjectPreferenceByProjectCode(loginUser, projectCode);
80+
}
81+
82+
}

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ public enum Status {
287287

288288
PROJECT_PARAMETER_CODE_EMPTY(10220, "project parameter code empty", "项目参数code为空"),
289289

290+
CREATE_PROJECT_PREFERENCE_ERROR(10300, "create project preference error", "创建项目偏好设置错误"),
291+
292+
UPDATE_PROJECT_PREFERENCE_ERROR(10301, "update project preference error", "更新项目偏好设置错误"),
293+
QUERY_PROJECT_PREFERENCE_ERROR(10302, "query project preference error", "查询项目偏好设置错误"),
294+
290295
UDF_FUNCTION_NOT_EXIST(20001, "UDF function not found", "UDF函数不存在"),
291296
UDF_FUNCTION_EXISTS(20002, "UDF function already exists", "UDF函数已存在"),
292297
RESOURCE_NOT_EXIST(20004, "resource not exist", "资源不存在"),
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.api.service;
19+
20+
import org.apache.dolphinscheduler.api.utils.Result;
21+
import org.apache.dolphinscheduler.dao.entity.User;
22+
23+
public interface ProjectPreferenceService {
24+
25+
Result updateProjectPreference(User loginUser, long projectCode, String preferences);
26+
27+
Result queryProjectPreferenceByProjectCode(User loginUser, long projectCode);
28+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.api.service.impl;
19+
20+
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.PROJECT;
21+
22+
import org.apache.dolphinscheduler.api.enums.Status;
23+
import org.apache.dolphinscheduler.api.service.ProjectPreferenceService;
24+
import org.apache.dolphinscheduler.api.service.ProjectService;
25+
import org.apache.dolphinscheduler.api.utils.Result;
26+
import org.apache.dolphinscheduler.common.utils.CodeGenerateUtils;
27+
import org.apache.dolphinscheduler.dao.entity.Project;
28+
import org.apache.dolphinscheduler.dao.entity.ProjectPreference;
29+
import org.apache.dolphinscheduler.dao.entity.User;
30+
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
31+
import org.apache.dolphinscheduler.dao.mapper.ProjectPreferenceMapper;
32+
33+
import java.util.Date;
34+
import java.util.Objects;
35+
36+
import lombok.extern.slf4j.Slf4j;
37+
38+
import org.springframework.beans.factory.annotation.Autowired;
39+
import org.springframework.stereotype.Service;
40+
41+
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
42+
43+
@Service
44+
@Slf4j
45+
public class ProjectPreferenceServiceImpl extends BaseServiceImpl
46+
implements
47+
ProjectPreferenceService {
48+
49+
@Autowired
50+
private ProjectPreferenceMapper projectPreferenceMapper;
51+
52+
@Autowired
53+
private ProjectService projectService;
54+
55+
@Autowired
56+
private ProjectMapper projectMapper;
57+
58+
@Override
59+
public Result updateProjectPreference(User loginUser, long projectCode, String preferences) {
60+
Result result = new Result();
61+
62+
// check if the user has the writing permission for project
63+
Project project = projectMapper.queryByCode(projectCode);
64+
boolean hasProjectAndWritePerm = projectService.hasProjectAndWritePerm(loginUser, project, result);
65+
if (!hasProjectAndWritePerm) {
66+
return result;
67+
}
68+
69+
ProjectPreference projectPreference = projectPreferenceMapper
70+
.selectOne(new QueryWrapper<ProjectPreference>().lambda().eq(ProjectPreference::getProjectCode,
71+
projectCode));
72+
73+
Date now = new Date();
74+
if (Objects.isNull(projectPreference)) {
75+
projectPreference = new ProjectPreference();
76+
projectPreference.setProjectCode(projectCode);
77+
projectPreference.setPreferences(preferences);
78+
projectPreference.setUserId(loginUser.getId());
79+
projectPreference.setCode(CodeGenerateUtils.getInstance().genCode());
80+
projectPreference.setCreateTime(now);
81+
projectPreference.setUpdateTime(now);
82+
if (projectPreferenceMapper.insert(projectPreference) > 0) {
83+
log.info("Project preference is created and id is :{}", projectPreference.getId());
84+
result.setData(projectPreference);
85+
putMsg(result, Status.SUCCESS);
86+
} else {
87+
log.error("Project preference create error, projectCode:{}.", projectPreference.getProjectCode());
88+
putMsg(result, Status.CREATE_PROJECT_PREFERENCE_ERROR);
89+
}
90+
} else {
91+
projectPreference.setPreferences(preferences);
92+
projectPreference.setUserId(loginUser.getId());
93+
projectPreference.setUpdateTime(now);
94+
95+
if (projectPreferenceMapper.updateById(projectPreference) > 0) {
96+
log.info("Project preference is updated and id is :{}", projectPreference.getId());
97+
result.setData(projectPreference);
98+
putMsg(result, Status.SUCCESS);
99+
} else {
100+
log.error("Project preference update error, projectCode:{}.", projectPreference.getProjectCode());
101+
putMsg(result, Status.UPDATE_PROJECT_PREFERENCE_ERROR);
102+
}
103+
}
104+
return result;
105+
}
106+
107+
@Override
108+
public Result queryProjectPreferenceByProjectCode(User loginUser, long projectCode) {
109+
Result result = new Result();
110+
111+
Project project = projectMapper.queryByCode(projectCode);
112+
boolean hasProjectAndPerm = projectService.hasProjectAndPerm(loginUser, project, result, PROJECT);
113+
if (!hasProjectAndPerm) {
114+
return result;
115+
}
116+
117+
ProjectPreference projectPreference = projectPreferenceMapper
118+
.selectOne(new QueryWrapper<ProjectPreference>().lambda().eq(ProjectPreference::getProjectCode,
119+
projectCode));
120+
121+
result.setData(projectPreference);
122+
123+
putMsg(result, Status.SUCCESS);
124+
return result;
125+
}
126+
}

dolphinscheduler-api/src/main/resources/i18n/messages.properties

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,9 @@ UPDATE_PROJECT_PARAMETER_NOTES=update project parameter
447447
PROJECT_PARAMETER_CODE=project parameter code
448448
DELETE_PROJECT_PARAMETER_NOTES=delete project parameter
449449
QUERY_PROJECT_PARAMETER_LIST_PAGING_NOTES=query project parameter list paging
450-
QUERY_PROJECT_PARAMETER_NOTES=query project parameter
450+
QUERY_PROJECT_PARAMETER_NOTES=query project parameter
451+
452+
PROJECT_PREFERENCE_TAG=project preference related operation
453+
UPDATE_PROJECT_PREFERENCE_NOTES=update project preference
454+
PROJECT_PREFERENCES=project preferences
455+
QUERY_PROJECT_PREFERENCE_NOTES=query project preference
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dolphinscheduler.api.controller;
19+
20+
import org.apache.dolphinscheduler.api.enums.Status;
21+
import org.apache.dolphinscheduler.api.service.impl.ProjectPreferenceServiceImpl;
22+
import org.apache.dolphinscheduler.api.utils.Result;
23+
import org.apache.dolphinscheduler.common.enums.UserType;
24+
import org.apache.dolphinscheduler.dao.entity.User;
25+
26+
import org.junit.jupiter.api.Assertions;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.mockito.InjectMocks;
30+
import org.mockito.Mock;
31+
import org.mockito.Mockito;
32+
import org.mockito.junit.jupiter.MockitoExtension;
33+
import org.mockito.junit.jupiter.MockitoSettings;
34+
import org.mockito.quality.Strictness;
35+
36+
@ExtendWith(MockitoExtension.class)
37+
@MockitoSettings(strictness = Strictness.LENIENT)
38+
public class ProjectPreferenceControllerTest {
39+
40+
@InjectMocks
41+
private ProjectPreferenceController projectPreferenceController;
42+
43+
@Mock
44+
private ProjectPreferenceServiceImpl projectPreferenceService;
45+
46+
@Test
47+
public void testUpdateProjectPreference() {
48+
User loginUser = getGeneralUser();
49+
50+
Mockito.when(projectPreferenceService.updateProjectPreference(Mockito.any(), Mockito.anyLong(),
51+
Mockito.anyString())).thenReturn(getSuccessResult());
52+
53+
Result result = projectPreferenceController.updateProjectPreference(loginUser, 1, "value");
54+
Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode());
55+
}
56+
57+
@Test
58+
public void testQueryProjectPreferenceByProjectCode() {
59+
User loginUser = getGeneralUser();
60+
61+
Mockito.when(projectPreferenceService.queryProjectPreferenceByProjectCode(Mockito.any(), Mockito.anyLong()))
62+
.thenReturn(getSuccessResult());
63+
Result result = projectPreferenceController.queryProjectPreferenceByProjectCode(loginUser, 1);
64+
Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode());
65+
}
66+
67+
private User getGeneralUser() {
68+
User loginUser = new User();
69+
loginUser.setUserType(UserType.GENERAL_USER);
70+
loginUser.setUserName("userName");
71+
loginUser.setId(1);
72+
return loginUser;
73+
}
74+
75+
private Result getSuccessResult() {
76+
Result result = new Result();
77+
result.setCode(Status.SUCCESS.getCode());
78+
result.setMsg(Status.SUCCESS.getMsg());
79+
return result;
80+
}
81+
82+
}

0 commit comments

Comments
 (0)