Search in sources :

Example 1 with AppFilter

use of com.axelor.app.internal.AppFilter 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;
}
Also used : Locale(java.util.Locale) TemplateContext(com.axelor.apps.message.db.TemplateContext) Context(com.axelor.rpc.Context) PrintTemplate(com.axelor.apps.base.db.PrintTemplate) Print(com.axelor.apps.base.db.Print) Print(com.axelor.apps.base.db.Print) MetaModel(com.axelor.meta.db.MetaModel) AppFilter(com.axelor.app.internal.AppFilter) Language(com.axelor.apps.base.db.Language) TemplateMaker(com.axelor.tool.template.TemplateMaker) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) Transactional(com.google.inject.persist.Transactional)

Example 2 with AppFilter

use of com.axelor.app.internal.AppFilter 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);
}
Also used : Context(com.axelor.rpc.Context) Locale(java.util.Locale) AxelorException(com.axelor.exception.AxelorException) PrintTemplateLineTest(com.axelor.apps.base.db.PrintTemplateLineTest) AxelorException(com.axelor.exception.AxelorException) AppFilter(com.axelor.app.internal.AppFilter) Language(com.axelor.apps.base.db.Language) TemplateMaker(com.axelor.tool.template.TemplateMaker) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) Transactional(com.google.inject.persist.Transactional)

Aggregations

AppFilter (com.axelor.app.internal.AppFilter)2 Language (com.axelor.apps.base.db.Language)2 Model (com.axelor.db.Model)2 MetaModel (com.axelor.meta.db.MetaModel)2 Context (com.axelor.rpc.Context)2 TemplateMaker (com.axelor.tool.template.TemplateMaker)2 Transactional (com.google.inject.persist.Transactional)2 Locale (java.util.Locale)2 Print (com.axelor.apps.base.db.Print)1 PrintTemplate (com.axelor.apps.base.db.PrintTemplate)1 PrintTemplateLineTest (com.axelor.apps.base.db.PrintTemplateLineTest)1 TemplateContext (com.axelor.apps.message.db.TemplateContext)1 AxelorException (com.axelor.exception.AxelorException)1