Search in sources :

Example 1 with ExcelRuleListener

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

the class RuleBatchServiceImpl method readExcel.

private ExcelRuleListener readExcel(InputStream inputStream) {
    LOGGER.info("Start to read excel");
    ExcelRuleListener excelRuleListener = partitionByRuleName(inputStream);
    LOGGER.info("Finish to read excel. template rule content: {}", excelRuleListener.getTemplateExcelContent());
    LOGGER.info("Finish to read excel. custom rule content: {}", excelRuleListener.getCustomExcelContent());
    LOGGER.info("Finish to read excel. multi rule content: {}", excelRuleListener.getMultiTemplateExcelContent());
    LOGGER.info("Finish to read excel. template file rule content: {}", excelRuleListener.getTemplateFileExcelContent());
    return excelRuleListener;
}
Also used : ExcelRuleListener(com.webank.wedatasphere.qualitis.rule.excel.ExcelRuleListener)

Example 2 with ExcelRuleListener

use of com.webank.wedatasphere.qualitis.rule.excel.ExcelRuleListener 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)

Example 3 with ExcelRuleListener

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

the class RuleBatchServiceImpl method partitionByRuleName.

private ExcelRuleListener partitionByRuleName(InputStream inputStream) {
    ExcelRuleListener listener = new ExcelRuleListener();
    ExcelReader excelReader = new ExcelReader(inputStream, null, listener);
    List<Sheet> sheets = excelReader.getSheets();
    for (Sheet sheet : sheets) {
        if (sheet.getSheetName().equals(ExcelSheetName.TEMPLATE_RULE_NAME)) {
            sheet.setClazz(ExcelTemplateRuleByProject.class);
            sheet.setHeadLineMun(1);
            excelReader.read(sheet);
        } else if (sheet.getSheetName().equals(ExcelSheetName.CUSTOM_RULE_NAME)) {
            sheet.setClazz(ExcelCustomRuleByProject.class);
            sheet.setHeadLineMun(1);
            excelReader.read(sheet);
        } else if (sheet.getSheetName().equals(ExcelSheetName.MULTI_TEMPLATE_RULE_NAME)) {
            sheet.setClazz(ExcelMultiTemplateRuleByProject.class);
            sheet.setHeadLineMun(1);
            excelReader.read(sheet);
        } else if (sheet.getSheetName().equals(ExcelSheetName.TEMPLATE_FILE_RULE_NAME)) {
            sheet.setClazz(ExcelTemplateFileRuleByProject.class);
            sheet.setHeadLineMun(1);
            excelReader.read(sheet);
        }
    }
    return listener;
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) ExcelCustomRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject) ExcelTemplateFileRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateFileRuleByProject) Sheet(com.alibaba.excel.metadata.Sheet) ExcelRuleListener(com.webank.wedatasphere.qualitis.rule.excel.ExcelRuleListener)

Aggregations

ExcelRuleListener (com.webank.wedatasphere.qualitis.rule.excel.ExcelRuleListener)3 ExcelCustomRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject)2 ExcelTemplateFileRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateFileRuleByProject)2 ExcelReader (com.alibaba.excel.ExcelReader)1 Sheet (com.alibaba.excel.metadata.Sheet)1 User (com.webank.wedatasphere.qualitis.entity.User)1 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)1 Project (com.webank.wedatasphere.qualitis.project.entity.Project)1 ExcelMultiTemplateRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelMultiTemplateRuleByProject)1 ExcelTemplateRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject)1 AddProjectRequest (com.webank.wedatasphere.qualitis.project.request.AddProjectRequest)1 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)1 ArrayList (java.util.ArrayList)1