Search in sources :

Example 16 with Template

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

the class RuleNodeServiceImpl method synchroRuleTemplate.

public void synchroRuleTemplate(RuleNodeRequest ruleNodeRequest, Rule rule, Template template, Set<RuleDataSource> ruleDataSources) throws IOException {
    Template templateInDb = ruleTemplateDao.findById(template.getId());
    Template templateNameInDb = null;
    if (StringUtils.isNotBlank(template.getImportExportName())) {
        templateNameInDb = ruleTemplateDao.findByImportExportName(template.getImportExportName());
    }
    if (templateInDb == null && templateNameInDb == null) {
        LOGGER.info("Start to synchronize the rule template. Template: {}", template);
        ObjectMapper objectMapper = new ObjectMapper();
        // Template meta info.
        Set<TemplateOutputMeta> templateOutputMetaSet = objectMapper.readValue(ruleNodeRequest.getTemplateTemplateOutputMetaObject(), new TypeReference<Set<TemplateOutputMeta>>() {
        });
        Set<TemplateMidTableInputMeta> templateMidTableInputMetaSet = objectMapper.readValue(ruleNodeRequest.getTemplateTemplateMidTableInputMetaObject(), new TypeReference<Set<TemplateMidTableInputMeta>>() {
        });
        Set<TemplateStatisticsInputMeta> templateStatisticsInputMetaSet = objectMapper.readValue(ruleNodeRequest.getTemplateTemplateStatisticsInputMetaObject(), new TypeReference<Set<TemplateStatisticsInputMeta>>() {
        });
        Template savedTemplate = ruleTemplateDao.saveTemplate(template);
        Set<TemplateOutputMeta> templateOutputMetas = new HashSet<>();
        for (TemplateOutputMeta outputMeta : templateOutputMetaSet) {
            outputMeta.setTemplate(savedTemplate);
            templateOutputMetas.add(templateOutputMetaDao.saveTemplateOutputMeta(outputMeta));
        }
        savedTemplate.setTemplateOutputMetas(templateOutputMetas);
        LOGGER.info("Success to save template output meta. TemplateOutputMetas: {}", savedTemplate.getTemplateOutputMetas());
        List<TemplateMidTableInputMeta> templateMidTableInputMetas = new ArrayList<>();
        for (TemplateMidTableInputMeta templateMidTableInputMeta : templateMidTableInputMetaSet) {
            templateMidTableInputMeta.setTemplate(savedTemplate);
            templateMidTableInputMetas.add(templateMidTableInputMeta);
        }
        templateMidTableInputMetas.sort(Comparator.comparing(TemplateMidTableInputMeta::getId));
        savedTemplate.setTemplateMidTableInputMetas(templateMidTableInputMetaService.saveAll(templateMidTableInputMetas));
        LOGGER.info("Success to save template mid_table input meta. TemplateMidTableInputMetas: {}", savedTemplate.getTemplateMidTableInputMetas());
        List<TemplateStatisticsInputMeta> templateStatisticsInputMetas = new ArrayList<>();
        for (TemplateStatisticsInputMeta templateStatisticsInputMeta : templateStatisticsInputMetaSet) {
            templateStatisticsInputMeta.setTemplate(savedTemplate);
            templateStatisticsInputMetas.add(templateStatisticsInputMeta);
        }
        savedTemplate.setStatisticAction(templateStatisticsInputMetaService.saveAll(templateStatisticsInputMetas));
        LOGGER.info("Success to save template statistics input meta. templateStatisticsInputMetas: {}", savedTemplate.getStatisticAction());
        Set<Integer> templateDateTypes = ruleDataSources.stream().filter(currRuleDataSource -> currRuleDataSource.getDatasourceType() != null).map(RuleDataSource::getDatasourceType).collect(Collectors.toSet());
        for (Integer templateDataType : templateDateTypes) {
            TemplateDataSourceType templateDataSourceType = new TemplateDataSourceType(templateDataType, savedTemplate);
            templateDataSourceTypeDao.save(templateDataSourceType);
        }
        LOGGER.info("Success to save template data types. Template data types: {}", Arrays.toString(templateDateTypes.toArray()));
        LOGGER.info("Finished to synchronize the rule template. Template: {}", savedTemplate);
        rule.setTemplate(savedTemplate);
    } else if (templateInDb != null) {
        rule.setTemplate(templateInDb);
    } else if (templateNameInDb != null) {
        rule.setTemplate(templateNameInDb);
    } else {
        rule.setTemplate(template);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) TemplateMidTableInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateMidTableInputMeta) TemplateOutputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) TemplateStatisticsInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType) HashSet(java.util.HashSet)

Example 17 with Template

use of com.webank.wedatasphere.qualitis.rule.entity.Template 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 18 with Template

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

the class RuleTemplateServiceImpl method getCustomRuleTemplateByUser.

@Override
public GeneralResponse<GetAllResponse<RuleTemplateResponse>> getCustomRuleTemplateByUser(PageRequest request) throws UnExpectedRequestException {
    // Check Arguments
    PageRequest.checkRequest(request);
    // Get userId
    Long userId = HttpUtils.getUserId(httpServletRequest);
    int size = request.getSize();
    int page = request.getPage();
    List<Template> templates = userRuleTemplateDao.findByUserId(userId, page, size);
    long total = userRuleTemplateDao.countByUserId(userId);
    GetAllResponse<RuleTemplateResponse> response = new GetAllResponse<>();
    response.setTotal(total);
    List<RuleTemplateResponse> responseList = new ArrayList<>();
    for (Template template : templates) {
        RuleTemplateResponse ruleTemplateResponse = new RuleTemplateResponse(template);
        List<Integer> types = templateDataSourceTypeDao.findByTemplate(template).stream().map(TemplateDataSourceType::getDataSourceTypeId).collect(Collectors.toList());
        ruleTemplateResponse.setDatasourceType(types);
        responseList.add(ruleTemplateResponse);
    }
    response.setData(responseList);
    LOGGER.info("Succeed to get custom rule_template. response: {}", response);
    return new GeneralResponse<>("200", "{&GET_CUSTOM_RULE_TEMPLATE_SUCCESSFULLY}", response);
}
Also used : ArrayList(java.util.ArrayList) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) RuleTemplateResponse(com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse)

Example 19 with Template

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

the class RuleTemplateServiceImpl method getDefaultRuleTemplate.

@Override
public GeneralResponse<GetAllResponse<RuleTemplateResponse>> getDefaultRuleTemplate(TemplatePageRequest request) throws UnExpectedRequestException {
    // Check Arguments
    TemplatePageRequest.checkRequest(request);
    int size = request.getSize();
    int page = request.getPage();
    String dataSourceType = request.getDataSourceType();
    Integer dataSourceTypeCode = TemplateDataSourceTypeEnum.getCode(dataSourceType);
    List<User> users = new ArrayList<>();
    List<Template> templates;
    List<Department> departments = new ArrayList<>();
    long total = 0;
    User userInDb = userDao.findById(HttpUtils.getUserId(httpServletRequest));
    List<UserRole> userRoles = userRoleDao.findByUser(userInDb);
    Integer roleType = roleService.getRoleType(userRoles);
    if (roleType.equals(RoleDefaultTypeEnum.ADMIN.getCode())) {
        templates = ruleTemplateDao.findAllDefaultTemplate(page, size);
        total = ruleTemplateDao.countAllDefaultTemplate();
    } else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
        for (UserRole userRole : userRoles) {
            Department department = userRole.getRole().getDepartment();
            if (department != null) {
                departments.add(department);
                List<User> userList = userDao.findByDepartment(department);
                users.addAll(userList);
            }
        }
        templates = ruleTemplateDao.findTemplates(RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode(), RuleTemplateTypeEnum.SINGLE_SOURCE_TEMPLATE.getCode(), departments.size() == 0 ? null : departments, users.size() == 0 ? null : users, dataSourceTypeCode, page, size);
        total = ruleTemplateDao.countTemplates(RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode(), RuleTemplateTypeEnum.SINGLE_SOURCE_TEMPLATE.getCode(), departments.size() == 0 ? null : departments, users.size() == 0 ? null : users, dataSourceTypeCode);
    } else {
        Department department = userInDb.getDepartment();
        if (department != null) {
            departments.add(department);
        }
        users.add(userInDb);
        templates = ruleTemplateDao.findTemplates(RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode(), RuleTemplateTypeEnum.SINGLE_SOURCE_TEMPLATE.getCode(), departments.size() == 0 ? null : departments, users.size() == 0 ? null : users, dataSourceTypeCode, page, size);
        total = ruleTemplateDao.countTemplates(RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode(), RuleTemplateTypeEnum.SINGLE_SOURCE_TEMPLATE.getCode(), departments.size() == 0 ? null : departments, users.size() == 0 ? null : users, dataSourceTypeCode);
    }
    GetAllResponse<RuleTemplateResponse> response = new GetAllResponse<>();
    response.setTotal(total);
    List<RuleTemplateResponse> responseList = new ArrayList<>();
    for (Template template : templates) {
        RuleTemplateResponse ruleTemplateResponse = new RuleTemplateResponse(template);
        List<Integer> types = templateDataSourceTypeDao.findByTemplate(template).stream().map(TemplateDataSourceType::getDataSourceTypeId).collect(Collectors.toList());
        ruleTemplateResponse.setDatasourceType(types);
        responseList.add(ruleTemplateResponse);
    }
    response.setData(responseList);
    LOGGER.info("Succeed to find default rule_template. response: {}", response);
    return new GeneralResponse<>("200", "{&GET_RULE_TEMPLATE_SUCCESSFULLY}", response);
}
Also used : TemplateUser(com.webank.wedatasphere.qualitis.rule.entity.TemplateUser) User(com.webank.wedatasphere.qualitis.entity.User) ArrayList(java.util.ArrayList) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) 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) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse)

Example 20 with Template

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

the class RuleTemplateServiceImpl method checkTemplateName.

private void checkTemplateName(String templateName) throws UnExpectedRequestException {
    List<Template> templates = ruleTemplateDao.getAllTemplate();
    LOGGER.info("Number of templates in database is {}", templates.size());
    for (Template template : templates) {
        if (templateName.equals(template.getName())) {
            throw new UnExpectedRequestException("Template name {&ALREADY_EXIST}");
        }
    }
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) Template(com.webank.wedatasphere.qualitis.rule.entity.Template)

Aggregations

Template (com.webank.wedatasphere.qualitis.rule.entity.Template)29 ArrayList (java.util.ArrayList)17 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)13 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)11 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)10 AlarmConfig (com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)9 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)9 TemplateDataSourceType (com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType)9 HashSet (java.util.HashSet)8 TemplateOutputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta)7 TemplateStatisticsInputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta)6 RuleTemplateResponse (com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse)6 List (java.util.List)6 User (com.webank.wedatasphere.qualitis.entity.User)5 UserRole (com.webank.wedatasphere.qualitis.entity.UserRole)5 TemplateUser (com.webank.wedatasphere.qualitis.rule.entity.TemplateUser)5 Date (java.util.Date)5 Set (java.util.Set)5 Transactional (org.springframework.transaction.annotation.Transactional)5 Department (com.webank.wedatasphere.qualitis.entity.Department)4