Search in sources :

Example 91 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class ProjectServiceImpl method modifyProjectDetail.

@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> modifyProjectDetail(ModifyProjectDetailRequest request, boolean workflow) throws UnExpectedRequestException, PermissionDeniedRequestException, RoleNotFoundException {
    // Check Arguments
    ModifyProjectDetailRequest.checkRequest(request);
    // Check existence of project
    Project projectInDb = projectDao.findById(request.getProjectId());
    if (projectInDb == null) {
        throw new UnExpectedRequestException("project id {&DOES_NOT_EXIST}");
    }
    LOGGER.info("Succeed to get project. project: {}", projectInDb);
    // Get userId
    Long userId = HttpUtils.getUserId(httpServletRequest);
    User user;
    if (userId == null) {
        user = userDao.findByUsername(request.getUsername());
        if (user == null) {
            throw new UnExpectedRequestException(String.format("{&FAILED_TO_FIND_USER} %s", request.getUsername()));
        }
    } else {
        user = userDao.findById(userId);
        if (user == null) {
            throw new UnExpectedRequestException(String.format("{&FAILED_TO_FIND_USER} %s", userId));
        }
    }
    // Check if user has permission modifying project
    List<Integer> permissions = new ArrayList<>();
    permissions.add(ProjectUserPermissionEnum.BUSSMAN.getCode());
    permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
    checkProjectPermission(projectInDb, user.getUserName(), permissions);
    // Check project name
    Project otherProject = projectDao.findByNameAndCreateUser(request.getProjectName(), user.getUserName());
    if (otherProject != null && !otherProject.getId().equals(projectInDb.getId())) {
        throw new UnExpectedRequestException(String.format("Project name: %s already exist", request.getProjectName()));
    }
    // Save project.
    String oldLabels = "";
    String newLabels = "";
    if (CollectionUtils.isNotEmpty(projectInDb.getProjectLabels())) {
        oldLabels = projectInDb.getProjectLabels().stream().map(ProjectLabel::getLabelName).collect(Collectors.joining());
    }
    if (CollectionUtils.isNotEmpty(request.getProjectLabelStrs())) {
        newLabels = request.getProjectLabelStrs().stream().collect(Collectors.joining());
    }
    if (!oldLabels.equals(newLabels)) {
        projectEventService.recordModifyProject(projectInDb, user.getUserName(), "Project labels", oldLabels, newLabels, EventTypeEnum.MODIFY_PROJECT.getCode());
    }
    if (StringUtils.isNotEmpty(projectInDb.getCnName()) && !projectInDb.getCnName().equals(request.getCnName())) {
        projectEventService.recordModifyProject(projectInDb, user.getUserName(), "Chinese Name", projectInDb.getCnName(), request.getCnName(), EventTypeEnum.MODIFY_PROJECT.getCode());
    }
    if (!projectInDb.getName().equals(request.getProjectName())) {
        projectEventService.recordModifyProject(projectInDb, user.getUserName(), "English Name", projectInDb.getName(), request.getProjectName(), EventTypeEnum.MODIFY_PROJECT.getCode());
    }
    if (!projectInDb.getDescription().equals(request.getDescription())) {
        projectEventService.recordModifyProject(projectInDb, user.getUserName(), "Describe", projectInDb.getDescription(), request.getDescription(), EventTypeEnum.MODIFY_PROJECT.getCode());
    }
    projectInDb.setCnName(request.getCnName());
    projectInDb.setName(request.getProjectName());
    projectInDb.setDescription(request.getDescription());
    // Delete old projectLabel.
    projectLabelDao.deleteByProject(projectInDb);
    LOGGER.info("Succeed to delete all project_label, project_id: {}", request.getProjectId());
    // Create new project labels.
    addProjectLabels(request.getProjectLabelStrs(), projectInDb);
    // Record modify user
    projectInDb.setModifyUser(user.getUserName());
    projectInDb.setModifyTime(ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date()));
    Project savedProject = projectDao.saveProject(projectInDb);
    LOGGER.info("Succeed to modify project. project: {}", savedProject);
    if (workflow) {
        // Clear old project user.
        List<ProjectUser> projectUsers = projectUserDao.findByProject(projectInDb);
        Role role = roleDao.findByRoleName(ADMIN);
        List<String> admins = userRoleDao.findByRole(role).stream().map(UserRole::getUser).map(User::getUserName).collect(Collectors.toList());
        projectUsers = projectUsers.stream().filter(projectUser -> !admins.contains(projectUser.getUserName())).filter(projectUser -> !projectUser.getUserName().equals(request.getUsername())).collect(Collectors.toList());
        for (ProjectUser projectUser : projectUsers) {
            projectUserDao.deleteByProjectAndUserName(projectInDb, projectUser.getUserName());
        }
    }
    authorizeUsers(projectInDb, user, request.getAuthorizeProjectUserRequests(), true);
    return new GeneralResponse<>("200", "{&MODIFY_PROJECT_DETAIL_SUCCESSFULLY}", new ProjectDetailResponse(savedProject, null));
}
Also used : Role(com.webank.wedatasphere.qualitis.entity.Role) ProjectEventDao(com.webank.wedatasphere.qualitis.project.dao.ProjectEventDao) ProjectEvent(com.webank.wedatasphere.qualitis.project.entity.ProjectEvent) LoggerFactory(org.slf4j.LoggerFactory) ProjectLabel(com.webank.wedatasphere.qualitis.project.entity.ProjectLabel) Autowired(org.springframework.beans.factory.annotation.Autowired) ProjectUserDao(com.webank.wedatasphere.qualitis.project.dao.ProjectUserDao) StringUtils(org.apache.commons.lang3.StringUtils) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) DeleteFileRuleRequest(com.webank.wedatasphere.qualitis.rule.request.DeleteFileRuleRequest) EventTypeEnum(com.webank.wedatasphere.qualitis.project.constant.EventTypeEnum) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) DeleteRuleRequest(com.webank.wedatasphere.qualitis.rule.request.DeleteRuleRequest) HttpUtils(com.webank.wedatasphere.qualitis.util.HttpUtils) AddProjectRequest(com.webank.wedatasphere.qualitis.project.request.AddProjectRequest) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse) ProjectService(com.webank.wedatasphere.qualitis.project.service.ProjectService) ProjectEventService(com.webank.wedatasphere.qualitis.project.service.ProjectEventService) ProjectDetailResponse(com.webank.wedatasphere.qualitis.project.response.ProjectDetailResponse) Context(javax.ws.rs.core.Context) ProjectUserPermissionEnum(com.webank.wedatasphere.qualitis.project.constant.ProjectUserPermissionEnum) PageRequest(com.webank.wedatasphere.qualitis.request.PageRequest) CustomRuleService(com.webank.wedatasphere.qualitis.rule.service.CustomRuleService) Collectors(java.util.stream.Collectors) ExecutionManagerImpl(com.webank.wedatasphere.qualitis.submitter.impl.ExecutionManagerImpl) RuleService(com.webank.wedatasphere.qualitis.rule.service.RuleService) RoleNotFoundException(javax.management.relation.RoleNotFoundException) RoleDao(com.webank.wedatasphere.qualitis.dao.RoleDao) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ProjectEventResponse(com.webank.wedatasphere.qualitis.project.response.ProjectEventResponse) java.util(java.util) Project(com.webank.wedatasphere.qualitis.project.entity.Project) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) UserRoleDao(com.webank.wedatasphere.qualitis.dao.UserRoleDao) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) ProjectDao(com.webank.wedatasphere.qualitis.project.dao.ProjectDao) HttpServletRequest(javax.servlet.http.HttpServletRequest) User(com.webank.wedatasphere.qualitis.entity.User) CollectionUtils(org.apache.commons.collections.CollectionUtils) Propagation(org.springframework.transaction.annotation.Propagation) Service(org.springframework.stereotype.Service) ProjectLabelDao(com.webank.wedatasphere.qualitis.project.dao.ProjectLabelDao) AuthorizeProjectUserRequest(com.webank.wedatasphere.qualitis.project.request.AuthorizeProjectUserRequest) ModifyProjectDetailRequest(com.webank.wedatasphere.qualitis.project.request.ModifyProjectDetailRequest) DeleteProjectRequest(com.webank.wedatasphere.qualitis.project.request.DeleteProjectRequest) Logger(org.slf4j.Logger) ProjectTypeEnum(com.webank.wedatasphere.qualitis.project.constant.ProjectTypeEnum) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) RuleTypeEnum(com.webank.wedatasphere.qualitis.rule.constant.RuleTypeEnum) FileRuleService(com.webank.wedatasphere.qualitis.rule.service.FileRuleService) ProjectResponse(com.webank.wedatasphere.qualitis.project.response.ProjectResponse) DeleteMultiSourceRequest(com.webank.wedatasphere.qualitis.rule.request.multi.DeleteMultiSourceRequest) MultiSourceRuleService(com.webank.wedatasphere.qualitis.rule.service.MultiSourceRuleService) DeleteCustomRuleRequest(com.webank.wedatasphere.qualitis.rule.request.DeleteCustomRuleRequest) RuleDao(com.webank.wedatasphere.qualitis.rule.dao.RuleDao) ProjectUserService(com.webank.wedatasphere.qualitis.project.service.ProjectUserService) UserDao(com.webank.wedatasphere.qualitis.dao.UserDao) Transactional(org.springframework.transaction.annotation.Transactional) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) User(com.webank.wedatasphere.qualitis.entity.User) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) ProjectLabel(com.webank.wedatasphere.qualitis.project.entity.ProjectLabel) Role(com.webank.wedatasphere.qualitis.entity.Role) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) Project(com.webank.wedatasphere.qualitis.project.entity.Project) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) ProjectDetailResponse(com.webank.wedatasphere.qualitis.project.response.ProjectDetailResponse) Transactional(org.springframework.transaction.annotation.Transactional)

Example 92 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class ProjectUserServiceImpl method getAllUsers.

@Override
public GeneralResponse<List<String>> getAllUsers(PageRequest request, Long projectCreatorId) throws UnExpectedRequestException {
    PageRequest.checkRequest(request);
    int page = request.getPage();
    int size = request.getSize();
    User projectCreator = userDao.findById(projectCreatorId);
    if (projectCreator == null) {
        throw new UnExpectedRequestException("User {&DOES_NOT_EXIST}, name: " + projectCreator.getUserName());
    }
    LOGGER.info("Get all users by: " + projectCreator.getUserName());
    List<User> users = userDao.findAllUser(page, size);
    return new GeneralResponse<>("200", "{&FIND_ALL_USERS_SUCCESSFULLY}", users.stream().map(User::getUserName).collect(Collectors.toList()));
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) User(com.webank.wedatasphere.qualitis.entity.User) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser)

Example 93 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class ProjectUserServiceImpl method authorizePermission.

@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<ProjectUserResponse> authorizePermission(AuthorizeProjectUserRequest authorizeProjectUserRequest, Long loginUserId, boolean modify) throws UnExpectedRequestException, PermissionDeniedRequestException, RoleNotFoundException {
    List<ProjectUser> projectUsers = new ArrayList<>();
    AuthorizeProjectUserRequest.checkRequest(authorizeProjectUserRequest);
    Project projectInDb = projectDao.findById(authorizeProjectUserRequest.getProjectId());
    String projectUser = authorizeProjectUserRequest.getProjectUser();
    LOGGER.info("User[id={}] start to authorize user[name={}]", loginUserId, projectUser);
    if (projectInDb == null) {
        throw new UnExpectedRequestException("{&PROJECT}: [ID=" + authorizeProjectUserRequest.getProjectId() + "] {&DOES_NOT_EXIST}");
    }
    User projectUserInDb = userDao.findByUsername(projectUser);
    if (projectUserInDb == null) {
        LOGGER.warn("Project user is from outside, qualitis will auto add user. Name: " + projectUser);
        userService.autoAddUser(projectUser);
    }
    User loginUser = userDao.findById(loginUserId);
    if (!checkPermission(projectInDb, loginUser.getUserName(), ProjectUserPermissionEnum.CREATOR.getCode())) {
        throw new PermissionDeniedRequestException("{&NO_PERMISSION_MODIFYING_PROJECT}", 403);
    }
    if (loginUser.getUserName().equals(projectUser)) {
        return null;
    }
    List<Integer> permissions = new ArrayList<>();
    if (modify) {
        projectUserDao.deleteByProjectAndUserName(projectInDb, projectUser);
        LOGGER.info("Success to delete original project user permissions.");
    }
    for (Integer permission : authorizeProjectUserRequest.getProjectPermissions()) {
        ProjectUser tmp = new ProjectUser(permission, projectInDb, projectUser);
        LOGGER.info("User[name={}] get permission[ID={}].", projectUser, permission);
        projectUsers.add(tmp);
        permissions.add(permission);
    }
    projectUserDao.saveAll(projectUsers);
    // projectEventService.record(projectInDb.getId(), loginUser.getUserName(), "authorized", projectUser, EventTypeEnum.MODIFY_PROJECT.getCode());
    ProjectUserResponse projectUserResponse = new ProjectUserResponse(projectInDb.getName(), loginUser.getUserName(), projectUser);
    projectUserResponse.setPermissions(permissions);
    return new GeneralResponse<>("200", "{&SUCCESS_TO_ADD_PROJECT_USER}", projectUserResponse);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) Project(com.webank.wedatasphere.qualitis.project.entity.Project) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) User(com.webank.wedatasphere.qualitis.entity.User) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) ProjectUserResponse(com.webank.wedatasphere.qualitis.project.response.ProjectUserResponse) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 94 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class ProjectUserServiceImpl method deletePermission.

@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse deletePermission(AuthorizeProjectUserRequest request, Long loginUserId) throws UnExpectedRequestException, PermissionDeniedRequestException {
    Long projectId = request.getProjectId();
    Project projectInDb = projectDao.findById(projectId);
    if (projectInDb == null) {
        throw new UnExpectedRequestException("{&PROJECT}: [ID=" + projectId + "] {&DOES_NOT_EXIST}");
    }
    User loginUser = userDao.findById(loginUserId);
    if (!checkPermission(projectInDb, loginUser.getUserName(), ProjectUserPermissionEnum.CREATOR.getCode())) {
        throw new PermissionDeniedRequestException("{&NO_PERMISSION_MODIFYING_PROJECT}", 403);
    }
    projectUserDao.deleteByProjectAndUserName(projectInDb, request.getProjectUser());
    // projectEventService.record(projectInDb.getId(), loginUser.getUserName(), "authorized", request.getProjectUser(), EventTypeEnum.MODIFY_PROJECT.getCode());
    return new GeneralResponse<>("200", "{&DELETE_USER_SUCCESSFULLY}", null);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) Project(com.webank.wedatasphere.qualitis.project.entity.Project) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) User(com.webank.wedatasphere.qualitis.entity.User) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) Transactional(org.springframework.transaction.annotation.Transactional)

Example 95 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class OuterWorkflowProjectServiceImpl method addWorkflowProject.

@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class, RoleNotFoundException.class })
public GeneralResponse<ProjectDetailResponse> addWorkflowProject(AddProjectRequest request, String username) throws UnExpectedRequestException, RoleNotFoundException, PermissionDeniedRequestException {
    LOGGER.info("Add workflow project request: " + new Gson().toJson(request));
    // Check Arguments
    Set<String> labels = request.getProjectLabels();
    AddProjectRequest.checkRequest(request);
    if (request.getUsername() == null) {
        throw new UnExpectedRequestException("Username: {&CAN_NOT_BE_NULL_OR_EMPTY}");
    }
    // Check existence of user
    User userInDb = userDao.findByUsername(username);
    if (userInDb == null) {
        userService.autoAddUser(username);
        userInDb = userDao.findByUsername(username);
    }
    // Check existence of project by project name.
    Project projectInDb = projectDao.findByNameAndCreateUser(request.getProjectName(), username);
    boolean needSuffix = false;
    if (projectInDb != null) {
        if (projectInDb.getProjectType().equals(ProjectTypeEnum.WORKFLOW_PROJECT.getCode())) {
            ProjectDetailResponse response = new ProjectDetailResponse(projectInDb, null);
            LOGGER.info("Succeed to find exists workflow project, response: {}", response);
            return new GeneralResponse<>("200", "{&GET_PROJECT_DETAIL_SUCCESSFULLY}", response);
        } else {
            needSuffix = true;
        }
    }
    Project newProject = projectService.addProjectReal(userInDb.getId(), needSuffix ? request.getProjectName() + "_workflow" : request.getProjectName(), request.getCnName(), request.getDescription());
    newProject.setProjectType(ProjectTypeEnum.WORKFLOW_PROJECT.getCode());
    Project savedProject = projectDao.saveProject(newProject);
    projectService.addProjectLabels(labels, savedProject);
    // Authorized users.
    projectService.authorizeUsers(savedProject, userInDb, request.getAuthorizeProjectUserRequests(), false);
    ProjectDetailResponse response = new ProjectDetailResponse(savedProject, null);
    LOGGER.info("Succeed to add workflow project, response: {}", response);
    return new GeneralResponse<>("200", "{&ADD_PROJECT_SUCCESSFULLY}", response);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) Project(com.webank.wedatasphere.qualitis.project.entity.Project) User(com.webank.wedatasphere.qualitis.entity.User) Gson(com.google.gson.Gson) ProjectDetailResponse(com.webank.wedatasphere.qualitis.project.response.ProjectDetailResponse) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)150 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)97 ArrayList (java.util.ArrayList)56 Transactional (org.springframework.transaction.annotation.Transactional)54 Project (com.webank.wedatasphere.qualitis.project.entity.Project)33 User (com.webank.wedatasphere.qualitis.entity.User)32 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)32 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)25 Map (java.util.Map)24 ClusterInfo (com.webank.wedatasphere.qualitis.entity.ClusterInfo)22 List (java.util.List)22 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)19 IOException (java.io.IOException)19 MetaDataAcquireFailedException (com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException)17 Date (java.util.Date)17 HashMap (java.util.HashMap)17 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)15 RuleGroup (com.webank.wedatasphere.qualitis.rule.entity.RuleGroup)15 Collectors (java.util.stream.Collectors)15 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)14