use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.
the class ProjectBatchServiceImpl method uploadProjectsReal.
private GeneralResponse<?> uploadProjectsReal(InputStream fileInputStream, String fileName, String userName, boolean aomp) throws IOException, UnExpectedRequestException, PermissionDeniedRequestException, RoleNotFoundException {
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 + "]", 422);
}
if (userName == null) {
return new GeneralResponse<>("401", "{&PLEASE_LOGIN}", null);
}
User user = userDao.findByUsername(userName);
Long userId = user.getId();
// Read file and create project
ExcelProjectListener listener = readExcel(fileInputStream);
// Check if excel file is empty
if (listener.getExcelProjectContent().isEmpty() && listener.getExcelRuleContent().isEmpty() && listener.getExcelCustomRuleContent().isEmpty() && listener.getExcelMultiRuleContent().isEmpty() && listener.getTemplateFileExcelContent().isEmpty() && listener.getExcelMetricContent().isEmpty()) {
throw new UnExpectedRequestException("{&FILE_CAN_NOT_BE_EMPTY_OR_FILE_CAN_NOT_BE_RECOGNIZED}", 422);
}
for (ExcelProject excelProject : listener.getExcelProjectContent()) {
Project project = projectDao.findByNameAndCreateUser(excelProject.getProjectName(), userName);
if (project != null) {
if (!aomp) {
LOGGER.info("hint for user to decide to override or not.");
}
// Means update project.
LOGGER.info("Start to update project[name={}] with upload project file.", project.getName());
ModifyProjectDetailRequest request = convertExcelProjectToModifyProjectRequest(excelProject, project, userName);
projectService.modifyProjectDetail(request, false);
} else {
// Check excel project arguments is valid or not
AddProjectRequest request = convertExcelProjectToAddProjectRequest(excelProject);
projectService.addProject(request, userId);
}
}
for (ExcelRuleMetric excelRuleMetric : listener.getExcelMetricContent()) {
RuleMetric ruleMetric = ruleMetricDao.findByName(excelRuleMetric.getName());
if (ruleMetric != null) {
if (!aomp) {
LOGGER.info("hint for user to decide to override or not.");
}
LOGGER.info("Start to update rule metric[name={}] with upload rule metric file.", ruleMetric.getName());
modifyRuleMetric(excelRuleMetric, ruleMetric, userName);
} else {
addRuleMetric(excelRuleMetric, userName);
}
}
// Create rules according to excel sheet
Map<String, Map<String, List<ExcelTemplateRuleByProject>>> excelTemplateRulePartitionedByProject = listener.getExcelRuleContent();
Map<String, Map<String, List<ExcelCustomRuleByProject>>> excelCustomRulePartitionedByProject = listener.getExcelCustomRuleContent();
Map<String, Map<String, List<ExcelMultiTemplateRuleByProject>>> excelMultiTemplateRulePartitionedByProject = listener.getExcelMultiRuleContent();
Map<String, Map<String, List<ExcelTemplateFileRuleByProject>>> excelTemplateFileRulePartitionedByProject = listener.getTemplateFileExcelContent();
Set<String> allProjects = new HashSet<>();
allProjects.addAll(excelTemplateRulePartitionedByProject.keySet());
allProjects.addAll(excelCustomRulePartitionedByProject.keySet());
allProjects.addAll(excelMultiTemplateRulePartitionedByProject.keySet());
allProjects.addAll(excelTemplateFileRulePartitionedByProject.keySet());
for (String projectName : allProjects) {
try {
Project projectInDb = projectDao.findByNameAndCreateUser(projectName, userName);
if (projectInDb == null) {
throw new UnExpectedRequestException("{&PROJECT}: [" + projectName + "] {&DOES_NOT_EXIST}");
}
ruleBatchService.getAndSaveRule(excelTemplateRulePartitionedByProject.get(projectName), excelCustomRulePartitionedByProject.get(projectName), excelMultiTemplateRulePartitionedByProject.get(projectName), excelTemplateFileRulePartitionedByProject.get(projectName), projectInDb, userName, aomp);
} catch (Exception e) {
throw new UnExpectedRequestException(e.getMessage());
}
}
fileInputStream.close();
return new GeneralResponse<>("200", "{&SUCCEED_TO_UPLOAD_FILE}", null);
}
use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.
the class ProjectServiceImpl method deleteProject.
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> deleteProject(DeleteProjectRequest request) throws UnExpectedRequestException, PermissionDeniedRequestException {
// Check Arguments
DeleteProjectRequest.checkRequest(request);
// Check existence of project
Project projectInDb = projectDao.findById(request.getProjectId());
if (projectInDb == null) {
throw new UnExpectedRequestException("project_id {&DOES_NOT_EXIST}");
}
Long userId = HttpUtils.getUserId(httpServletRequest);
User user = null;
if (userId == null) {
user = userDao.findByUsername(request.getUsername());
if (user == null) {
throw new UnExpectedRequestException(String.format("{&FAILED_TO_FIND_USER} %s", request.getUsername()));
}
} else {
user = userDao.findById(userId);
}
// Check project permission
List<Integer> permissions = new ArrayList<>();
permissions.add(ProjectUserPermissionEnum.CREATOR.getCode());
checkProjectPermission(projectInDb, user.getUserName(), permissions);
List<Rule> rules = ruleDao.findByProject(projectInDb);
deleteAllRules(rules);
// Delete project
projectDao.deleteProject(projectInDb);
LOGGER.info("Succeed to delete project. project_id: {}", request.getProjectId());
return new GeneralResponse<>("200", "{&DELETE_PROJECT_SUCCESSFULLY}", null);
}
use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.
the class ProjectServiceImpl method addProjectReal.
@Override
public Project addProjectReal(Long userId, String projectName, String cnName, String projectDescription) throws UnExpectedRequestException {
User user = userDao.findById(userId);
// Automatically grant the highest authority to the system administrator.
Role role = roleDao.findByRoleName(ADMIN);
List<User> admins = userRoleDao.findByRole(role).stream().map(UserRole::getUser).collect(Collectors.toList());
// Check existence of project by project name.
Project projectInDb = projectDao.findByNameAndCreateUser(projectName, user.getUserName());
if (projectInDb != null) {
throw new UnExpectedRequestException(String.format("{&PROJECT}:%s {&ALREADY_EXIST}", projectName));
}
// Save project.
Project newProject = new Project(projectName, cnName, projectDescription, user.getUserName(), user.getChineseName(), user.getDepartment() != null ? user.getDepartment().getName() : "", ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date()));
// Create project user.
if (admins.contains(user)) {
createProjectUser(newProject, user);
} else {
createProjectUser(newProject, user);
for (User currentAdmin : admins) {
createProjectUser(newProject, currentAdmin);
}
}
return newProject;
}
use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.
the class ProjectBatchServiceImpl method modifyRuleMetric.
private void modifyRuleMetric(ExcelRuleMetric excelRuleMetric, RuleMetric ruleMetric, String userName) throws UnExpectedRequestException, PermissionDeniedRequestException {
// Check en code existence.
RuleMetric ruleMetricInDb = ruleMetricDao.findByEnCode(excelRuleMetric.getEnCode());
if (ruleMetricInDb != null && ruleMetricInDb.getId().longValue() != ruleMetric.getId().longValue()) {
throw new UnExpectedRequestException("Rule Metric [EN_CODE=" + excelRuleMetric.getEnCode() + "] {&DOES_NOT_EXIST}");
}
LOGGER.info("Start to modify rule metric, modify request: [{}], user: [{}]", excelRuleMetric.toString(), userName);
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 (ruleMetric.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(ruleMetric);
if (ruleMetricDepartmentUser != null && managedDepartment.contains(ruleMetricDepartmentUser.getDepartment())) {
LOGGER.info("Rule metric[{}] comes from department: {}", ruleMetric.toString(), ruleMetricDepartmentUser.getDepartment().getName());
} 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 (!ruleMetric.getLevel().equals(RuleMetricLevelEnum.PERSONAL_METRIC.getCode()) || !ruleMetric.getCreateUser().equals(loginUser.getUserName())) {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
}
ruleMetric.setName(excelRuleMetric.getName());
ruleMetric.setMetricDesc(excelRuleMetric.getMetricDesc());
ruleMetric.setSubSystemName(excelRuleMetric.getSubSystemName());
ruleMetric.setFullCnName(excelRuleMetric.getFullCnName());
ruleMetric.setModifyUser(userName);
ruleMetric.setModifyTime(ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date()));
ruleMetric.setType(excelRuleMetric.getType());
ruleMetric.setFrequency(Integer.parseInt(excelRuleMetric.getFrequency()));
ruleMetric.setEnCode(excelRuleMetric.getEnCode());
ruleMetric.setDepartmentName(excelRuleMetric.getDepartmentName());
ruleMetric.setDevDepartmentName(excelRuleMetric.getDevDepartmentName());
ruleMetric.setOpsDepartmentName(excelRuleMetric.getOpsDepartmentName());
ruleMetric.setAvailable(excelRuleMetric.getAvailable());
ruleMetricDao.add(ruleMetric);
}
use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.
the class RuleNodeServiceImpl method importRuleGroup.
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<RuleResponse> importRuleGroup(RuleNodeRequests ruleNodeRequests) throws UnExpectedRequestException, IOException, PermissionDeniedRequestException {
// Project, rule group.
Project projectInDb = projectDao.findById(ruleNodeRequests.getNewProjectId());
ObjectMapper objectMapper = new ObjectMapper();
RuleGroup ruleGroup = objectMapper.readValue(ruleNodeRequests.getRuleNodeRequests().iterator().next().getRuleGroupObject(), RuleGroup.class);
RuleGroup ruleGroupInDb = ruleGroupDao.saveRuleGroup(new RuleGroup(ruleGroup.getRuleGroupName(), projectInDb.getId()));
if (DEV_CENTER.equals(localConfig.getCenter())) {
CopyRuleRequest copyRuleRequest = new CopyRuleRequest();
copyRuleRequest.setSourceRuleGroupId(ruleGroup.getId());
copyRuleRequest.setCreateUser(projectInDb.getCreateUser());
copyRuleRequest.setTargetRuleGroupId(ruleGroupInDb.getId());
copyRuleRequest.setTargetProjectId(ruleNodeRequests.getNewProjectId());
copyRuleByRuleGroupId(copyRuleRequest);
return new GeneralResponse<>("200", "{&IMPORT_RULE_SUCCESSFULLY}", new RuleResponse(ruleGroupInDb.getId()));
}
if (projectInDb != null) {
LOGGER.info("Project info : {}", projectInDb.toString());
} else {
// For special case:
// A project already exists in DSS. when creating a workflow in this project and publish it, Qualitis service cannot find the project.
// Because Qualitis did not run when this project had been created in DSS.
LOGGER.info("Start to create workflow project. Project[id = {}] is not exist.", ruleNodeRequests.getNewProjectId());
String userName = ruleNodeRequests.getUserName();
User currentUser = userDao.findByUsername(userName);
if (currentUser == null) {
LOGGER.info("Start to create user. User name is {}", userName);
try {
userService.autoAddUser(userName);
currentUser = userDao.findByUsername(userName);
} catch (RoleNotFoundException e) {
LOGGER.error("Role cannot be found. Exception: {}", e);
}
}
Project newProject = projectService.addProjectReal(currentUser.getId(), currentUser.getUserName() + "_project_" + UUID.randomUUID().toString(), currentUser.getUserName() + "_项目_" + UUID.randomUUID().toString(), "Auto created.");
newProject.setProjectType(ProjectTypeEnum.WORKFLOW_PROJECT.getCode());
projectInDb = projectDao.saveProject(newProject);
LOGGER.info("Succeed to create project. New project: {}", projectInDb.toString());
}
for (RuleNodeRequest request : ruleNodeRequests.getRuleNodeRequests()) {
importRule(request, projectInDb, ruleGroupInDb, objectMapper);
}
return new GeneralResponse<>("200", "{&IMPORT_RULE_SUCCESSFULLY}", new RuleResponse(ruleGroupInDb.getId()));
}
Aggregations