use of com.webank.wedatasphere.qualitis.response.GeneralResponse 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);
}
use of com.webank.wedatasphere.qualitis.response.GeneralResponse in project Qualitis by WeBankFinTech.
the class RuleMetricServiceImpl method upload.
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> upload(InputStream fileInputStream, FormDataContentDisposition fileDisposition) throws UnExpectedRequestException, IOException {
// Check Arguments
if (fileInputStream == null || fileDisposition == null) {
throw new UnExpectedRequestException("{&FILE_CAN_NOT_BE_NULL_OR_EMPTY}");
}
// Check suffix name of file
String fileName = fileDisposition.getFileName();
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 + "]");
}
String userName = HttpUtils.getUserName(httpServletRequest);
LOGGER.info(userName + " start to upload rule metrics.");
ExcelRuleMetricListener listener = new ExcelRuleMetricListener();
List<ExcelRuleMetric> excelRuleMetrics = listener.getRuleMetricContent();
ExcelReader excelReader = new ExcelReader(fileInputStream, null, listener);
List<Sheet> sheets = excelReader.getSheets();
for (Sheet sheet : sheets) {
if (sheet.getSheetName().equals(ExcelSheetName.RULE_METRIC_NAME)) {
sheet.setClazz(ExcelRuleMetric.class);
sheet.setHeadLineMun(1);
excelReader.read(sheet);
}
}
if (CollectionUtils.isEmpty(excelRuleMetrics)) {
throw new UnExpectedRequestException("{&FILE_CAN_NOT_BE_EMPTY_OR_FILE_CAN_NOT_BE_RECOGNIZED}");
}
try {
for (ExcelRuleMetric excelRuleMetric : excelRuleMetrics) {
AddRuleMetricRequest addRuleMetricRequest = new AddRuleMetricRequest();
addRuleMetric(addRuleMetricRequest);
}
LOGGER.info("Succeed to add all rule metrics");
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
} finally {
fileInputStream.close();
}
return new GeneralResponse<>("200", "{&FAILED_TO_UPLOAD_RULE_METRIC}", null);
}
use of com.webank.wedatasphere.qualitis.response.GeneralResponse in project Qualitis by WeBankFinTech.
the class RuleMetricServiceImpl method queryRuleMetric.
@Override
public GeneralResponse<GetAllResponse<RuleMetricResponse>> queryRuleMetric(RuleMetricQueryRequest request) throws UnExpectedRequestException {
CommonChecker.checkObject(request, "Rule Metric query request");
if (StringUtils.isBlank(request.getSubSystemName())) {
request.setSubSystemName("");
}
if (StringUtils.isBlank(request.getRuleMetricName())) {
request.setRuleMetricName("%");
} else {
request.setRuleMetricName("%" + request.getRuleMetricName() + "%");
}
String userName = HttpUtils.getUserName(httpServletRequest);
LOGGER.info("Start to get all rule metric, page request: [{}], user: [{}]", request.toString(), userName);
User loginUser = userDao.findByUsername(userName);
List<UserRole> userRoles = userRoleDao.findByUser(loginUser);
Integer roleType = roleService.getRoleType(userRoles);
List<RuleMetric> ruleMetrics = new ArrayList<>();
long total = 0;
if (roleType.equals(RoleDefaultTypeEnum.ADMIN.getCode())) {
LOGGER.info("SYS_ADMIN will get all rule metrics in query.");
ruleMetrics.addAll(ruleMetricDao.queryAllRuleMetrics(request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable(), request.getPage(), request.getSize()));
total = ruleMetricDao.countQueryAllRuleMetrics(request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable());
} else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
LOGGER.info("DEPARTMENT_ADMIN will get rule metrics of all management departments and all projectors.");
List<Department> departments = new ArrayList<>();
for (UserRole temp : userRoles) {
Department department = temp.getRole().getDepartment();
if (department != null) {
departments.add(department);
}
}
ruleMetrics.addAll(ruleMetricDao.queryRuleMetrics(RuleMetricLevelEnum.DEFAULT_METRIC.getCode(), departments, null, request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable(), request.getPage(), request.getSize()));
total = ruleMetricDao.countQueryRuleMetrics(RuleMetricLevelEnum.DEFAULT_METRIC.getCode(), departments, null, request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable());
} else {
LOGGER.info("PROJECTOR will get rule metrics of department and own.");
List<Department> departments = new ArrayList<>();
departments.add(loginUser.getDepartment());
ruleMetrics.addAll(ruleMetricDao.queryRuleMetrics(RuleMetricLevelEnum.DEFAULT_METRIC.getCode(), departments, loginUser, request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable(), request.getPage(), request.getSize()));
total = ruleMetricDao.countQueryRuleMetrics(RuleMetricLevelEnum.DEFAULT_METRIC.getCode(), departments, loginUser, request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable());
}
GetAllResponse<RuleMetricResponse> response = new GetAllResponse<>();
List<RuleMetricResponse> ruleMetricResponses = new ArrayList<>();
for (RuleMetric ruleMetric : ruleMetrics) {
ruleMetricResponses.add(new RuleMetricResponse(ruleMetric));
}
response.setData(ruleMetricResponses);
response.setTotal(total);
return new GeneralResponse<>("200", "{&RULE_METRIC_QUERY_SUCCESS}", response);
}
use of com.webank.wedatasphere.qualitis.response.GeneralResponse in project Qualitis by WeBankFinTech.
the class MetaDataController method getTablesByDataSource.
@GET
@Path("data_source/tables")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public GeneralResponse<GetAllResponse<TableInfoDetail>> getTablesByDataSource(@QueryParam("clusterName") String clusterName, @QueryParam("proxyUser") String proxyUser, @QueryParam("dataSourceId") Long dataSourceId, @QueryParam("dbName") String dbName) {
try {
if (dataSourceId == null || StringUtils.isBlank(dbName) || StringUtils.isBlank(clusterName)) {
throw new UnExpectedRequestException("Request " + "{&CAN_NOT_BE_NULL_OR_EMPTY}");
}
Map response = metaDataService.getTablesByDataSource(clusterName, proxyUser, dataSourceId, dbName);
GetAllResponse allResponse = new GetAllResponse();
List<String> tables = (List<String>) response.get("tables");
List<TableInfoDetail> tableInfoDetails = new ArrayList<>(CollectionUtils.isEmpty(tables) ? 0 : tables.size());
for (String table : tables) {
TableInfoDetail tableInfoDetail = new TableInfoDetail(table);
tableInfoDetails.add(tableInfoDetail);
}
allResponse.setTotal(CollectionUtils.isEmpty(tableInfoDetails) ? 0 : tableInfoDetails.size());
allResponse.setData(tableInfoDetails);
return new GeneralResponse<>("200", "{&GET_TABLE_SUCCESSFULLY}", allResponse);
} catch (MetaDataAcquireFailedException e) {
LOGGER.error(e.getMessage(), e);
return new GeneralResponse<>("500", e.getMessage(), null);
} catch (UnExpectedRequestException e) {
LOGGER.error(e.getMessage(), e);
return new GeneralResponse<>("500", e.getMessage(), null);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
return new GeneralResponse<>("500", "{&FAILED_TO_GET_TABLE_BY_DATABASE}", null);
}
}
use of com.webank.wedatasphere.qualitis.response.GeneralResponse in project Qualitis by WeBankFinTech.
the class PermissionServiceImpl method addPermission.
@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<PermissionResponse> addPermission(AddPermissionRequest request) throws UnExpectedRequestException {
// Check Arguments
AddPermissionRequest.checkRequest(request);
String method = request.getMethod().trim();
String url = request.getUrl().trim();
// Find permission by method and url
Permission permissionInDb = permissionDao.findByMethodAndUrl(method, url);
if (permissionInDb != null) {
throw new UnExpectedRequestException("{&METHOD_AND_URL_ALREADY_EXIST}, request: " + request);
}
Permission newPermission = new Permission();
newPermission.setMethod(method);
newPermission.setUrl(url);
Permission savedPermission = permissionDao.savePermission(newPermission);
LOGGER.info("Succeed to add permission, id: {}, method: {}, url: {}, current_user: {}", savedPermission.getId(), method, url, HttpUtils.getUserName(httpServletRequest));
return new GeneralResponse<>("200", "{&ADD_PERMISSION_SUCCESSFULLY}", new PermissionResponse(savedPermission));
}
Aggregations