Search in sources :

Example 1 with PrintTemplate

use of com.axelor.apps.base.db.PrintTemplate 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 PrintTemplate

use of com.axelor.apps.base.db.PrintTemplate in project axelor-open-suite by axelor.

the class PrintTemplateController method generatePrint.

public void generatePrint(ActionRequest request, ActionResponse response) {
    Context context = request.getContext();
    Map templateContext = (Map) context.get("template");
    PrintTemplate printTemplate = null;
    if (templateContext != null) {
        printTemplate = Beans.get(PrintTemplateRepository.class).find(Long.parseLong(templateContext.get("id").toString()));
    }
    Long objectId = Long.parseLong(context.get("_objectId").toString());
    try {
        response.setCanClose(true);
        Print print = Beans.get(PrintTemplateService.class).generatePrint(objectId, printTemplate);
        response.setView(getPrintView(print));
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Context(com.axelor.rpc.Context) Print(com.axelor.apps.base.db.Print) PrintTemplate(com.axelor.apps.base.db.PrintTemplate) PrintTemplateService(com.axelor.apps.base.service.PrintTemplateService) Map(java.util.Map) AxelorException(com.axelor.exception.AxelorException)

Example 3 with PrintTemplate

use of com.axelor.apps.base.db.PrintTemplate in project axelor-open-suite by axelor.

the class PrintTemplateController method openPrint.

public void openPrint(ActionRequest request, ActionResponse response) {
    Model context = request.getContext().asType(Model.class);
    String model = request.getModel();
    LOG.debug("Print template wizard call for model : {}", model);
    Query<PrintTemplate> printTemplateQuery = Beans.get(PrintTemplateRepository.class).all().filter("self.metaModel.fullName = ?", model);
    try {
        long templatesCount = printTemplateQuery.count();
        LOG.debug("Print templates count : {} ", templatesCount);
        if (templatesCount == 0) {
            response.setError(I18n.get("Please define a print template for the model :" + model));
        } else if (templatesCount == 1) {
            Print print = Beans.get(PrintTemplateService.class).generatePrint(context.getId(), printTemplateQuery.fetchOne());
            response.setView(getPrintView(print));
        } else if (templatesCount >= 2) {
            response.setView(ActionView.define(I18n.get("Select template")).model(Wizard.class.getName()).add("form", "select-print-template-wizard-form").param("show-confirm", "false").context("_objectId", context.getId().toString()).context("_templateContextModel", model).map());
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Print(com.axelor.apps.base.db.Print) PrintTemplate(com.axelor.apps.base.db.PrintTemplate) Model(com.axelor.db.Model) Wizard(com.axelor.apps.base.db.Wizard) AxelorException(com.axelor.exception.AxelorException)

Aggregations

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