Search in sources :

Example 1 with FileInfoEntity

use of com.varsql.web.model.entity.app.FileInfoEntity in project varsql by varsqlinfo.

the class ExportServiceImpl method downloadTableData.

/**
 * @method  : downloadTableData
 * @desc : download table data
 * @author   : ytkim
 * @date   : 2022. 1. 28.
 * @param preferencesInfo
 * @param req
 * @param res
 * @throws Exception
 */
public void downloadTableData(PreferencesRequestDTO preferencesInfo, HttpServletRequest req, HttpServletResponse res) throws Exception {
    ResponseResult result = downloadTableData2(preferencesInfo, req, res);
    FileInfoEntity fie = result.getItem();
    VarsqlUtils.setResponseDownAttr(res, req, fie.getFileName());
    res.setHeader("Content-Length", "" + fie.getFileSize());
    File file = FileServiceUtils.getFileInfoToFile(fie);
    byte[] b = new byte[2048];
    try (BufferedInputStream fin = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream outs = new BufferedOutputStream(res.getOutputStream())) {
        int read = 0;
        while ((read = fin.read(b)) != -1) {
            outs.write(b, 0, read);
        }
        outs.flush();
        IOUtils.close(fin);
        IOUtils.close(outs);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ResponseResult(com.vartech.common.app.beans.ResponseResult) FileInfoEntity(com.varsql.web.model.entity.app.FileInfoEntity) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 2 with FileInfoEntity

use of com.varsql.web.model.entity.app.FileInfoEntity in project varsql by varsqlinfo.

the class FileImportExportServiceImpl method importFile.

/**
 * @Method Name  : importFile
 * @Method 설명 : import file
 * @작성자   : ytkim
 * @작성일   : 2020. 11. 22.
 * @변경이력  :
 */
public ResponseResult importFile(FileImportInfo fii) {
    String conuid = fii.getConuid();
    String importType = fii.getImportType();
    List<FileInfoEntity> importFileList = fileInfoEntityRepository.findAllById(Arrays.asList(CommUtils.getFileIds(fii.getFileIds())));
    ResponseResult result = new ResponseResult();
    List<FileImportResult> resultInfo = new ArrayList<>();
    importFileList.forEach(fileInfo -> {
        FileImportResult fileImportResult = new FileImportResult();
        try {
            fileImportResult.setFileName(fileInfo.getFileName());
            SQLExecuteResult ser = null;
            if ("sql".equals(importType)) {
                ser = sqlImport(fileInfo, conuid);
            } else if ("xml".equals(importType)) {
                ser = xmlImport(fileInfo, conuid);
            } else if ("json".equals(importType)) {
                ser = jsonImport(fileInfo, conuid);
            } else if ("csv".equals(importType)) {
                ser = csvImport(fileInfo, conuid);
            }
            if (ser.getResultCode() != null) {
                fileImportResult.setResultCode(ser.getResultCode());
            }
            fileImportResult.setResultCount(ser.getExecuteCount());
            fileImportResult.setMessage(ser.getMessage());
        } catch (IOException | SQLException e) {
            fileImportResult.setResultCode(VarsqlAppCode.ERROR);
            fileImportResult.setMessage(e.getMessage());
            logger.error("file import error importType : {} , conuid : {} , fileInfo :{} ", importType, fileInfo);
            logger.error("error message : {}", e.getMessage(), e);
        }
        resultInfo.add(fileImportResult);
    });
    result.setItemList(resultInfo);
    return result;
}
Also used : SQLException(java.sql.SQLException) ResponseResult(com.vartech.common.app.beans.ResponseResult) ArrayList(java.util.ArrayList) SQLExecuteResult(com.varsql.core.sql.executor.SQLExecuteResult) FileInfoEntity(com.varsql.web.model.entity.app.FileInfoEntity) FileImportResult(com.varsql.web.dto.file.FileImportResult) IOException(java.io.IOException)

Example 3 with FileInfoEntity

use of com.varsql.web.model.entity.app.FileInfoEntity in project varsql by varsqlinfo.

the class UserPreferencesServiceFileImpl method importFileList.

public ResponseResult importFileList(UploadFileType fileType, SearchParameter searchParameter) {
    Page<FileInfoEntity> result = fileInfoEntityRepository.findAll(FileInfoSpec.fileTypeSearch(fileType, SecurityUtil.userViewId(), searchParameter), VarsqlUtils.convertSearchInfoToPage(searchParameter));
    List<Map> fileList = new ArrayList<>();
    result.getContent().forEach(entity -> {
        Map item = new HashMap();
        item.put("fileId", entity.getFileId());
        item.put("fileName", entity.getFileName());
        item.put("ext", entity.getFileExt());
        item.put("regDt", entity.getRegDt());
        item.put("fileSize", entity.getFileSize());
        fileList.add(item);
    });
    return VarsqlUtils.getResponseResult(fileList, result.getTotalElements(), searchParameter);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileInfoEntity(com.varsql.web.model.entity.app.FileInfoEntity) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with FileInfoEntity

use of com.varsql.web.model.entity.app.FileInfoEntity in project varsql by varsqlinfo.

the class UserPreferencesServiceFileImpl method zipFileDetail.

public ResponseResult zipFileDetail(String fileId, String fileName) {
    FileInfoEntity entity = fileInfoEntityRepository.findByFileId(fileId);
    if (entity == null) {
        new FileNotFoundException("fileId not found : " + fileId);
    }
    StringBuffer sb = new StringBuffer();
    try (ZipFile zipFile = new ZipFile(FileServiceUtils.getFileInfoToFile(entity))) {
        ZipEntry entry = zipFile.getEntry(fileName);
        if (entry.getName().equals(fileName)) {
            try (InputStreamReader isr = new InputStreamReader(zipFile.getInputStream(entry));
                BufferedReader br = new BufferedReader(isr)) {
                String read_data = "";
                int lineCount = 0;
                while ((read_data = br.readLine()) != null) {
                    if (lineCount >= MAX_LINE) {
                        break;
                    }
                    lineCount++;
                    sb.append(read_data).append(BlankConstants.NEW_LINE);
                }
                IOUtils.close(br);
                IOUtils.close(isr);
            }
        }
        IOUtils.close(zipFile);
    } catch (Exception e) {
        logger.error("detail fileId : {}", fileId, e);
    }
    return VarsqlUtils.getResponseResultItemOne(sb.toString());
}
Also used : ZipFile(java.util.zip.ZipFile) InputStreamReader(java.io.InputStreamReader) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(com.varsql.core.exception.FileNotFoundException) BufferedReader(java.io.BufferedReader) FileInfoEntity(com.varsql.web.model.entity.app.FileInfoEntity) FileNotFoundException(com.varsql.core.exception.FileNotFoundException)

Example 5 with FileInfoEntity

use of com.varsql.web.model.entity.app.FileInfoEntity in project varsql by varsqlinfo.

the class UserPreferencesServiceFileImpl method detail.

public ResponseResult detail(String fileId) {
    FileInfoEntity entity = fileInfoEntityRepository.findByFileId(fileId);
    if (entity == null) {
        new FileNotFoundException("fileId not found : " + fileId);
    }
    if (UploadFileType.EXPORT.equals(UploadFileType.getDivType(entity.getFileDiv()))) {
        Map item;
        List<Map> fileList = new ArrayList<>();
        try (ZipFile zipFile = new ZipFile(FileServiceUtils.getFileInfoToFile(entity))) {
            final Enumeration<? extends ZipEntry> entries = zipFile.entries();
            ZipEntry entry;
            while (entries.hasMoreElements()) {
                entry = entries.nextElement();
                item = new HashMap();
                item.put("fileName", entry.getName());
                item.put("fileSize", entry.getSize());
                item.put("compressFileSize", entry.getCompressedSize());
                item.put("updDt", DateUtils.dateformat(VarsqlConstants.DATE_TIME_FORMAT, entry.getLastModifiedTime().toMillis()));
                fileList.add(item);
            }
            IOUtils.close(zipFile);
        } catch (Exception e) {
            logger.error("detail fileId : {}", fileId, e);
        }
        return VarsqlUtils.getResponseResultItemList(fileList);
    } else if (UploadFileType.IMPORT.equals(UploadFileType.getDivType(entity.getFileDiv()))) {
        StringBuffer sb = new StringBuffer();
        Map item = new HashMap();
        try (InputStreamReader isr = new InputStreamReader(new FileInputStream(FileServiceUtils.getFileInfoToFile(entity)));
            BufferedReader br = new BufferedReader(isr)) {
            String read_data = "";
            int lineCount = 0;
            while ((read_data = br.readLine()) != null) {
                if (lineCount >= MAX_LINE) {
                    break;
                }
                lineCount++;
                sb.append(read_data).append(BlankConstants.NEW_LINE);
            }
            item.put("lineCount", lineCount);
            item.put("content", sb.toString());
            IOUtils.close(br);
            IOUtils.close(isr);
        } catch (Exception e) {
            logger.error("detail fileId : {}", fileId, e);
        }
        return VarsqlUtils.getResponseResultItemOne(item);
    }
    return VarsqlUtils.getResponseResultItemOne(null);
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(com.varsql.core.exception.FileNotFoundException) ArrayList(java.util.ArrayList) FileNotFoundException(com.varsql.core.exception.FileNotFoundException) FileInputStream(java.io.FileInputStream) ZipFile(java.util.zip.ZipFile) BufferedReader(java.io.BufferedReader) FileInfoEntity(com.varsql.web.model.entity.app.FileInfoEntity) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

FileInfoEntity (com.varsql.web.model.entity.app.FileInfoEntity)10 ResponseResult (com.vartech.common.app.beans.ResponseResult)5 ArrayList (java.util.ArrayList)5 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ZipEntry (java.util.zip.ZipEntry)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 FileNotFoundException (com.varsql.core.exception.FileNotFoundException)2 SQLExecuteResult (com.varsql.core.sql.executor.SQLExecuteResult)2 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 ZipFile (java.util.zip.ZipFile)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ProgressInfo (com.varsql.core.common.beans.ProgressInfo)1 VarsqlFileType (com.varsql.core.common.code.VarsqlFileType)1 BlankConstants (com.varsql.core.common.constants.BlankConstants)1 VarsqlConstants (com.varsql.core.common.constants.VarsqlConstants)1 SecurityUtil (com.varsql.core.common.util.SecurityUtil)1