Search in sources :

Example 51 with MetaFile

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

the class ImportCityController method importCity.

/**
 * Import cities
 *
 * @param request
 * @param response
 * @throws InterruptedException
 */
@SuppressWarnings("unchecked")
public void importCity(ActionRequest request, ActionResponse response) {
    try {
        List<ImportHistory> importHistoryList = null;
        Map<String, Object> importCityMap = null;
        MetaFile errorFile = null;
        String typeSelect = (String) request.getContext().get("typeSelect");
        if (CityRepository.TYPE_SELECT_GEONAMES.equals(typeSelect)) {
            String importTypeSelect = (String) request.getContext().get("importTypeSelect");
            switch(importTypeSelect) {
                case CityRepository.IMPORT_TYPE_SELECT_AUTO:
                    String downloadFileName = (String) request.getContext().get("autoImportTypeSelect");
                    importCityMap = Beans.get(ImportCityService.class).importFromGeonamesAutoConfig(downloadFileName, typeSelect);
                    break;
                case CityRepository.IMPORT_TYPE_SELECT_MANUAL:
                    Map<String, Object> map = (LinkedHashMap<String, Object>) request.getContext().get("metaFile");
                    importCityMap = Beans.get(ImportCityService.class).importFromGeonamesManualConfig(map, typeSelect);
                    break;
                default:
                    break;
            }
        }
        if (importCityMap.containsKey("importHistoryList") && importCityMap.containsKey("errorFile")) {
            importHistoryList = (List<ImportHistory>) importCityMap.get("importHistoryList");
            errorFile = (MetaFile) importCityMap.get("errorFile");
            if (errorFile != null) {
                response.setFlash(I18n.get(IExceptionMessage.CITIES_IMPORT_FAILED));
                response.setAttr("errorFile", "hidden", false);
                response.setValue("errorFile", errorFile);
            } else {
                response.setAttr("$importHistoryList", "hidden", false);
                response.setAttr("errorFile", "hidden", true);
                response.setAttr("$importHistoryList", "value", importHistoryList);
                response.setFlash(I18n.get(ITranslation.BASE_GEONAMES_CITY_IMPORT_COMPLETED));
            }
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : MetaFile(com.axelor.meta.db.MetaFile) ImportHistory(com.axelor.apps.base.db.ImportHistory) LinkedHashMap(java.util.LinkedHashMap)

Example 52 with MetaFile

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

the class LunchVoucherMgtServiceImpl method export.

@Transactional(rollbackOn = { Exception.class })
public void export(LunchVoucherMgt lunchVoucherMgt) throws IOException {
    MetaFile metaFile = new MetaFile();
    metaFile.setFileName(I18n.get("LunchVoucherCommand") + " - " + appBaseService.getTodayDate(lunchVoucherMgt.getCompany()).format(DateTimeFormatter.ISO_DATE) + ".csv");
    Path tempFile = MetaFiles.createTempFile(null, ".csv");
    final OutputStream os = new FileOutputStream(tempFile.toFile());
    try (final Writer writer = new OutputStreamWriter(os)) {
        List<String> header = new ArrayList<>();
        header.add(escapeCsv(I18n.get("Company code")));
        header.add(escapeCsv(I18n.get("Lunch Voucher's number")));
        header.add(escapeCsv(I18n.get("Employee")));
        header.add(escapeCsv(I18n.get("Lunch Voucher format")));
        writer.write(Joiner.on(";").join(header));
        for (LunchVoucherMgtLine lunchVoucherMgtLine : lunchVoucherMgt.getLunchVoucherMgtLineList()) {
            List<String> line = new ArrayList<>();
            line.add(escapeCsv(lunchVoucherMgt.getCompany().getCode()));
            line.add(escapeCsv(lunchVoucherMgtLine.getLunchVoucherNumber().toString()));
            line.add(escapeCsv(lunchVoucherMgtLine.getEmployee().getName()));
            line.add(escapeCsv(lunchVoucherMgtLine.getEmployee().getLunchVoucherFormatSelect().toString()));
            writer.write("\n");
            writer.write(Joiner.on(";").join(line));
        }
        Beans.get(MetaFiles.class).upload(tempFile.toFile(), metaFile);
    } catch (Exception e) {
        Throwables.propagate(e);
    } finally {
        Files.deleteIfExists(tempFile);
    }
    /*
     */
    // lunchVoucherMgt.setExported(true);
    lunchVoucherMgt.setCsvFile(metaFile);
    lunchVoucherMgt.setExportDate(appBaseService.getTodayDate(lunchVoucherMgt.getCompany()));
    lunchVoucherMgtRepository.save(lunchVoucherMgt);
}
Also used : Path(java.nio.file.Path) MetaFiles(com.axelor.meta.MetaFiles) ArrayList(java.util.ArrayList) MetaFile(com.axelor.meta.db.MetaFile) AxelorException(com.axelor.exception.AxelorException) Transactional(com.google.inject.persist.Transactional)

Example 53 with MetaFile

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

the class ObjectDataExportServiceImpl method writeExcel.

private MetaFile writeExcel(Map<String, List<String[]>> data) throws IOException {
    XSSFWorkbook workBook = new XSSFWorkbook();
    for (Entry<String, List<String[]>> modelEntry : data.entrySet()) {
        XSSFSheet sheet = workBook.createSheet(modelEntry.getKey());
        int count = 0;
        for (String[] recordArray : modelEntry.getValue()) {
            XSSFRow row = sheet.createRow(count);
            int cellCount = 0;
            for (String val : recordArray) {
                XSSFCell cell = row.createCell(cellCount);
                cell.setCellValue(val);
                cellCount++;
            }
            count++;
        }
    }
    File excelFile = MetaFiles.createTempFile("Data", ".xls").toFile();
    FileOutputStream out = new FileOutputStream(excelFile);
    workBook.write(out);
    out.close();
    return metaFiles.upload(excelFile);
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile)

Example 54 with MetaFile

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

the class ImportAccountChart method importAccountChart.

public Object importAccountChart(Object bean, Map<String, Object> values) throws IOException {
    assert bean instanceof AccountChart;
    AccountChart accountChart = (AccountChart) bean;
    File zipFile = this.getZipFile(accountChart);
    try {
        final MetaFile metaFile = metaFiles.upload(zipFile);
        accountChart.setMetaFile(metaFile);
    } catch (Exception e) {
        e.printStackTrace();
        LOG.warn("Can't load file {} for accountChart {}", zipFile.getName(), accountChart.getName());
    }
    FileUtils.deleteDirectory(zipFile.getParentFile());
    return accountChart;
}
Also used : AccountChart(com.axelor.apps.account.db.AccountChart) MetaFile(com.axelor.meta.db.MetaFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) IOException(java.io.IOException)

Example 55 with MetaFile

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

the class BpmDeploymentServiceImpl method addDmn.

protected void addDmn(DeploymentBuilder deploymentBuilder, Set<MetaFile> dmnFiles) {
    MetaFileRepository metaFileRepo = Beans.get(MetaFileRepository.class);
    for (MetaFile dmnFile : dmnFiles) {
        dmnFile = metaFileRepo.find(dmnFile.getId());
        deploymentBuilder.addModelInstance(dmnFile.getId() + ".dmn", Dmn.readModelFromFile(MetaFiles.getPath(dmnFile).toFile()));
    }
}
Also used : MetaFileRepository(com.axelor.meta.db.repo.MetaFileRepository) MetaFile(com.axelor.meta.db.MetaFile)

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