Search in sources :

Example 1 with Context

use of com.axelor.rpc.Context 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 Context

use of com.axelor.rpc.Context in project axelor-open-suite by axelor.

the class FileTabServiceImpl method getShowRecordIds.

@SuppressWarnings("unchecked")
@Override
public String getShowRecordIds(FileTab fileTab, String field) throws ClassNotFoundException {
    Context context = new Context(fileTab.getClass());
    Class<? extends Model> klass = (Class<? extends Model>) Class.forName(fileTab.getClass().getName());
    JsonContext jsonContext = new JsonContext(context, Mapper.of(klass).getProperty("attrs"), fileTab.getAttrs());
    List<Object> recordList = (List<Object>) jsonContext.get(field);
    if (CollectionUtils.isEmpty(recordList)) {
        return null;
    }
    String ids = recordList.stream().map(obj -> {
        Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
        return recordMap.get("id").toString();
    }).collect(Collectors.joining(","));
    return ids;
}
Also used : JsonContext(com.axelor.rpc.JsonContext) Context(com.axelor.rpc.Context) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) EntityHelper(com.axelor.db.EntityHelper) Model(com.axelor.db.Model) MetaField(com.axelor.meta.db.MetaField) Inject(com.google.inject.Inject) Mapper(com.axelor.db.mapper.Mapper) Collectors(java.util.stream.Collectors) JsonContext(com.axelor.rpc.JsonContext) FileField(com.axelor.apps.base.db.FileField) Strings(com.google.common.base.Strings) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) Beans(com.axelor.inject.Beans) FileFieldRepository(com.axelor.apps.base.db.repo.FileFieldRepository) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) FileTab(com.axelor.apps.base.db.FileTab) Context(com.axelor.rpc.Context) JsonContext(com.axelor.rpc.JsonContext) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) Map(java.util.Map)

Example 3 with Context

use of com.axelor.rpc.Context in project axelor-open-suite by axelor.

the class ConvertLeadWizardController method convertLead.

@SuppressWarnings("unchecked")
public void convertLead(ActionRequest request, ActionResponse response) {
    try {
        Context context = request.getContext();
        Map<String, Object> leadContext = (Map<String, Object>) context.get("_lead");
        Lead lead = Beans.get(LeadRepository.class).find(((Integer) leadContext.get("id")).longValue());
        Partner partner = createPartnerData(context);
        Partner contactPartner = null;
        if (partner != null) {
            contactPartner = createContactData(context, partner);
        }
        try {
            lead = Beans.get(LeadService.class).convertLead(lead, partner, contactPartner);
        } catch (Exception e) {
            TraceBackService.trace(e);
        }
        if (lead.getPartner() == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.CONVERT_LEAD_ERROR));
        }
        openPartner(response, lead);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : Context(com.axelor.rpc.Context) LeadRepository(com.axelor.apps.crm.db.repo.LeadRepository) AxelorException(com.axelor.exception.AxelorException) Lead(com.axelor.apps.crm.db.Lead) Map(java.util.Map) Partner(com.axelor.apps.base.db.Partner) AxelorException(com.axelor.exception.AxelorException)

Example 4 with Context

use of com.axelor.rpc.Context in project axelor-open-suite by axelor.

the class ContractLineController method createAnalyticDistributionWithTemplate.

public void createAnalyticDistributionWithTemplate(ActionRequest request, ActionResponse response) {
    ContractLine contractLine = request.getContext().asType(ContractLine.class);
    Context parentContext = request.getContext().getParent();
    Contract contract = null;
    if (parentContext.get("_model").equals(Contract.class.getCanonicalName())) {
        contract = parentContext.asType(Contract.class);
    } else if (parentContext.getParent() != null && parentContext.getParent().get("_model").equals(Contract.class.getCanonicalName())) {
        contract = parentContext.getParent().asType(Contract.class);
    }
    contractLine = Beans.get(ContractLineService.class).createAnalyticDistributionWithTemplate(contractLine, contract);
    response.setValue("analyticMoveLineList", contractLine.getAnalyticMoveLineList());
}
Also used : Context(com.axelor.rpc.Context) ContractLine(com.axelor.apps.contract.db.ContractLine) Contract(com.axelor.apps.contract.db.Contract)

Example 5 with Context

use of com.axelor.rpc.Context in project axelor-open-suite by axelor.

the class PackLineController method getPack.

public Pack getPack(Context context) {
    Context parentContext = context.getParent();
    PackLine packLine = context.asType(PackLine.class);
    Pack pack = packLine.getPack();
    if (parentContext != null && parentContext.getContextClass().equals(Pack.class)) {
        pack = parentContext.asType(Pack.class);
    }
    return pack;
}
Also used : Context(com.axelor.rpc.Context) Pack(com.axelor.apps.sale.db.Pack) PackLine(com.axelor.apps.sale.db.PackLine)

Aggregations

Context (com.axelor.rpc.Context)149 AxelorException (com.axelor.exception.AxelorException)52 BigDecimal (java.math.BigDecimal)37 Map (java.util.Map)37 HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)23 SaleOrder (com.axelor.apps.sale.db.SaleOrder)19 List (java.util.List)18 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)17 Invoice (com.axelor.apps.account.db.Invoice)16 LinkedHashMap (java.util.LinkedHashMap)15 Product (com.axelor.apps.base.db.Product)14 Model (com.axelor.db.Model)13 StockMove (com.axelor.apps.stock.db.StockMove)12 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)12 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)11 PurchaseOrder (com.axelor.apps.purchase.db.PurchaseOrder)11 LocalDate (java.time.LocalDate)11 Beans (com.axelor.inject.Beans)10 ActionRequest (com.axelor.rpc.ActionRequest)10