Search in sources :

Example 1 with User

use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.

the class ProjectBatchServiceImpl method uploadProjectsReal.

private GeneralResponse<?> uploadProjectsReal(InputStream fileInputStream, String fileName, String userName, boolean aomp) throws IOException, UnExpectedRequestException, PermissionDeniedRequestException, RoleNotFoundException {
    String suffixName = fileName.substring(fileName.lastIndexOf('.'));
    if (!suffixName.equals(SUPPORT_EXCEL_SUFFIX_NAME)) {
        throw new UnExpectedRequestException("{&DO_NOT_SUPPORT_SUFFIX_NAME}: [" + suffixName + "]. {&ONLY_SUPPORT} [" + SUPPORT_EXCEL_SUFFIX_NAME + "]", 422);
    }
    if (userName == null) {
        return new GeneralResponse<>("401", "{&PLEASE_LOGIN}", null);
    }
    User user = userDao.findByUsername(userName);
    Long userId = user.getId();
    // Read file and create project
    ExcelProjectListener listener = readExcel(fileInputStream);
    // Check if excel file is empty
    if (listener.getExcelProjectContent().isEmpty() && listener.getExcelRuleContent().isEmpty() && listener.getExcelCustomRuleContent().isEmpty() && listener.getExcelMultiRuleContent().isEmpty() && listener.getTemplateFileExcelContent().isEmpty() && listener.getExcelMetricContent().isEmpty()) {
        throw new UnExpectedRequestException("{&FILE_CAN_NOT_BE_EMPTY_OR_FILE_CAN_NOT_BE_RECOGNIZED}", 422);
    }
    for (ExcelProject excelProject : listener.getExcelProjectContent()) {
        Project project = projectDao.findByNameAndCreateUser(excelProject.getProjectName(), userName);
        if (project != null) {
            if (!aomp) {
                LOGGER.info("hint for user to decide to override or not.");
            }
            // Means update project.
            LOGGER.info("Start to update project[name={}] with upload project file.", project.getName());
            ModifyProjectDetailRequest request = convertExcelProjectToModifyProjectRequest(excelProject, project, userName);
            projectService.modifyProjectDetail(request, false);
        } else {
            // Check excel project arguments is valid or not
            AddProjectRequest request = convertExcelProjectToAddProjectRequest(excelProject);
            projectService.addProject(request, userId);
        }
    }
    for (ExcelRuleMetric excelRuleMetric : listener.getExcelMetricContent()) {
        RuleMetric ruleMetric = ruleMetricDao.findByName(excelRuleMetric.getName());
        if (ruleMetric != null) {
            if (!aomp) {
                LOGGER.info("hint for user to decide to override or not.");
            }
            LOGGER.info("Start to update rule metric[name={}] with upload rule metric file.", ruleMetric.getName());
            modifyRuleMetric(excelRuleMetric, ruleMetric, userName);
        } else {
            addRuleMetric(excelRuleMetric, userName);
        }
    }
    // Create rules according to excel sheet
    Map<String, Map<String, List<ExcelTemplateRuleByProject>>> excelTemplateRulePartitionedByProject = listener.getExcelRuleContent();
    Map<String, Map<String, List<ExcelCustomRuleByProject>>> excelCustomRulePartitionedByProject = listener.getExcelCustomRuleContent();
    Map<String, Map<String, List<ExcelMultiTemplateRuleByProject>>> excelMultiTemplateRulePartitionedByProject = listener.getExcelMultiRuleContent();
    Map<String, Map<String, List<ExcelTemplateFileRuleByProject>>> excelTemplateFileRulePartitionedByProject = listener.getTemplateFileExcelContent();
    Set<String> allProjects = new HashSet<>();
    allProjects.addAll(excelTemplateRulePartitionedByProject.keySet());
    allProjects.addAll(excelCustomRulePartitionedByProject.keySet());
    allProjects.addAll(excelMultiTemplateRulePartitionedByProject.keySet());
    allProjects.addAll(excelTemplateFileRulePartitionedByProject.keySet());
    for (String projectName : allProjects) {
        try {
            Project projectInDb = projectDao.findByNameAndCreateUser(projectName, userName);
            if (projectInDb == null) {
                throw new UnExpectedRequestException("{&PROJECT}: [" + projectName + "] {&DOES_NOT_EXIST}");
            }
            ruleBatchService.getAndSaveRule(excelTemplateRulePartitionedByProject.get(projectName), excelCustomRulePartitionedByProject.get(projectName), excelMultiTemplateRulePartitionedByProject.get(projectName), excelTemplateFileRulePartitionedByProject.get(projectName), projectInDb, userName, aomp);
        } catch (Exception e) {
            throw new UnExpectedRequestException(e.getMessage());
        }
    }
    fileInputStream.close();
    return new GeneralResponse<>("200", "{&SUCCEED_TO_UPLOAD_FILE}", null);
}
Also used : RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ExcelMultiTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelMultiTemplateRuleByProject) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) User(com.webank.wedatasphere.qualitis.entity.User) AddProjectRequest(com.webank.wedatasphere.qualitis.project.request.AddProjectRequest) ExcelCustomRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject) ModifyProjectDetailRequest(com.webank.wedatasphere.qualitis.project.request.ModifyProjectDetailRequest) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) ZipException(net.lingala.zip4j.exception.ZipException) RoleNotFoundException(javax.management.relation.RoleNotFoundException) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) IOException(java.io.IOException) WriteExcelException(com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) ExcelMultiTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelMultiTemplateRuleByProject) ExcelCustomRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject) ExcelTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject) Project(com.webank.wedatasphere.qualitis.project.entity.Project) ExcelProjectListener(com.webank.wedatasphere.qualitis.project.excel.ExcelProjectListener) ExcelTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject)

Example 2 with User

use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.

the class ProjectServiceImpl method deleteProject.

@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> deleteProject(DeleteProjectRequest request) throws UnExpectedRequestException, PermissionDeniedRequestException {
    // Check Arguments
    DeleteProjectRequest.checkRequest(request);
    // Check existence of project
    Project projectInDb = projectDao.findById(request.getProjectId());
    if (projectInDb == null) {
        throw new UnExpectedRequestException("project_id {&DOES_NOT_EXIST}");
    }
    Long userId = HttpUtils.getUserId(httpServletRequest);
    User user = null;
    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);
    }
    // Check project permission
    List<Integer> permissions = new ArrayList<>();
    permissions.add(ProjectUserPermissionEnum.CREATOR.getCode());
    checkProjectPermission(projectInDb, user.getUserName(), permissions);
    List<Rule> rules = ruleDao.findByProject(projectInDb);
    deleteAllRules(rules);
    // Delete project
    projectDao.deleteProject(projectInDb);
    LOGGER.info("Succeed to delete project. project_id: {}", request.getProjectId());
    return new GeneralResponse<>("200", "{&DELETE_PROJECT_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) User(com.webank.wedatasphere.qualitis.entity.User) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with User

use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.

the class ProjectServiceImpl method addProjectReal.

@Override
public Project addProjectReal(Long userId, String projectName, String cnName, String projectDescription) throws UnExpectedRequestException {
    User user = userDao.findById(userId);
    // Automatically grant the highest authority to the system administrator.
    Role role = roleDao.findByRoleName(ADMIN);
    List<User> admins = userRoleDao.findByRole(role).stream().map(UserRole::getUser).collect(Collectors.toList());
    // Check existence of project by project name.
    Project projectInDb = projectDao.findByNameAndCreateUser(projectName, user.getUserName());
    if (projectInDb != null) {
        throw new UnExpectedRequestException(String.format("{&PROJECT}:%s {&ALREADY_EXIST}", projectName));
    }
    // Save project.
    Project newProject = new Project(projectName, cnName, projectDescription, user.getUserName(), user.getChineseName(), user.getDepartment() != null ? user.getDepartment().getName() : "", ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date()));
    // Create project user.
    if (admins.contains(user)) {
        createProjectUser(newProject, user);
    } else {
        createProjectUser(newProject, user);
        for (User currentAdmin : admins) {
            createProjectUser(newProject, currentAdmin);
        }
    }
    return newProject;
}
Also used : Role(com.webank.wedatasphere.qualitis.entity.Role) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) Project(com.webank.wedatasphere.qualitis.project.entity.Project) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) User(com.webank.wedatasphere.qualitis.entity.User) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser)

Example 4 with User

use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.

the class ProjectBatchServiceImpl method modifyRuleMetric.

private void modifyRuleMetric(ExcelRuleMetric excelRuleMetric, RuleMetric ruleMetric, String userName) throws UnExpectedRequestException, PermissionDeniedRequestException {
    // Check en code existence.
    RuleMetric ruleMetricInDb = ruleMetricDao.findByEnCode(excelRuleMetric.getEnCode());
    if (ruleMetricInDb != null && ruleMetricInDb.getId().longValue() != ruleMetric.getId().longValue()) {
        throw new UnExpectedRequestException("Rule Metric [EN_CODE=" + excelRuleMetric.getEnCode() + "] {&DOES_NOT_EXIST}");
    }
    LOGGER.info("Start to modify rule metric, modify request: [{}], user: [{}]", excelRuleMetric.toString(), userName);
    User loginUser = userDao.findByUsername(userName);
    List<UserRole> userRoles = userRoleDao.findByUser(loginUser);
    Integer roleType = roleService.getRoleType(userRoles);
    if (roleType.equals(RoleDefaultTypeEnum.ADMIN.getCode())) {
        LOGGER.info("First level(created by SYS_ADMIN) indicator will be modified soon.");
    } else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
        LOGGER.info("Second level(created by DEPARTMENT_ADMIN) indicator will be modified soon.");
        if (ruleMetric.getLevel().equals(RuleMetricLevelEnum.DEFAULT_METRIC.getCode())) {
            throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
        }
        List<Department> managedDepartment = new ArrayList<>();
        for (UserRole userRole : userRoles) {
            Department department = userRole.getRole().getDepartment();
            if (department != null) {
                managedDepartment.add(department);
            }
        }
        RuleMetricDepartmentUser ruleMetricDepartmentUser = ruleMetricDepartmentUserDao.findByRuleMetric(ruleMetric);
        if (ruleMetricDepartmentUser != null && managedDepartment.contains(ruleMetricDepartmentUser.getDepartment())) {
            LOGGER.info("Rule metric[{}] comes from department: {}", ruleMetric.toString(), ruleMetricDepartmentUser.getDepartment().getName());
        } else {
            throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
        }
    } else {
        LOGGER.info("Third level(created by PROJECTOR) indicator will be modified soon.");
        if (!ruleMetric.getLevel().equals(RuleMetricLevelEnum.PERSONAL_METRIC.getCode()) || !ruleMetric.getCreateUser().equals(loginUser.getUserName())) {
            throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
        }
    }
    ruleMetric.setName(excelRuleMetric.getName());
    ruleMetric.setMetricDesc(excelRuleMetric.getMetricDesc());
    ruleMetric.setSubSystemName(excelRuleMetric.getSubSystemName());
    ruleMetric.setFullCnName(excelRuleMetric.getFullCnName());
    ruleMetric.setModifyUser(userName);
    ruleMetric.setModifyTime(ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date()));
    ruleMetric.setType(excelRuleMetric.getType());
    ruleMetric.setFrequency(Integer.parseInt(excelRuleMetric.getFrequency()));
    ruleMetric.setEnCode(excelRuleMetric.getEnCode());
    ruleMetric.setDepartmentName(excelRuleMetric.getDepartmentName());
    ruleMetric.setDevDepartmentName(excelRuleMetric.getDevDepartmentName());
    ruleMetric.setOpsDepartmentName(excelRuleMetric.getOpsDepartmentName());
    ruleMetric.setAvailable(excelRuleMetric.getAvailable());
    ruleMetricDao.add(ruleMetric);
}
Also used : RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) Department(com.webank.wedatasphere.qualitis.entity.Department) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) User(com.webank.wedatasphere.qualitis.entity.User) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole)

Example 5 with User

use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.

the class RuleNodeServiceImpl method importRuleGroup.

@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<RuleResponse> importRuleGroup(RuleNodeRequests ruleNodeRequests) throws UnExpectedRequestException, IOException, PermissionDeniedRequestException {
    // Project, rule group.
    Project projectInDb = projectDao.findById(ruleNodeRequests.getNewProjectId());
    ObjectMapper objectMapper = new ObjectMapper();
    RuleGroup ruleGroup = objectMapper.readValue(ruleNodeRequests.getRuleNodeRequests().iterator().next().getRuleGroupObject(), RuleGroup.class);
    RuleGroup ruleGroupInDb = ruleGroupDao.saveRuleGroup(new RuleGroup(ruleGroup.getRuleGroupName(), projectInDb.getId()));
    if (DEV_CENTER.equals(localConfig.getCenter())) {
        CopyRuleRequest copyRuleRequest = new CopyRuleRequest();
        copyRuleRequest.setSourceRuleGroupId(ruleGroup.getId());
        copyRuleRequest.setCreateUser(projectInDb.getCreateUser());
        copyRuleRequest.setTargetRuleGroupId(ruleGroupInDb.getId());
        copyRuleRequest.setTargetProjectId(ruleNodeRequests.getNewProjectId());
        copyRuleByRuleGroupId(copyRuleRequest);
        return new GeneralResponse<>("200", "{&IMPORT_RULE_SUCCESSFULLY}", new RuleResponse(ruleGroupInDb.getId()));
    }
    if (projectInDb != null) {
        LOGGER.info("Project info : {}", projectInDb.toString());
    } else {
        // For special case:
        // A project already exists in DSS. when creating a workflow in this project and publish it, Qualitis service cannot find the project.
        // Because Qualitis did not run when this project had been created in DSS.
        LOGGER.info("Start to create workflow project. Project[id = {}] is not exist.", ruleNodeRequests.getNewProjectId());
        String userName = ruleNodeRequests.getUserName();
        User currentUser = userDao.findByUsername(userName);
        if (currentUser == null) {
            LOGGER.info("Start to create user. User name is {}", userName);
            try {
                userService.autoAddUser(userName);
                currentUser = userDao.findByUsername(userName);
            } catch (RoleNotFoundException e) {
                LOGGER.error("Role cannot be found. Exception: {}", e);
            }
        }
        Project newProject = projectService.addProjectReal(currentUser.getId(), currentUser.getUserName() + "_project_" + UUID.randomUUID().toString(), currentUser.getUserName() + "_项目_" + UUID.randomUUID().toString(), "Auto created.");
        newProject.setProjectType(ProjectTypeEnum.WORKFLOW_PROJECT.getCode());
        projectInDb = projectDao.saveProject(newProject);
        LOGGER.info("Succeed to create project. New project: {}", projectInDb.toString());
    }
    for (RuleNodeRequest request : ruleNodeRequests.getRuleNodeRequests()) {
        importRule(request, projectInDb, ruleGroupInDb, objectMapper);
    }
    return new GeneralResponse<>("200", "{&IMPORT_RULE_SUCCESSFULLY}", new RuleResponse(ruleGroupInDb.getId()));
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) Project(com.webank.wedatasphere.qualitis.project.entity.Project) ExcelMultiTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelMultiTemplateRuleByProject) ExcelTemplateFileRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateFileRuleByProject) User(com.webank.wedatasphere.qualitis.entity.User) RuleNodeRequest(com.webank.wedatasphere.qualitis.rule.request.RuleNodeRequest) RuleResponse(com.webank.wedatasphere.qualitis.rule.response.RuleResponse) RoleNotFoundException(javax.management.relation.RoleNotFoundException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) RuleGroup(com.webank.wedatasphere.qualitis.rule.entity.RuleGroup) CopyRuleRequest(com.webank.wedatasphere.qualitis.rule.request.CopyRuleRequest) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

User (com.webank.wedatasphere.qualitis.entity.User)54 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)38 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)30 Transactional (org.springframework.transaction.annotation.Transactional)23 ArrayList (java.util.ArrayList)22 UserRole (com.webank.wedatasphere.qualitis.entity.UserRole)21 Department (com.webank.wedatasphere.qualitis.entity.Department)15 List (java.util.List)14 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)13 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)13 GetAllResponse (com.webank.wedatasphere.qualitis.response.GetAllResponse)12 RuleMetricDepartmentUser (com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser)10 Project (com.webank.wedatasphere.qualitis.project.entity.Project)10 ProjectUser (com.webank.wedatasphere.qualitis.project.entity.ProjectUser)7 Application (com.webank.wedatasphere.qualitis.entity.Application)6 Task (com.webank.wedatasphere.qualitis.entity.Task)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 UserDao (com.webank.wedatasphere.qualitis.dao.UserDao)5 ApplicationResponse (com.webank.wedatasphere.qualitis.response.ApplicationResponse)5 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)5