use of com.webank.wedatasphere.qualitis.request.DownloadRuleMetricRequest in project Qualitis by WeBankFinTech.
the class RuleMetricServiceImpl method download.
@Override
public GeneralResponse<?> download(DownloadRuleMetricRequest request, HttpServletResponse response) throws UnExpectedRequestException, IOException, WriteExcelException, PermissionDeniedRequestException {
// Check rule metric IDs permission.
PageRequest pageRequest = new PageRequest(0, Integer.MAX_VALUE);
List<RuleMetricResponse> ownRuleMetric = getAllRuleMetric(pageRequest).getData().getData().stream().collect(Collectors.toList());
if (ownRuleMetric.size() <= 0 || ownRuleMetric.size() >= MAX_RULE_METRIC_COUNT) {
throw new UnExpectedRequestException("The number of metrics is illegal");
}
List<Long> downloadIds = request.getRuleMetricIds();
List<Long> ownIds = ownRuleMetric.stream().map(RuleMetricResponse::getId).collect(Collectors.toList());
if (ownIds.containsAll(downloadIds)) {
List<RuleMetricResponse> downloadRuleMetric = ownRuleMetric.stream().filter(ruleMetricResponse -> downloadIds.contains(ruleMetricResponse.getId())).collect(Collectors.toList());
List<ExcelRuleMetric> excelRuleMetrics = new ArrayList<>(downloadRuleMetric.size());
for (RuleMetricResponse ruleMetricResponse : downloadRuleMetric) {
ExcelRuleMetric excelRuleMetric = new ExcelRuleMetric();
excelRuleMetric.setName(ruleMetricResponse.getName());
excelRuleMetric.setChName(ruleMetricResponse.getCnName());
excelRuleMetric.setMetricDesc(ruleMetricResponse.getMetricDesc());
int bussCode = ruleMetricResponse.getBussCode();
excelRuleMetric.setDimension(bussCode + "");
if (RuleMetricBussCodeEnum.SUBSYSTEM.getCode().equals(bussCode)) {
excelRuleMetric.setFullCnName(ruleMetricResponse.getFullCnName());
excelRuleMetric.setSubSystemName(ruleMetricResponse.getSubSystemName());
excelRuleMetric.setSubSystemId(String.valueOf(ruleMetricResponse.getSubSystemId()));
} else if (RuleMetricBussCodeEnum.PRODUCT.getCode().equals(bussCode)) {
excelRuleMetric.setProductId(ruleMetricResponse.getProductId());
excelRuleMetric.setProductName(ruleMetricResponse.getProductName());
} else if (RuleMetricBussCodeEnum.CUSTOM.getCode().equals(bussCode)) {
excelRuleMetric.setBussCustom(ruleMetricResponse.getBussCustom());
}
excelRuleMetric.setFrequency(String.valueOf(ruleMetricResponse.getFrequency()));
excelRuleMetric.setDevDepartmentName(ruleMetricResponse.getDevDepartmentName());
excelRuleMetric.setOpsDepartmentName(ruleMetricResponse.getOpsDepartmentName());
excelRuleMetric.setDepartmentCode(ruleMetricResponse.getDepartmentCode());
excelRuleMetric.setDepartmentName(ruleMetricResponse.getDepartmentName());
excelRuleMetric.setAvailable(ruleMetricResponse.getAvailable());
excelRuleMetric.setEnCode(ruleMetricResponse.getEnCode());
excelRuleMetric.setType(ruleMetricResponse.getType());
excelRuleMetrics.add(excelRuleMetric);
}
String fileName = "batch_metrics_export_" + FILE_DATE_FORMATTER.format(new Date());
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName);
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
OutputStream outputStream = response.getOutputStream();
writeExcelToOutput(excelRuleMetrics, outputStream);
outputStream.flush();
} else {
throw new PermissionDeniedRequestException("HAS_NO_PERMISSION_TO_ACCESS", 403);
}
LOGGER.info("Succeed to download all rule metrics in type of excel");
return null;
}
Aggregations