<commit_before>/*
 * Copyright 2011 The Kuali Foundation.
 *
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl1.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.kfs.module.ar.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kuali.kfs.integration.cg.ContractsAndGrantsAgencyAddress;
import org.kuali.kfs.integration.cg.ContractsAndGrantsBillingAgency;
import org.kuali.kfs.module.ar.businessobject.Customer;
import org.kuali.kfs.module.ar.businessobject.CustomerAddress;
import org.kuali.kfs.module.ar.document.service.CustomerService;
import org.kuali.kfs.module.ar.service.CustomerDocumentService;
import org.kuali.kfs.sys.KFSPropertyConstants;
import org.kuali.kfs.sys.service.NonTransactional;
import org.kuali.rice.kew.api.KewApiServiceLocator;
import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
import org.kuali.rice.kew.api.exception.WorkflowException;
import org.kuali.rice.kns.document.MaintenanceDocument;
import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
import org.kuali.rice.krad.service.DocumentService;
import org.kuali.rice.krad.service.KualiModuleService;
import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of the Customer Document Service.
 */
@Transactional
public class CustomerDocumentServiceImpl implements CustomerDocumentService {

    protected CustomerService customerService;
    protected DocumentService documentService;
    protected KualiModuleService kualiModuleService;
    protected MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService;
    protected WorkflowDocumentService workflowDocumentService;

    @Override
    public String createAndSaveCustomer(String description, ContractsAndGrantsBillingAgency agency) throws WorkflowException {
        MaintenanceDocument doc = null;
        doc = (MaintenanceDocument) documentService.getNewDocument(maintenanceDocumentDictionaryService.getDocumentTypeName(Customer.class));
        // set a description to say that this application document has been created by the Agency Document
        doc.getDocumentHeader().setDocumentDescription(description);
        // to set the explanation to reference the agency number
        doc.getDocumentHeader().setExplanation(description + "(Agency Number - " + agency.getAgencyNumber() + " )");
        // refresh nonupdatable references and save the Customer Document
        doc.refreshNonUpdateableReferences();


        // If the document gets created successfully, then we set values to the agency as well as the customer.
        Customer customer = (Customer) doc.getNewMaintainableObject().getBusinessObject();

        // this step is done before so that the customer name is
        // in - should be uppercase for the Customer to be
        // identified.
        customer.setCustomerName(agency.getReportingName().toUpperCase());
        customer.setCustomerTypeCode(agency.getCustomerTypeCode());

        if (agency.isActive()) {
            customer.setActive(true);
        }

        // To call the customer service to get the customer number and set it to the customer number
        String customerNumber = customerService.getNextCustomerNumber(customer);
        customer.setCustomerNumber(customerNumber);

        List<? extends ContractsAndGrantsAgencyAddress> agencyAddresses = new ArrayList<ContractsAndGrantsAgencyAddress>();
        if (agency.getAgencyAddresses() == null || agency.getAgencyAddresses().isEmpty()) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put(KFSPropertyConstants.AGENCY_NUMBER, agency.getAgencyNumber());
            agencyAddresses = kualiModuleService.getResponsibleModuleService(ContractsAndGrantsAgencyAddress.class).getExternalizableBusinessObjectsList(ContractsAndGrantsAgencyAddress.class, map);
        } else {
            agencyAddresses = agency.getAgencyAddresses();
        }
        for (ContractsAndGrantsAgencyAddress agencyAddress : agencyAddresses) {
            CustomerAddress customerAddress = new CustomerAddress();
            customerAddress.setCustomerAddressName(agencyAddress.getAgencyAddressName());
            customerAddress.setCustomerAddressTypeCode(agencyAddress.getCustomerAddressTypeCode());
            customerAddress.setCustomerLine1StreetAddress(agencyAddress.getAgencyLine1StreetAddress());
            customerAddress.setCustomerLine2StreetAddress(agencyAddress.getAgencyLine2StreetAddress());
            customerAddress.setCustomerCityName(agencyAddress.getAgencyCityName());
            customerAddress.setCustomerCountryCode(agencyAddress.getAgencyCountryCode());
            customerAddress.setCustomerStateCode(agencyAddress.getAgencyStateCode());
            customerAddress.setCustomerZipCode(agencyAddress.getAgencyZipCode());
            customerAddress.setCustomerAddressInternationalProvinceName(agencyAddress.getAgencyAddressInternationalProvinceName());
            customerAddress.setCustomerInternationalMailCode(agencyAddress.getAgencyInternationalMailCode());
            customer.getCustomerAddresses().add(customerAddress);
        }

        documentService.saveDocument(doc);
        documentService.prepareWorkflowDocument(doc);
        // the next step is to route the document through the workflow
        workflowDocumentService.route(doc.getDocumentHeader().getWorkflowDocument(), "", null);


        final DocumentAttributeIndexingQueue documentAttributeIndexingQueue = KewApiServiceLocator.getDocumentAttributeIndexingQueue();
        documentAttributeIndexingQueue.indexDocument(doc.getDocumentNumber());

        return customerNumber;
    }

    /**
     * This method gets the document service
     *
     * @return the document service
     */
    public DocumentService getDocumentService() {
        return documentService;
    }
<commit_msg>KFSTP-1347,copy email address to new customer created from Agency<commit_after>/*
 * Copyright 2011 The Kuali Foundation.
 *
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl1.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.kfs.module.ar.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kuali.kfs.integration.cg.ContractsAndGrantsAgencyAddress;
import org.kuali.kfs.integration.cg.ContractsAndGrantsBillingAgency;
import org.kuali.kfs.module.ar.businessobject.Customer;
import org.kuali.kfs.module.ar.businessobject.CustomerAddress;
import org.kuali.kfs.module.ar.document.service.CustomerService;
import org.kuali.kfs.module.ar.service.CustomerDocumentService;
import org.kuali.kfs.sys.KFSPropertyConstants;
import org.kuali.kfs.sys.service.NonTransactional;
import org.kuali.rice.kew.api.KewApiServiceLocator;
import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
import org.kuali.rice.kew.api.exception.WorkflowException;
import org.kuali.rice.kns.document.MaintenanceDocument;
import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
import org.kuali.rice.krad.service.DocumentService;
import org.kuali.rice.krad.service.KualiModuleService;
import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of the Customer Document Service.
 */
@Transactional
public class CustomerDocumentServiceImpl implements CustomerDocumentService {

    protected CustomerService customerService;
    protected DocumentService documentService;
    protected KualiModuleService kualiModuleService;
    protected MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService;
    protected WorkflowDocumentService workflowDocumentService;

    @Override
    public String createAndSaveCustomer(String description, ContractsAndGrantsBillingAgency agency) throws WorkflowException {
        MaintenanceDocument doc = null;
        doc = (MaintenanceDocument) documentService.getNewDocument(maintenanceDocumentDictionaryService.getDocumentTypeName(Customer.class));
        // set a description to say that this application document has been created by the Agency Document
        doc.getDocumentHeader().setDocumentDescription(description);
        // to set the explanation to reference the agency number
        doc.getDocumentHeader().setExplanation(description + "(Agency Number - " + agency.getAgencyNumber() + " )");
        // refresh nonupdatable references and save the Customer Document
        doc.refreshNonUpdateableReferences();


        // If the document gets created successfully, then we set values to the agency as well as the customer.
        Customer customer = (Customer) doc.getNewMaintainableObject().getBusinessObject();

        // this step is done before so that the customer name is
        // in - should be uppercase for the Customer to be
        // identified.
        customer.setCustomerName(agency.getReportingName().toUpperCase());
        customer.setCustomerTypeCode(agency.getCustomerTypeCode());

        if (agency.isActive()) {
            customer.setActive(true);
        }

        // To call the customer service to get the customer number and set it to the customer number
        String customerNumber = customerService.getNextCustomerNumber(customer);
        customer.setCustomerNumber(customerNumber);

        List<? extends ContractsAndGrantsAgencyAddress> agencyAddresses = new ArrayList<ContractsAndGrantsAgencyAddress>();
        if (agency.getAgencyAddresses() == null || agency.getAgencyAddresses().isEmpty()) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put(KFSPropertyConstants.AGENCY_NUMBER, agency.getAgencyNumber());
            agencyAddresses = kualiModuleService.getResponsibleModuleService(ContractsAndGrantsAgencyAddress.class).getExternalizableBusinessObjectsList(ContractsAndGrantsAgencyAddress.class, map);
        } else {
            agencyAddresses = agency.getAgencyAddresses();
        }
        for (ContractsAndGrantsAgencyAddress agencyAddress : agencyAddresses) {
            CustomerAddress customerAddress = new CustomerAddress();
            customerAddress.setCustomerAddressName(agencyAddress.getAgencyAddressName());
            customerAddress.setCustomerAddressTypeCode(agencyAddress.getCustomerAddressTypeCode());
            customerAddress.setCustomerLine1StreetAddress(agencyAddress.getAgencyLine1StreetAddress());
            customerAddress.setCustomerLine2StreetAddress(agencyAddress.getAgencyLine2StreetAddress());
            customerAddress.setCustomerCityName(agencyAddress.getAgencyCityName());
            customerAddress.setCustomerCountryCode(agencyAddress.getAgencyCountryCode());
            customerAddress.setCustomerStateCode(agencyAddress.getAgencyStateCode());
            customerAddress.setCustomerZipCode(agencyAddress.getAgencyZipCode());
            customerAddress.setCustomerAddressInternationalProvinceName(agencyAddress.getAgencyAddressInternationalProvinceName());
            customerAddress.setCustomerInternationalMailCode(agencyAddress.getAgencyInternationalMailCode());
            customerAddress.setCustomerEmailAddress(agencyAddress.getAgencyContactEmailAddress());
            customer.getCustomerAddresses().add(customerAddress);
        }

        documentService.saveDocument(doc);
        documentService.prepareWorkflowDocument(doc);
        // the next step is to route the document through the workflow
        workflowDocumentService.route(doc.getDocumentHeader().getWorkflowDocument(), "", null);


        final DocumentAttributeIndexingQueue documentAttributeIndexingQueue = KewApiServiceLocator.getDocumentAttributeIndexingQueue();
        documentAttributeIndexingQueue.indexDocument(doc.getDocumentNumber());

        return customerNumber;
    }

    /**
     * This method gets the document service
     *
     * @return the document service
     */
    public DocumentService getDocumentService() {
        return documentService;
    }

{'fraction_non_alphanumeric': 0.07743953612573434, 'fraction_numerical': 0.0019836728465705347, 'mean_word_length': 4.383162217659137, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4630230', 'n_tokens_mistral': 3314, 'n_tokens_neox': 3196, 'n_words': 727}