use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class PrintTemplateLineServiceImpl method checkExpression.
@SuppressWarnings("unchecked")
@Transactional
@Override
public void checkExpression(Long objectId, MetaModel metaModel, PrintTemplateLine printTemplateLine) throws ClassNotFoundException, AxelorException {
if (metaModel == null) {
return;
}
String model = metaModel.getFullName();
String simpleModel = metaModel.getName();
PrintTemplateLineTest test = printTemplateLine.getPrintTemplateLineTest();
Context scriptContext = null;
if (StringUtils.notEmpty(model)) {
Class<? extends Model> modelClass = (Class<? extends Model>) Class.forName(model);
Model modelObject = JPA.find(modelClass, objectId);
if (ObjectUtils.notEmpty(modelObject)) {
scriptContext = new Context(Mapper.toMap(modelObject), modelClass);
}
}
String resultOfTitle = null;
String resultOfContent = null;
Locale locale = Optional.ofNullable(printTemplateLine.getPrintTemplate().getLanguage()).map(Language::getCode).map(Locale::new).orElseGet(AppFilter::getLocale);
TemplateMaker maker = initMaker(objectId, model, simpleModel, locale);
try {
if (StringUtils.notEmpty(printTemplateLine.getTitle())) {
maker.setTemplate(printTemplateLine.getTitle());
resultOfTitle = maker.make();
}
} catch (Exception e) {
resultOfTitle = "Error in title";
}
try {
if (StringUtils.notEmpty(printTemplateLine.getContent())) {
maker.setTemplate(printTemplateLine.getContent());
resultOfContent = maker.make();
}
} catch (Exception e) {
resultOfContent = "Error in content";
}
test.setContentResult(resultOfTitle + "<br>" + resultOfContent);
Boolean present = Boolean.TRUE;
if (StringUtils.notEmpty(printTemplateLine.getConditions())) {
Object evaluation = null;
try {
evaluation = templateContextService.computeTemplateContext(printTemplateLine.getConditions(), scriptContext);
} catch (Exception e) {
throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.PRINT_TEMPLATE_CONDITION_MUST_BE_BOOLEAN));
}
if (evaluation instanceof Boolean) {
present = (Boolean) evaluation;
}
}
test.setConditionsResult(present);
printTemplateLineRepo.save(printTemplateLine);
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class ConfiguratorServiceImpl method fixRelationalField.
protected void fixRelationalField(Model parentModel, Model value, MetaField metaField) throws AxelorException {
if (value != null) {
Mapper mapper = Mapper.of(parentModel.getClass());
try {
String className = MetaTool.computeFullClassName(metaField);
Model manyToOneDbValue = JPA.find((Class<Model>) Class.forName(className), value.getId());
mapper.set(parentModel, metaField.getName(), manyToOneDbValue);
} catch (Exception e) {
throw new AxelorException(Configurator.class, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, e.getMessage());
}
}
}
use of com.axelor.db.Model 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.db.Model in project axelor-open-suite by axelor.
the class BamlModelController method execute.
public void execute(ActionRequest request, ActionResponse response) {
Context context = request.getContext();
String model = (String) context.get("modelName");
Model entity = null;
if (context.get("recordId") != null && model != null) {
Long recordId = Long.parseLong(context.get("recordId").toString());
entity = WkfContextHelper.getRepository(model).find(recordId);
}
Map<String, Object> bamlModelMap = (Map<String, Object>) context.get("bamlModel");
BamlModel bamlModel = Beans.get(BamlModelRepository.class).find(Long.parseLong(bamlModelMap.get("id").toString()));
Model object = Beans.get(BamlService.class).execute(bamlModel, entity);
String modelName = object.getClass().getSimpleName();
String dasherizeModel = Inflector.getInstance().dasherize(modelName);
String title = object.getClass().getSimpleName();
String formView = dasherizeModel + "-form";
String gridView = dasherizeModel + "-grid";
String jsonModel = null;
if (object instanceof MetaJsonRecord) {
jsonModel = ((MetaJsonRecord) object).getJsonModel();
title = Beans.get(MetaJsonModelRepository.class).findByName(jsonModel).getTitle();
if (Strings.isNullOrEmpty(title)) {
title = jsonModel;
}
formView = "custom-model-" + jsonModel + "-form";
gridView = "custom-model-" + jsonModel + "-grid";
}
response.setCanClose(true);
ActionViewBuilder builder = ActionView.define(I18n.get(title)).model(object.getClass().getName()).add("form", formView).add("grid", gridView).context("_showRecord", object.getId());
if (jsonModel != null) {
builder.context("jsonModel", jsonModel);
builder.domain("self.jsonModel = :jsonModel");
}
response.setView(builder.map());
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class WkfDmnModelController method executeDmn.
public void executeDmn(ActionRequest request, ActionResponse response) throws AxelorException {
Context context = request.getContext();
String decisionId = (String) ((Map<String, Object>) context.get("dmnTable")).get("decisionId");
String ctxModel = (String) context.get("ctxModel");
Long ctxRecordId = Long.parseLong(context.get("ctxRecordId").toString());
if (ctxRecordId == null || ctxModel == null) {
return;
}
Model model = WkfContextHelper.getRepository(ctxModel).find(ctxRecordId);
Beans.get(DmnService.class).executeDmn(decisionId, model);
response.setCanClose(true);
}
Aggregations