Search in sources :

Example 1 with MetaFile

use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.

the class MoveLineExportServiceImpl method writeMoveLineToCsvFile.

protected MetaFile writeMoveLineToCsvFile(Company company, String fileName, String[] columnHeader, List<String[]> allMoveData, AccountingReport accountingReport) throws AxelorException, IOException {
    String filePath = accountConfigService.getAccountConfig(company).getExportPath();
    String dataExportDir = appAccountService.getDataExportDir();
    for (String[] items : allMoveData) {
        for (int i = 0; i < items.length; i++) {
            if (items[i] != null) {
                items[i] = items[i].replaceAll("(\r\n|\n\r|\r|\n|\\|)", " ");
            }
        }
    }
    filePath = filePath == null ? dataExportDir : dataExportDir + filePath;
    new File(filePath).mkdirs();
    log.debug("Full path to export : {}{}", filePath, fileName);
    CsvTool.csvWriter(filePath, fileName, '|', columnHeader, allMoveData);
    Path path = Paths.get(filePath, fileName);
    try (InputStream is = new FileInputStream(path.toFile())) {
        return Beans.get(MetaFiles.class).attach(is, fileName, accountingReport).getMetaFile();
    }
}
Also used : Path(java.nio.file.Path) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) FileInputStream(java.io.FileInputStream)

Example 2 with MetaFile

use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.

the class DataImportServiceImpl method importData.

private MetaFile importData(List<CSVInput> inputs) throws IOException {
    if (CollectionUtils.isEmpty(inputs)) {
        return null;
    }
    CSVConfig config = new CSVConfig();
    config.setInputs(inputs);
    if (!CollectionUtils.isEmpty(adapterMap.values())) {
        config.getAdapters().addAll(adapterMap.values());
    }
    CSVImporter importer = new CSVImporter(config, dataDir.getAbsolutePath());
    ImporterListener listener = new ImporterListener("importData");
    importer.addListener(listener);
    importer.setContext(importContext);
    importer.run();
    if (!listener.isImported()) {
        MetaFile logFile = this.createImportLogFile(listener);
        return logFile;
    }
    return null;
}
Also used : ImporterListener(com.axelor.apps.base.service.imports.listener.ImporterListener) MetaFile(com.axelor.meta.db.MetaFile) CSVImporter(com.axelor.data.csv.CSVImporter) CSVConfig(com.axelor.data.csv.CSVConfig)

Example 3 with MetaFile

use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.

the class PrintServiceImpl method generatePDF.

@Override
@Transactional
public Map<String, Object> generatePDF(Print print) throws AxelorException {
    try {
        print = printRepo.find(print.getId());
        String html = generateHtml(print);
        ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
        try (PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfOutputStream))) {
            pdfDoc.setDefaultPageSize(print.getDisplayTypeSelect() == PrintRepository.DISPLAY_TYPE_LANDSCAPE ? PageSize.A4.rotate() : PageSize.A4);
            if (print.getPrintPdfFooter() != null && !print.getHidePrintSettings()) {
                com.itextpdf.layout.Document doc = new com.itextpdf.layout.Document(pdfDoc);
                pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new TableFooterEventHandler(doc, print));
            }
            ConverterProperties converterProperties = new ConverterProperties();
            converterProperties.setBaseUri(attachmentPath);
            HtmlConverter.convertToPdf(html, pdfDoc, converterProperties);
        }
        String documentName = (StringUtils.notEmpty(print.getDocumentName()) ? print.getDocumentName() : "") + "-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + FILE_EXTENSION_PDF;
        InputStream pdfInputStream = new ByteArrayInputStream(pdfOutputStream.toByteArray());
        MetaFile metaFile = metaFiles.upload(pdfInputStream, documentName);
        File file = MetaFiles.getPath(metaFile).toFile();
        String fileLink = PdfTool.getFileLinkFromPdfFile(PdfTool.printCopiesToFile(file, 1), metaFile.getFileName());
        if (ObjectUtils.notEmpty(file) && file.exists() && (print.getAttach() || StringUtils.notEmpty(print.getMetaFileField()))) {
            Class<? extends Model> modelClass = (Class<? extends Model>) Class.forName(print.getMetaModel().getFullName());
            Model objectModel = JPA.find(modelClass, print.getObjectId());
            if (ObjectUtils.notEmpty(objectModel)) {
                if (print.getAttach()) {
                    metaFiles.attach(metaFile, documentName, objectModel);
                }
                if (StringUtils.notEmpty(print.getMetaFileField())) {
                    saveMetaFileInModel(modelClass, objectModel, metaFile, print.getMetaFileField());
                }
            }
        }
        if (CollectionUtils.isNotEmpty(print.getPrintSet())) {
            for (Print subPrint : print.getPrintSet()) {
                generatePDF(subPrint);
            }
        }
        return ActionView.define(documentName).add("html", fileLink).map();
    } catch (IOException | ClassNotFoundException e) {
        throw new AxelorException(e, TraceBackRepository.CATEGORY_INCONSISTENCY);
    }
}
Also used : ConverterProperties(com.itextpdf.html2pdf.ConverterProperties) AxelorException(com.axelor.exception.AxelorException) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Print(com.axelor.apps.base.db.Print) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(com.axelor.db.Model) MetaFile(com.axelor.meta.db.MetaFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) Transactional(com.google.inject.persist.Transactional)

Example 4 with MetaFile

use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.

the class PrintServiceImpl method attachMetaFiles.

@Override
public void attachMetaFiles(Print print, Set<MetaFile> metaFiles) {
    Preconditions.checkNotNull(print.getId());
    if (metaFiles == null || metaFiles.isEmpty()) {
        return;
    }
    LOG.debug("Add metafiles to object {} : {}", Print.class.getName(), print.getId());
    for (MetaFile metaFile : metaFiles) {
        Beans.get(MetaFiles.class).attach(metaFile, metaFile.getFileName(), print);
    }
}
Also used : Print(com.axelor.apps.base.db.Print) MetaFiles(com.axelor.meta.MetaFiles) MetaFile(com.axelor.meta.db.MetaFile)

Example 5 with MetaFile

use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.

the class XmlHandler method exportObject.

@Transactional
public MetaFile exportObject() {
    group = Beans.get(GroupRepository.class).all().filter("self.code = 'admins'").fetchOne();
    try {
        log.debug("Attachment dir: {}", AppService.getFileUploadDir());
        String uploadDir = AppService.getFileUploadDir();
        if (uploadDir == null || !new File(uploadDir).exists()) {
            return null;
        }
        MetaFile metaFile = new MetaFile();
        String fileName = "ExportObject-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyMMddHHmmSS")) + ".csv";
        metaFile.setFileName(fileName);
        metaFile.setFilePath(fileName);
        metaFile = Beans.get(MetaFileRepository.class).save(metaFile);
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        saxParserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        saxParserFactory.setXIncludeAware(false);
        SAXParser parser = saxParserFactory.newSAXParser();
        updateObjectMap(ModuleManager.getResolution(), parser, new XmlHandler());
        writeObjects(MetaFiles.getPath(metaFile).toFile());
        return metaFile;
    } catch (ParserConfigurationException | SAXException | IOException | AxelorException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AxelorException(com.axelor.exception.AxelorException) SAXParser(javax.xml.parsers.SAXParser) MetaFile(com.axelor.meta.db.MetaFile) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException) Transactional(com.google.inject.persist.Transactional)

Aggregations

MetaFile (com.axelor.meta.db.MetaFile)87 File (java.io.File)50 FileInputStream (java.io.FileInputStream)25 IOException (java.io.IOException)24 AxelorException (com.axelor.exception.AxelorException)21 MetaFiles (com.axelor.meta.MetaFiles)18 Transactional (com.google.inject.persist.Transactional)17 ArrayList (java.util.ArrayList)13 Path (java.nio.file.Path)12 InputStream (java.io.InputStream)10 ZipFile (java.util.zip.ZipFile)9 FileOutputStream (java.io.FileOutputStream)8 MetaFileRepository (com.axelor.meta.db.repo.MetaFileRepository)7 ImportHistory (com.axelor.apps.base.db.ImportHistory)6 DMSFile (com.axelor.dms.db.DMSFile)6 HashMap (java.util.HashMap)6 ZipEntry (java.util.zip.ZipEntry)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ZipInputStream (java.util.zip.ZipInputStream)4 App (com.axelor.apps.base.db.App)3