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);
}
Aggregations