Search in sources :

Example 96 with UnExpectedRequestException

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);
}
Also used : RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) Department(com.webank.wedatasphere.qualitis.entity.Department) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) User(com.webank.wedatasphere.qualitis.entity.User) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole)

Example 97 with UnExpectedRequestException

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());
    }
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ZipFile(net.lingala.zip4j.ZipFile) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipException(net.lingala.zip4j.exception.ZipException) IOException(java.io.IOException) FileHeader(net.lingala.zip4j.model.FileHeader) ZipFile(net.lingala.zip4j.ZipFile) File(java.io.File)

Example 98 with UnExpectedRequestException

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);
}
Also used : ExcelMultiTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelMultiTemplateRuleByProject) ExcelCustomRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelCustomRuleByProject) ExcelTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject) Project(com.webank.wedatasphere.qualitis.project.entity.Project) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 99 with UnExpectedRequestException

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());
        }
    }
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) RuleQueryProject(com.webank.wedatasphere.qualitis.query.response.RuleQueryProject) Project(com.webank.wedatasphere.qualitis.project.entity.Project) ArrayList(java.util.ArrayList) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) RuleQueryRule(com.webank.wedatasphere.qualitis.query.response.RuleQueryRule) Transactional(org.springframework.transaction.annotation.Transactional)

Example 100 with UnExpectedRequestException

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;
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) User(com.webank.wedatasphere.qualitis.entity.User) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) ColumnInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.column.ColumnInfoDetail) GetUserTableByCsIdRequest(com.webank.wedatasphere.qualitis.metadata.request.GetUserTableByCsIdRequest) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) MetaDataAcquireFailedException(com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) CsTableInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail) MetaDataAcquireFailedException(com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) RuleQueryRule(com.webank.wedatasphere.qualitis.query.response.RuleQueryRule) GetUserColumnByCsRequest(com.webank.wedatasphere.qualitis.metadata.request.GetUserColumnByCsRequest)

Aggregations

UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)150 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)97 ArrayList (java.util.ArrayList)56 Transactional (org.springframework.transaction.annotation.Transactional)54 Project (com.webank.wedatasphere.qualitis.project.entity.Project)33 User (com.webank.wedatasphere.qualitis.entity.User)32 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)32 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)25 Map (java.util.Map)24 ClusterInfo (com.webank.wedatasphere.qualitis.entity.ClusterInfo)22 List (java.util.List)22 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)19 IOException (java.io.IOException)19 MetaDataAcquireFailedException (com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException)17 Date (java.util.Date)17 HashMap (java.util.HashMap)17 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)15 RuleGroup (com.webank.wedatasphere.qualitis.rule.entity.RuleGroup)15 Collectors (java.util.stream.Collectors)15 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)14