use of com.odysseusinc.arachne.commons.types.DBMSType in project ArachneCentralAPI by OHDSI.
the class AnalysisFilesSavingServiceImpl method generateFilesForEachDialectAndAddToZip.
private void generateFilesForEachDialectAndAddToZip(ZipOutputStream zos, MultipartFile file) throws IOException {
String statement = IOUtils.toString(file.getInputStream(), StandardCharsets.UTF_8);
String renderedSql = SqlRender.renderSql(statement, null, null);
String baseName = FilenameUtils.getBaseName(file.getOriginalFilename());
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
for (DBMSType dialect : DBMSType.values()) {
String sql = SqlTranslate.translateSql(renderedSql, dialect.getOhdsiDB());
String fileName = String.format("%s.%s.%s", baseName, dialect.getLabel().replace(" ", "-"), extension);
ZipUtil.addZipEntry(zos, fileName, new ByteArrayInputStream(sql.getBytes(StandardCharsets.UTF_8)));
}
}
use of com.odysseusinc.arachne.commons.types.DBMSType in project ArachneCentralAPI by OHDSI.
the class BaseAnalysisController method doAddCommonEntityToAnalysis.
protected void doAddCommonEntityToAnalysis(T analysis, DataReference dataReference, IUser user, CommonAnalysisType analysisType, List<MultipartFile> files) throws IOException {
files.stream().filter(f -> !CommonAnalysisType.COHORT.equals(analysisType) || !f.getName().endsWith(CommonFileUtils.OHDSI_JSON_EXT)).forEach(f -> {
try {
analysisService.saveFile(f, user, analysis, f.getName(), detectExecutable(analysisType, f), dataReference);
} catch (IOException e) {
LOGGER.error("Failed to save file", e);
}
});
if (analysisType.equals(CommonAnalysisType.COHORT)) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
class StringContainer {
String value = CommonAnalysisType.COHORT.getTitle();
}
final StringContainer generatedFileName = new StringContainer();
try (final ZipOutputStream zos = new ZipOutputStream(out)) {
files.forEach(file -> {
try {
if (file.getName().endsWith(CommonFileUtils.OHDSI_SQL_EXT)) {
String statement = org.apache.commons.io.IOUtils.toString(file.getInputStream(), "UTF-8");
String renderedSql = SqlRender.renderSql(statement, null, null);
DBMSType[] dbTypes = new DBMSType[] { DBMSType.POSTGRESQL, DBMSType.ORACLE, DBMSType.MS_SQL_SERVER, DBMSType.REDSHIFT, DBMSType.PDW };
String baseName = FilenameUtils.getBaseName(file.getOriginalFilename());
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
for (final DBMSType dialect : dbTypes) {
final String sql = SqlTranslate.translateSql(renderedSql, DBMSType.MS_SQL_SERVER.getOhdsiDB(), dialect.getOhdsiDB());
final String fileName = baseName + "." + dialect.getLabel().replaceAll(" ", "-") + "." + extension;
ZipUtil.addZipEntry(zos, fileName, new ByteArrayInputStream(sql.getBytes("UTF-8")));
}
final String shortBaseName = baseName.replaceAll("\\.ohdsi", "");
if (!generatedFileName.value.contains(shortBaseName)) {
generatedFileName.value += "_" + shortBaseName;
}
} else {
String fileName = file.getName();
ZipUtil.addZipEntry(zos, fileName, file.getInputStream());
}
} catch (IOException e) {
LOGGER.error("Failed to add file to archive", e);
throw new RuntimeIOException(e.getMessage(), e);
}
});
}
String fileName = generatedFileName.value + ".zip";
final MultipartFile sqlArchive = new MockMultipartFile(fileName, fileName, "application/zip", out.toByteArray());
analysisService.saveFile(sqlArchive, user, analysis, fileName, false, dataReference);
}
}
Aggregations