use of com.axelor.data.Importer in project axelor-open-suite by axelor.
the class AppServiceImpl method refreshApp.
@Override
public void refreshApp() throws IOException {
File dataDir = Files.createTempDirectory(null).toFile();
File imgDir = new File(dataDir, "img");
imgDir.mkdir();
CSVConfig csvConfig = new CSVConfig();
csvConfig.setInputs(new ArrayList<>());
List<MetaModel> metaModels = metaModelRepo.all().filter("self.name != 'App' and self.name like 'App%' and self.packageName = ?1", App.class.getPackage().getName()).fetch();
final List<String> appFieldTargetList = Stream.of(JPA.fields(App.class)).filter(p -> p.getType() == PropertyType.ONE_TO_ONE).filter(p -> p.getName().startsWith("app")).map(Property::getTarget).map(Class::getName).collect(Collectors.toList());
log.debug("Total app models: {}", metaModels.size());
for (MetaModel metaModel : metaModels) {
if (!appFieldTargetList.contains(metaModel.getFullName())) {
log.debug("Not a App class : {}", metaModel.getName());
continue;
}
Class<?> klass;
try {
klass = Class.forName(metaModel.getFullName());
} catch (ClassNotFoundException e) {
continue;
}
Object obj = null;
Query query = JPA.em().createQuery("SELECT id FROM " + metaModel.getName());
try {
obj = query.setMaxResults(1).getSingleResult();
} catch (Exception ex) {
}
if (obj != null) {
continue;
}
log.debug("App without app record: {}", metaModel.getName());
String csvName = "base_" + inflector.camelize(klass.getSimpleName(), true) + ".csv";
String pngName = inflector.dasherize(klass.getSimpleName()) + ".png";
CSVInput input = new CSVInput();
input.setFileName(csvName);
input.setTypeName(App.class.getName());
input.setCallable("com.axelor.csv.script.ImportApp:importApp");
input.setSearch("self.code =:code");
input.setSeparator(';');
csvConfig.getInputs().add(input);
CSVInput appInput = new CSVInput();
appInput.setFileName(csvName);
appInput.setTypeName(klass.getName());
appInput.setSearch("self.app.code =:code");
appInput.setSeparator(';');
CSVBind appBind = new CSVBind();
appBind.setColumn("code");
appBind.setField("app");
appBind.setSearch("self.code = :code");
appInput.getBindings().add(appBind);
csvConfig.getInputs().add(appInput);
InputStream stream = klass.getResourceAsStream("/data-init/input/" + csvName);
copyStream(stream, new File(dataDir, csvName));
stream = klass.getResourceAsStream("/data-init/input/img/" + pngName);
copyStream(stream, new File(imgDir, pngName));
}
if (!csvConfig.getInputs().isEmpty()) {
CSVImporter importer = new CSVImporter(csvConfig, dataDir.getAbsolutePath());
importer.run();
}
}
Aggregations