Search in sources :

Example 56 with MetaFile

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

the class ImportAdvancedImport method importPicture.

public Object importPicture(String value, String pathVal) throws IOException {
    if (Strings.isNullOrEmpty(value)) {
        return null;
    }
    Path path = Paths.get(pathVal);
    if (Strings.isNullOrEmpty(value)) {
        return null;
    }
    File image = path.resolve(value).toFile();
    if (!image.exists() || image.isDirectory()) {
        return null;
    }
    MetaFile metaFile = metaFiles.upload(image);
    return metaFile;
}
Also used : Path(java.nio.file.Path) MetaFile(com.axelor.meta.db.MetaFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile)

Example 57 with MetaFile

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

the class WkfModelServiceImpl method importWkfModels.

@Override
public String importWkfModels(MetaFile metaFile, boolean isTranslate, String sourceLanguage, String targetLanguage) throws AxelorException {
    if (metaFile == null) {
        return null;
    }
    String extension = Files.getFileExtension(metaFile.getFileName());
    if (extension == null || !extension.equals("xml")) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(ITranslation.INVALID_WKF_MODEL_XML));
    }
    try {
        InputStream inputStream = getClass().getResourceAsStream(IMPORT_CONFIG_PATH);
        File configFile = File.createTempFile("config", ".xml");
        FileOutputStream fout = new FileOutputStream(configFile);
        IOUtil.copyCompletely(inputStream, fout);
        File xmlFile = MetaFiles.getPath(metaFile).toFile();
        File tempDir = Files.createTempDir();
        File importFile = new File(tempDir, "wkfModels.xml");
        Files.copy(xmlFile, importFile);
        if (isTranslate) {
            importFile = this.translateNodeName(importFile, sourceLanguage, targetLanguage);
        }
        XMLImporter importer = new XMLImporter(configFile.getAbsolutePath(), tempDir.getAbsolutePath());
        final StringBuilder log = new StringBuilder();
        Listener listner = new Listener() {

            @Override
            public void imported(Integer imported, Integer total) {
            }

            @Override
            public void imported(Model arg0) {
            }

            @Override
            public void handle(Model arg0, Exception err) {
                log.append("Error in import: " + err.getStackTrace().toString());
            }
        };
        importer.addListener(listner);
        importer.run();
        FileUtils.forceDelete(configFile);
        FileUtils.forceDelete(tempDir);
        FileUtils.forceDelete(xmlFile);
        MetaFileRepository metaFileRepository = Beans.get(MetaFileRepository.class);
        metaFileRepository.remove(metaFile);
        return log.toString();
    } catch (Exception e) {
        return e.getMessage();
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) MetaFileRepository(com.axelor.meta.db.repo.MetaFileRepository) Listener(com.axelor.data.Listener) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) WkfModel(com.axelor.apps.bpm.db.WkfModel) MetaModel(com.axelor.meta.db.MetaModel) Model(com.axelor.db.Model) XMLImporter(com.axelor.data.xml.XMLImporter) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) AxelorException(com.axelor.exception.AxelorException)

Example 58 with MetaFile

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

the class ImportProduct method importProduct.

public Object importProduct(Object bean, Map<String, Object> values) {
    assert bean instanceof Product;
    Product product = (Product) bean;
    String fileName = (String) values.get("picture_fileName");
    if (!StringUtils.isEmpty(fileName)) {
        final Path path = (Path) values.get("__path__");
        try {
            final File image = path.resolve(fileName).toFile();
            if (image != null && image.isFile()) {
                final MetaFile metaFile = metaFiles.upload(image);
                product.setPicture(metaFile);
            } else {
                LOG.debug("No image file found: {}", image == null ? path.toAbsolutePath() : image.getAbsolutePath());
            }
        } catch (Exception e) {
            LOG.error("Error when importing product picture : {}", e);
        }
    }
    return productRepo.save(product);
}
Also used : Path(java.nio.file.Path) Product(com.axelor.apps.base.db.Product) MetaFile(com.axelor.meta.db.MetaFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) AxelorException(com.axelor.exception.AxelorException)

Example 59 with MetaFile

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

the class HumanResourceMobileController method insertOrUpdateExpenseLine.

/*
   * This method is used in mobile application.
   * It was in ExpenseServiceImpl
   * @param request
   * @param response
   *
   * POST /open-suite-webapp/ws/action/com.axelor.apps.hr.mobile.HumanResourceMobileController:insertOrUpdateExpenseLine
   * Content-Type: application/json
   *
   * URL: com.axelor.apps.hr.mobile.HumanResourceMobileController:insertOrUpdateExpenseLine
   * fields: (id,) project, expenseType, date, comments, toInvoice, unTaxTotal, taxTotal, justification
   *
   * payload:
   * { "data": {
   * 		"action": "com.axelor.apps.hr.mobile.HumanResourceMobileController:insertOrUpdateExpenseLine",
   *        "id": 1,
   * 		"project": 2,
   * 		"expenseType": 10,
   * 		"date": "2018-02-22",
   * 		"comments": "No",
   * 		"toInvoice": "no",
   * 		"unTaxTotal": 100,
   *	 	"taxTotal": 2,
   *		"justification": "no"
   * } }
   */
@Transactional
public void insertOrUpdateExpenseLine(ActionRequest request, ActionResponse response) {
    try {
        User user = AuthUtils.getUser();
        Map<String, Object> requestData = request.getData();
        Project project = Beans.get(ProjectRepository.class).find(Long.valueOf(requestData.get("project").toString()));
        Product product = Beans.get(ProductRepository.class).find(Long.valueOf(requestData.get("expenseType").toString()));
        if (user != null) {
            ExpenseService expenseService = Beans.get(ExpenseService.class);
            Expense expense = expenseService.getOrCreateExpense(user);
            ExpenseLine expenseLine;
            Object idO = requestData.get("id");
            if (idO != null) {
                expenseLine = Beans.get(ExpenseLineRepository.class).find(Long.valueOf(idO.toString()));
            } else {
                expenseLine = new ExpenseLine();
            }
            expenseLine.setExpenseDate(LocalDate.parse(requestData.get("date").toString(), DateTimeFormatter.ISO_DATE));
            expenseLine.setComments(requestData.get("comments").toString());
            expenseLine.setExpenseProduct(product);
            expenseLine.setProject(project);
            expenseLine.setUser(user);
            expenseLine.setTotalAmount(new BigDecimal(requestData.get("unTaxTotal").toString()));
            expenseLine.setTotalTax(new BigDecimal(requestData.get("taxTotal").toString()));
            expenseLine.setUntaxedAmount(expenseLine.getTotalAmount().subtract(expenseLine.getTotalTax()));
            expenseLine.setToInvoice(new Boolean(requestData.get("toInvoice").toString()));
            String justification = (String) requestData.get("justification");
            if (!Strings.isNullOrEmpty(justification)) {
                String MIME_IMAGE_X_ICON = "image/x-icon";
                String MIME_IMAGE_SVG_XML = "image/svg+xml";
                String MIME_IMAGE_BMP = "image/bmp";
                String MIME_IMAGE_GIF = "image/gif";
                String MIME_IMAGE_JPEG = "image/jpeg";
                String MIME_IMAGE_TIFF = "image/tiff";
                String MIME_IMAGE_PNG = "image/png";
                Map<String, String> mimeTypeMapping = new HashMap<>(200);
                mimeTypeMapping.put(MIME_IMAGE_X_ICON, "ico");
                mimeTypeMapping.put(MIME_IMAGE_SVG_XML, "svg");
                mimeTypeMapping.put(MIME_IMAGE_BMP, "bmp");
                mimeTypeMapping.put(MIME_IMAGE_GIF, "gif");
                mimeTypeMapping.put(MIME_IMAGE_JPEG, "jpg");
                mimeTypeMapping.put(MIME_IMAGE_TIFF, "tif");
                mimeTypeMapping.put(MIME_IMAGE_PNG, "png");
                byte[] decodedFile = Base64.getDecoder().decode(justification);
                String formatName = URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(decodedFile));
                String extension = "";
                if (mimeTypeMapping.containsKey(formatName)) {
                    extension = "." + mimeTypeMapping.get(formatName);
                    File file = MetaFiles.createTempFile("justification", extension).toFile();
                    Files.write(decodedFile, file);
                    MetaFile metaFile = Beans.get(MetaFiles.class).upload(file);
                    expenseLine.setJustificationMetaFile(metaFile);
                }
            }
            expense.addGeneralExpenseLineListItem(expenseLine);
            expense = expenseService.compute(expense);
            Beans.get(ExpenseRepository.class).save(expense);
            HashMap<String, Object> data = new HashMap<>();
            data.put("id", expenseLine.getId());
            response.setData(data);
            response.setTotal(1);
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : MetaFiles(com.axelor.meta.MetaFiles) User(com.axelor.auth.db.User) ProjectRepository(com.axelor.apps.project.db.repo.ProjectRepository) HashMap(java.util.HashMap) ProductRepository(com.axelor.apps.base.db.repo.ProductRepository) Product(com.axelor.apps.base.db.Product) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) Project(com.axelor.apps.project.db.Project) Expense(com.axelor.apps.hr.db.Expense) ByteArrayInputStream(java.io.ByteArrayInputStream) ExpenseRepository(com.axelor.apps.hr.db.repo.ExpenseRepository) ExpenseService(com.axelor.apps.hr.service.expense.ExpenseService) ExpenseLine(com.axelor.apps.hr.db.ExpenseLine) MetaFile(com.axelor.meta.db.MetaFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) Transactional(com.google.inject.persist.Transactional)

Example 60 with MetaFile

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

the class ImportVehicle method importVehicle.

public Object importVehicle(Object bean, Map<String, Object> values) {
    assert bean instanceof Vehicle;
    Vehicle vehicle = (Vehicle) bean;
    final Path path = (Path) values.get("__path__");
    String fileName = (String) values.get("image_fileName");
    if (Strings.isNullOrEmpty(fileName)) {
        return bean;
    }
    final File image = path.resolve(fileName).toFile();
    try {
        final MetaFile metaFile = metaFiles.upload(image);
        vehicle.setImage(metaFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bean;
}
Also used : Vehicle(com.axelor.apps.fleet.db.Vehicle) Path(java.nio.file.Path) MetaFile(com.axelor.meta.db.MetaFile) IOException(java.io.IOException) MetaFile(com.axelor.meta.db.MetaFile) File(java.io.File)

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