use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class MetaDataServiceImpl method getDbsByDataSource.
@Override
public Map getDbsByDataSource(String clusterName, String proxyUser, Long dataSourceId) throws UnExpectedRequestException, MetaDataAcquireFailedException {
// Get login user
String userName = HttpUtils.getUserName(httpServletRequest);
if (StringUtils.isNotBlank(proxyUser)) {
userName = proxyUser;
}
GeneralResponse<Map> dataSourceConnectParams = metaDataClient.getDataSourceConnectParams(clusterName, userName, dataSourceId, null);
Map connectParamsReal = (Map) dataSourceConnectParams.getData().get("connectParams");
if (connectParamsReal.size() == 0) {
throw new UnExpectedRequestException("{&THE_DATASOURCE_IS_NOT_DEPLOYED}");
}
return metaDataClient.getDbsByDataSource(clusterName, userName, dataSourceId);
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class MetaDataServiceImpl method filterTablesWithBlackList.
private List<String> filterTablesWithBlackList(List<String> blackList, List<String> sourceTableName) throws UnExpectedRequestException {
for (String black : blackList) {
int filterType = Integer.parseInt(black.split(SpecCharEnum.COLON.getValue())[0]);
if (BlackListFilterTypeEnum.BEGIN_WITH.getCode().intValue() == filterType) {
String blackStr = black.split(SpecCharEnum.COLON.getValue())[1];
sourceTableName = sourceTableName.stream().filter(tableName -> !tableName.startsWith(blackStr)).collect(Collectors.toList());
} else if (BlackListFilterTypeEnum.CONTAINS.getCode().intValue() == filterType) {
String blackStr = black.split(SpecCharEnum.COLON.getValue())[1];
sourceTableName = sourceTableName.stream().filter(tableName -> !tableName.contains(blackStr)).collect(Collectors.toList());
} else if (BlackListFilterTypeEnum.END_WITH.getCode().intValue() == filterType) {
String blackStr = black.split(SpecCharEnum.COLON.getValue())[1];
sourceTableName = sourceTableName.stream().filter(tableName -> !tableName.endsWith(blackStr)).collect(Collectors.toList());
} else if (BlackListFilterTypeEnum.SAME_TABLE.getCode().intValue() == filterType) {
sourceTableName.clear();
} else if (BlackListFilterTypeEnum.REG_TABLE.getCode().intValue() == filterType) {
String blackStr = black.split(SpecCharEnum.COLON.getValue())[1];
Pattern blackPattern = Pattern.compile(blackStr);
sourceTableName = sourceTableName.stream().filter(tableName -> !blackPattern.matcher(tableName).matches()).collect(Collectors.toList());
} else {
throw new UnExpectedRequestException("Black list filter filter type error.");
}
}
return sourceTableName;
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class MetaDataServiceImpl method addMultiDbRules.
@Override
@Transactional(rollbackFor = { Exception.class, UnExpectedRequestException.class })
public String addMultiDbRules(MulDbRequest request) throws UnExpectedRequestException, MetaDataAcquireFailedException {
MulDbRequest.checkRequst(request);
String loginUser = HttpUtils.getUserName(httpServletRequest);
LOGGER.info("Start to get all tables with source database and target database.");
List<String> sourceTableName = new ArrayList<>();
List<String> targetTableName = new ArrayList<>();
if (request.getSourceLinkisDataSourceId() == null) {
GetUserTableByDbIdRequest sourceRequest = new GetUserTableByDbIdRequest(0, Integer.MAX_VALUE, request.getClusterName(), request.getSourceDb());
sourceRequest.setProxyUser(request.getProxyUser());
GeneralResponse<GetAllResponse<TableInfoDetail>> sourceTableInfos = getUserTableByDbId(sourceRequest);
if (sourceTableInfos.getData().getTotal() <= 0) {
throw new UnExpectedRequestException("Source database has no tables.");
}
sourceTableName.addAll(sourceTableInfos.getData().getData().stream().map(TableInfoDetail::getTableName).collect(Collectors.toList()));
} else {
Map response = getTablesByDataSource(request.getClusterName(), request.getProxyUser(), request.getSourceLinkisDataSourceId(), request.getSourceDb());
List<String> tables = (List<String>) response.get("tables");
for (String table : tables) {
sourceTableName.add(table);
}
}
if (request.getTargetLinkisDataSourceId() == null) {
GetUserTableByDbIdRequest targetRequest = new GetUserTableByDbIdRequest(0, Integer.MAX_VALUE, request.getClusterName(), request.getTargetDb());
targetRequest.setProxyUser(request.getProxyUser());
GeneralResponse<GetAllResponse<TableInfoDetail>> targetTableInfos = getUserTableByDbId(targetRequest);
if (targetTableInfos.getData().getTotal() <= 0) {
throw new UnExpectedRequestException("Target database has no tables.");
}
targetTableName.addAll(targetTableInfos.getData().getData().stream().map(TableInfoDetail::getTableName).collect(Collectors.toList()));
} else {
Map response = getTablesByDataSource(request.getClusterName(), request.getProxyUser(), request.getTargetLinkisDataSourceId(), request.getTargetDb());
List<String> tables = (List<String>) response.get("tables");
for (String table : tables) {
targetTableName.add(table);
}
}
// Collect the table with the same name that exists in the source database and the target database.
sourceTableName.retainAll(targetTableName);
Set<String> ruleFailedTable = new HashSet<>();
Long projectId = request.getProjectId();
List<String> ruleTables = new ArrayList<>(ruleDataSourceDao.findByProjectId(projectId).stream().map(RuleDataSource::getTableName).distinct().collect(Collectors.toList()));
// Filter tables for which rules have been created.
sourceTableName = sourceTableName.stream().filter(tableName -> !ruleTables.contains(tableName)).collect(Collectors.toList());
// Rule name index starts with the last created rule.
int ruleIndex = ruleDao.findByProject(projectDao.findById(projectId)).size();
// Filter tables with black list.
sourceTableName = filterTablesWithBlackList(request.getBlackList(), sourceTableName);
// White list.
List<String> whiteList = request.getWhiteList();
// Filter special table to compare with accuracy template.
List<FilterRequest> filterRequests = request.getFilterRequests();
List<FilterRequest> sameTableFilterRequests = filterRequests.stream().filter(filterRequest -> filterRequest.getSourceTable().equals(filterRequest.getTargetTable())).collect(Collectors.toList());
List<String> filterSameTableName = sameTableFilterRequests.stream().map(FilterRequest::getSourceTable).collect(Collectors.toList());
sourceTableName.removeAll(filterSameTableName);
filterRequests.removeAll(sameTableFilterRequests);
List<String> filterDiffTableName = filterRequests.stream().map(filterRequest -> filterRequest.getSourceTable() + SpecCharEnum.COLON.getValue() + filterRequest.getTargetTable()).collect(Collectors.toList());
whiteList.removeAll(filterDiffTableName);
filterRequests.addAll(sameTableFilterRequests);
// Check flag: create rules in a loop, and only do permission verification for the first time.
boolean check = true;
int sourceTableSize = sourceTableName.size();
int filterRequestsSize = filterRequests.size();
int total = ruleIndex + sourceTableSize + whiteList.size() + filterRequestsSize;
int i = ruleIndex;
for (; i < ruleIndex + sourceTableSize; i++) {
String currentSameTable = sourceTableName.get(i - ruleIndex);
try {
AddMultiSourceRuleRequest addMultiSourceRuleRequest = constructRequest(request, null, currentSameTable, currentSameTable, i, total, loginUser);
LOGGER.info("Start to add {}th multi source rule.", i);
if (i > 0) {
check = false;
}
multiSourceRuleService.addMultiSourceRule(addMultiSourceRuleRequest, check);
} catch (Exception e) {
LOGGER.error("One of rule failed to add, rule index:[{}], table name: [{}], exception:{}", i, currentSameTable, e.getMessage());
ruleFailedTable.add(currentSameTable);
}
LOGGER.info("Finish to add {}th multi source rule.", i);
}
// With white list.
withWhiteListAndFilterRequest(i, ruleIndex, sourceTableSize, total, whiteList, filterRequests, request, loginUser, check);
// Record the table that failed to create a rule.
saveRuleFailedTableInLabel(ruleFailedTable, projectId);
// projectEventService.record(projectId, loginUser, "create multi-source rule with dbs", "Success: " + (total - ruleFailedTable.size()) + "; Failed: " + ruleFailedTable.size(), EventTypeEnum.MODIFY_PROJECT.getCode());
return request.getSourceDb() + " vs " + request.getSourceDb() + ": " + "{&CONTINUE_CREATE_RULES}";
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class RuleMetricServiceImpl method modifyRuleMetricReal.
private GeneralResponse<RuleMetricResponse> modifyRuleMetricReal(ModifyRuleMetricRequest request, String userName) throws UnExpectedRequestException, PermissionDeniedRequestException {
if (request == null) {
throw new UnExpectedRequestException("{&REQUEST_CAN_NOT_BE_NULL}");
}
// Check rule metric existence.
RuleMetric ruleMetricInDb = ruleMetricDao.findById(request.getId());
if (ruleMetricInDb == null) {
throw new UnExpectedRequestException("Rule Metric [ID=" + request.getId() + "] {&DOES_NOT_EXIST}");
}
LOGGER.info("Start to modify rule metric, modify request: [{}], user: [{}]", request.toString(), userName);
if (!ruleMetricInDb.getName().equals(request.getName())) {
checkDuplicateName(request.getName());
}
if (!ruleMetricInDb.getEnCode().equals(request.getEnCode())) {
checkDuplicateCode(request.getEnCode());
}
User loginUser = userDao.findByUsername(userName);
List<UserRole> userRoles = userRoleDao.findByUser(loginUser);
Integer roleType = roleService.getRoleType(userRoles);
if (roleType.equals(RoleDefaultTypeEnum.ADMIN.getCode())) {
LOGGER.info("First level(created by SYS_ADMIN) indicator will be modified soon.");
} else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
LOGGER.info("Second level(created by DEPARTMENT_ADMIN) indicator will be modified soon.");
if (ruleMetricInDb.getLevel().equals(RuleMetricLevelEnum.DEFAULT_METRIC.getCode())) {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
List<Department> managedDepartment = new ArrayList<>();
for (UserRole userRole : userRoles) {
Department department = userRole.getRole().getDepartment();
if (department != null) {
managedDepartment.add(department);
}
}
RuleMetricDepartmentUser ruleMetricDepartmentUser = ruleMetricDepartmentUserDao.findByRuleMetric(ruleMetricInDb);
if (ruleMetricDepartmentUser != null && managedDepartment.contains(ruleMetricDepartmentUser.getDepartment())) {
LOGGER.info("Rule metric[{}]", ruleMetricInDb.toString());
} else {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
} else {
LOGGER.info("Third level(created by PROJECTOR) indicator will be modified soon.");
if (!ruleMetricInDb.getLevel().equals(RuleMetricLevelEnum.PERSONAL_METRIC.getCode()) || !ruleMetricInDb.getCreateUser().equals(loginUser.getUserName())) {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
}
Integer bussCode = request.getBussCode();
ruleMetricInDb.setName(request.getName());
ruleMetricInDb.setCnName(request.getCnName());
ruleMetricInDb.setMetricDesc(request.getDesc());
ruleMetricInDb.setBussCode(bussCode);
if (RuleMetricBussCodeEnum.SUBSYSTEM.getCode().equals(bussCode)) {
ruleMetricInDb.setSubSystemName(request.getSubSystemName());
ruleMetricInDb.setFullCnName(request.getFullCnName());
// Empty them
ruleMetricInDb.setBussCustom("");
ruleMetricInDb.setProductName("");
} else if (RuleMetricBussCodeEnum.PRODUCT.getCode().equals(bussCode)) {
ruleMetricInDb.setProductName(request.getProductName());
ruleMetricInDb.setSubSystemName("");
ruleMetricInDb.setFullCnName("");
ruleMetricInDb.setBussCustom("");
} else if (RuleMetricBussCodeEnum.CUSTOM.getCode().equals(bussCode)) {
ruleMetricInDb.setBussCustom(request.getBussCustom());
ruleMetricInDb.setProductName("");
ruleMetricInDb.setSubSystemName("");
ruleMetricInDb.setFullCnName("");
}
ruleMetricInDb.setModifyUser(userName);
ruleMetricInDb.setModifyTime(ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date()));
ruleMetricInDb.setType(request.getType());
ruleMetricInDb.setEnCode(request.getEnCode());
ruleMetricInDb.setFrequency(request.getFrequency());
ruleMetricInDb.setDepartmentName(request.getDepartmentName());
ruleMetricInDb.setDevDepartmentName(request.getDevDepartmentName());
ruleMetricInDb.setOpsDepartmentName(request.getOpsDepartmentName());
ruleMetricInDb.setAvailable(request.getAvailable());
RuleMetricResponse response = new RuleMetricResponse(ruleMetricDao.add(ruleMetricInDb));
return new GeneralResponse<>("200", "{&MODIFY_RULE_METRIC_SUCCESSFULLY}", response);
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class RuleMetricServiceImpl method getRuleMetricDetail.
@Override
public GeneralResponse<RuleMetricResponse> getRuleMetricDetail(long id) throws UnExpectedRequestException, PermissionDeniedRequestException {
if (id <= 0) {
throw new UnExpectedRequestException("{&REQUEST_CAN_NOT_BE_NULL}");
}
// Check rule metric existence.
RuleMetric ruleMetricInDb = ruleMetricDao.findById(id);
if (ruleMetricInDb == null) {
throw new UnExpectedRequestException("Rule Metric ID [" + id + "] {&DOES_NOT_EXIST}");
}
String userName = HttpUtils.getUserName(httpServletRequest);
LOGGER.info("Start to get rule metric, rule metric ID: [{}], user: [{}]", id, userName);
User loginUser = userDao.findByUsername(userName);
List<UserRole> userRoles = userRoleDao.findByUser(loginUser);
Integer roleType = roleService.getRoleType(userRoles);
RuleMetricDepartmentUser ruleMetricDepartmentUser = ruleMetricDepartmentUserDao.findByRuleMetric(ruleMetricInDb);
if (roleType.equals(RoleDefaultTypeEnum.ADMIN.getCode())) {
LOGGER.info("SYS_ADMIN will get rule metric.");
} else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
LOGGER.info("DEPARTMENT_ADMIN will get rule metric.");
List<Department> managedDepartment = new ArrayList<>();
for (UserRole userRole : userRoles) {
Department department = userRole.getRole().getDepartment();
if (department != null) {
managedDepartment.add(department);
}
}
if (ruleMetricDepartmentUser.getDepartment() != null) {
List<Department> res = managedDepartment.stream().filter(department -> department.getId() == ruleMetricDepartmentUser.getDepartment().getId()).collect(Collectors.toList());
if (res.size() > 0) {
LOGGER.info("Rule metric[{}] comes from department: {}", ruleMetricInDb.toString(), ruleMetricDepartmentUser.getDepartment().getName());
} else {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
} else if (ruleMetricInDb.getLevel().equals(RuleMetricLevelEnum.DEFAULT_METRIC.getCode())) {
LOGGER.info("DEPARTMENT_ADMIN will get first level rule metric.");
} else {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
} else {
LOGGER.info("PROJECTOR will get rule metric.");
Department department = loginUser.getDepartment();
if (ruleMetricInDb.getLevel().equals(RuleMetricLevelEnum.DEPARTMENT_METRIC) && !department.equals(ruleMetricDepartmentUser.getDepartment())) {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
if (ruleMetricInDb.getLevel().equals(RuleMetricLevelEnum.PERSONAL_METRIC) && !loginUser.equals(ruleMetricDepartmentUser.getUser())) {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
}
RuleMetricResponse ruleMetricResponse = new RuleMetricResponse(ruleMetricInDb);
return new GeneralResponse<>("200", "{&GET_RULE_METRIC_SUCCESSFULLY}", ruleMetricResponse);
}
Aggregations