use of com.varsql.web.exception.DataDownloadException in project varsql by varsqlinfo.
the class SQLServiceImpl method dataExport.
/**
* 데이터 내보내기.
* @param paramMap
* @param req
*/
@SuppressWarnings("rawtypes")
public void dataExport(ParamMap paramMap, SqlExecuteDTO sqlExecuteInfo, HttpServletRequest req, HttpServletResponse res) throws Exception {
String objectName = sqlExecuteInfo.getObjectName();
if (!sqlExecuteInfo.getBaseSchema().equals(sqlExecuteInfo.getSchema())) {
objectName = sqlExecuteInfo.getSchema() + "." + objectName;
}
StringBuilder reqQuerySb = new StringBuilder().append("select * from ").append(objectName).append(" where 1=1 ");
String conditionQuery = paramMap.getString("conditionQuery");
if (!StringUtils.isBlank(conditionQuery)) {
conditionQuery = StringUtils.lTrim(conditionQuery);
if (conditionQuery.startsWith("where")) {
conditionQuery = conditionQuery.replaceFirst("where", "");
}
if (conditionQuery.startsWith("and")) {
conditionQuery = conditionQuery.replaceFirst("and", "");
}
conditionQuery = " and " + conditionQuery;
reqQuerySb.append(conditionQuery);
}
sqlExecuteInfo.setSqlParam("{}");
sqlExecuteInfo.setSql(reqQuerySb.toString());
String exportCharset = paramMap.getString("charset", VarsqlConstants.CHAR_SET);
OutputStream outstream = null;
AbstractWriter writer = null;
try {
VarsqlFileType exportType = sqlExecuteInfo.getExportType();
Path fileExportPath = FileServiceUtils.getSavePath(UploadFileType.EXPORT);
String downloadFilePath = FileUtils.pathConcat(fileExportPath.toAbsolutePath().toString(), exportType.concatExtension(VartechUtils.generateUUID()));
outstream = new FileOutputStream(downloadFilePath);
if (VarsqlFileType.CSV.equals(exportType)) {
writer = new CSVWriter(outstream, ',', exportCharset);
} else if (VarsqlFileType.JSON.equals(exportType)) {
writer = new JSONWriter(outstream, "row", exportCharset);
} else if (VarsqlFileType.XML.equals(exportType)) {
writer = new XMLWriter(outstream, "row", exportCharset);
} else if (VarsqlFileType.EXCEL.equals(exportType)) {
writer = new ExcelWriter(outstream);
} else {
writer = new SQLWriter(outstream, DBType.getDBType(sqlExecuteInfo.getDbType()), objectName, exportCharset);
}
logger.debug("data export downloadFilePath :{} , query : {}", downloadFilePath, sqlExecuteInfo.getSql());
final String tableName = objectName;
SQLExecuteResult ser = new SelectExecutor().execute(sqlExecuteInfo, new AbstractSQLExecutorHandler(writer) {
private boolean firstFlag = true;
@Override
public boolean handle(SQLHandlerParameter handleParam) {
if (firstFlag) {
WriteMetadataInfo whi = new WriteMetadataInfo("exportInfo");
List<ExportColumnInfo> columns = new LinkedList<ExportColumnInfo>();
handleParam.getColumnInfoList().forEach(item -> {
ExportColumnInfo gci = new ExportColumnInfo();
gci.setName(item.getLabel());
gci.setAlias(item.getKey());
gci.setType(item.getDbType());
gci.setNumber(item.isNumber());
gci.setLob(item.isLob());
columns.add(gci);
});
whi.addMetedata("tableName", tableName);
whi.addMetedata("columns", columns);
getWriter().setMetadata(whi);
if (VarsqlFileType.SQL.equals(exportType)) {
((SQLWriter) getWriter()).setColumnInfos(handleParam.getColumnInfoList());
}
firstFlag = false;
}
try {
getWriter().addRow(handleParam.getRowObject());
} catch (IOException e) {
logger.error(e.getMessage(), e);
return false;
}
return true;
}
});
writer.writeAndClose();
if (ser.getResultCode() != null) {
throw new DataDownloadException(ser.getResultCode(), ser.getMessage(), new VarsqlAppException(ser.getMessage()));
}
String exportFileName = ValidateUtils.getValidFileName(paramMap.getString("fileName", objectName));
VarsqlUtils.setResponseDownAttr(res, req, exportType.concatExtension(exportFileName));
try (FileInputStream fileInputStream = new FileInputStream(new File(downloadFilePath));
OutputStream downloadStream = res.getOutputStream()) {
byte[] buf = new byte[8192];
int read = 0;
while ((read = fileInputStream.read(buf)) != -1) {
downloadStream.write(buf, 0, read);
}
downloadStream.flush();
downloadStream.close();
fileInputStream.close();
}
} catch (Exception e) {
throw e;
} finally {
IOUtils.close(writer);
IOUtils.close(outstream);
}
}
Aggregations