use of com.webank.wedatasphere.qualitis.project.entity.Project in project Qualitis by WeBankFinTech.
the class MultiSourceRuleServiceImpl method modifyRuleDetailReal.
private GeneralResponse<RuleResponse> modifyRuleDetailReal(ModifyMultiSourceRequest request, String loginUser) throws UnExpectedRequestException, PermissionDeniedRequestException {
// Check Arguments
CommonChecker.checkObject(request, "request");
String csId = request.getCsId();
boolean cs = false;
if (StringUtils.isNotBlank(csId)) {
cs = true;
}
ModifyMultiSourceRequest.checkRequest(request, cs);
// Check existence of rule
Rule ruleInDb = ruleDao.findById(request.getRuleId());
if (ruleInDb == null) {
throw new UnExpectedRequestException("rule_id [" + request.getRuleId() + "] {&DOES_NOT_EXIST}");
}
// Check existence of project
Project projectInDb = projectService.checkProjectExistence(ruleInDb.getProject().getId(), loginUser);
// Check permissions of project
List<Integer> permissions = new ArrayList<>();
permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
projectService.checkProjectPermission(projectInDb, loginUser, permissions);
if (!ruleInDb.getRuleType().equals(RuleTypeEnum.MULTI_TEMPLATE_RULE.getCode())) {
throw new UnExpectedRequestException("rule_id: [" + request.getRuleId() + "]) {&IS_NOT_A_MULTI_TEMPLATE_RULE}");
}
LOGGER.info("Succeed to find multi 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());
// Check existence of rule id and if multi-table rule
Template templateInDb = ruleTemplateService.checkRuleTemplate(request.getMultiSourceRuleTemplateId());
if (!templateInDb.getTemplateType().equals(RuleTemplateTypeEnum.MULTI_SOURCE_TEMPLATE.getCode())) {
throw new UnExpectedRequestException("Template id :" + request.getMultiSourceRuleTemplateId() + " {&IS_NOT_A_MULTI_SOURCE_TEMPLATE}");
}
// Delete rule config by rule
alarmConfigService.deleteByRule(ruleInDb);
LOGGER.info("Succeed to delete all alarm_config. rule_id: {}", ruleInDb.getId());
// Delete rule datasource by 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);
// Delete rule datasource mapping by rule
ruleDataSourceMappingService.deleteByRule(ruleInDb);
LOGGER.info("Succeed to delete all rule_dataSource_mapping. rule_id: {}", ruleInDb.getId());
// Delete rule variable by rule
ruleVariableService.deleteByRule(ruleInDb);
LOGGER.info("Succeed to delete all rule_variable. rule_id: {}", ruleInDb.getId());
// Delete child rule
if (ruleInDb.getChildRule() != null) {
ruleDao.deleteRule(ruleInDb.getChildRule());
LOGGER.info("Succeed to delete all child rule. rule_id: {}", ruleInDb.getId());
}
AddMultiSourceRuleRequest addMultiSourceRuleRequest = new AddMultiSourceRuleRequest();
BeanUtils.copyProperties(request, addMultiSourceRuleRequest);
String leftUuid = UuidGenerator.generate();
String rightUuid = UuidGenerator.generate();
Rule savedRule = generateRule(addMultiSourceRuleRequest, projectInDb, true, ruleInDb, false, cs, leftUuid, rightUuid, loginUser);
if (templateInDb.getChildTemplate() != null) {
// Generate child rule
AddMultiSourceRuleRequest childRequest = generateChildRequest(addMultiSourceRuleRequest, templateInDb.getChildTemplate());
Rule childRule = generateRule(childRequest, null, false, null, true, cs, leftUuid, rightUuid, loginUser);
childRule.setParentRule(savedRule);
ruleDao.saveRule(childRule);
LOGGER.info("Succeed to generate child rule");
}
RuleResponse response = new RuleResponse(savedRule);
LOGGER.info("Succeed to modify multi source rule, rule_id: {}", savedRule.getId());
// projectEventService.record(savedRule.getProject().getId(), loginUser, "modify", "multi source rule[name= " + savedRule.getName() + "].", EventTypeEnum.MODIFY_PROJECT.getCode());
return new GeneralResponse<>("200", "{&MODIFY_MULTI_RULE_SUCCESSFULLY}", response);
}
use of com.webank.wedatasphere.qualitis.project.entity.Project 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);
}
use of com.webank.wedatasphere.qualitis.project.entity.Project in project Qualitis by WeBankFinTech.
the class RuleNodeServiceImpl method modifyRuleByCsId.
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<RuleResponse> modifyRuleByCsId(ModifyRuleRequest request) throws UnExpectedRequestException {
CommonChecker.checkObject(request, "ModifyRuleRequest");
Project projectInDb = projectDao.findById(request.getProjectId());
if (projectInDb == null) {
projectInDb = projectDao.findByName(request.getProjectName());
if (projectInDb == null) {
throw new UnExpectedRequestException("Project is not exist.");
}
}
if (projectInDb.getProjectType().equals(ProjectTypeEnum.NORMAL_PROJECT.getCode())) {
throw new UnExpectedRequestException("Project is not workflow project.");
}
List<Rule> rules = ruleDao.findByProject(projectInDb);
if (rules.isEmpty() || rules.get(0) == null) {
throw new UnExpectedRequestException("Project [id = " + projectInDb.getId() + "] does not have rules.");
}
LOGGER.info("Start to update context service ID.");
for (Rule ruleInDb : rules) {
try {
ObjectMapper mapper = new ObjectMapper();
if (ruleInDb.getCsId() == null || ruleInDb.getCsId().length() == 0) {
continue;
}
JsonNode ruleCsId = mapper.readTree(ruleInDb.getCsId());
JsonNode requestCsId = mapper.readTree(request.getCsId());
String ruleCsIdValue = ruleCsId.get("value").toString();
String requestCsIdValue = requestCsId.get("value").toString();
String ruleFlowStr = ruleCsIdValue.substring(1, ruleCsIdValue.length() - 1).replaceAll("\\\\", "");
String requestFlowStr = requestCsIdValue.substring(1, requestCsIdValue.length() - 1).replaceAll("\\\\", "");
JsonNode ruleFlow = mapper.readTree(ruleFlowStr);
JsonNode requestFlow = mapper.readTree(requestFlowStr);
// Converting string to json and compare the flow name.
if (ruleFlow.get("flow").toString().equals(requestFlow.get("flow").toString())) {
ruleInDb.setCsId(request.getCsId());
ruleDao.saveRule(ruleInDb);
LOGGER.info("Succeed to save rule. rule_id: {}, rule_cs_id: {}", ruleInDb.getId(), ruleInDb.getCsId());
}
} catch (NullPointerException e) {
LOGGER.error("Update context service ID NPE.", e);
} catch (JsonProcessingException e) {
LOGGER.error("Update context service ID exception.", e);
} catch (IOException e) {
LOGGER.error("Update context service ID exception.", e);
}
}
return new GeneralResponse<>("200", "{&MODIFY_RULE_SUCCESSFULLY}", null);
}
use of com.webank.wedatasphere.qualitis.project.entity.Project in project Qualitis by WeBankFinTech.
the class RuleServiceImpl method addRuleReal.
private GeneralResponse<RuleResponse> addRuleReal(AddRuleRequest request, String loginUser) throws UnExpectedRequestException, PermissionDeniedRequestException {
// Check Arguments
AddRuleRequest.checkRequest(request, false);
// Check existence of rule template
Template templateInDb = ruleTemplateService.checkRuleTemplate(request.getRuleTemplateId());
// Check datasource size
checkDataSourceNumber(templateInDb, request.getDatasource());
// Check if cluster supported
checkDataSourceClusterLimit(request.getDatasource());
// Check Arguments size
checkRuleArgumentSize(request.getTemplateArgumentRequests(), templateInDb);
// 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 existence of rule name
checkRuleName(request.getRuleName(), projectInDb, null);
RuleGroup ruleGroup;
if (request.getRuleGroupId() != null) {
ruleGroup = ruleGroupDao.findById(request.getRuleGroupId());
if (ruleGroup == null) {
throw new UnExpectedRequestException(String.format("Rule Group: %s {&DOES_NOT_EXIST}", request.getRuleGroupId()));
}
} else {
ruleGroup = ruleGroupDao.saveRuleGroup(new RuleGroup("Group_" + UUID.randomUUID().toString().replace("-", ""), projectInDb.getId()));
}
// Create and save rule
Rule newRule = new Rule();
setBasicInfo(newRule, ruleGroup, templateInDb, projectInDb, request, loginUser);
String csId = request.getCsId();
// For workflow node context.
boolean cs = false;
if (StringUtils.isNotBlank(csId)) {
newRule.setCsId(csId);
cs = true;
}
Rule savedRule = ruleDao.saveRule(newRule);
LOGGER.info("Succeed to save rule, rule_id: {}", savedRule.getId());
// Save alarm_config,ruleVariable and ruleDataSource
// Generate rule variable and save
LOGGER.info("Start to generate and save rule_variable.");
// For repeat fps table name when concurrent task is running.
addUuid(request.getDatasource());
List<RuleVariable> ruleVariables = autoAdaptArgumentAndGetRuleVariable(templateInDb, request.getDatasource(), request.getTemplateArgumentRequests(), savedRule);
List<RuleVariable> savedRuleVariables = ruleVariableService.saveRuleVariable(ruleVariables);
LOGGER.info("Succeed to save rule_variables, rule_variables: {}", savedRuleVariables);
List<AlarmConfig> savedAlarmConfigs = new ArrayList<>();
if (request.getAlarm()) {
savedAlarmConfigs = alarmConfigService.checkAndSaveAlarmVariable(request.getAlarmVariable(), savedRule);
LOGGER.info("Succeed to save alarm_configs, alarm_configs: {}", savedAlarmConfigs);
}
List<RuleDataSource> savedRuleDataSource = ruleDataSourceService.checkAndSaveRuleDataSource(request.getDatasource(), savedRule, cs, loginUser);
LOGGER.info("Succeed to save rule_dataSources, rule_dataSources: {}, context service: {}", savedRuleDataSource, cs);
savedRule.setRuleVariables(new HashSet<>(savedRuleVariables));
savedRule.setAlarmConfigs(new HashSet<>(savedAlarmConfigs));
savedRule.setRuleDataSources(new HashSet<>(savedRuleDataSource));
// Update rule count of datasource
ruleDataSourceService.updateRuleDataSourceCount(savedRule, 1);
RuleResponse response = new RuleResponse(savedRule);
LOGGER.info("Succeed to add rule, rule_id: {}", response.getRuleId());
return new GeneralResponse<>("200", "{&ADD_RULE_SUCCESSFULLY}", response);
}
use of com.webank.wedatasphere.qualitis.project.entity.Project in project Qualitis by WeBankFinTech.
the class RuleServiceImpl method deleteRule.
@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> deleteRule(DeleteRuleRequest request) throws UnExpectedRequestException, PermissionDeniedRequestException {
// Check Arguments
DeleteRuleRequest.checkRequest(request);
String loginUser = HttpUtils.getUserName(httpServletRequest);
// Check existence of rule
Rule ruleInDb = ruleDao.findById(request.getRuleId());
if (ruleInDb == null || !ruleInDb.getRuleType().equals(RuleTypeEnum.SINGLE_TEMPLATE_RULE.getCode())) {
throw new UnExpectedRequestException("Rule id [" + request.getRuleId() + "]) {&IS_NOT_A_RULE_WITH_TEMPLATE}");
}
// Check existence of project
Project projectInDb = projectService.checkProjectExistence(ruleInDb.getProject().getId(), loginUser);
// Check permissions of project
List<Integer> permissions = new ArrayList<>();
permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
projectService.checkProjectPermission(projectInDb, loginUser, permissions);
// Update rule count of datasource
ruleDataSourceService.updateRuleDataSourceCount(ruleInDb, -1);
return deleteRuleReal(ruleInDb);
}
Aggregations