use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.
the class MoveLineExportServiceImpl method writeMoveLineToCsvFile.
protected MetaFile writeMoveLineToCsvFile(Company company, String fileName, String[] columnHeader, List<String[]> allMoveData, AccountingReport accountingReport) throws AxelorException, IOException {
String filePath = accountConfigService.getAccountConfig(company).getExportPath();
String dataExportDir = appAccountService.getDataExportDir();
for (String[] items : allMoveData) {
for (int i = 0; i < items.length; i++) {
if (items[i] != null) {
items[i] = items[i].replaceAll("(\r\n|\n\r|\r|\n|\\|)", " ");
}
}
}
filePath = filePath == null ? dataExportDir : dataExportDir + filePath;
new File(filePath).mkdirs();
log.debug("Full path to export : {}{}", filePath, fileName);
CsvTool.csvWriter(filePath, fileName, '|', columnHeader, allMoveData);
Path path = Paths.get(filePath, fileName);
try (InputStream is = new FileInputStream(path.toFile())) {
return Beans.get(MetaFiles.class).attach(is, fileName, accountingReport).getMetaFile();
}
}
use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.
the class DataImportServiceImpl method importData.
private MetaFile importData(List<CSVInput> inputs) throws IOException {
if (CollectionUtils.isEmpty(inputs)) {
return null;
}
CSVConfig config = new CSVConfig();
config.setInputs(inputs);
if (!CollectionUtils.isEmpty(adapterMap.values())) {
config.getAdapters().addAll(adapterMap.values());
}
CSVImporter importer = new CSVImporter(config, dataDir.getAbsolutePath());
ImporterListener listener = new ImporterListener("importData");
importer.addListener(listener);
importer.setContext(importContext);
importer.run();
if (!listener.isImported()) {
MetaFile logFile = this.createImportLogFile(listener);
return logFile;
}
return null;
}
use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.
the class PrintServiceImpl method generatePDF.
@Override
@Transactional
public Map<String, Object> generatePDF(Print print) throws AxelorException {
try {
print = printRepo.find(print.getId());
String html = generateHtml(print);
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
try (PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfOutputStream))) {
pdfDoc.setDefaultPageSize(print.getDisplayTypeSelect() == PrintRepository.DISPLAY_TYPE_LANDSCAPE ? PageSize.A4.rotate() : PageSize.A4);
if (print.getPrintPdfFooter() != null && !print.getHidePrintSettings()) {
com.itextpdf.layout.Document doc = new com.itextpdf.layout.Document(pdfDoc);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new TableFooterEventHandler(doc, print));
}
ConverterProperties converterProperties = new ConverterProperties();
converterProperties.setBaseUri(attachmentPath);
HtmlConverter.convertToPdf(html, pdfDoc, converterProperties);
}
String documentName = (StringUtils.notEmpty(print.getDocumentName()) ? print.getDocumentName() : "") + "-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + FILE_EXTENSION_PDF;
InputStream pdfInputStream = new ByteArrayInputStream(pdfOutputStream.toByteArray());
MetaFile metaFile = metaFiles.upload(pdfInputStream, documentName);
File file = MetaFiles.getPath(metaFile).toFile();
String fileLink = PdfTool.getFileLinkFromPdfFile(PdfTool.printCopiesToFile(file, 1), metaFile.getFileName());
if (ObjectUtils.notEmpty(file) && file.exists() && (print.getAttach() || StringUtils.notEmpty(print.getMetaFileField()))) {
Class<? extends Model> modelClass = (Class<? extends Model>) Class.forName(print.getMetaModel().getFullName());
Model objectModel = JPA.find(modelClass, print.getObjectId());
if (ObjectUtils.notEmpty(objectModel)) {
if (print.getAttach()) {
metaFiles.attach(metaFile, documentName, objectModel);
}
if (StringUtils.notEmpty(print.getMetaFileField())) {
saveMetaFileInModel(modelClass, objectModel, metaFile, print.getMetaFileField());
}
}
}
if (CollectionUtils.isNotEmpty(print.getPrintSet())) {
for (Print subPrint : print.getPrintSet()) {
generatePDF(subPrint);
}
}
return ActionView.define(documentName).add("html", fileLink).map();
} catch (IOException | ClassNotFoundException e) {
throw new AxelorException(e, TraceBackRepository.CATEGORY_INCONSISTENCY);
}
}
use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.
the class PrintServiceImpl method attachMetaFiles.
@Override
public void attachMetaFiles(Print print, Set<MetaFile> metaFiles) {
Preconditions.checkNotNull(print.getId());
if (metaFiles == null || metaFiles.isEmpty()) {
return;
}
LOG.debug("Add metafiles to object {} : {}", Print.class.getName(), print.getId());
for (MetaFile metaFile : metaFiles) {
Beans.get(MetaFiles.class).attach(metaFile, metaFile.getFileName(), print);
}
}
use of com.axelor.meta.db.MetaFile in project axelor-open-suite by axelor.
the class XmlHandler method exportObject.
@Transactional
public MetaFile exportObject() {
group = Beans.get(GroupRepository.class).all().filter("self.code = 'admins'").fetchOne();
try {
log.debug("Attachment dir: {}", AppService.getFileUploadDir());
String uploadDir = AppService.getFileUploadDir();
if (uploadDir == null || !new File(uploadDir).exists()) {
return null;
}
MetaFile metaFile = new MetaFile();
String fileName = "ExportObject-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyMMddHHmmSS")) + ".csv";
metaFile.setFileName(fileName);
metaFile.setFilePath(fileName);
metaFile = Beans.get(MetaFileRepository.class).save(metaFile);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
saxParserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
saxParserFactory.setXIncludeAware(false);
SAXParser parser = saxParserFactory.newSAXParser();
updateObjectMap(ModuleManager.getResolution(), parser, new XmlHandler());
writeObjects(MetaFiles.getPath(metaFile).toFile());
return metaFile;
} catch (ParserConfigurationException | SAXException | IOException | AxelorException e) {
e.printStackTrace();
}
return null;
}
Aggregations