use of com.webank.wedatasphere.qualitis.project.request.ModifyProjectDetailRequest 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.project.request.ModifyProjectDetailRequest in project Qualitis by WeBankFinTech.
the class ProjectBatchServiceImpl method convertExcelProjectToModifyProjectRequest.
private ModifyProjectDetailRequest convertExcelProjectToModifyProjectRequest(ExcelProject excelProject, Project project, String userName) {
// Construct ModifyProjectDetailRequest
ModifyProjectDetailRequest request = new ModifyProjectDetailRequest();
request.setUsername(userName);
request.setProjectId(project.getId());
request.setCnName(excelProject.getProjectChName());
request.setProjectName(excelProject.getProjectName());
String projectLabels = excelProject.getProjectLabels();
request.setDescription(excelProject.getProjectDescription());
if (projectLabels != null && projectLabels.length() > 0) {
String[] labels = projectLabels.split(" ");
Set<String> labelSet = new HashSet<>();
for (String label : labels) {
labelSet.add(label);
}
request.setProjectLabelStrs(labelSet);
}
return request;
}
use of com.webank.wedatasphere.qualitis.project.request.ModifyProjectDetailRequest in project Qualitis by WeBankFinTech.
the class ProjectServiceImpl method modifyProjectDetail.
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> modifyProjectDetail(ModifyProjectDetailRequest request, boolean workflow) throws UnExpectedRequestException, PermissionDeniedRequestException, RoleNotFoundException {
// Check Arguments
ModifyProjectDetailRequest.checkRequest(request);
// Check existence of project
Project projectInDb = projectDao.findById(request.getProjectId());
if (projectInDb == null) {
throw new UnExpectedRequestException("project id {&DOES_NOT_EXIST}");
}
LOGGER.info("Succeed to get project. project: {}", projectInDb);
// Get userId
Long userId = HttpUtils.getUserId(httpServletRequest);
User user;
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);
if (user == null) {
throw new UnExpectedRequestException(String.format("{&FAILED_TO_FIND_USER} %s", userId));
}
}
// Check if user has permission modifying project
List<Integer> permissions = new ArrayList<>();
permissions.add(ProjectUserPermissionEnum.BUSSMAN.getCode());
permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
checkProjectPermission(projectInDb, user.getUserName(), permissions);
// Check project name
Project otherProject = projectDao.findByNameAndCreateUser(request.getProjectName(), user.getUserName());
if (otherProject != null && !otherProject.getId().equals(projectInDb.getId())) {
throw new UnExpectedRequestException(String.format("Project name: %s already exist", request.getProjectName()));
}
// Save project.
String oldLabels = "";
String newLabels = "";
if (CollectionUtils.isNotEmpty(projectInDb.getProjectLabels())) {
oldLabels = projectInDb.getProjectLabels().stream().map(ProjectLabel::getLabelName).collect(Collectors.joining());
}
if (CollectionUtils.isNotEmpty(request.getProjectLabelStrs())) {
newLabels = request.getProjectLabelStrs().stream().collect(Collectors.joining());
}
if (!oldLabels.equals(newLabels)) {
projectEventService.recordModifyProject(projectInDb, user.getUserName(), "Project labels", oldLabels, newLabels, EventTypeEnum.MODIFY_PROJECT.getCode());
}
if (StringUtils.isNotEmpty(projectInDb.getCnName()) && !projectInDb.getCnName().equals(request.getCnName())) {
projectEventService.recordModifyProject(projectInDb, user.getUserName(), "Chinese Name", projectInDb.getCnName(), request.getCnName(), EventTypeEnum.MODIFY_PROJECT.getCode());
}
if (!projectInDb.getName().equals(request.getProjectName())) {
projectEventService.recordModifyProject(projectInDb, user.getUserName(), "English Name", projectInDb.getName(), request.getProjectName(), EventTypeEnum.MODIFY_PROJECT.getCode());
}
if (!projectInDb.getDescription().equals(request.getDescription())) {
projectEventService.recordModifyProject(projectInDb, user.getUserName(), "Describe", projectInDb.getDescription(), request.getDescription(), EventTypeEnum.MODIFY_PROJECT.getCode());
}
projectInDb.setCnName(request.getCnName());
projectInDb.setName(request.getProjectName());
projectInDb.setDescription(request.getDescription());
// Delete old projectLabel.
projectLabelDao.deleteByProject(projectInDb);
LOGGER.info("Succeed to delete all project_label, project_id: {}", request.getProjectId());
// Create new project labels.
addProjectLabels(request.getProjectLabelStrs(), projectInDb);
// Record modify user
projectInDb.setModifyUser(user.getUserName());
projectInDb.setModifyTime(ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date()));
Project savedProject = projectDao.saveProject(projectInDb);
LOGGER.info("Succeed to modify project. project: {}", savedProject);
if (workflow) {
// Clear old project user.
List<ProjectUser> projectUsers = projectUserDao.findByProject(projectInDb);
Role role = roleDao.findByRoleName(ADMIN);
List<String> admins = userRoleDao.findByRole(role).stream().map(UserRole::getUser).map(User::getUserName).collect(Collectors.toList());
projectUsers = projectUsers.stream().filter(projectUser -> !admins.contains(projectUser.getUserName())).filter(projectUser -> !projectUser.getUserName().equals(request.getUsername())).collect(Collectors.toList());
for (ProjectUser projectUser : projectUsers) {
projectUserDao.deleteByProjectAndUserName(projectInDb, projectUser.getUserName());
}
}
authorizeUsers(projectInDb, user, request.getAuthorizeProjectUserRequests(), true);
return new GeneralResponse<>("200", "{&MODIFY_PROJECT_DETAIL_SUCCESSFULLY}", new ProjectDetailResponse(savedProject, null));
}
Aggregations