Search in sources :

Example 1 with ExcelCustomRuleByProject

use of com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject in project Qualitis by WeBankFinTech.

the class RuleBatchServiceImpl method downloadRulesReal.

private GeneralResponse<?> downloadRulesReal(List<Rule> rules, HttpServletResponse response, String loginUser) throws IOException, WriteExcelException {
    String fileName = "batch_rules_export_" + FILE_DATE_FORMATTER.format(new Date()) + SUPPORT_EXCEL_SUFFIX_NAME;
    fileName = URLEncoder.encode(fileName, "UTF-8");
    response.setContentType("application/octet-stream");
    response.addHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName);
    response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
    String localeStr = httpServletRequest.getHeader("Content-Language");
    // Write to response.getOutputStream
    OutputStream outputStream = response.getOutputStream();
    List<ExcelCustomRuleByProject> customRules = getCustomRule(rules, localeStr);
    List<ExcelTemplateRuleByProject> templateRules = getTemplateRule(rules, localeStr);
    List<ExcelTemplateFileRuleByProject> templateFileRules = getFileRule(rules, localeStr);
    List<ExcelMultiTemplateRuleByProject> multiTemplateRules = getMultiTemplateRule(rules, localeStr);
    writeExcelToOutput(templateRules, customRules, multiTemplateRules, templateFileRules, outputStream);
    outputStream.flush();
    LOGGER.info("Succeed to download all rules in type of excel");
    // projectEventService.record(rules.iterator().next().getProject().getId(), loginUser, "download", "rules[" + Arrays.toString(rules.stream().map(Rule::getName).toArray()) + "].", EventTypeEnum.MODIFY_PROJECT.getCode());
    return null;
}
Also used : ExcelMultiTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelMultiTemplateRuleByProject) ExcelCustomRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject) OutputStream(java.io.OutputStream) ExcelTemplateFileRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateFileRuleByProject) Date(java.util.Date) ExcelTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject)

Example 2 with ExcelCustomRuleByProject

use of com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject 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 3 with ExcelCustomRuleByProject

use of com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject in project Qualitis by WeBankFinTech.

the class ProjectBatchServiceImpl method getExcelCustomRuleByProject.

private List<ExcelCustomRuleByProject> getExcelCustomRuleByProject(List<Project> projects, String localeStr) {
    List<ExcelCustomRuleByProject> excelCustomRuleByProjects = new ArrayList<>();
    for (Project project : projects) {
        List<Rule> rules = ruleDao.findByProject(project);
        List<ExcelCustomRuleByProject> excelCustomRules = ruleBatchService.getCustomRule(rules, localeStr);
        for (ExcelCustomRuleByProject excelCustomRule : excelCustomRules) {
            ExcelCustomRuleByProject excelCustomRuleByProject = new ExcelCustomRuleByProject();
            BeanUtils.copyProperties(excelCustomRule, excelCustomRuleByProject);
            excelCustomRuleByProject.setProjectName(project.getName());
            LOGGER.info("Collect excel line of custom rule: {}", excelCustomRuleByProject);
            excelCustomRuleByProjects.add(excelCustomRuleByProject);
        }
    }
    return excelCustomRuleByProjects;
}
Also used : 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) ExcelCustomRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule)

Example 4 with ExcelCustomRuleByProject

use of com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject in project Qualitis by WeBankFinTech.

the class ProjectBatchServiceImpl method writeProjectAndRules.

private GeneralResponse<?> writeProjectAndRules(List<Project> projects, HttpServletResponse response) throws IOException, WriteExcelException {
    String fileName = "batch_project_export_" + FILE_DATE_FORMATTER.format(new Date()) + SUPPORT_EXCEL_SUFFIX_NAME;
    fileName = URLEncoder.encode(fileName, "UTF-8");
    response.setContentType("application/octet-stream");
    response.addHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName);
    response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
    String localeStr = httpServletRequest.getHeader("Content-Language");
    OutputStream outputStream = response.getOutputStream();
    List<ExcelProject> excelProject = getExcelProject(projects);
    List<ExcelTemplateRuleByProject> excelTemplateRuleByProject = getExcelRuleByProject(projects, localeStr);
    List<ExcelCustomRuleByProject> excelCustomRuleByProject = getExcelCustomRuleByProject(projects, localeStr);
    List<ExcelTemplateFileRuleByProject> excelTemplateFileRuleByProject = getExcelTemplateFileRuleByProject(projects, localeStr);
    List<ExcelMultiTemplateRuleByProject> excelMultiTemplateRuleByProject = getExcelMultiTemplateRuleByProject(projects, localeStr);
    writeExcelToOutput(excelProject, excelTemplateRuleByProject, excelCustomRuleByProject, excelMultiTemplateRuleByProject, excelTemplateFileRuleByProject, outputStream);
    outputStream.flush();
    LOGGER.info("Succeed to download all projects in type of excel");
    return null;
}
Also used : ExcelMultiTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelMultiTemplateRuleByProject) ExcelCustomRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ExcelTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject)

Example 5 with ExcelCustomRuleByProject

use of com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject in project Qualitis by WeBankFinTech.

the class RuleBatchServiceImpl method constructAddCustomRuleRequest.

private List<AddCustomRuleRequest> constructAddCustomRuleRequest(Map<String, List<ExcelCustomRuleByProject>> customRulePartitionedByRuleName, Project project, String localeStr) throws UnExpectedRequestException {
    List<AddCustomRuleRequest> addCustomRuleRequests = new ArrayList<>();
    for (String ruleName : customRulePartitionedByRuleName.keySet()) {
        List<ExcelCustomRuleByProject> ruleInfos = customRulePartitionedByRuleName.get(ruleName);
        AddCustomRuleRequest addCustomRuleRequest = new AddCustomRuleRequest();
        ExcelCustomRuleByProject firstCommonInfo = ruleInfos.get(0);
        boolean alarm = false;
        Long projectId = project.getId();
        addCustomRuleRequest.setSpecifyStaticStartupParam(firstCommonInfo.getSpecifyStaticStartupParam());
        addCustomRuleRequest.setDeleteFailCheckResult(firstCommonInfo.getDeleteFailCheckResult());
        addCustomRuleRequest.setStaticStartupParam(firstCommonInfo.getStaticStartupParam());
        addCustomRuleRequest.setAbortOnFailure(firstCommonInfo.getAbortOnFailure());
        addCustomRuleRequest.setProxyUser(firstCommonInfo.getProxyUser());
        String ruleGroupName = firstCommonInfo.getRuleGroupName();
        String clusterName = firstCommonInfo.getClusterName();
        String outputName = firstCommonInfo.getOutputName();
        String fromContent = null;
        String whereContent = null;
        Integer functionType = null;
        String functionContent = null;
        Boolean saveMidTable = firstCommonInfo.getSaveMidTable();
        if (firstCommonInfo.getLinkisDataSourceId() != null) {
            addCustomRuleRequest.setLinkisDataSourceId(Long.parseLong(firstCommonInfo.getLinkisDataSourceId()));
            addCustomRuleRequest.setLinkisDataSourceName(firstCommonInfo.getLinkisDataSourceName());
            addCustomRuleRequest.setLinkisDataSourceType(firstCommonInfo.getLinkisDataSourceType());
        }
        if (StringUtils.isNotBlank(firstCommonInfo.getFunctionName()) && StringUtils.isNotBlank(firstCommonInfo.getFunctionContent()) && StringUtils.isNotBlank(firstCommonInfo.getFromContent()) && StringUtils.isNotBlank(firstCommonInfo.getWhereContent())) {
            functionType = FunctionTypeEnum.getFunctionTypeByName(firstCommonInfo.getFunctionName());
            functionContent = firstCommonInfo.getFunctionContent();
            whereContent = firstCommonInfo.getWhereContent();
            fromContent = firstCommonInfo.getFromContent();
        } else {
            addCustomRuleRequest.setSqlCheckArea(firstCommonInfo.getSqlCheckArea());
        }
        List<CustomAlarmConfigRequest> alarmConfigRequests = new ArrayList<>();
        for (ExcelCustomRuleByProject excelCustomRule : ruleInfos) {
            getCustomAlarmConfig(alarmConfigRequests, excelCustomRule, localeStr);
        }
        if (StringUtils.isBlank(ruleGroupName)) {
            throw new UnExpectedRequestException("RuleGroupName {&CAN_NOT_BE_NULL_OR_EMPTY}");
        }
        RuleGroup ruleGroupInDb = ruleGroupDao.findByRuleGroupNameAndProjectId(ruleGroupName, projectId);
        if (ruleGroupInDb != null) {
            addCustomRuleRequest.setRuleGroupId(ruleGroupInDb.getId());
        } else {
            RuleGroup ruleGroup = ruleGroupDao.saveRuleGroup(new RuleGroup(ruleGroupName, project.getId()));
            addCustomRuleRequest.setRuleGroupId(ruleGroup.getId());
        }
        addCustomRuleRequest.setRuleName(ruleName);
        addCustomRuleRequest.setRuleCnName(firstCommonInfo.getRuleCnName());
        addCustomRuleRequest.setRuleDetail(firstCommonInfo.getRuleDetail());
        if (!alarmConfigRequests.isEmpty()) {
            alarm = true;
        }
        addCustomRuleRequest.setAlarm(alarm);
        addCustomRuleRequest.setProjectId(projectId);
        addCustomRuleRequest.setOutputName(outputName);
        addCustomRuleRequest.setFunctionType(functionType);
        addCustomRuleRequest.setSaveMidTable(saveMidTable);
        addCustomRuleRequest.setFunctionContent(functionContent);
        addCustomRuleRequest.setAlarmVariable(alarmConfigRequests);
        addCustomRuleRequest.setWhereContent(whereContent);
        addCustomRuleRequest.setFromContent(fromContent);
        addCustomRuleRequest.setClusterName(clusterName);
        addCustomRuleRequests.add(addCustomRuleRequest);
    }
    return addCustomRuleRequests;
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ExcelCustomRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject) ArrayList(java.util.ArrayList) RuleGroup(com.webank.wedatasphere.qualitis.rule.entity.RuleGroup) AddCustomRuleRequest(com.webank.wedatasphere.qualitis.rule.request.AddCustomRuleRequest) CustomAlarmConfigRequest(com.webank.wedatasphere.qualitis.rule.request.CustomAlarmConfigRequest)

Aggregations

ExcelCustomRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject)7 ExcelMultiTemplateRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelMultiTemplateRuleByProject)5 ExcelTemplateRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject)5 ArrayList (java.util.ArrayList)3 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)2 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)2 Project (com.webank.wedatasphere.qualitis.project.entity.Project)2 ExcelTemplateFileRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateFileRuleByProject)2 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)2 OutputStream (java.io.OutputStream)2 RuleMetricDepartmentUser (com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser)1 User (com.webank.wedatasphere.qualitis.entity.User)1 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)1 ExcelProjectListener (com.webank.wedatasphere.qualitis.project.excel.ExcelProjectListener)1 AddProjectRequest (com.webank.wedatasphere.qualitis.project.request.AddProjectRequest)1 ModifyProjectDetailRequest (com.webank.wedatasphere.qualitis.project.request.ModifyProjectDetailRequest)1 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)1 AlarmConfig (com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)1 RuleGroup (com.webank.wedatasphere.qualitis.rule.entity.RuleGroup)1 WriteExcelException (com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException)1