Search in sources :

Example 6 with TemplateDataSourceType

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

the class RuleTemplateServiceImpl method modifyRuleTemplate.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public RuleTemplateResponse modifyRuleTemplate(ModifyRuleTemplateRequest request) throws UnExpectedRequestException, PermissionDeniedRequestException {
    AddRuleTemplateRequest addRuleTemplateRequest = ModifyRuleTemplateRequest.checkRequest(request);
    // Check template existence
    Template templateInDb = checkRuleTemplate(request.getTemplateId());
    // Check user info.
    User userInDb = userDao.findById(HttpUtils.getUserId(httpServletRequest));
    List<UserRole> userRoles = userRoleDao.findByUser(userInDb);
    Integer roleType = roleService.getRoleType(userRoles);
    if (roleType.equals(RoleDefaultTypeEnum.PROJECTOR.getCode())) {
        LOGGER.info("The projector is going to modify template.");
        if (templateInDb.getLevel().intValue() != RuleTemplateLevelEnum.PERSONAL_TEMPLATE.getCode()) {
            throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
        }
        if (!userInDb.equals(templateInDb.getCreateUser())) {
            throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
        }
    } else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
        LOGGER.info("The department admin is going to modify template.");
        if (templateInDb.getLevel().intValue() == RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode()) {
            throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
        }
        List<User> createUsers = new ArrayList<>();
        for (UserRole userRole : userRoles) {
            Department department = userRole.getRole().getDepartment();
            if (department != null) {
                List<User> userList = userDao.findByDepartment(department);
                createUsers.addAll(userList);
            }
        }
        if (!createUsers.contains(templateInDb.getCreateUser())) {
            throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
        }
    } else {
        LOGGER.info("The system admin is going to modify template.");
    }
    // delete output meta
    templateOutputMetaService.deleteByTemplate(templateInDb);
    // delete mid_table input meta
    templateMidTableInputMetaService.deleteByTemplate(templateInDb);
    // delete statistics input meta
    templateStatisticsInputMetaService.deleteByTemplate(templateInDb);
    // delete template type relationship
    templateDataSourceTypeDao.deleteByTemplate(templateInDb);
    // Save template.
    if (!templateInDb.getName().equals(request.getTemplateName())) {
        checkTemplateName(request.getTemplateName());
    }
    templateInDb.setName(request.getTemplateName());
    templateInDb.setClusterNum(request.getClusterNum());
    templateInDb.setDbNum(request.getDbNum());
    templateInDb.setTableNum(request.getTableNum());
    templateInDb.setFieldNum(request.getFieldNum());
    templateInDb.setActionType(request.getActionType());
    templateInDb.setMidTableAction(request.getMidTableAction());
    templateInDb.setSaveMidTable(request.getSaveMidTable());
    templateInDb.setShowSql(request.getMidTableAction());
    templateInDb.setTemplateType(request.getTemplateType());
    templateInDb.setModifyUser(userInDb);
    Template savedTemplate = ruleTemplateDao.saveTemplate(templateInDb);
    List<TemplateDataSourceType> templateDataSourceTypes = templateDataSourceTypeDao.findByTemplate(savedTemplate);
    List<Integer> templateDataSourceTypeIntegers = templateDataSourceTypes.stream().map(TemplateDataSourceType::getDataSourceTypeId).collect(Collectors.toList());
    for (Integer type : request.getDatasourceType()) {
        if (!templateDataSourceTypeIntegers.contains(type)) {
            TemplateDataSourceType templateDataSourceType = new TemplateDataSourceType(type, savedTemplate);
            templateDataSourceTypeDao.save(templateDataSourceType);
        }
    }
    LOGGER.info("Succeed to save rule template, template_id: {}", savedTemplate.getId());
    // Save template info.
    createAndSaveTemplateInfo(savedTemplate, addRuleTemplateRequest);
    return new RuleTemplateResponse(savedTemplate);
}
Also used : PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) TemplateUser(com.webank.wedatasphere.qualitis.rule.entity.TemplateUser) User(com.webank.wedatasphere.qualitis.entity.User) AddRuleTemplateRequest(com.webank.wedatasphere.qualitis.rule.request.AddRuleTemplateRequest) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) RuleTemplateResponse(com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse) Department(com.webank.wedatasphere.qualitis.entity.Department) TemplateDepartment(com.webank.wedatasphere.qualitis.rule.entity.TemplateDepartment) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) List(java.util.List) ArrayList(java.util.ArrayList) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with TemplateDataSourceType

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

the class RuleTemplateServiceImpl method deleteCustomTemplate.

@Override
public void deleteCustomTemplate(Template template) throws UnExpectedRequestException {
    if (template == null) {
        throw new UnExpectedRequestException("Template {&DOES_NOT_EXIST}");
    }
    if (!template.getTemplateType().equals(RuleTemplateTypeEnum.CUSTOM.getCode())) {
        throw new UnExpectedRequestException("Template(id:[" + template.getId() + "]) {&IS_NOT_A_CUSTOM_TEMPLATE}");
    }
    List<TemplateDataSourceType> templateDataSourceTypes = templateDataSourceTypeDao.findByTemplate(template);
    for (TemplateDataSourceType templateDataSourceType : templateDataSourceTypes) {
        templateDataSourceTypeDao.delete(templateDataSourceType);
    }
    ruleTemplateDao.deleteTemplate(template);
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType)

Example 8 with TemplateDataSourceType

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

the class RuleTemplateServiceImpl method addRuleTemplate.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class }, propagation = Propagation.REQUIRED)
public RuleTemplateResponse addRuleTemplate(AddRuleTemplateRequest request) throws UnExpectedRequestException {
    AddRuleTemplateRequest.checkRequest(request, false);
    LOGGER.info("Add default rule template request detail: {}", request.toString());
    // Save template.
    checkTemplateName(request.getTemplateName());
    Template newTemplate = new Template();
    newTemplate.setName(request.getTemplateName());
    newTemplate.setClusterNum(request.getClusterNum());
    newTemplate.setDbNum(request.getDbNum());
    newTemplate.setTableNum(request.getTableNum());
    newTemplate.setFieldNum(request.getFieldNum());
    newTemplate.setActionType(request.getActionType());
    newTemplate.setMidTableAction(request.getMidTableAction());
    newTemplate.setSaveMidTable(request.getSaveMidTable());
    newTemplate.setShowSql(request.getMidTableAction());
    newTemplate.setTemplateType(request.getTemplateType());
    newTemplate.setImportExportName(UuidGenerator.generate());
    // Save create user info.
    User userInDb = userDao.findById(HttpUtils.getUserId(httpServletRequest));
    newTemplate.setCreateUser(userInDb);
    Template savedTemplate = ruleTemplateDao.saveTemplate(newTemplate);
    for (Integer type : request.getDatasourceType()) {
        TemplateDataSourceType templateDataSourceType = new TemplateDataSourceType(type, savedTemplate);
        templateDataSourceTypeDao.save(templateDataSourceType);
    }
    LOGGER.info("Succeed to save rule template, template_id: {}", savedTemplate.getId());
    // Determine the template level and save association table.
    List<UserRole> userRoles = userRoleDao.findByUser(userInDb);
    Integer roleType = roleService.getRoleType(userRoles);
    // Save department template
    if (roleType.equals(RoleDefaultTypeEnum.ADMIN.getCode())) {
        savedTemplate.setLevel(RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode());
    } else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
        savedTemplate.setLevel(RuleTemplateLevelEnum.DEPARTMENT_TEMPLATE.getCode());
        saveTemplateDepartment(ruleTemplateDao.saveTemplate(savedTemplate), userRoles);
    } else {
        savedTemplate.setLevel(RuleTemplateLevelEnum.PERSONAL_TEMPLATE.getCode());
        saveTemplateUser(userInDb, ruleTemplateDao.saveTemplate(savedTemplate));
    }
    // Save template info.
    createAndSaveTemplateInfo(savedTemplate, request);
    return new RuleTemplateResponse(savedTemplate);
}
Also used : RuleTemplateResponse(com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse) TemplateUser(com.webank.wedatasphere.qualitis.rule.entity.TemplateUser) User(com.webank.wedatasphere.qualitis.entity.User) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with TemplateDataSourceType

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

the class CustomRuleServiceImpl method addCustomRuleReal.

private GeneralResponse<RuleResponse> addCustomRuleReal(AddCustomRuleRequest request, String loginUser) throws UnExpectedRequestException, PermissionDeniedRequestException {
    // Check Arguments
    AddCustomRuleRequest.checkRequest(request);
    String nowDate = ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date());
    // Generate Template, TemplateStatisticsInputMeta and save
    Template template = ruleTemplateService.addCustomTemplate(request);
    TemplateDataSourceType templateDataSourceType = new TemplateDataSourceType();
    if (request.getLinkisDataSourceType() == null) {
        templateDataSourceType.setDataSourceTypeId(TemplateDataSourceTypeEnum.HIVE.getCode());
    } else {
        templateDataSourceType.setDataSourceTypeId(TemplateDataSourceTypeEnum.MYSQL.getCode());
    }
    templateDataSourceType.setTemplate(template);
    templateDataSourceTypeDao.save(templateDataSourceType);
    // Save rule, rule_alarm_config and ruleDataSource
    // Check existence of project
    Project projectInDb = projectService.checkProjectExistence(request.getProjectId(), loginUser);
    // Check permissions of project
    List<Integer> permissions = new ArrayList<>();
    permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
    projectService.checkProjectPermission(projectInDb, loginUser, permissions);
    // Check unique of rule name
    ruleService.checkRuleName(request.getRuleName(), projectInDb, null);
    // Check if cluster name is supported
    ruleDataSourceService.checkDataSourceClusterSupport(request.getClusterName());
    RuleGroup ruleGroup;
    if (request.getRuleGroupId() != null) {
        ruleGroup = ruleGroupDao.findById(request.getRuleGroupId());
        if (ruleGroup == null) {
            throw new UnExpectedRequestException(String.format("Rule Group: %s {&CAN_NOT_BE_NULL_OR_EMPTY}", request.getRuleGroupId()));
        }
    } else {
        ruleGroup = ruleGroupDao.saveRuleGroup(new RuleGroup("Group_" + UUID.randomUUID().toString().replace("-", ""), projectInDb.getId()));
    }
    Rule newRule = new Rule();
    // Set basic info.
    setBasicInfo(newRule, projectInDb, ruleGroup, template, loginUser, nowDate, request);
    String csId = request.getCsId();
    // For context service.
    boolean cs = false;
    if (StringUtils.isNotBlank(csId)) {
        newRule.setCsId(csId);
        cs = true;
    }
    String fileId = request.getFileId();
    // For fps file check.
    boolean fps = false;
    if (StringUtils.isNotBlank(fileId)) {
        fps = true;
    }
    String sqlCheckArea = request.getSqlCheckArea();
    // For fps file check.
    boolean sqlCheck = false;
    if (StringUtils.isNotBlank(sqlCheckArea)) {
        sqlCheck = true;
    }
    Rule savedRule = ruleDao.saveRule(newRule);
    LOGGER.info("Succeed to save custom rule, rule_id: {}", savedRule.getId());
    List<AlarmConfig> savedAlarmConfigs = new ArrayList<>();
    if (request.getAlarm()) {
        savedAlarmConfigs = alarmConfigService.checkAndSaveCustomAlarmVariable(request.getAlarmVariable(), savedRule);
        LOGGER.info("Succeed to save alarm_configs, alarm_configs: {}", savedAlarmConfigs);
    }
    List<RuleDataSource> ruleDataSources = ruleDataSourceService.checkAndSaveCustomRuleDataSource(request.getClusterName(), request.getProxyUser(), loginUser, savedRule, cs, sqlCheck, request.getLinkisDataSourceId(), request.getLinkisDataSourceVersionId(), request.getLinkisDataSourceName(), request.getLinkisDataSourceType());
    savedRule.setAlarmConfigs(new HashSet<>(savedAlarmConfigs));
    if (CollectionUtils.isNotEmpty(ruleDataSources)) {
        savedRule.setRuleDataSources(new HashSet<>(ruleDataSources));
    }
    // Update rule count of datasource
    ruleDataSourceService.updateRuleDataSourceCount(savedRule, 1);
    RuleResponse response = new RuleResponse(savedRule);
    LOGGER.info("Succeed to add custom rule, rule_id: {}", savedRule.getId());
    return new GeneralResponse<>("200", "{&SUCCEED_TO_ADD_CUSTOM_RULE}", response);
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) RuleResponse(com.webank.wedatasphere.qualitis.rule.response.RuleResponse) ArrayList(java.util.ArrayList) Date(java.util.Date) RuleGroup(com.webank.wedatasphere.qualitis.rule.entity.RuleGroup) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) Project(com.webank.wedatasphere.qualitis.project.entity.Project) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)

Example 10 with TemplateDataSourceType

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

the class CustomRuleServiceImpl method modifyCustomRuleReal.

private GeneralResponse<RuleResponse> modifyCustomRuleReal(ModifyCustomRuleRequest request, String loginUser) throws UnExpectedRequestException, MetaDataAcquireFailedException, PermissionDeniedRequestException {
    ModifyCustomRuleRequest.checkRequest(request);
    Rule ruleInDb = ruleDao.findById(request.getRuleId());
    if (ruleInDb == null) {
        throw new UnExpectedRequestException("rule_id [" + request.getRuleId() + "] {&DOES_NOT_EXIST}");
    }
    Project projectInDb = projectService.checkProjectExistence(ruleInDb.getProject().getId(), loginUser);
    if (!ruleInDb.getRuleType().equals(RuleTypeEnum.CUSTOM_RULE.getCode())) {
        throw new UnExpectedRequestException("rule_id: [" + request.getRuleId() + "]) {&IS_NOT_A_CUSTOM_RULE}");
    }
    // Check permissions of project
    List<Integer> permissions = new ArrayList<>();
    permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
    projectService.checkProjectPermission(projectInDb, loginUser, permissions);
    LOGGER.info("Succeed to find custom rule. rule_id: {}", ruleInDb.getId());
    // Check existence of project name
    ruleService.checkRuleName(request.getRuleName(), ruleInDb.getProject(), ruleInDb.getId());
    // Check if cluster name supported
    ruleDataSourceService.checkDataSourceClusterSupport(request.getClusterName());
    String nowDate = ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date());
    // Delete alarm config by custom rule
    alarmConfigService.deleteByRule(ruleInDb);
    LOGGER.info("Succeed to delete all alarm_config. rule_id: {}", ruleInDb.getId());
    // Delete template of custom rule
    ruleTemplateService.deleteCustomTemplate(ruleInDb.getTemplate());
    LOGGER.info("Succeed to delete custom rule template. rule_id: {}", request.getRuleId());
    // Delete rule datasource of custom rule
    ruleDataSourceService.deleteByRule(ruleInDb);
    LOGGER.info("Succeed to delete all rule_dataSources. rule_id: {}", ruleInDb.getId());
    // Update rule count of datasource
    ruleDataSourceService.updateRuleDataSourceCount(ruleInDb, -1);
    // Save template, alarm config, rule datasource of custom rule
    AddCustomRuleRequest addCustomRuleRequest = new AddCustomRuleRequest();
    BeanUtils.copyProperties(request, addCustomRuleRequest);
    Template template = ruleTemplateService.addCustomTemplate(addCustomRuleRequest);
    TemplateDataSourceType templateDataSourceType = new TemplateDataSourceType();
    if (request.getLinkisDataSourceType() == null) {
        templateDataSourceType.setDataSourceTypeId(TemplateDataSourceTypeEnum.HIVE.getCode());
    } else {
        templateDataSourceType.setDataSourceTypeId(TemplateDataSourceTypeEnum.MYSQL.getCode());
    }
    templateDataSourceType.setTemplate(template);
    templateDataSourceTypeDao.save(templateDataSourceType);
    // Modify custom rule and save
    setBasicInfo(ruleInDb, template, loginUser, nowDate, request);
    String csId = request.getCsId();
    ruleInDb.setCsId(csId);
    boolean cs = false;
    if (StringUtils.isNotBlank(csId)) {
        cs = true;
    }
    String fileId = request.getFileId();
    boolean fps = false;
    if (StringUtils.isNotBlank(fileId)) {
        fps = true;
    }
    String sqlCheckArea = request.getSqlCheckArea();
    boolean sqlCheck = false;
    if (StringUtils.isNotBlank(sqlCheckArea)) {
        sqlCheck = true;
    }
    Rule savedRule = ruleDao.saveRule(ruleInDb);
    // Save alarm config and rule datasource
    List<AlarmConfig> savedAlarmConfigs = new ArrayList<>();
    if (request.getAlarm()) {
        savedAlarmConfigs = alarmConfigService.checkAndSaveCustomAlarmVariable(request.getAlarmVariable(), savedRule);
        LOGGER.info("Succeed to save alarm_configs, alarm_configs: {}", savedAlarmConfigs);
    }
    List<RuleDataSource> ruleDataSources = ruleDataSourceService.checkAndSaveCustomRuleDataSource(request.getClusterName(), request.getProxyUser(), loginUser, savedRule, cs, sqlCheck, request.getLinkisDataSourceId(), request.getLinkisDataSourceVersionId(), request.getLinkisDataSourceName(), request.getLinkisDataSourceType());
    savedRule.setAlarmConfigs(new HashSet<>(savedAlarmConfigs));
    if (CollectionUtils.isNotEmpty(ruleDataSources)) {
        savedRule.setRuleDataSources(new HashSet<>(ruleDataSources));
    }
    // Update rule count of datasource
    ruleDataSourceService.updateRuleDataSourceCount(ruleInDb, 1);
    RuleResponse response = new RuleResponse(savedRule);
    LOGGER.info("Succeed to modify custom rule, rule_id: {}", savedRule.getId());
    // projectEventService.record(savedRule.getProject().getId(), loginUser, "modify", "custom rule[name= " + savedRule.getName() + "].", EventTypeEnum.MODIFY_PROJECT.getCode());
    return new GeneralResponse<>("200", "{&SUCCEED_TO_MODIFY_CUSTOM_RULE}", response);
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) RuleResponse(com.webank.wedatasphere.qualitis.rule.response.RuleResponse) ArrayList(java.util.ArrayList) Date(java.util.Date) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) Project(com.webank.wedatasphere.qualitis.project.entity.Project) AddCustomRuleRequest(com.webank.wedatasphere.qualitis.rule.request.AddCustomRuleRequest) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)

Aggregations

TemplateDataSourceType (com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType)10 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)9 ArrayList (java.util.ArrayList)7 User (com.webank.wedatasphere.qualitis.entity.User)3 UserRole (com.webank.wedatasphere.qualitis.entity.UserRole)3 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)3 AlarmConfig (com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)3 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)3 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)3 TemplateOutputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta)3 TemplateStatisticsInputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta)3 TemplateUser (com.webank.wedatasphere.qualitis.rule.entity.TemplateUser)3 RuleTemplateResponse (com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse)3 List (java.util.List)3 Department (com.webank.wedatasphere.qualitis.entity.Department)2 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)2 Project (com.webank.wedatasphere.qualitis.project.entity.Project)2 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)2 TemplateDepartment (com.webank.wedatasphere.qualitis.rule.entity.TemplateDepartment)2 TemplateMidTableInputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateMidTableInputMeta)2