Search in sources :

Example 81 with MetaFile

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

the class DataImportServiceImpl method importData.

@Override
public MetaFile importData(AdvancedImport advancedImport) throws IOException, AxelorException, ClassNotFoundException {
    adapterMap = new HashMap<String, DataAdapter>();
    importContext = new HashMap<String, Object>();
    language = advancedImport.getLanguageSelect();
    dataDir = Files.createTempDir();
    String extension = Files.getFileExtension(advancedImport.getImportFile().getFileName());
    DataReaderService reader = dataReaderFactory.getDataReader(extension);
    reader.initialize(advancedImport.getImportFile(), advancedImport.getFileSeparator());
    List<CSVInput> inputs = this.process(reader, advancedImport);
    if (advancedImport.getAttachment() != null) {
        this.processAttachments(advancedImport.getAttachment());
    }
    MetaFile logFile = this.importData(inputs);
    FileUtils.forceDelete(dataDir);
    return logFile;
}
Also used : DataAdapter(com.axelor.data.adapter.DataAdapter) DataReaderService(com.axelor.apps.tool.reader.DataReaderService) CSVInput(com.axelor.data.csv.CSVInput) MetaFile(com.axelor.meta.db.MetaFile)

Example 82 with MetaFile

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

the class DataImportServiceImpl method processAttachments.

private void processAttachments(MetaFile attachments) throws ZipException, IOException {
    if (dataDir.isDirectory() && dataDir.list().length == 0) {
        return;
    }
    File attachmentFile = MetaFiles.getPath(attachments).toFile();
    try (ZipInputStream zis = new ZipInputStream(new FileInputStream(attachmentFile))) {
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        while ((ze = zis.getNextEntry()) != null) {
            String fileName = ze.getName();
            File extractFile = new File(dataDir, fileName);
            if (ze.isDirectory()) {
                extractFile.mkdirs();
                continue;
            } else {
                extractFile.getParentFile().mkdirs();
                extractFile.createNewFile();
            }
            try (FileOutputStream fos = new FileOutputStream(extractFile)) {
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
            }
            zis.closeEntry();
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) FileInputStream(java.io.FileInputStream)

Example 83 with MetaFile

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

the class ConvertDemoDataFileServiceImpl method convertDemoDataExcelFile.

@Override
public MetaFile convertDemoDataExcelFile(File excelFile) throws IOException, AxelorException, ParseException {
    File zipFile = this.createZIPFromExcel(excelFile);
    FileInputStream inStream = new FileInputStream(zipFile);
    MetaFile metaFile = metaFiles.upload(inStream, "demo_data_" + new SimpleDateFormat("ddMMyyyHHmm").format(new Date()) + ".zip");
    inStream.close();
    zipFile.delete();
    return metaFile;
}
Also used : MetaFile(com.axelor.meta.db.MetaFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) SimpleDateFormat(java.text.SimpleDateFormat) FileInputStream(java.io.FileInputStream) Date(java.util.Date)

Example 84 with MetaFile

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

the class MessageServiceImpl method sendByEmail.

@Override
@Transactional(rollbackOn = { Exception.class })
public Message sendByEmail(Message message, Boolean isTemporaryEmail) throws MessagingException, AxelorException {
    EmailAccount mailAccount = message.getMailAccount();
    if (mailAccount == null) {
        return message;
    }
    log.debug("Sending email...");
    MailAccountService mailAccountService = Beans.get(MailAccountService.class);
    com.axelor.mail.MailAccount account = new SmtpAccount(mailAccount.getHost(), mailAccount.getPort().toString(), mailAccount.getLogin(), mailAccountService.getDecryptPassword(mailAccount.getPassword()), mailAccountService.getSecurity(mailAccount));
    List<String> replytoRecipients = this.getEmailAddresses(message.getReplyToEmailAddressSet());
    List<String> toRecipients = this.getEmailAddresses(message.getToEmailAddressSet());
    List<String> ccRecipients = this.getEmailAddresses(message.getCcEmailAddressSet());
    List<String> bccRecipients = this.getEmailAddresses(message.getBccEmailAddressSet());
    if (toRecipients.isEmpty() && ccRecipients.isEmpty() && bccRecipients.isEmpty()) {
        throw new AxelorException(message, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MESSAGE_6));
    }
    MailSender sender = new MailSender(account);
    MailBuilder mailBuilder = sender.compose();
    mailBuilder.subject(message.getSubject());
    if (!Strings.isNullOrEmpty(mailAccount.getFromAddress())) {
        String fromAddress = mailAccount.getFromAddress();
        if (!Strings.isNullOrEmpty(mailAccount.getFromName())) {
            fromAddress = String.format("%s <%s>", mailAccount.getFromName(), mailAccount.getFromAddress());
        } else if (message.getFromEmailAddress() != null) {
            if (!Strings.isNullOrEmpty(message.getFromEmailAddress().getAddress())) {
                log.debug("Override from :::  {}", this.getFullEmailAddress(message.getFromEmailAddress()));
                mailBuilder.from(this.getFullEmailAddress(message.getFromEmailAddress()));
            } else {
                throw new AxelorException(message, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, IExceptionMessage.MESSAGE_5);
            }
        }
        mailBuilder.from(fromAddress);
    }
    if (replytoRecipients != null && !replytoRecipients.isEmpty()) {
        mailBuilder.replyTo(Joiner.on(",").join(replytoRecipients));
    }
    if (!toRecipients.isEmpty()) {
        mailBuilder.to(Joiner.on(",").join(toRecipients));
    }
    if (ccRecipients != null && !ccRecipients.isEmpty()) {
        mailBuilder.cc(Joiner.on(",").join(ccRecipients));
    }
    if (bccRecipients != null && !bccRecipients.isEmpty()) {
        mailBuilder.bcc(Joiner.on(",").join(bccRecipients));
    }
    if (!Strings.isNullOrEmpty(message.getContent())) {
        mailBuilder.html(message.getContent());
    }
    if (!isTemporaryEmail) {
        for (MetaAttachment metaAttachment : getMetaAttachments(message)) {
            MetaFile metaFile = metaAttachment.getMetaFile();
            mailBuilder.attach(metaFile.getFileName(), MetaFiles.getPath(metaFile).toString());
        }
        getEntityManager().flush();
        getEntityManager().lock(message, LockModeType.PESSIMISTIC_WRITE);
        // send email using a separate process to avoid thread blocking
        sendMailQueueService.submitMailJob(mailBuilder, message);
    } else {
        // No separate thread or JPA persistence lock required
        try {
            mailBuilder.send();
        } catch (IOException e) {
            log.debug("Exception when sending email", e);
            TraceBackService.trace(e);
        }
        log.debug("Email sent.");
    }
    return message;
}
Also used : AxelorException(com.axelor.exception.AxelorException) EmailAccount(com.axelor.apps.message.db.EmailAccount) SmtpAccount(com.axelor.mail.SmtpAccount) MailSender(com.axelor.mail.MailSender) IOException(java.io.IOException) MailBuilder(com.axelor.mail.MailBuilder) MetaFile(com.axelor.meta.db.MetaFile) MetaAttachment(com.axelor.meta.db.MetaAttachment) Transactional(com.google.inject.persist.Transactional)

Example 85 with MetaFile

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

the class CSVReaderService method initialize.

@Override
public boolean initialize(MetaFile input, String separator) {
    if (input == null) {
        return false;
    }
    fileName = input.getFileName().replaceAll(".csv", "");
    File inFile = MetaFiles.getPath(input).toFile();
    if (!inFile.exists()) {
        return false;
    }
    try (FileInputStream inSteam = new FileInputStream(inFile)) {
        csvReader = new CSVReader(new InputStreamReader(inSteam, StandardCharsets.UTF_8), separator.charAt(0));
        totalRows = csvReader.readAll();
        if (csvReader.getLinesRead() == 0) {
            return false;
        }
    } catch (IOException e) {
        LOG.error(e.getMessage());
        return false;
    }
    return true;
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) IOException(java.io.IOException) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) FileInputStream(java.io.FileInputStream)

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