Search in sources :

Example 1 with AddProjectRequest

use of com.webank.wedatasphere.qualitis.project.request.AddProjectRequest 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 AddProjectRequest

use of com.webank.wedatasphere.qualitis.project.request.AddProjectRequest in project Qualitis by WeBankFinTech.

the class ProjectBatchServiceImpl method convertExcelProjectToAddProjectRequest.

private AddProjectRequest convertExcelProjectToAddProjectRequest(ExcelProject excelProject) {
    // Construct AddProjectRequest
    AddProjectRequest addProjectRequest = new AddProjectRequest();
    addProjectRequest.setCnName(excelProject.getProjectChName());
    addProjectRequest.setProjectName(excelProject.getProjectName());
    addProjectRequest.setDescription(excelProject.getProjectDescription());
    String projectLabels = excelProject.getProjectLabels();
    if (projectLabels != null && projectLabels.length() > 0) {
        String[] labels = projectLabels.split(" ");
        Set<String> labelSet = new HashSet<>();
        for (String label : labels) {
            labelSet.add(label);
        }
        addProjectRequest.setProjectLabels(labelSet);
    }
    return addProjectRequest;
}
Also used : AddProjectRequest(com.webank.wedatasphere.qualitis.project.request.AddProjectRequest)

Example 3 with AddProjectRequest

use of com.webank.wedatasphere.qualitis.project.request.AddProjectRequest in project Qualitis by WeBankFinTech.

the class RuleBatchServiceImpl method uploadRulesReal.

private GeneralResponse<?> uploadRulesReal(InputStream fileInputStream, String fileName, String userName, Long projectId, boolean aomp) throws UnExpectedRequestException, ClusterInfoNotConfigException, ParseException, SemanticException, MetaDataAcquireFailedException, TaskNotExistException, IOException, PermissionDeniedRequestException {
    if (userName == null) {
        return new GeneralResponse<>("401", "{&PLEASE_LOGIN}", null);
    }
    // Check suffix name of file
    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);
    }
    ExcelRuleListener excelRuleListener = readExcel(fileInputStream);
    if (excelRuleListener.getCustomExcelContent().isEmpty() && excelRuleListener.getTemplateExcelContent().isEmpty() && excelRuleListener.getMultiTemplateExcelContent().isEmpty() && excelRuleListener.getTemplateFileExcelContent().isEmpty()) {
        throw new UnExpectedRequestException("{&FILE_CAN_NOT_BE_EMPTY_OR_FILE_CAN_NOT_BE_RECOGNIZED}", 422);
    }
    // Record project event.
    List<String> ruleNames = new ArrayList<>(excelRuleListener.getTemplateExcelContent().keySet().size() + excelRuleListener.getCustomExcelContent().keySet().size() + excelRuleListener.getTemplateFileExcelContent().keySet().size() + excelRuleListener.getMultiTemplateExcelContent().keySet().size());
    ruleNames.addAll(excelRuleListener.getCustomExcelContent().keySet());
    ruleNames.addAll(excelRuleListener.getTemplateExcelContent().keySet());
    ruleNames.addAll(excelRuleListener.getTemplateFileExcelContent().keySet());
    ruleNames.addAll(excelRuleListener.getMultiTemplateExcelContent().keySet());
    if (projectId != null) {
        Project projectInDb = projectDao.findById(projectId);
        if (projectInDb == null) {
            throw new UnExpectedRequestException("{&PROJECT_ID} {&DOES_NOT_EXIST}");
        }
        // Check permissions of project
        List<Integer> permissions = new ArrayList<>();
        permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
        projectService.checkProjectPermission(projectInDb, userName, permissions);
        getAndSaveRule(excelRuleListener.getTemplateExcelContent(), excelRuleListener.getCustomExcelContent(), excelRuleListener.getMultiTemplateExcelContent(), excelRuleListener.getTemplateFileExcelContent(), projectInDb, userName, aomp);
    // projectEventService.record(projectInDb.getId(), userName, "upload", "[" + Arrays.toString(ruleNames.toArray()) + "].", EventTypeEnum.MODIFY_PROJECT.getCode());
    } else {
        // All three rules are derived from the same project.
        String projectName = "";
        if (CollectionUtils.isNotEmpty(excelRuleListener.getTemplateExcelContent().keySet())) {
            projectName = excelRuleListener.getTemplateExcelContent().values().iterator().next().iterator().next().getProjectName();
        } else if (CollectionUtils.isNotEmpty(excelRuleListener.getCustomExcelContent().keySet())) {
            projectName = excelRuleListener.getCustomExcelContent().values().iterator().next().iterator().next().getProjectName();
        } else if (CollectionUtils.isNotEmpty(excelRuleListener.getMultiTemplateExcelContent().keySet())) {
            projectName = excelRuleListener.getMultiTemplateExcelContent().values().iterator().next().iterator().next().getProjectName();
        } else if (CollectionUtils.isNotEmpty(excelRuleListener.getTemplateFileExcelContent().keySet())) {
            projectName = excelRuleListener.getTemplateFileExcelContent().values().iterator().next().iterator().next().getProjectName();
        }
        Project project = projectDao.findByNameAndCreateUser(projectName, userName);
        if (project == null) {
            // Auto create project for aomp rules upload.
            AddProjectRequest addProjectRequest = new AddProjectRequest();
            String currentProjectName = "AOMP_AUTO_PROJECT_" + System.currentTimeMillis();
            addProjectRequest.setProjectName(currentProjectName);
            addProjectRequest.setDescription("This is for aomp");
            User user = userDao.findByUsername(userName);
            Long userId = user.getId();
            projectService.addProject(addProjectRequest, userId);
            project = projectDao.findByNameAndCreateUser(currentProjectName, userName);
        }
        getAndSaveRule(excelRuleListener.getTemplateExcelContent(), excelRuleListener.getCustomExcelContent(), excelRuleListener.getMultiTemplateExcelContent(), excelRuleListener.getTemplateFileExcelContent(), project, userName, aomp);
    // projectEventService.record(project.getId(), userName, "upload", "[" + Arrays.toString(ruleNames.toArray()) + "].", EventTypeEnum.MODIFY_PROJECT.getCode());
    }
    fileInputStream.close();
    return new GeneralResponse<>("200", "{&SUCCEED_TO_UPLOAD_FILE}", null);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ExcelCustomRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject) Project(com.webank.wedatasphere.qualitis.project.entity.Project) ExcelMultiTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelMultiTemplateRuleByProject) ExcelTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject) ExcelTemplateFileRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateFileRuleByProject) User(com.webank.wedatasphere.qualitis.entity.User) AddProjectRequest(com.webank.wedatasphere.qualitis.project.request.AddProjectRequest) ArrayList(java.util.ArrayList) ExcelRuleListener(com.webank.wedatasphere.qualitis.rule.excel.ExcelRuleListener)

Aggregations

AddProjectRequest (com.webank.wedatasphere.qualitis.project.request.AddProjectRequest)3 User (com.webank.wedatasphere.qualitis.entity.User)2 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)2 Project (com.webank.wedatasphere.qualitis.project.entity.Project)2 ExcelCustomRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject)2 ExcelMultiTemplateRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelMultiTemplateRuleByProject)2 ExcelTemplateRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject)2 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)2 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)1 RuleMetricDepartmentUser (com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser)1 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)1 ExcelProjectListener (com.webank.wedatasphere.qualitis.project.excel.ExcelProjectListener)1 ExcelTemplateFileRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateFileRuleByProject)1 ModifyProjectDetailRequest (com.webank.wedatasphere.qualitis.project.request.ModifyProjectDetailRequest)1 ExcelRuleListener (com.webank.wedatasphere.qualitis.rule.excel.ExcelRuleListener)1 WriteExcelException (com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 RoleNotFoundException (javax.management.relation.RoleNotFoundException)1 ZipException (net.lingala.zip4j.exception.ZipException)1