Search in sources :

Example 1 with DMSFile

use of com.axelor.dms.db.DMSFile in project axelor-open-suite by axelor.

the class UnitCostCalculationServiceImpl method exportUnitCostCalc.

@Override
public MetaFile exportUnitCostCalc(UnitCostCalculation unitCostCalculation, String fileName) throws IOException {
    List<String[]> list = new ArrayList<>();
    List<UnitCostCalcLine> unitCostCalcLineList = unitCostCalculation.getUnitCostCalcLineList();
    Collections.sort(unitCostCalcLineList, new Comparator<UnitCostCalcLine>() {

        @Override
        public int compare(UnitCostCalcLine unitCostCalcLine1, UnitCostCalcLine unitCostCalcLine2) {
            return unitCostCalcLine1.getProduct().getCode().compareTo(unitCostCalcLine2.getProduct().getCode());
        }
    });
    for (UnitCostCalcLine unitCostCalcLine : unitCostCalcLineList) {
        String[] item = new String[4];
        item[0] = unitCostCalcLine.getProduct() == null ? "" : unitCostCalcLine.getProduct().getCode();
        item[1] = unitCostCalcLine.getProduct() == null ? "" : unitCostCalcLine.getProduct().getName();
        item[2] = unitCostCalcLine.getComputedCost().toString();
        item[3] = unitCostCalcLine.getCostToApply().toString();
        list.add(item);
    }
    File file = MetaFiles.createTempFile(fileName, ".csv").toFile();
    log.debug("File located at: {}", file.getPath());
    String[] headers = { I18n.get("Product_code"), I18n.get("Product_name"), I18n.get("Product_currency"), I18n.get("Computed_cost"), I18n.get("Cost_to_apply") };
    CsvTool.csvWriter(file.getParent(), file.getName(), ';', '"', headers, list);
    try (InputStream is = new FileInputStream(file)) {
        DMSFile dmsFile = Beans.get(MetaFiles.class).attach(is, file.getName(), unitCostCalculation);
        return dmsFile.getMetaFile();
    }
}
Also used : MetaFiles(com.axelor.meta.MetaFiles) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) UnitCostCalcLine(com.axelor.apps.production.db.UnitCostCalcLine) DMSFile(com.axelor.dms.db.DMSFile) DMSFile(com.axelor.dms.db.DMSFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile)

Example 2 with DMSFile

use of com.axelor.dms.db.DMSFile in project axelor-open-suite by axelor.

the class TemplateMessageServiceImpl method getMetaFiles.

@Override
public Set<MetaFile> getMetaFiles(Template template, Templates templates, Map<String, Object> templatesContext) throws AxelorException, IOException {
    List<DMSFile> metaAttachments = Query.of(DMSFile.class).filter("self.relatedId = ?1 AND self.relatedModel = ?2", template.getId(), EntityHelper.getEntityClass(template).getName()).fetch();
    Set<MetaFile> metaFiles = Sets.newHashSet();
    for (DMSFile metaAttachment : metaAttachments) {
        if (!metaAttachment.getIsDirectory())
            metaFiles.add(metaAttachment.getMetaFile());
    }
    log.debug("Metafile to attach: {}", metaFiles);
    return metaFiles;
}
Also used : DMSFile(com.axelor.dms.db.DMSFile) MetaFile(com.axelor.meta.db.MetaFile)

Example 3 with DMSFile

use of com.axelor.dms.db.DMSFile in project axelor-open-suite by axelor.

the class DeclarationOfExchangesExporter method attach.

protected String attach(String path) {
    DMSFile dmsFile;
    try (InputStream is = new FileInputStream(path)) {
        dmsFile = Beans.get(MetaFiles.class).attach(is, getFileName(), declarationOfExchanges);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    MetaFile metaFile = dmsFile.getMetaFile();
    return String.format("ws/rest/com.axelor.meta.db.MetaFile/%d/content/download?v=%d/%s", metaFile.getId(), metaFile.getVersion(), path);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DMSFile(com.axelor.dms.db.DMSFile) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) MetaFile(com.axelor.meta.db.MetaFile) FileInputStream(java.io.FileInputStream)

Example 4 with DMSFile

use of com.axelor.dms.db.DMSFile in project axelor-open-suite by axelor.

the class JobApplicationServiceImpl method setDMSFile.

@Override
@Transactional(rollbackOn = { Exception.class })
public void setDMSFile(JobApplication jobApplication) {
    if (jobApplication.getResume() == null) {
        DMSFile toDelete = dmsFileRepo.find(jobApplication.getResumeId());
        if (toDelete != null) {
            metaFiles.delete(toDelete);
        }
        jobApplication.setResumeId(null);
    } else {
        MetaFile resume = jobApplication.getResume();
        DMSFile resumeFile = metaFiles.attach(resume, resume.getFileName(), jobApplication);
        jobApplication.setResumeId(resumeFile.getId());
    }
    jobApplicationRepo.save(jobApplication);
}
Also used : DMSFile(com.axelor.dms.db.DMSFile) MetaFile(com.axelor.meta.db.MetaFile) Transactional(com.google.inject.persist.Transactional)

Example 5 with DMSFile

use of com.axelor.dms.db.DMSFile in project axelor-open-suite by axelor.

the class DMSImportWizardServiceImpl method createDmsTree.

@Transactional
public void createDmsTree(ZipInputStream zipInputStream, ZipFile zipfile) throws IOException {
    ZipEntry zipEntry = zipInputStream.getNextEntry();
    Map<String, DMSFile> dmsMap = new HashMap<>();
    while (zipEntry != null) {
        String fileName = getFileName(zipEntry);
        DMSFile dmsFile = new DMSFile();
        dmsFile.setFileName(fileName);
        String parentName = getParentName(zipEntry, fileName);
        dmsFile.setParent(dmsMap.get(parentName));
        LOG.debug("Praent file: {}", dmsFile.getParent());
        if (zipEntry.isDirectory()) {
            dmsFile.setIsDirectory(true);
            dmsFile = Beans.get(DMSFileRepository.class).save(dmsFile);
            dmsMap.put(zipEntry.getName(), dmsFile);
            LOG.debug("DMS Directory created: {}", dmsFile.getFileName());
        } else {
            String fileType = fileName.substring(fileName.indexOf(".") + 1);
            try {
                File tempDir = File.createTempFile("", "");
                File file = new File(tempDir, fileName);
                InputStream is = zipfile.getInputStream(zipEntry);
                FileUtils.copyInputStreamToFile(is, file);
                MetaFiles.checkType(file);
                MetaFile metaFile = metaFiles.upload(zipInputStream, fileName);
                dmsFile.setMetaFile(metaFile);
                dmsFile = Beans.get(DMSFileRepository.class).save(dmsFile);
                LOG.debug("DMS File created: {}", dmsFile.getFileName());
            } catch (IllegalArgumentException e) {
                LOG.debug("File type is not allowed : {}", fileType);
            }
        }
        zipEntry = zipInputStream.getNextEntry();
    }
}
Also used : HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) DMSFile(com.axelor.dms.db.DMSFile) MetaFile(com.axelor.meta.db.MetaFile) DMSFile(com.axelor.dms.db.DMSFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) Transactional(com.google.inject.persist.Transactional)

Aggregations

DMSFile (com.axelor.dms.db.DMSFile)6 MetaFile (com.axelor.meta.db.MetaFile)5 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Transactional (com.google.inject.persist.Transactional)2 File (java.io.File)2 UnitCostCalcLine (com.axelor.apps.production.db.UnitCostCalcLine)1 DMSFileService (com.axelor.apps.projectdms.service.DMSFileService)1 MetaFiles (com.axelor.meta.MetaFiles)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ZipInputStream (java.util.zip.ZipInputStream)1