<commit_before>package edu.arizona.kfs.fp.batch.service.impl;

import edu.arizona.kfs.fp.batch.dataaccess.TransactionPostingDao;
import edu.arizona.kfs.fp.batch.service.BankDocumentService;
import edu.arizona.kfs.fp.batch.service.BankParametersAccessService;
import edu.arizona.kfs.fp.businessobject.BankTransaction;
import edu.arizona.kfs.sys.KFSConstants;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.kuali.kfs.coa.service.AccountingPeriodService;
import org.kuali.kfs.sys.businessobject.Bank;
import org.kuali.kfs.sys.service.OptionsService;
import org.kuali.rice.krad.bo.DocumentHeader;
import org.kuali.rice.krad.rules.rule.event.SaveOnlyDocumentEvent;
import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
import org.kuali.rice.krad.bo.Note;
import org.kuali.rice.krad.document.Document;
import org.kuali.rice.krad.service.BusinessObjectService;
import org.kuali.rice.krad.service.DocumentService;
import org.kuali.rice.krad.util.ObjectUtils;



/**
 * Created by nataliac on 5/3/17.
 */
public abstract class AbstractBankDocumentService implements BankDocumentService {
    private static final Logger LOG = Logger.getLogger(AbstractBankDocumentService.class);

    protected DocumentService documentService;
    protected WorkflowDocumentService workflowDocumentService;
    protected TransactionPostingDao transactionPostingDao;
    protected BankParametersAccessService bankParametersAccessService;
    protected BusinessObjectService boService;
    protected AccountingPeriodService accountingPeriodService;
    protected OptionsService optionsService;


    protected abstract KFSConstants.BankTransactionDocumentType getDocumentType();


    /**
     * This method builds the Header of the Document
     */
    protected void buildHeader(Document document, BankTransaction bankTransaction) {
        DocumentHeader documentHeader = document.getDocumentHeader();
        //set description
        String documentDescription = StringUtils.isBlank(bankTransaction.getDescriptiveTxt6())?bankTransaction.getBankReference():bankTransaction.getDescription();
        documentHeader.setDocumentDescription(StringUtils.left(documentDescription, KFSConstants.BankTransactionConstants.MAX_DESCRIPTION_LEN));

        //build explanation string
        String accountNumber = bankTransaction.getAccountNumber().toString();
        String baiTypeStr = bankTransaction.getBaiType().toString();
        String custRefNo = bankTransaction.getCustRefNo();
        accountNumber = (StringUtils.isBlank(accountNumber)) ? "EMPTY" : accountNumber;
        accountNumber = StringUtils.left(accountNumber, KFSConstants.BankTransactionConstants.MAX_NUMBER_LEN);
        custRefNo = (StringUtils.isBlank(custRefNo)) ? "EMPTY" : custRefNo;
        custRefNo = StringUtils.left(custRefNo, KFSConstants.BankTransactionConstants.MAX_NUMBER_LEN);
        String explanation = accountNumber + " - " + baiTypeStr + " - " + custRefNo;

        documentHeader.setExplanation(explanation);
        LOG.debug("buildHeader finished. Header = "+documentHeader.toString());
    }



    protected void createDocumentNote(Document bankDocument, BankTransaction bankTransaction) {
        LOG.debug(" createDocumentNote() for " + bankTransaction.toString());
        try {
            String noteText = "Description: = " + bankTransaction.getDescriptiveTxt6() + " - " + bankTransaction.getDescriptiveTxt7();
            Note note = getDocumentService().createNoteFromDocument(bankDocument, noteText);
            bankDocument.addNote(note);
        } catch (Exception e) {
            LOG.error("Error when creating note for bankTransaction: " + bankTransaction.toString(), e);
            throw new RuntimeException("Error when creating note for bankTransaction: " + bankTransaction.toString(), e);
        }
    }

    /**
     * Returns trimmed down description from the bankTransaction to set in the document as following:
     *  1. If bankTransaction.DescriptiveTxt6 is empty, it will return the bankTransaction.BankReference
     *  2. Otherwise it will return bankTransaction.DescriptiveTxt6 concatenated with bankTransaction.DescriptiveTxt7
     *  Result above is trimmed down KFSConstants.BankTransactionConstants.MAX_DESCRIPTION_LEN leftmost charaters
     *
     * @param bankTransaction
     * @return
     */
    protected String getDocumentDescription(BankTransaction bankTransaction){
        String documentDescription = bankTransaction.getDescription();
        if ( StringUtils.isBlank(bankTransaction.getDescriptiveTxt6()) ){
            documentDescription = bankTransaction.getBankReference();
        }
        return StringUtils.left(documentDescription, KFSConstants.BankTransactionConstants.MAX_DESCRIPTION_LEN);
    }

    public void blanketApproveBankDocument(Document bankDocument) {
        LOG.info(" Saving and blanketApproving [" + bankDocument.getClass().toString() + "] " + bankDocument.getDocumentNumber());
        try {

            LOG.info(" Saving [" + bankDocument.getClass().toString() + "] " + bankDocument.getDocumentNumber());
            getDocumentService().saveDocument(bankDocument, SaveOnlyDocumentEvent.class);
            getDocumentService().saveDocumentNotes(bankDocument);
            LOG.info(" BlanketApproving [" + bankDocument.getClass().toString() + "] " + bankDocument.getDocumentNumber());
            getDocumentService().prepareWorkflowDocument(bankDocument);
            // calling workflow service to bypass business rule checks
            getWorkflowDocumentService().blanketApprove(bankDocument.getDocumentHeader().getWorkflowDocument(), "", null);
        } catch (Exception e) {
            LOG.error("Error when creating and blanketApproving document " + bankDocument.getDocumentNumber(), e);
            throw new RuntimeException("Could not create and blanketApproving document " + bankDocument.getDocumentNumber(), e);
        }
    }

    /**
     * Finds the appropriate Bank from bankTransaction AccountNumber. If none is configured for that particular AccountNumber,
     * it will return the default bank configured for the documentType.
     * If it can't find any default banks for the docType or AccountNumber, it will throw a Runtime exception.
     * @return Bank - non empty bank object.
     */
    protected Bank findBank(BankTransaction bankTransaction) {
        Bank bank = getTransactionPostingDao().getBankObjectByAccountNumber(bankTransaction.getAccountNumber().toString());
        if (ObjectUtils.isNull(bank)) {
            String bankCode = getBankParametersAccessService().getDefaultBankByDocType(getDocumentType());
            bank = new Bank();
            bank.setBankCode(bankCode);
        }

        if (bank == null) {
            LOG.error("Invalid System Configuration for  Account [" +bankTransaction.getAccountNumber()+ "] DocType ["+getDocumentType().name()+"]:  No bank or default bank configured.");
            throw new RuntimeException("Invalid System Configuration for Account [" +bankTransaction.getAccountNumber()+ "] DocType ["+getDocumentType().name()+"]:  No bank or default bank configured.");
        }

        LOG.debug("AbstractBankDocumentServiceImpl: findBank for " + bankTransaction + " = " + bank.getBankCode());
        return bank;
    }


    public DocumentService getDocumentService() {
        return documentService;
    }

    public void setDocumentService(DocumentService documentService) {
        this.documentService = documentService;
    }
<commit_msg>UAF-4211: Adding a null check after the bankCode is retrieved from the DefaultBankByDocType<commit_after>package edu.arizona.kfs.fp.batch.service.impl;

import edu.arizona.kfs.fp.batch.dataaccess.TransactionPostingDao;
import edu.arizona.kfs.fp.batch.service.BankDocumentService;
import edu.arizona.kfs.fp.batch.service.BankParametersAccessService;
import edu.arizona.kfs.fp.businessobject.BankTransaction;
import edu.arizona.kfs.sys.KFSConstants;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.kuali.kfs.coa.service.AccountingPeriodService;
import org.kuali.kfs.sys.businessobject.Bank;
import org.kuali.kfs.sys.service.OptionsService;
import org.kuali.rice.krad.bo.DocumentHeader;
import org.kuali.rice.krad.rules.rule.event.SaveOnlyDocumentEvent;
import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
import org.kuali.rice.krad.bo.Note;
import org.kuali.rice.krad.document.Document;
import org.kuali.rice.krad.service.BusinessObjectService;
import org.kuali.rice.krad.service.DocumentService;
import org.kuali.rice.krad.util.ObjectUtils;



/**
 * Created by nataliac on 5/3/17.
 */
public abstract class AbstractBankDocumentService implements BankDocumentService {
    private static final Logger LOG = Logger.getLogger(AbstractBankDocumentService.class);

    protected DocumentService documentService;
    protected WorkflowDocumentService workflowDocumentService;
    protected TransactionPostingDao transactionPostingDao;
    protected BankParametersAccessService bankParametersAccessService;
    protected BusinessObjectService boService;
    protected AccountingPeriodService accountingPeriodService;
    protected OptionsService optionsService;


    protected abstract KFSConstants.BankTransactionDocumentType getDocumentType();


    /**
     * This method builds the Header of the Document
     */
    protected void buildHeader(Document document, BankTransaction bankTransaction) {
        DocumentHeader documentHeader = document.getDocumentHeader();
        //set description
        String documentDescription = StringUtils.isBlank(bankTransaction.getDescriptiveTxt6())?bankTransaction.getBankReference():bankTransaction.getDescription();
        documentHeader.setDocumentDescription(StringUtils.left(documentDescription, KFSConstants.BankTransactionConstants.MAX_DESCRIPTION_LEN));

        //build explanation string
        String accountNumber = bankTransaction.getAccountNumber().toString();
        String baiTypeStr = bankTransaction.getBaiType().toString();
        String custRefNo = bankTransaction.getCustRefNo();
        accountNumber = (StringUtils.isBlank(accountNumber)) ? "EMPTY" : accountNumber;
        accountNumber = StringUtils.left(accountNumber, KFSConstants.BankTransactionConstants.MAX_NUMBER_LEN);
        custRefNo = (StringUtils.isBlank(custRefNo)) ? "EMPTY" : custRefNo;
        custRefNo = StringUtils.left(custRefNo, KFSConstants.BankTransactionConstants.MAX_NUMBER_LEN);
        String explanation = accountNumber + " - " + baiTypeStr + " - " + custRefNo;

        documentHeader.setExplanation(explanation);
        LOG.debug("buildHeader finished. Header = "+documentHeader.toString());
    }



    protected void createDocumentNote(Document bankDocument, BankTransaction bankTransaction) {
        LOG.debug(" createDocumentNote() for " + bankTransaction.toString());
        try {
            String noteText = "Description: = " + bankTransaction.getDescriptiveTxt6() + " - " + bankTransaction.getDescriptiveTxt7();
            Note note = getDocumentService().createNoteFromDocument(bankDocument, noteText);
            bankDocument.addNote(note);
        } catch (Exception e) {
            LOG.error("Error when creating note for bankTransaction: " + bankTransaction.toString(), e);
            throw new RuntimeException("Error when creating note for bankTransaction: " + bankTransaction.toString(), e);
        }
    }

    /**
     * Returns trimmed down description from the bankTransaction to set in the document as following:
     *  1. If bankTransaction.DescriptiveTxt6 is empty, it will return the bankTransaction.BankReference
     *  2. Otherwise it will return bankTransaction.DescriptiveTxt6 concatenated with bankTransaction.DescriptiveTxt7
     *  Result above is trimmed down KFSConstants.BankTransactionConstants.MAX_DESCRIPTION_LEN leftmost charaters
     *
     * @param bankTransaction
     * @return
     */
    protected String getDocumentDescription(BankTransaction bankTransaction){
        String documentDescription = bankTransaction.getDescription();
        if ( StringUtils.isBlank(bankTransaction.getDescriptiveTxt6()) ){
            documentDescription = bankTransaction.getBankReference();
        }
        return StringUtils.left(documentDescription, KFSConstants.BankTransactionConstants.MAX_DESCRIPTION_LEN);
    }

    public void blanketApproveBankDocument(Document bankDocument) {
        LOG.info(" Saving and blanketApproving [" + bankDocument.getClass().toString() + "] " + bankDocument.getDocumentNumber());
        try {

            LOG.info(" Saving [" + bankDocument.getClass().toString() + "] " + bankDocument.getDocumentNumber());
            getDocumentService().saveDocument(bankDocument, SaveOnlyDocumentEvent.class);
            getDocumentService().saveDocumentNotes(bankDocument);
            LOG.info(" BlanketApproving [" + bankDocument.getClass().toString() + "] " + bankDocument.getDocumentNumber());
            getDocumentService().prepareWorkflowDocument(bankDocument);
            // calling workflow service to bypass business rule checks
            getWorkflowDocumentService().blanketApprove(bankDocument.getDocumentHeader().getWorkflowDocument(), "", null);
        } catch (Exception e) {
            LOG.error("Error when creating and blanketApproving document " + bankDocument.getDocumentNumber(), e);
            throw new RuntimeException("Could not create and blanketApproving document " + bankDocument.getDocumentNumber(), e);
        }
    }

    /**
     * Finds the appropriate Bank from bankTransaction AccountNumber. If none is configured for that particular AccountNumber,
     * it will return the default bank configured for the documentType.
     * If it can't find any default banks for the docType or AccountNumber, it will throw a Runtime exception.
     * @return Bank - non empty bank object.
     */
    protected Bank findBank(BankTransaction bankTransaction) {
        Bank bank = getTransactionPostingDao().getBankObjectByAccountNumber(bankTransaction.getAccountNumber().toString());
        if (ObjectUtils.isNull(bank)) {
            String bankCode = getBankParametersAccessService().getDefaultBankByDocType(getDocumentType());
            if ( StringUtils.isNotEmpty( bankCode )) {
                bank = getTransactionPostingDao().getBankObjectByBankCode(bankCode);
            }
        }

        if (bank == null) {
            LOG.error("Invalid System Configuration for  Account [" +bankTransaction.getAccountNumber()+ "] DocType ["+getDocumentType().name()+"]:  No bank or default bank configured.");
            throw new RuntimeException("Invalid System Configuration for Account [" +bankTransaction.getAccountNumber()+ "] DocType ["+getDocumentType().name()+"]:  No bank or default bank configured.");
        }

        LOG.debug("AbstractBankDocumentServiceImpl: findBank for " + bankTransaction + " = " + bank.getBankCode());
        return bank;
    }


    public DocumentService getDocumentService() {
        return documentService;
    }

    public void setDocumentService(DocumentService documentService) {
        this.documentService = documentService;
    }

{'fraction_non_alphanumeric': 0.09183472377323787, 'fraction_numerical': 0.002102082375353084, 'mean_word_length': 4.024422442244225, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3408054', 'n_tokens_mistral': 3858, 'n_tokens_neox': 3791, 'n_words': 833}