<commit_before>package org.estatio.canonical.invoice;

import java.math.BigDecimal;
import java.util.Optional;

import javax.inject.Inject;

import org.apache.isis.applib.annotation.DomainService;
import org.apache.isis.applib.annotation.NatureOfService;
import org.apache.isis.applib.annotation.Programmatic;

import org.estatio.canonical.DtoFactoryAbstract;
import org.estatio.canonical.invoice.v1.InvoiceDto;
import org.estatio.canonical.invoice.v1.InvoiceItemDto;
import org.estatio.dom.DtoMappingHelper;
import org.estatio.dom.asset.FixedAsset;
import org.estatio.dom.asset.financial.FixedAssetFinancialAccountRepository;
import org.estatio.dom.bankmandate.BankMandate;
import org.estatio.dom.invoice.Invoice;
import org.estatio.dom.lease.Lease;

@DomainService(
        nature = NatureOfService.DOMAIN
)
public class InvoiceDtoFactory extends DtoFactoryAbstract {

    private final InvoiceItemDtoFactory invoiceItemDtoFactory = new InvoiceItemDtoFactory();

    @Programmatic
    public InvoiceDto newDto(final Invoice invoice) {
        InvoiceDto dto = new InvoiceDto();

        dto.setSelf(mappingHelper.oidDtoFor(invoice));
        dto.setAtPath(invoice.getApplicationTenancyPath());
        dto.setBuyerParty(mappingHelper.oidDtoFor(invoice.getBuyer()));
        dto.setSellerParty(mappingHelper.oidDtoFor(invoice.getSeller()));

        dto.setDueDate(asXMLGregorianCalendar(invoice.getDueDate()));
        dto.setInvoiceDate(asXMLGregorianCalendar(invoice.getInvoiceDate()));
        dto.setInvoiceNumber(invoice.getInvoiceNumber());
        dto.setPaymentMethod(invoice.getPaymentMethod().asDto());
        dto.setCollectionNumber(invoice.getCollectionNumber());

        final Lease lease = invoice.getLease();
        if (lease != null){
            dto.setAgreementReference(lease.getReference());
            final BankMandate paidBy = lease.getPaidBy();
            dto.setPaidByMandate(mappingHelper.oidDtoFor(paidBy));
            dto.setBuyerBankAccount(mappingHelper.oidDtoFor(paidBy.getBankAccount()));
        }

        final Optional<FixedAsset> fixedAssetIfAny = Optional.ofNullable(invoice.getFixedAsset());
        if (fixedAssetIfAny.isPresent()) {
            final FixedAsset fixedAsset = fixedAssetIfAny.get();
            dto.setFixedAssetReference(fixedAsset.getReference());
            dto.setFixedAssetExternalReference(fixedAsset.getExternalReference());

            // there should be only one
            dto.setSellerBankAccount(mappingHelper.oidDtoFor(invoice.getSellerBankAccount()));
        }

        invoice.getItems().stream().forEach(item -> dto.getItems().add(invoiceItemDtoFactory.newDto(item)));<commit_msg>EST-526: Prevent InvoiceDtoFactory of throwing NPE when no bank mandate is available<commit_after>package org.estatio.canonical.invoice;

import java.math.BigDecimal;
import java.util.Optional;

import javax.inject.Inject;

import org.apache.isis.applib.annotation.DomainService;
import org.apache.isis.applib.annotation.NatureOfService;
import org.apache.isis.applib.annotation.Programmatic;

import org.estatio.canonical.DtoFactoryAbstract;
import org.estatio.canonical.invoice.v1.InvoiceDto;
import org.estatio.canonical.invoice.v1.InvoiceItemDto;
import org.estatio.dom.DtoMappingHelper;
import org.estatio.dom.asset.FixedAsset;
import org.estatio.dom.asset.financial.FixedAssetFinancialAccountRepository;
import org.estatio.dom.bankmandate.BankMandate;
import org.estatio.dom.invoice.Invoice;
import org.estatio.dom.lease.Lease;

@DomainService(
        nature = NatureOfService.DOMAIN
)
public class InvoiceDtoFactory extends DtoFactoryAbstract {

    private final InvoiceItemDtoFactory invoiceItemDtoFactory = new InvoiceItemDtoFactory();

    @Programmatic
    public InvoiceDto newDto(final Invoice invoice) {
        InvoiceDto dto = new InvoiceDto();

        dto.setSelf(mappingHelper.oidDtoFor(invoice));
        dto.setAtPath(invoice.getApplicationTenancyPath());
        dto.setBuyerParty(mappingHelper.oidDtoFor(invoice.getBuyer()));
        dto.setSellerParty(mappingHelper.oidDtoFor(invoice.getSeller()));

        dto.setDueDate(asXMLGregorianCalendar(invoice.getDueDate()));
        dto.setInvoiceDate(asXMLGregorianCalendar(invoice.getInvoiceDate()));
        dto.setInvoiceNumber(invoice.getInvoiceNumber());
        dto.setPaymentMethod(invoice.getPaymentMethod().asDto());
        dto.setCollectionNumber(invoice.getCollectionNumber());

        final Lease lease = invoice.getLease();
        if (lease != null){
            dto.setAgreementReference(lease.getReference());
            final BankMandate paidBy = lease.getPaidBy();
            if (paidBy != null) {
                dto.setPaidByMandate(mappingHelper.oidDtoFor(paidBy));
                dto.setBuyerBankAccount(mappingHelper.oidDtoFor(paidBy.getBankAccount()));
            }
        }

        final Optional<FixedAsset> fixedAssetIfAny = Optional.ofNullable(invoice.getFixedAsset());
        if (fixedAssetIfAny.isPresent()) {
            final FixedAsset fixedAsset = fixedAssetIfAny.get();
            dto.setFixedAssetReference(fixedAsset.getReference());
            dto.setFixedAssetExternalReference(fixedAsset.getExternalReference());

            // there should be only one
            dto.setSellerBankAccount(mappingHelper.oidDtoFor(invoice.getSellerBankAccount()));
        }

        invoice.getItems().stream().forEach(item -> dto.getItems().add(invoiceItemDtoFactory.newDto(item)));
{'fraction_non_alphanumeric': 0.10522456461961503, 'fraction_numerical': 0.0012832263978001834, 'mean_word_length': 4.161778618732261, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1894252', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1615, 'n_words': 211}