Search in sources :

Example 41 with GeneralResponse

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);
}
Also used : ExcelRuleMetric(com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric) RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) User(com.webank.wedatasphere.qualitis.entity.User) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) RuleMetricResponse(com.webank.wedatasphere.qualitis.response.RuleMetricResponse) Department(com.webank.wedatasphere.qualitis.entity.Department) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) List(java.util.List) ArrayList(java.util.ArrayList)

Example 42 with GeneralResponse

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);
}
Also used : ExcelRuleMetric(com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric) ExcelReader(com.alibaba.excel.ExcelReader) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ExcelRuleMetricListener(com.webank.wedatasphere.qualitis.excel.ExcelRuleMetricListener) AddRuleMetricRequest(com.webank.wedatasphere.qualitis.request.AddRuleMetricRequest) Sheet(com.alibaba.excel.metadata.Sheet) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) IOException(java.io.IOException) WriteExcelException(com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 43 with GeneralResponse

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);
}
Also used : ExcelRuleMetric(com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric) RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) User(com.webank.wedatasphere.qualitis.entity.User) ArrayList(java.util.ArrayList) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) RuleMetricResponse(com.webank.wedatasphere.qualitis.response.RuleMetricResponse) Department(com.webank.wedatasphere.qualitis.entity.Department) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) List(java.util.List) ArrayList(java.util.ArrayList) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse)

Example 44 with GeneralResponse

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);
    }
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ArrayList(java.util.ArrayList) ResourceAccessException(org.springframework.web.client.ResourceAccessException) MetaDataAcquireFailedException(com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) MetaDataAcquireFailedException(com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException) ArrayList(java.util.ArrayList) List(java.util.List) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse) Map(java.util.Map) CsTableInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail) TableInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.table.TableInfoDetail) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) GET(javax.ws.rs.GET)

Example 45 with GeneralResponse

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));
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) UserSpecPermission(com.webank.wedatasphere.qualitis.entity.UserSpecPermission) Permission(com.webank.wedatasphere.qualitis.entity.Permission) RolePermission(com.webank.wedatasphere.qualitis.entity.RolePermission) PermissionResponse(com.webank.wedatasphere.qualitis.response.PermissionResponse) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)146 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)96 Transactional (org.springframework.transaction.annotation.Transactional)49 ArrayList (java.util.ArrayList)48 User (com.webank.wedatasphere.qualitis.entity.User)40 GetAllResponse (com.webank.wedatasphere.qualitis.response.GetAllResponse)30 Project (com.webank.wedatasphere.qualitis.project.entity.Project)28 Map (java.util.Map)27 ClusterInfo (com.webank.wedatasphere.qualitis.entity.ClusterInfo)26 MetaDataAcquireFailedException (com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException)24 List (java.util.List)22 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)21 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)17 IOException (java.io.IOException)16 UserRole (com.webank.wedatasphere.qualitis.entity.UserRole)15 Date (java.util.Date)15 Task (com.webank.wedatasphere.qualitis.entity.Task)14 JSONObject (org.json.JSONObject)14 HttpEntity (org.springframework.http.HttpEntity)14 HttpHeaders (org.springframework.http.HttpHeaders)14