<commit_before>/*
 * Copyright 2005-2010 The Kuali Foundation
 * 
 * Licensed under the Educational Community License, Version 2.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.kra.award.awardhierarchy.sync.helpers;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.kuali.kra.award.awardhierarchy.sync.AwardSyncChange;
import org.kuali.kra.award.awardhierarchy.sync.AwardSyncException;
import org.kuali.kra.award.awardhierarchy.sync.AwardSyncType;
import org.kuali.kra.award.awardhierarchy.sync.AwardSyncXmlExport;
import org.kuali.kra.award.home.Award;
import org.kuali.kra.award.paymentreports.awardreports.AwardReportTerm;
import org.kuali.kra.award.paymentreports.awardreports.AwardReportTermRecipient;
import org.kuali.rice.kns.bo.PersistableBusinessObject;

/**
 * Award Hierarchy Sync Helper for Report Recipient.
 */
public class AwardSyncReportRecipientHelper extends AwardSyncHelperBase {
    
    @SuppressWarnings("unchecked")
    @Override
    public void applySyncChange(Award award, AwardSyncChange change) 
        throws NoSuchFieldException, IntrospectionException, IllegalAccessException, InvocationTargetException, 
        ClassNotFoundException, NoSuchMethodException, InstantiationException, AwardSyncException {
        Collection reports = award.getAwardReportTermItems();
        AwardReportTerm report = (AwardReportTerm) getAwardSyncUtilityService().findMatchingBo(reports, change.getXmlExport().getKeys());
        if (StringUtils.equals(change.getSyncType(), AwardSyncType.ADD_SYNC.getSyncValue())) {
            if (report != null) {
                this.setValuesOnSyncable(report, change.getXmlExport().getValues(), change);
            } else {
                throw new AwardSyncException("Not applicable", true);
            }
        } else {
            if (report != null) {
                List<AwardSyncXmlExport> recipientChanges = (List<AwardSyncXmlExport>) change.getXmlExport().getValues().get("awardReportTermRecipients");
                for (AwardSyncXmlExport recipientChange : recipientChanges) {
                    AwardReportTermRecipient recipient = (AwardReportTermRecipient) getAwardSyncUtilityService().findMatchingBo((Collection) report.getAwardReportTermRecipients(), recipientChange.getKeys());
                    if (recipient != null) {
                        report.getAwardReportTermRecipients().remove(recipient);
                    }
                }
            } else {
                throw new AwardSyncException("Not applicable", true);
            }
        }
    }
        
    @Override
    protected String getObjectDesc(PersistableBusinessObject syncableObject, String attrName) {
        return "Recipient";
    }
    
    @Override
    protected String getDataDesc(PersistableBusinessObject syncableObject, String attrName) {
        AwardReportTermRecipient recipient = (AwardReportTermRecipient) syncableObject;
        String retval = recipient.getContactType().getDescription() + DELIMITER;
        if (recipient.getRolodex().getFullName() != null) {<commit_msg>KRACOEUS-4209: Fix for an NPE that could be triggered when syncing report recipients.<commit_after>/*
 * Copyright 2005-2010 The Kuali Foundation
 * 
 * Licensed under the Educational Community License, Version 2.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.kra.award.awardhierarchy.sync.helpers;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.kuali.kra.award.awardhierarchy.sync.AwardSyncChange;
import org.kuali.kra.award.awardhierarchy.sync.AwardSyncException;
import org.kuali.kra.award.awardhierarchy.sync.AwardSyncType;
import org.kuali.kra.award.awardhierarchy.sync.AwardSyncXmlExport;
import org.kuali.kra.award.home.Award;
import org.kuali.kra.award.paymentreports.awardreports.AwardReportTerm;
import org.kuali.kra.award.paymentreports.awardreports.AwardReportTermRecipient;
import org.kuali.rice.kns.bo.PersistableBusinessObject;

/**
 * Award Hierarchy Sync Helper for Report Recipient.
 */
public class AwardSyncReportRecipientHelper extends AwardSyncHelperBase {
    
    @SuppressWarnings("unchecked")
    @Override
    public void applySyncChange(Award award, AwardSyncChange change) 
        throws NoSuchFieldException, IntrospectionException, IllegalAccessException, InvocationTargetException, 
        ClassNotFoundException, NoSuchMethodException, InstantiationException, AwardSyncException {
        Collection reports = award.getAwardReportTermItems();
        AwardReportTerm report = (AwardReportTerm) getAwardSyncUtilityService().findMatchingBo(reports, change.getXmlExport().getKeys());
        if (StringUtils.equals(change.getSyncType(), AwardSyncType.ADD_SYNC.getSyncValue())) {
            if (report != null) {
                this.setValuesOnSyncable(report, change.getXmlExport().getValues(), change);
            } else {
                throw new AwardSyncException("Not applicable", true);
            }
        } else {
            if (report != null) {
                List<AwardSyncXmlExport> recipientChanges = (List<AwardSyncXmlExport>) change.getXmlExport().getValues().get("awardReportTermRecipients");
                for (AwardSyncXmlExport recipientChange : recipientChanges) {
                    AwardReportTermRecipient recipient = (AwardReportTermRecipient) getAwardSyncUtilityService().findMatchingBo((Collection) report.getAwardReportTermRecipients(), recipientChange.getKeys());
                    if (recipient != null) {
                        report.getAwardReportTermRecipients().remove(recipient);
                    }
                }
            } else {
                throw new AwardSyncException("Not applicable", true);
            }
        }
    }
        
    @Override
    protected String getObjectDesc(PersistableBusinessObject syncableObject, String attrName) {
        return "Recipient";
    }
    
    @Override
    protected String getDataDesc(PersistableBusinessObject syncableObject, String attrName) {
        AwardReportTermRecipient recipient = (AwardReportTermRecipient) syncableObject;
        String retval =  "";
        if (recipient.getContactType() != null) {
            retval += recipient.getContactType().getDescription();
        }
        retval += DELIMITER;
        if (recipient.getRolodex().getFullName() != null) {
{'fraction_non_alphanumeric': 0.08742371981958079, 'fraction_numerical': 0.00344919076678164, 'mean_word_length': 3.560798548094374, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5401239', 'n_tokens_mistral': 2107, 'n_tokens_neox': 2023, 'n_words': 457}