use of com.axelor.data.Listener in project axelor-open-suite by axelor.
the class DataBackupRestoreService method restore.
/* Restore the Data using provided zip File and prepare Log File and Return it*/
public File restore(MetaFile zipedBackupFile) {
Logger LOG = LoggerFactory.getLogger(getClass());
File tempDir = Files.createTempDir();
String dirPath = tempDir.getAbsolutePath();
StringBuilder sb = new StringBuilder();
try {
unZip(zipedBackupFile, dirPath);
String configFName = tempDir.getAbsolutePath() + File.separator + DataBackupServiceImpl.CONFIG_FILE_NAME;
CSVImporter csvImporter = new CSVImporter(configFName, tempDir.getAbsolutePath());
csvImporter.addListener(new Listener() {
String modelName;
StringBuilder sb1 = new StringBuilder();
@Override
public void handle(Model bean, Exception e) {
if (e.getMessage() != null && !e.getMessage().equals("null")) {
if (bean != null) {
sb1.append(bean.getClass().getSimpleName() + " : \n" + e.getMessage() + "\n\n");
} else {
sb1.append(e.getMessage() + "\n\n");
}
}
}
@Override
public void imported(Model model) {
modelName = model.getClass().getSimpleName();
}
@Override
public void imported(Integer total, Integer count) {
String str = "", strError = "";
if (!StringUtils.isBlank(sb1)) {
strError = "Errors : \n" + sb1.toString();
}
str = "Total Records : {" + total + "} - Success Records : {" + count + "} \n";
if (total != 0 && count != 0) {
sb.append(modelName + " : \n");
}
sb.append(strError).append(str + "-----------------------------------------\n");
sb1.setLength(0);
}
});
csvImporter.run();
LOG.info("Data Restore Completed");
FileUtils.cleanDirectory(new File(tempDir.getAbsolutePath()));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmSS");
String logFileName = "DataBackupLog_" + LocalDateTime.now().format(formatter) + ".log";
File file = new File(tempDir.getAbsolutePath(), logFileName);
PrintWriter pw = new PrintWriter(file);
pw.write(sb.toString());
pw.close();
return file;
} catch (IOException e) {
TraceBackService.trace(e);
return null;
}
}
use of com.axelor.data.Listener 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();
}
}
use of com.axelor.data.Listener in project axelor-open-suite by axelor.
the class WkfModelServiceImpl method getXMLImpoter.
protected XMLImporter getXMLImpoter(String configFile, String dataFile) {
XMLImporter importer = new XMLImporter(configFile, dataFile);
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);
return importer;
}
use of com.axelor.data.Listener in project axelor-open-suite by axelor.
the class ConfiguratorCreatorImportServiceImpl method importConfiguratorCreators.
@Transactional(rollbackOn = { Exception.class })
@Override
public String importConfiguratorCreators(InputStream xmlInputStream, String configFilePath) throws IOException {
InputStream inputStream = this.getClass().getResourceAsStream(configFilePath);
File configFile = File.createTempFile("config", ".xml");
FileOutputStream fout = new FileOutputStream(configFile);
IOUtil.copyCompletely(inputStream, fout);
File tempDir = Files.createTempDir();
File importFile = new File(tempDir, "configurator-creator.xml");
FileUtils.copyInputStreamToFile(xmlInputStream, importFile);
XMLImporter importer = new XMLImporter(configFile.getAbsolutePath(), tempDir.getAbsolutePath());
final StringBuilder importLog = new StringBuilder();
Listener listener = new Listener() {
@Override
public void imported(Integer imported, Integer total) {
importLog.append("Total records: " + total + ", Total imported: " + total);
}
@Override
public void imported(Model arg0) {
try {
completeAfterImport(arg0);
} catch (AxelorException e) {
importLog.append("Error in import: " + Arrays.toString(e.getStackTrace()));
}
}
@Override
public void handle(Model arg0, Exception err) {
importLog.append("Error in import: " + Arrays.toString(err.getStackTrace()));
}
};
importer.addListener(listener);
importer.run();
FileUtils.forceDelete(configFile);
FileUtils.forceDelete(tempDir);
return importLog.toString();
}
use of com.axelor.data.Listener in project axelor-open-suite by axelor.
the class AppLoaderImportServiceImpl method importApp.
protected File importApp(AppLoader appLoader, File dataDir) throws IOException, FileNotFoundException {
File logFile = appLoader.getImportLog() != null ? MetaFiles.getPath(appLoader.getImportLog()).toFile() : MetaFiles.createTempFile("import-", "log").toFile();
PrintWriter pw = new PrintWriter(logFile);
for (File confiFile : getAppImportConfigFiles(dataDir)) {
XMLImporter xmlImporter = new XMLImporter(confiFile.getAbsolutePath(), dataDir.getAbsolutePath());
xmlImporter.setContext(getImportContext(appLoader));
xmlImporter.addListener(new Listener() {
@Override
public void imported(Integer total, Integer success) {
}
@Override
public void imported(Model bean) {
}
@Override
public void handle(Model bean, Exception e) {
pw.write("Error Importing: " + bean);
e.printStackTrace(pw);
}
});
xmlImporter.run();
}
pw.flush();
pw.close();
return logFile;
}
Aggregations