use of com.axelor.apps.base.db.Language in project axelor-open-suite by axelor.
the class PrintTemplateServiceImpl method generatePrint.
@SuppressWarnings("unchecked")
@Override
@Transactional(rollbackOn = { AxelorException.class, Exception.class })
public Print generatePrint(Long objectId, PrintTemplate printTemplate) throws AxelorException, IOException, ClassNotFoundException {
MetaModel metaModel = printTemplate.getMetaModel();
if (metaModel == null) {
return null;
}
String model = metaModel.getFullName();
String simpleModel = metaModel.getName();
// debug
LOG.debug("");
LOG.debug("model : {}", model);
LOG.debug("simpleModel : {}", simpleModel);
LOG.debug("object id : {}", objectId);
LOG.debug("printTemplate : {}", printTemplate);
LOG.debug("");
Locale locale = Optional.ofNullable(printTemplate.getLanguage()).map(Language::getCode).map(Locale::new).orElseGet(AppFilter::getLocale);
TemplateMaker maker = initMaker(objectId, model, simpleModel, locale);
Print print = new Print();
if (StringUtils.notEmpty(printTemplate.getDocumentName())) {
maker.setTemplate(printTemplate.getDocumentName());
print.setDocumentName(maker.make());
}
print.setMetaModel(metaModel);
print.setObjectId(objectId);
print.setCompany(printTemplate.getCompany());
print.setLanguage(printTemplate.getLanguage());
print.setHidePrintSettings(printTemplate.getHidePrintSettings());
print.setFormatSelect(printTemplate.getFormatSelect());
print.setDisplayTypeSelect(printTemplate.getDisplayTypeSelect());
print.setIsEditable(printTemplate.getIsEditable());
print.setAttach(printTemplate.getAttach());
print.setMetaFileField(printTemplate.getMetaFileField());
if (!printTemplate.getHidePrintSettings()) {
if (StringUtils.notEmpty(printTemplate.getPrintTemplatePdfHeader())) {
maker.setTemplate(printTemplate.getPrintTemplatePdfHeader());
print.setPrintPdfHeader(maker.make());
}
if (StringUtils.notEmpty(printTemplate.getPrintTemplatePdfFooter())) {
maker.setTemplate(printTemplate.getPrintTemplatePdfFooter());
print.setPrintPdfFooter(maker.make());
print.setFooterFontSize(printTemplate.getFooterFontSize());
print.setFooterFontType(printTemplate.getFooterFontType());
print.setFooterTextAlignment(printTemplate.getFooterTextAlignment());
print.setIsFooterUnderLine(printTemplate.getIsFooterUnderLine());
print.setFooterFontColor(printTemplate.getFooterFontColor());
}
print.setLogoPositionSelect(printTemplate.getLogoPositionSelect());
print.setLogoWidth(printTemplate.getLogoWidth());
print.setHeaderContentWidth(printTemplate.getHeaderContentWidth());
}
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);
}
}
rank = 0;
int level = 1;
if (CollectionUtils.isNotEmpty(printTemplate.getPrintTemplateLineList())) {
processPrintTemplateLineList(printTemplate.getPrintTemplateLineList(), print, null, scriptContext, maker, level);
}
if (CollectionUtils.isNotEmpty(printTemplate.getPrintTemplateSet())) {
for (PrintTemplate subPrintTemplate : printTemplate.getPrintTemplateSet()) {
Long subObjectId = objectId;
if (StringUtils.notEmpty(subPrintTemplate.getMetaModelDefaultPath())) {
Object subMetaModelObject = templateContextService.computeTemplateContext(subPrintTemplate.getMetaModelDefaultPath(), scriptContext);
Class<? extends Model> subModelClass = (Class<? extends Model>) Class.forName(subPrintTemplate.getMetaModel().getFullName());
subObjectId = (Long) Mapper.of(subModelClass).get(subMetaModelObject, "id");
}
Print subPrint = generatePrint(subObjectId, subPrintTemplate);
print.addPrintSetItem(subPrint);
}
}
print = printRepo.save(print);
printService.attachMetaFiles(print, getMetaFiles(maker, printTemplate));
return print;
}
use of com.axelor.apps.base.db.Language in project axelor-open-suite by axelor.
the class GenerateMessageController method templateDomain.
public void templateDomain(ActionRequest request, ActionResponse response) {
Context context = request.getContext();
String model = (String) context.get("_templateContextModel");
Object languageObj = context.get("language");
Language language;
if (languageObj == null) {
language = null;
} else if (languageObj instanceof Map) {
Map<String, Object> languageMap = (Map<String, Object>) languageObj;
language = Beans.get(LanguageRepository.class).find(Long.parseLong(languageMap.get("id").toString()));
} else if (languageObj instanceof Language) {
language = (Language) languageObj;
} else {
throw new IllegalArgumentException("erreur...");
}
String domain;
if (language == null) {
domain = "self.metaModel.fullName = '" + model + "' and self.isSystem != true";
} else {
domain = "self.metaModel.fullName = '" + model + "' and self.isSystem != true and self.language.id = " + language.getId();
}
response.setAttr("_xTemplate", "domain", domain);
}
use of com.axelor.apps.base.db.Language 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);
}
Aggregations