use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class ProjectBatchServiceImpl method addRuleMetric.
private void addRuleMetric(ExcelRuleMetric excelRuleMetric, String userName) throws UnExpectedRequestException {
RuleMetric ruleMetricInDb = ruleMetricDao.findByEnCode(excelRuleMetric.getEnCode());
if (ruleMetricInDb != null) {
throw new UnExpectedRequestException("En code + [" + ruleMetricInDb.getEnCode() + "] + of metric {&ALREADY_EXIST}");
}
LOGGER.info("Start to add rule metric, user: [{}]", userName);
User loginUser = userDao.findByUsername(userName);
List<UserRole> userRoles = userRoleDao.findByUser(loginUser);
Integer roleType = roleService.getRoleType(userRoles);
RuleMetric newRuleMetric = new RuleMetric();
newRuleMetric.setCreateUser(userName);
newRuleMetric.setName(excelRuleMetric.getName());
newRuleMetric.setCnName(excelRuleMetric.getChName());
newRuleMetric.setEnCode(excelRuleMetric.getEnCode());
newRuleMetric.setMetricDesc(excelRuleMetric.getMetricDesc());
newRuleMetric.setCreateTime(ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date()));
RuleMetric savedRuleMetric = ruleMetricDao.add(newRuleMetric);
if (roleType.equals(RoleDefaultTypeEnum.ADMIN.getCode())) {
LOGGER.info("First level(created by SYS_ADMIN) indicator will be created soon.");
savedRuleMetric.setLevel(RuleMetricLevelEnum.DEFAULT_METRIC.getCode());
} else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
LOGGER.info("Second level(created by DEPARTMENT_ADMIN) indicator will be created soon.");
savedRuleMetric.setLevel(RuleMetricLevelEnum.DEPARTMENT_METRIC.getCode());
for (UserRole temp : userRoles) {
Department department = temp.getRole().getDepartment();
if (department != null) {
RuleMetricDepartmentUser ruleMetricDepartmentUser = new RuleMetricDepartmentUser();
ruleMetricDepartmentUser.setDepartment(department);
ruleMetricDepartmentUser.setRuleMetric(savedRuleMetric);
ruleMetricDepartmentUserDao.add(ruleMetricDepartmentUser);
LOGGER.info("Succeed to save rule metric department user.");
}
}
} else {
LOGGER.info("Third level(created by PROJECTOR) indicator will be created soon.");
savedRuleMetric.setLevel(RuleMetricLevelEnum.PERSONAL_METRIC.getCode());
RuleMetricDepartmentUser ruleMetricDepartmentUser = new RuleMetricDepartmentUser();
ruleMetricDepartmentUser.setDepartment(loginUser.getDepartment());
ruleMetricDepartmentUser.setRuleMetric(savedRuleMetric);
ruleMetricDepartmentUser.setUser(loginUser);
ruleMetricDepartmentUserDao.add(ruleMetricDepartmentUser);
LOGGER.info("Succeed to save rule metric department user.");
}
savedRuleMetric.setBussCode(Integer.parseInt(excelRuleMetric.getDimension()));
savedRuleMetric.setCreateUser(userName);
savedRuleMetric.setFrequency(Integer.parseInt(excelRuleMetric.getFrequency()));
savedRuleMetric.setDevDepartmentName(excelRuleMetric.getDevDepartmentName());
savedRuleMetric.setOpsDepartmentName(excelRuleMetric.getOpsDepartmentName());
savedRuleMetric.setDepartmentName(excelRuleMetric.getDepartmentName());
savedRuleMetric.setSubSystemName(excelRuleMetric.getSubSystemName());
savedRuleMetric.setProductName(excelRuleMetric.getProductName());
savedRuleMetric.setFullCnName(excelRuleMetric.getFullCnName());
savedRuleMetric.setBussCustom(excelRuleMetric.getBussCustom());
savedRuleMetric.setAvailable(excelRuleMetric.getAvailable());
savedRuleMetric.setType(excelRuleMetric.getType());
ruleMetricDao.add(savedRuleMetric);
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class ProjectBatchServiceImpl method unzipAndGet.
private void unzipAndGet(File zipFile, List<File> extractedFileList, String destUnzipPath, File destDir, InputStream fileInputStream) throws UnExpectedRequestException {
OutputStream out = null;
try {
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(zipFile);
while ((read = fileInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
throw new UnExpectedRequestException("{&FAILED_TO_UPLOAD_PROJECTS}, caused by: " + e.getMessage());
} finally {
try {
out.close();
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
zipFile.delete();
throw new UnExpectedRequestException("{&FAILED_TO_UPLOAD_PROJECTS}, caused by: " + e.getMessage());
}
}
// First, create a. zip file that points to the disk.
ZipFile zFile = new ZipFile(zipFile);
try {
// Extract the file to the unzip directory
zFile.extractAll(destUnzipPath);
List<FileHeader> headerList = zFile.getFileHeaders();
for (FileHeader fileHeader : headerList) {
if (!fileHeader.isDirectory()) {
extractedFileList.add(new File(destDir, fileHeader.getFileName()));
}
}
} catch (ZipException e) {
LOGGER.error(e.getMessage(), e);
zipFile.delete();
destDir.delete();
throw new UnExpectedRequestException("{&FAILED_TO_UPLOAD_PROJECTS}, caused by: " + e.getMessage());
}
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class ProjectBatchServiceImpl method downloadProjects.
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> downloadProjects(DownloadProjectRequest request, HttpServletResponse response) throws UnExpectedRequestException, WriteExcelException, IOException, PermissionDeniedRequestException {
// Check Arguments
DownloadProjectRequest.checkRequest(request);
String loginUser = HttpUtils.getUserName(httpServletRequest);
List<Project> projectsInDb = new ArrayList<>();
for (Long projectId : request.getProjectId()) {
Project projectInDb = projectDao.findById(projectId);
if (projectInDb == null) {
throw new UnExpectedRequestException("{&PROJECT_ID} : [" + projectId + "] {&DOES_NOT_EXIST}");
}
// Check permissions of project
List<Integer> permissions = new ArrayList<>();
permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
projectService.checkProjectPermission(projectInDb, loginUser, permissions);
projectsInDb.add(projectInDb);
}
// Write project and rules
return writeProjectAndRules(projectsInDb, response);
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class RuleQueryServiceImpl method deleteRules.
@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public void deleteRules(RulesDeleteRequest request) throws UnExpectedRequestException, PermissionDeniedRequestException {
for (long ruleId : request.getRuleIdList()) {
Rule ruleInDb = ruleDao.findById(ruleId);
String loginUser = HttpUtils.getUserName(httpServletRequest);
// Check existence of rule
if (ruleInDb == null) {
throw new UnExpectedRequestException("Rule id [" + ruleId + "]) {&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);
// Update rule count of datasource
ruleDataSourceService.updateRuleDataSourceCount(ruleInDb, -1);
// Delete rule
ruleDao.deleteRule(ruleInDb);
LOGGER.info("Succeed to delete rule. rule_id: {}", ruleInDb.getId());
if (ruleInDb.getRuleType().equals(RuleTypeEnum.CUSTOM_RULE.getCode())) {
// Delete template of custom rule
ruleTemplateService.deleteCustomTemplate(ruleInDb.getTemplate());
LOGGER.info("Succeed to delete custom rule. rule_id: {}", ruleInDb.getId());
}
}
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class RuleQueryServiceImpl method getColumnsByTableName.
@Override
public List<ColumnInfoDetail> getColumnsByTableName(String clusterName, Long datasourceId, String dbName, String tableName, String userName) throws UnExpectedRequestException, MetaDataAcquireFailedException {
List<ColumnInfoDetail> result = null;
User user = userDao.findByUsername(userName);
if (user == null) {
throw new UnExpectedRequestException(String.format("{&FAILED_TO_FIND_USER} %s", userName));
}
try {
if (StringUtils.isBlank(dbName)) {
List<Rule> rules = ruleDataSourceDao.findRuleByDataSource(clusterName, dbName, tableName, "%", userName);
if (rules == null || rules.isEmpty()) {
LOGGER.info("No rules for this datasource!");
return null;
}
LOGGER.info("Rules related with context service table: [{}] are: {}", tableName, rules.toArray());
String csId = rules.get(0).getCsId();
String nodeName = rules.get(0).getName();
GetUserTableByCsIdRequest getUserTableByCsIdRequest = new GetUserTableByCsIdRequest();
getUserTableByCsIdRequest.setClusterName(clusterName);
getUserTableByCsIdRequest.setLoginUser(userName);
getUserTableByCsIdRequest.setNodeName(nodeName);
getUserTableByCsIdRequest.setCsId(csId);
DataInfo<CsTableInfoDetail> csTableInfoDetails = metaDataClient.getTableByCsId(getUserTableByCsIdRequest);
if (csTableInfoDetails.getTotalCount() == 0) {
LOGGER.info("Cannot find context service table with existed rules!");
return null;
}
for (CsTableInfoDetail csTableInfoDetail : csTableInfoDetails.getContent()) {
if (csTableInfoDetail.getTableName().equals(tableName)) {
GetUserColumnByCsRequest getUserColumnByCsRequest = new GetUserColumnByCsRequest();
getUserColumnByCsRequest.setClusterName(clusterName);
getUserColumnByCsRequest.setContextKey(csTableInfoDetail.getContextKey());
getUserColumnByCsRequest.setCsId(csId);
getUserColumnByCsRequest.setLoginUser(userName);
result = metaDataClient.getColumnByCsId(getUserColumnByCsRequest).getContent();
setRuleCount(result, user.getId(), clusterName, dbName, tableName);
break;
} else {
continue;
}
}
} else {
if (datasourceId == null) {
result = metaDataClient.getColumnInfo(clusterName, dbName, tableName, userName);
} else {
result = metaDataClient.getColumnsByDataSource(clusterName, userName, datasourceId, dbName, tableName).getContent();
}
setRuleCount(result, user.getId(), clusterName, dbName, tableName);
}
LOGGER.info("Datasource table number of columns: {}", result == null ? 0 : result.size());
} catch (MetaDataAcquireFailedException e) {
LOGGER.error("Datasource colums info {&DOES_NOT_EXIST}. Exception: {}", e);
throw new MetaDataAcquireFailedException("Datasource colums info {&DOES_NOT_EXIST}. Exception: " + e.getMessage());
} catch (UnExpectedRequestException e) {
LOGGER.error("{&QUERY_PARAM_HAS_ERROR}. Exception: {}", e);
throw new UnExpectedRequestException("{&QUERY_PARAM_HAS_ERROR}. Exception: " + e.getMessage());
} catch (Exception e) {
LOGGER.error("exception: {}", e);
}
return result;
}
Aggregations