Search in sources :

Example 1 with AddRuleTemplateRequest

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

Aggregations

Department (com.webank.wedatasphere.qualitis.entity.Department)1 User (com.webank.wedatasphere.qualitis.entity.User)1 UserRole (com.webank.wedatasphere.qualitis.entity.UserRole)1 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)1 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)1 TemplateDataSourceType (com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType)1 TemplateDepartment (com.webank.wedatasphere.qualitis.rule.entity.TemplateDepartment)1 TemplateUser (com.webank.wedatasphere.qualitis.rule.entity.TemplateUser)1 AddRuleTemplateRequest (com.webank.wedatasphere.qualitis.rule.request.AddRuleTemplateRequest)1 RuleTemplateResponse (com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Transactional (org.springframework.transaction.annotation.Transactional)1