Search in sources :

Example 6 with MetaFile

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

the class CatalogController method showPdf.

public void showPdf(ActionRequest request, ActionResponse response) {
    try {
        Catalog catalog = Beans.get(CatalogRepository.class).find(request.getContext().asType(Catalog.class).getId());
        MetaFile pdf = catalog.getPdfFile();
        String title = catalog.getName();
        Path path = MetaFiles.getPath(pdf.getFileName());
        String fileLink = PdfTool.getFileLinkFromPdfFile(PdfTool.printCopiesToFile(path.toFile(), 1), title + ".pdf");
        response.setView(ActionView.define(title).add("html", fileLink).map());
    } catch (Exception e) {
        TraceBackService.trace(e);
    }
}
Also used : Path(java.nio.file.Path) CatalogRepository(com.axelor.apps.crm.db.repo.CatalogRepository) MetaFile(com.axelor.meta.db.MetaFile) Catalog(com.axelor.apps.crm.db.Catalog)

Example 7 with MetaFile

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

the class InventoryService method exportInventoryAsCSV.

@Transactional(rollbackOn = { Exception.class })
public MetaFile exportInventoryAsCSV(Inventory inventory) throws IOException {
    List<String[]> list = new ArrayList<>();
    for (InventoryLine inventoryLine : inventory.getInventoryLineList()) {
        String[] item = new String[9];
        String realQty = "";
        item[0] = (inventoryLine.getProduct() == null) ? "" : inventoryLine.getProduct().getName();
        item[1] = (inventoryLine.getProduct() == null) ? "" : inventoryLine.getProduct().getCode();
        item[2] = (inventoryLine.getProduct() == null) ? "" : ((inventoryLine.getProduct().getProductCategory() == null) ? "" : inventoryLine.getProduct().getProductCategory().getName());
        item[3] = (inventoryLine.getRack() == null) ? "" : inventoryLine.getRack();
        item[4] = (inventoryLine.getTrackingNumber() == null) ? "" : inventoryLine.getTrackingNumber().getTrackingNumberSeq();
        item[5] = inventoryLine.getCurrentQty().toString();
        if (inventoryLine.getRealQty() != null && inventory.getStatusSelect() != InventoryRepository.STATUS_DRAFT && inventory.getStatusSelect() != InventoryRepository.STATUS_PLANNED) {
            realQty = inventoryLine.getRealQty().toString();
        }
        item[6] = realQty;
        item[7] = (inventoryLine.getDescription() == null) ? "" : inventoryLine.getDescription();
        String lastInventoryDateTString = "";
        StockLocationLine stockLocationLine = stockLocationLineService.getStockLocationLine(inventory.getStockLocation(), inventoryLine.getProduct());
        if (stockLocationLine != null) {
            ZonedDateTime lastInventoryDateT = stockLocationLine.getLastInventoryDateT();
            lastInventoryDateTString = lastInventoryDateT == null ? "" : lastInventoryDateT.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
        }
        item[8] = lastInventoryDateTString;
        list.add(item);
    }
    Collections.sort(list, new // sort the list by code product
    Comparator<String[]>() {

        @Override
        public int compare(String[] strings, String[] otherStrings) {
            return strings[1].compareTo(otherStrings[1]);
        }
    });
    String fileName = computeExportFileName(inventory);
    File file = MetaFiles.createTempFile(fileName, ".csv").toFile();
    log.debug("File Located at: {}", file.getPath());
    String[] headers = { PRODUCT_NAME, PRODUCT_CODE, PRODUCT_CATEGORY, RACK, TRACKING_NUMBER, CURRENT_QUANTITY, REAL_QUANTITY, DESCRIPTION, LAST_INVENTORY_DATE };
    CsvTool.csvWriter(file.getParent(), file.getName(), ';', '"', headers, list);
    try (InputStream is = new FileInputStream(file)) {
        return Beans.get(MetaFiles.class).upload(is, fileName + ".csv");
    }
}
Also used : MetaFiles(com.axelor.meta.MetaFiles) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) ZonedDateTime(java.time.ZonedDateTime) StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) InventoryLine(com.axelor.apps.stock.db.InventoryLine) Transactional(com.google.inject.persist.Transactional)

Example 8 with MetaFile

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

the class ProductBaseRepository method save.

@Override
public Product save(Product product) {
    try {
        if (appBaseService.getAppBase().getGenerateProductSequence() && Strings.isNullOrEmpty(product.getCode())) {
            product.setCode(Beans.get(ProductService.class).getSequence(product));
        }
    } catch (Exception e) {
        TraceBackService.traceExceptionFromSaveMethod(e);
        throw new PersistenceException(e.getMessage(), e);
    }
    product.setFullName(String.format(FULL_NAME_FORMAT, product.getCode(), product.getName()));
    if (product.getId() != null) {
        Product oldProduct = Beans.get(ProductRepository.class).find(product.getId());
        translationService.updateFormatedValueTranslations(oldProduct.getFullName(), FULL_NAME_FORMAT, product.getCode(), product.getName());
    } else {
        translationService.createFormatedValueTranslations(FULL_NAME_FORMAT, product.getCode(), product.getName());
    }
    product = super.save(product);
    if (product.getBarCode() == null && appBaseService.getAppBase().getActivateBarCodeGeneration()) {
        try {
            boolean addPadding = false;
            InputStream inStream;
            if (!appBaseService.getAppBase().getEditProductBarcodeType()) {
                inStream = barcodeGeneratorService.createBarCode(product.getSerialNumber(), appBaseService.getAppBase().getBarcodeTypeConfig(), addPadding);
            } else {
                inStream = barcodeGeneratorService.createBarCode(product.getSerialNumber(), product.getBarcodeTypeConfig(), addPadding);
            }
            if (inStream != null) {
                MetaFile barcodeFile = metaFiles.upload(inStream, String.format("ProductBarCode%d.png", product.getId()));
                product.setBarCode(barcodeFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AxelorException e) {
            TraceBackService.traceExceptionFromSaveMethod(e);
            throw new ValidationException(e);
        }
    }
    return super.save(product);
}
Also used : AxelorException(com.axelor.exception.AxelorException) ValidationException(javax.validation.ValidationException) InputStream(java.io.InputStream) PersistenceException(javax.persistence.PersistenceException) Product(com.axelor.apps.base.db.Product) MetaFile(com.axelor.meta.db.MetaFile) IOException(java.io.IOException) IOException(java.io.IOException) AxelorException(com.axelor.exception.AxelorException) PersistenceException(javax.persistence.PersistenceException) ValidationException(javax.validation.ValidationException)

Example 9 with MetaFile

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

the class CampaignServiceImpl method generateLog.

protected MetaFile generateLog(String errorPartners, String errorLeads, MetaFile metaFile, Long campaignId) {
    if (metaFile == null) {
        metaFile = new MetaFile();
        metaFile.setFileName("EmailLog" + campaignId + ".text");
    }
    StringBuilder builder = new StringBuilder();
    builder.append(I18n.get(IExceptionMessage.EMAIL_ERROR1));
    builder.append("\n");
    if (!errorPartners.isEmpty()) {
        builder.append(I18n.get("Partners") + ":\n");
        builder.append(errorPartners);
    }
    if (!errorLeads.isEmpty()) {
        builder.append(I18n.get("Leads") + ":\n");
        builder.append(errorLeads);
    }
    ByteArrayInputStream stream = new ByteArrayInputStream(builder.toString().getBytes());
    try {
        return Beans.get(MetaFiles.class).upload(stream, metaFile.getFileName());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : MetaFiles(com.axelor.meta.MetaFiles) ByteArrayInputStream(java.io.ByteArrayInputStream) MetaFile(com.axelor.meta.db.MetaFile) IOException(java.io.IOException)

Example 10 with MetaFile

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

the class ImportService method importImg.

// Import methods specific for import from AppBuilder
private MetaFile importImg(String name, String data) {
    if (data == null) {
        return null;
    }
    byte[] img = Base64.getDecoder().decode(data);
    ByteArrayInputStream inImg = new ByteArrayInputStream(img);
    MetaFile metaFile = metaFileRepo.all().filter("self.fileName = ?1", name).fetchOne();
    try {
        if (metaFile != null) {
            return metaFiles.upload(inImg, metaFile);
        } else {
            return metaFiles.upload(inImg, name);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MetaFile(com.axelor.meta.db.MetaFile) IOException(java.io.IOException)

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